tui-core: Use char for single-glyph primitives
ee12de9c837c47069225b81cc9d85bc0c8a4963f
Stores fixed-width UI glyphs as chars instead of string slices across terminal primitives.
1 parent
5abdb971
crates/diff/src/renderer.rs
+6 -6
| 20 | 20 | /// Width of old and new line number cells. |
|
| 21 | 21 | const LINE_NUMBER_WIDTH: usize = 5; |
|
| 22 | 22 | /// Width of the hunk marker cell. |
|
| 23 | 23 | const MARKER_WIDTH: usize = 3; |
|
| 24 | 24 | /// Scrollbar track glyph (distinct from box-drawing borders so the column reads as a track). |
|
| 25 | - | const SCROLLBAR_TRACK: &str = "๐ฎ"; |
|
| 25 | + | const SCROLLBAR_TRACK: char = '๐ฎ'; |
|
| 26 | 26 | /// Scrollbar thumb glyph. |
|
| 27 | - | const SCROLLBAR_THUMB: &str = "โ"; |
|
| 27 | + | const SCROLLBAR_THUMB: char = 'โ'; |
|
| 28 | 28 | /// Scrollbar thumb color, one fixed-color shade darker than dim chrome. |
|
| 29 | 29 | const SCROLLBAR_THUMB_COLOR: Color = Color::Fixed(238); |
|
| 30 | 30 | /// File-card divider glyph (used between header/hunks and between hunks). |
|
| 31 | - | const DIVIDER: &str = "โ"; |
|
| 31 | + | const DIVIDER: char = 'โ'; |
|
| 32 | 32 | /// Sticky file-header divider glyph. |
|
| 33 | - | const STICKY_DIVIDER: &str = "โด"; |
|
| 33 | + | const STICKY_DIVIDER: char = 'โด'; |
|
| 34 | 34 | /// Pending file marker. |
|
| 35 | - | const FILE_MARKER: &str = "โ"; |
|
| 35 | + | const FILE_MARKER: char = 'โ'; |
|
| 36 | 36 | /// Rename arrow. |
|
| 37 | 37 | const RENAME_ARROW: &str = "->"; |
|
| 38 | 38 | /// Missing trailing newline marker. |
|
| 39 | 39 | const NO_NEWLINE_MARKER: &str = "\\ No newline at end of file"; |
|
| 40 | 40 | /// Marker cell shown between hunks in the diff marker column. |
| 888 | 888 | } |
|
| 889 | 889 | ||
| 890 | 890 | /// Returns a file header line. |
|
| 891 | 891 | fn header_line(&self, cols: usize) -> Line { |
|
| 892 | 892 | let mut left = Line::blank(); |
|
| 893 | - | left.push(Label::new(FILE_MARKER).fg(colors::fixed::DIM)); |
|
| 893 | + | left.push(Label::from_char(FILE_MARKER).fg(colors::fixed::DIM)); |
|
| 894 | 894 | left.push(Label::space()); |
|
| 895 | 895 | left = left.extend(self.path_line()); |
|
| 896 | 896 | ||
| 897 | 897 | if let Some(badge) = self.badge() { |
|
| 898 | 898 | left.push(Label::space()); |
a.rs
+0 -0
a.rs
+0 -0
b.rs
+0 -0
a.rs
+0 -0
crates/highlight/src/theme.rs
+15 -17
| 107 | 107 | } |
|
| 108 | 108 | ||
| 109 | 109 | impl Theme { |
|
| 110 | 110 | /// Compiles a raw theme. |
|
| 111 | 111 | pub fn compile(raw: &RawTheme) -> Self { |
|
| 112 | - | let mut default = Style::default(); |
|
| 112 | + | let mut default = Style::NONE; |
|
| 113 | 113 | let mut rules = Vec::new(); |
|
| 114 | 114 | let raw_rules = raw.settings.as_deref().or(raw.token_colors.as_deref()); |
|
| 115 | 115 | ||
| 116 | 116 | for rule in raw_rules.unwrap_or_default() { |
|
| 117 | 117 | let style = Style::from_raw(&rule.settings); |
| 200 | 200 | segment: Segment, |
|
| 201 | 201 | ) -> StyleSpan { |
|
| 202 | 202 | let Some(span) = segment.best_covering(scopes, grammar) else { |
|
| 203 | 203 | return segment.styled(None, self.default); |
|
| 204 | 204 | }; |
|
| 205 | - | let style = cache |
|
| 206 | - | .style_for(span.scope) |
|
| 207 | - | .unwrap_or_else(|| self.style_for_scope_id(grammar, span.scope)); |
|
| 208 | - | segment.styled(Some(span.scope), style) |
|
| 209 | - | } |
|
| 210 | - | ||
| 211 | - | /// Resolves a style for an interned scope id without going through the cache. |
|
| 212 | - | fn style_for_scope_id(&self, grammar: &Grammar, scope: ScopeId) -> Style { |
|
| 213 | - | grammar |
|
| 214 | - | .scopes |
|
| 215 | - | .get(scope.index()) |
|
| 216 | - | .map_or(self.default, |name| self.style_for_scope(name)) |
|
| 205 | + | segment.styled(Some(span.scope), cache.style_for(span.scope)) |
|
| 217 | 206 | } |
|
| 218 | 207 | } |
|
| 219 | 208 | ||
| 220 | 209 | impl FromStr for Theme { |
|
| 221 | 210 | type Err = Error; |
| 256 | 245 | .map(|scope| theme.style_for_scope(scope)), |
|
| 257 | 246 | ); |
|
| 258 | 247 | self.key = Some(key); |
|
| 259 | 248 | } |
|
| 260 | 249 | ||
| 261 | - | /// Returns the cached style for an interned scope id, if available. |
|
| 262 | - | fn style_for(&self, scope: ScopeId) -> Option<Style> { |
|
| 263 | - | self.styles.get(scope.index()).copied() |
|
| 250 | + | /// Returns the cached style for an interned scope id. |
|
| 251 | + | fn style_for(&self, scope: ScopeId) -> Style { |
|
| 252 | + | self.styles |
|
| 253 | + | .get(scope.index()) |
|
| 254 | + | .copied() |
|
| 255 | + | .unwrap_or(Style::NONE) |
|
| 264 | 256 | } |
|
| 265 | 257 | } |
|
| 266 | 258 | ||
| 267 | 259 | impl StyleScratch { |
|
| 268 | 260 | /// Clears per-line scratch storage while retaining allocations. |
| 386 | 378 | Ok(style) |
|
| 387 | 379 | } |
|
| 388 | 380 | } |
|
| 389 | 381 | ||
| 390 | 382 | impl Style { |
|
| 383 | + | /// Empty style with no foreground color or font flags. |
|
| 384 | + | pub const NONE: Self = Self { |
|
| 385 | + | foreground: None, |
|
| 386 | + | font_style: FontStyle::empty(), |
|
| 387 | + | }; |
|
| 388 | + | ||
| 391 | 389 | /// Builds a runtime style from raw TextMate settings. |
|
| 392 | 390 | fn from_raw(raw: &RawStyle) -> Self { |
|
| 393 | - | let mut style = Self::default(); |
|
| 391 | + | let mut style = Self::NONE; |
|
| 394 | 392 | if let Some(foreground) = &raw.foreground { |
|
| 395 | 393 | style.foreground = foreground.parse().ok(); |
|
| 396 | 394 | } |
|
| 397 | 395 | if let Some(flags) = &raw.font_style { |
|
| 398 | 396 | style.font_style = flags.parse().unwrap_or_else(|error| match error {}); |
crates/tui-core/src/backend.rs
+1 -1
| 129 | 129 | ||
| 130 | 130 | backend |
|
| 131 | 131 | .draw(&[DirtyRun { |
|
| 132 | 132 | x: 0, |
|
| 133 | 133 | y: 0, |
|
| 134 | - | cells: &[crate::buffer::Cell::new("x", Style::default())], |
|
| 134 | + | cells: &[crate::buffer::Cell::new('x', Style::default())], |
|
| 135 | 135 | }]) |
|
| 136 | 136 | .unwrap(); |
|
| 137 | 137 | backend.set_cursor_position(Point::new(2, 0)).unwrap(); |
|
| 138 | 138 | assert_eq!(backend.lines(), vec!["x "]); |
|
| 139 | 139 | assert_eq!(backend.cursor(), Some(Point::new(2, 0))); |
crates/tui-core/src/border.rs
+26 -28
| 9 | 9 | ||
| 10 | 10 | /// Border glyph set. |
|
| 11 | 11 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
|
| 12 | 12 | pub struct BorderChars { |
|
| 13 | 13 | /// Top-left corner. |
|
| 14 | - | pub top_left: &'static str, |
|
| 14 | + | pub top_left: char, |
|
| 15 | 15 | /// Top-right corner. |
|
| 16 | - | pub top_right: &'static str, |
|
| 16 | + | pub top_right: char, |
|
| 17 | 17 | /// Bottom-left corner. |
|
| 18 | - | pub bottom_left: &'static str, |
|
| 18 | + | pub bottom_left: char, |
|
| 19 | 19 | /// Bottom-right corner. |
|
| 20 | - | pub bottom_right: &'static str, |
|
| 20 | + | pub bottom_right: char, |
|
| 21 | 21 | /// Vertical edge. |
|
| 22 | - | pub vertical: &'static str, |
|
| 22 | + | pub vertical: char, |
|
| 23 | 23 | /// Horizontal edge. |
|
| 24 | - | pub horizontal: &'static str, |
|
| 24 | + | pub horizontal: char, |
|
| 25 | 25 | /// Left divider junction. |
|
| 26 | - | pub divider_left: &'static str, |
|
| 26 | + | pub divider_left: char, |
|
| 27 | 27 | /// Right divider junction. |
|
| 28 | - | pub divider_right: &'static str, |
|
| 28 | + | pub divider_right: char, |
|
| 29 | 29 | } |
|
| 30 | 30 | ||
| 31 | 31 | impl BorderChars { |
|
| 32 | 32 | /// Rounded border glyphs. |
|
| 33 | 33 | pub const ROUNDED: Self = Self { |
|
| 34 | - | top_left: "โญ", |
|
| 35 | - | top_right: "โฎ", |
|
| 36 | - | bottom_left: "โฐ", |
|
| 37 | - | bottom_right: "โฏ", |
|
| 38 | - | vertical: "โ", |
|
| 39 | - | horizontal: "โ", |
|
| 40 | - | divider_left: "โ", |
|
| 41 | - | divider_right: "โค", |
|
| 34 | + | top_left: 'โญ', |
|
| 35 | + | top_right: 'โฎ', |
|
| 36 | + | bottom_left: 'โฐ', |
|
| 37 | + | bottom_right: 'โฏ', |
|
| 38 | + | vertical: 'โ', |
|
| 39 | + | horizontal: 'โ', |
|
| 40 | + | divider_left: 'โ', |
|
| 41 | + | divider_right: 'โค', |
|
| 42 | 42 | }; |
|
| 43 | 43 | } |
|
| 44 | 44 | ||
| 45 | 45 | impl Default for BorderChars { |
|
| 46 | 46 | fn default() -> Self { |
| 80 | 80 | cols, |
|
| 81 | 81 | ) |
|
| 82 | 82 | } |
|
| 83 | 83 | ||
| 84 | 84 | /// Returns a divider line with `cols` inner columns. |
|
| 85 | - | pub fn divider(self, cols: usize, sigil: &str) -> Line { |
|
| 86 | - | self.horizontal( |
|
| 87 | - | self.chars.divider_left, |
|
| 88 | - | sigil, |
|
| 89 | - | self.chars.divider_right, |
|
| 90 | - | cols, |
|
| 91 | - | ) |
|
| 85 | + | pub fn divider(self, cols: usize, sigil: char) -> Line { |
|
| 86 | + | Line::default() |
|
| 87 | + | .item(self.paint(self.chars.divider_left)) |
|
| 88 | + | .item(self.paint(std::iter::repeat_n(sigil, cols).collect::<String>())) |
|
| 89 | + | .item(self.paint(self.chars.divider_right)) |
|
| 92 | 90 | } |
|
| 93 | 91 | ||
| 94 | 92 | /// Returns a bottom border line with `cols` inner columns. |
|
| 95 | 93 | pub fn bottom(self, cols: usize) -> Line { |
|
| 96 | 94 | self.horizontal( |
| 155 | 153 | self.set(buf, area.x, y, self.chars.vertical); |
|
| 156 | 154 | self.set(buf, right, y, self.chars.vertical); |
|
| 157 | 155 | } |
|
| 158 | 156 | } |
|
| 159 | 157 | ||
| 160 | - | fn horizontal(self, left: &str, fill: &str, right: &str, cols: usize) -> Line { |
|
| 158 | + | fn horizontal(self, left: char, fill: char, right: char, cols: usize) -> Line { |
|
| 161 | 159 | Line::default() |
|
| 162 | 160 | .item(self.paint(left)) |
|
| 163 | - | .item(self.paint(fill.repeat(cols))) |
|
| 161 | + | .item(self.paint(std::iter::repeat_n(fill, cols).collect::<String>())) |
|
| 164 | 162 | .item(self.paint(right)) |
|
| 165 | 163 | } |
|
| 166 | 164 | ||
| 167 | 165 | fn paint<T>(self, item: T) -> Paint<T> { |
|
| 168 | 166 | Paint::new(item).with_style(self.style) |
|
| 169 | 167 | } |
|
| 170 | 168 | ||
| 171 | - | fn set(self, buf: &mut Buffer, x: usize, y: usize, glyph: &str) { |
|
| 172 | - | buf.set_glyph(x, y, glyph, self.style); |
|
| 169 | + | fn set(self, buf: &mut Buffer, x: usize, y: usize, glyph: char) { |
|
| 170 | + | buf.set_char(x, y, glyph, self.style); |
|
| 173 | 171 | } |
|
| 174 | 172 | } |
|
| 175 | 173 | ||
| 176 | 174 | #[cfg(test)] |
|
| 177 | 175 | mod tests { |
| 187 | 185 | assert!( |
|
| 188 | 186 | line.labels() |
|
| 189 | 187 | .iter() |
|
| 190 | 188 | .all(|label| { label.paint.style().fg_color() == Color::Red }) |
|
| 191 | 189 | ); |
|
| 192 | - | assert_eq!(border.divider(2, "โ").to_string(), "โโโโค"); |
|
| 190 | + | assert_eq!(border.divider(2, 'โ').to_string(), "โโโโค"); |
|
| 193 | 191 | assert_eq!(border.bottom(1).to_string(), "โฐโโฏ"); |
|
| 194 | 192 | } |
|
| 195 | 193 | ||
| 196 | 194 | #[test] |
|
| 197 | 195 | fn border_draws_degenerate_and_rectangular_buffers() { |
crates/tui-core/src/buffer.rs
+30 -22
| 36 | 36 | /// Continuation cell occupied by a wide leading glyph. |
|
| 37 | 37 | Continuation, |
|
| 38 | 38 | } |
|
| 39 | 39 | ||
| 40 | 40 | impl Cell { |
|
| 41 | - | /// Creates a cell from a glyph and style. |
|
| 42 | - | pub fn new(glyph: impl AsRef<str>, style: Style) -> Self { |
|
| 41 | + | /// Creates a cell from one character and style. |
|
| 42 | + | pub fn new(ch: char, style: Style) -> Self { |
|
| 43 | + | Self { |
|
| 44 | + | glyph: normalize_char(ch), |
|
| 45 | + | style, |
|
| 46 | + | } |
|
| 47 | + | } |
|
| 48 | + | ||
| 49 | + | /// Creates a cell from a glyph string and style. |
|
| 50 | + | pub(crate) fn from_glyph(glyph: impl AsRef<str>, style: Style) -> Self { |
|
| 43 | 51 | Self { |
|
| 44 | 52 | glyph: normalize_glyph(glyph.as_ref()), |
|
| 45 | 53 | style, |
|
| 46 | 54 | } |
|
| 47 | 55 | } |
| 91 | 99 | glyph: CellGlyph::Continuation, |
|
| 92 | 100 | style, |
|
| 93 | 101 | } |
|
| 94 | 102 | } |
|
| 95 | 103 | ||
| 96 | - | /// Creates a cell from one character. |
|
| 97 | - | fn from_char(ch: char, style: Style) -> Self { |
|
| 98 | - | Self { |
|
| 99 | - | glyph: normalize_char(ch), |
|
| 100 | - | style, |
|
| 101 | - | } |
|
| 102 | - | } |
|
| 103 | - | ||
| 104 | 104 | /// Creates a cell from one printable ASCII byte. |
|
| 105 | 105 | fn from_printable_ascii(byte: u8, style: Style) -> Self { |
|
| 106 | 106 | Self { |
|
| 107 | 107 | glyph: if byte == b' ' { |
|
| 108 | 108 | CellGlyph::Blank |
| 228 | 228 | } |
|
| 229 | 229 | } |
|
| 230 | 230 | ||
| 231 | 231 | /// Sets one styled glyph by absolute position. |
|
| 232 | 232 | pub fn set_glyph(&mut self, x: usize, y: usize, glyph: impl AsRef<str>, style: Style) { |
|
| 233 | - | self.set(x, y, Cell::new(glyph, style)); |
|
| 233 | + | self.set(x, y, Cell::from_glyph(glyph, style)); |
|
| 234 | + | } |
|
| 235 | + | ||
| 236 | + | /// Sets one styled character by absolute position. |
|
| 237 | + | pub(crate) fn set_char(&mut self, x: usize, y: usize, ch: char, style: Style) { |
|
| 238 | + | self.set(x, y, Cell::new(ch, style)); |
|
| 234 | 239 | } |
|
| 235 | 240 | ||
| 236 | 241 | /// Writes styled text by absolute position. |
|
| 237 | 242 | pub fn set_str(&mut self, x: usize, y: usize, text: &str, style: Style) { |
|
| 238 | 243 | if text.is_ascii() |
| 253 | 258 | continue; |
|
| 254 | 259 | } |
|
| 255 | 260 | if width > self.area.right().saturating_sub(cursor) { |
|
| 256 | 261 | break; |
|
| 257 | 262 | } |
|
| 258 | - | self.set(cursor, y, Cell::from_char(ch, style)); |
|
| 263 | + | self.set(cursor, y, Cell::new(ch, style)); |
|
| 259 | 264 | cursor += width; |
|
| 260 | 265 | } |
|
| 261 | 266 | } |
|
| 262 | 267 | ||
| 263 | 268 | /// Writes printable ASCII text by absolute position. |
| 509 | 514 | ||
| 510 | 515 | #[test] |
|
| 511 | 516 | fn cell_normalizes_to_one_terminal_glyph() { |
|
| 512 | 517 | let style = Style::new(crate::Color::Red); |
|
| 513 | 518 | ||
| 514 | - | let cell = Cell::new("ab", style); |
|
| 515 | - | let control = Cell::new("\n", style); |
|
| 519 | + | let cell = Cell::from_glyph("ab", style); |
|
| 520 | + | let control = Cell::from_glyph("\n", style); |
|
| 516 | 521 | ||
| 517 | 522 | assert_eq!(cell.glyph(), "a"); |
|
| 518 | 523 | assert_eq!(cell.style(), style); |
|
| 519 | 524 | assert_eq!(control.glyph(), " "); |
|
| 520 | 525 | assert!(!cell.is_continuation()); |
| 523 | 528 | #[test] |
|
| 524 | 529 | fn cell_uses_inline_storage_for_blank_and_ascii_glyphs() { |
|
| 525 | 530 | let style = Style::new(crate::Color::Blue); |
|
| 526 | 531 | ||
| 527 | 532 | assert!(matches!(Cell::blank().glyph, CellGlyph::Blank)); |
|
| 528 | - | assert!(matches!(Cell::new(" ", style).glyph, CellGlyph::Blank)); |
|
| 529 | 533 | assert!(matches!( |
|
| 530 | - | Cell::new("x", style).glyph, |
|
| 534 | + | Cell::from_glyph(" ", style).glyph, |
|
| 535 | + | CellGlyph::Blank |
|
| 536 | + | )); |
|
| 537 | + | assert!(matches!( |
|
| 538 | + | Cell::from_glyph("x", style).glyph, |
|
| 531 | 539 | CellGlyph::Ascii(b'x') |
|
| 532 | 540 | )); |
|
| 533 | 541 | assert!(matches!( |
|
| 534 | - | Cell::new("โญ", style).glyph, |
|
| 542 | + | Cell::from_glyph("โญ", style).glyph, |
|
| 535 | 543 | CellGlyph::Unicode('โญ') |
|
| 536 | 544 | )); |
|
| 537 | 545 | assert!(matches!( |
|
| 538 | - | Cell::new("โค๏ธ", style).glyph, |
|
| 546 | + | Cell::from_glyph("โค๏ธ", style).glyph, |
|
| 539 | 547 | CellGlyph::Grapheme(glyph) if glyph == "โค๏ธ" |
|
| 540 | 548 | )); |
|
| 541 | 549 | assert!(matches!( |
|
| 542 | - | Cell::new("ฮป", style).glyph, |
|
| 550 | + | Cell::from_glyph("ฮป", style).glyph, |
|
| 543 | 551 | CellGlyph::Unicode('ฮป') |
|
| 544 | 552 | )); |
|
| 545 | 553 | } |
|
| 546 | 554 | ||
| 547 | 555 | #[test] |
| 625 | 633 | let mut buffer = Buffer::empty(Rect::new(1, 1, 4, 2)); |
|
| 626 | 634 | ||
| 627 | 635 | buffer.reset_dirty(); |
|
| 628 | 636 | buffer.fill( |
|
| 629 | 637 | Rect::new(3, 0, 4, 4), |
|
| 630 | - | Cell::new("x", Style::new(crate::Color::Blue)), |
|
| 638 | + | Cell::new('x', Style::new(crate::Color::Blue)), |
|
| 631 | 639 | ); |
|
| 632 | - | buffer.set(0, 0, Cell::new("z", Style::default())); |
|
| 640 | + | buffer.set(0, 0, Cell::new('z', Style::default())); |
|
| 633 | 641 | ||
| 634 | 642 | assert_eq!(buffer.lines(), vec![" xx", " xx"]); |
|
| 635 | 643 | assert_eq!(buffer.dirty_runs().len(), 2); |
|
| 636 | 644 | } |
|
| 637 | 645 |
| 686 | 694 | ||
| 687 | 695 | #[test] |
|
| 688 | 696 | fn wide_fill_tiles_by_visual_width() { |
|
| 689 | 697 | let mut buffer = Buffer::empty(Rect::new(0, 0, 5, 1)); |
|
| 690 | 698 | ||
| 691 | - | buffer.fill(buffer.area(), Cell::new("๐", Style::default())); |
|
| 699 | + | buffer.fill(buffer.area(), Cell::new('๐', Style::default())); |
|
| 692 | 700 | ||
| 693 | 701 | assert_eq!(buffer.line(0), "๐๐ "); |
|
| 694 | 702 | assert!(buffer.get(1, 0).unwrap().is_continuation()); |
|
| 695 | 703 | assert!(buffer.get(3, 0).unwrap().is_continuation()); |
|
| 696 | 704 | } |
crates/tui-core/src/label.rs
+10 -0
| 40 | 40 | /// Creates a new label. |
|
| 41 | 41 | pub fn new(s: &str) -> Self { |
|
| 42 | 42 | Self::raw(clean(s.to_owned()), Style::default()) |
|
| 43 | 43 | } |
|
| 44 | 44 | ||
| 45 | + | /// Creates a new label from one character. |
|
| 46 | + | pub fn from_char(ch: char) -> Self { |
|
| 47 | + | let content = if matches!(ch, '\n' | '\r') { |
|
| 48 | + | String::new() |
|
| 49 | + | } else { |
|
| 50 | + | ch.to_string() |
|
| 51 | + | }; |
|
| 52 | + | Self::raw(content, Style::default()) |
|
| 53 | + | } |
|
| 54 | + | ||
| 45 | 55 | /// Creates a blank label. |
|
| 46 | 56 | pub fn blank() -> Self { |
|
| 47 | 57 | Self::raw(String::new(), Style::default()) |
|
| 48 | 58 | } |
|
| 49 | 59 |
crates/tui-core/src/table.rs
+5 -5
| 83 | 83 | style: Option<Style>, |
|
| 84 | 84 | }, |
|
| 85 | 85 | /// Horizontal divider row. |
|
| 86 | 86 | Divider { |
|
| 87 | 87 | /// Horizontal line sigil used for this divider. |
|
| 88 | - | sigil: &'static str, |
|
| 88 | + | sigil: char, |
|
| 89 | 89 | }, |
|
| 90 | 90 | } |
|
| 91 | 91 | ||
| 92 | 92 | /// Fixed-width table with `W` columns. |
|
| 93 | 93 | #[derive(Debug)] |
| 167 | 167 | Row::Data { cells, style } => { |
|
| 168 | 168 | lines.push(self.cells_line(cells, *style, border, cols)); |
|
| 169 | 169 | } |
|
| 170 | 170 | Row::Divider { sigil } => { |
|
| 171 | 171 | if let Some(border) = border { |
|
| 172 | - | lines.push(border.divider(cols, sigil)); |
|
| 172 | + | lines.push(border.divider(cols, *sigil)); |
|
| 173 | 173 | } else { |
|
| 174 | 174 | lines.push(Line::default()); |
|
| 175 | 175 | } |
|
| 176 | 176 | } |
|
| 177 | 177 | } |
| 245 | 245 | self.outer(parent) |
|
| 246 | 246 | } |
|
| 247 | 247 | ||
| 248 | 248 | /// Adds a divider row. |
|
| 249 | 249 | pub fn divider(&mut self) { |
|
| 250 | - | self.divider_with_sigil("โ"); |
|
| 250 | + | self.divider_with_sigil('โ'); |
|
| 251 | 251 | } |
|
| 252 | 252 | ||
| 253 | 253 | /// Adds a divider row with an explicit line sigil. |
|
| 254 | - | pub fn divider_with_sigil(&mut self, sigil: &'static str) { |
|
| 254 | + | pub fn divider_with_sigil(&mut self, sigil: char) { |
|
| 255 | 255 | self.rows.push(Row::Divider { sigil }); |
|
| 256 | 256 | } |
|
| 257 | 257 | ||
| 258 | 258 | /// Adds a data row. |
|
| 259 | 259 | pub fn push(&mut self, row: [T; W]) { |
| 640 | 640 | spacing: 3, |
|
| 641 | 641 | ..TableOptions::default() |
|
| 642 | 642 | }); |
|
| 643 | 643 | ||
| 644 | 644 | t.push(["Code", "Name"]); |
|
| 645 | - | t.divider_with_sigil("โ"); |
|
| 645 | + | t.divider_with_sigil('โ'); |
|
| 646 | 646 | t.push(["FR", "France"]); |
|
| 647 | 647 | ||
| 648 | 648 | assert_eq!( |
|
| 649 | 649 | t.display(Constraint::UNBOUNDED), |
|
| 650 | 650 | r" |
crates/tui-core/src/vstack.rs
+5 -5
| 9 | 9 | use super::{Border, Buffer, Cell, Constraint, Label, Length, Rect, Size, Style, Widget}; |
|
| 10 | 10 | ||
| 11 | 11 | /// Default horizontal padding inside a stack border. |
|
| 12 | 12 | pub const DEFAULT_PADDING: usize = 1; |
|
| 13 | 13 | /// Default divider glyph. |
|
| 14 | - | pub const DEFAULT_DIVIDER_GLYPH: &str = "โ"; |
|
| 14 | + | pub const DEFAULT_DIVIDER_GLYPH: char = 'โ'; |
|
| 15 | 15 | /// Default divider style. |
|
| 16 | 16 | pub const DEFAULT_DIVIDER_STYLE: Style = Style::new(super::Color::Unset); |
|
| 17 | 17 | ||
| 18 | 18 | /// Options for [`VStack`]. |
|
| 19 | 19 | #[derive(Debug)] |
| 268 | 268 | ||
| 269 | 269 | if rows == 0 || row_y >= area.bottom() { |
|
| 270 | 270 | continue; |
|
| 271 | 271 | } |
|
| 272 | 272 | if let Some(border) = self.opts.border { |
|
| 273 | - | buf.set_glyph(area.x, row_y, border.chars.divider_left, border.style); |
|
| 273 | + | buf.set_char(area.x, row_y, border.chars.divider_left, border.style); |
|
| 274 | 274 | buf.fill(Rect::new(content.x, row_y, content.cols, 1), cell.clone()); |
|
| 275 | 275 | ||
| 276 | 276 | if area.cols > 1 { |
|
| 277 | - | buf.set_glyph( |
|
| 277 | + | buf.set_char( |
|
| 278 | 278 | area.right().saturating_sub(1), |
|
| 279 | 279 | row_y, |
|
| 280 | 280 | border.chars.divider_right, |
|
| 281 | 281 | border.style, |
|
| 282 | 282 | ); |
| 452 | 452 | fn bordered_divider_keeps_border_and_fill_styles_separate() { |
|
| 453 | 453 | let mut v = VStack::default() |
|
| 454 | 454 | .border(Some(Border::new(Color::Blue))) |
|
| 455 | 455 | .padding(0) |
|
| 456 | 456 | .child(Line::new("top")) |
|
| 457 | - | .divider_cell(Cell::new("โ", Style::new(Color::Red))) |
|
| 457 | + | .divider_cell(Cell::new('โ', Style::new(Color::Red))) |
|
| 458 | 458 | .child(Line::new("bottom")); |
|
| 459 | 459 | let mut buffer = Buffer::empty(Rect::from_size(v.size(Constraint::UNBOUNDED))); |
|
| 460 | 460 | ||
| 461 | 461 | v.render(buffer.area(), &mut buffer); |
|
| 462 | 462 |
| 500 | 500 | #[test] |
|
| 501 | 501 | fn fill_child_absorbs_remaining_height() { |
|
| 502 | 502 | let mut stack = VStack::default() |
|
| 503 | 503 | .padding(0) |
|
| 504 | 504 | .child(Line::new("top")) |
|
| 505 | - | .fill(crate::Fill::new(".", crate::Style::default())); |
|
| 505 | + | .fill(crate::Fill::new('.', crate::Style::default())); |
|
| 506 | 506 | let constraint = Constraint::new(Size::new(5, 4), Size::new(5, 4)); |
|
| 507 | 507 | ||
| 508 | 508 | assert_eq!(stack.display(constraint), "top \n.....\n.....\n.....\n"); |
|
| 509 | 509 | } |
|
| 510 | 510 |
crates/tui-core/src/widget.rs
+6 -6
| 138 | 138 | /// Fill cell. |
|
| 139 | 139 | cell: Cell, |
|
| 140 | 140 | } |
|
| 141 | 141 | ||
| 142 | 142 | impl Fill { |
|
| 143 | - | /// Creates a fill widget from a glyph and style. |
|
| 144 | - | pub fn new(glyph: impl AsRef<str>, style: Style) -> Self { |
|
| 143 | + | /// Creates a fill widget from one character and style. |
|
| 144 | + | pub fn new(ch: char, style: Style) -> Self { |
|
| 145 | 145 | Self { |
|
| 146 | - | cell: Cell::new(glyph, style), |
|
| 146 | + | cell: Cell::new(ch, style), |
|
| 147 | 147 | } |
|
| 148 | 148 | } |
|
| 149 | 149 | ||
| 150 | 150 | /// Creates a fill widget from a cell. |
|
| 151 | 151 | pub fn cell(cell: Cell) -> Self { |
| 230 | 230 | } |
|
| 231 | 231 | } |
|
| 232 | 232 | ||
| 233 | 233 | impl From<Paint<String>> for Fill { |
|
| 234 | 234 | fn from(paint: Paint<String>) -> Self { |
|
| 235 | - | Self::new(paint.content(), paint.style()) |
|
| 235 | + | Self::cell(Cell::from_glyph(paint.content(), paint.style())) |
|
| 236 | 236 | } |
|
| 237 | 237 | } |
|
| 238 | 238 | ||
| 239 | 239 | #[cfg(test)] |
|
| 240 | 240 | mod tests { |
| 251 | 251 | assert_eq!(Label::new("hello").size(Constraint::UNBOUNDED).cols, 5); |
|
| 252 | 252 | } |
|
| 253 | 253 | ||
| 254 | 254 | #[test] |
|
| 255 | 255 | fn widget_display_renders_through_buffer() { |
|
| 256 | - | let display = Fill::new("x", Style::default()).display(Constraint::tight(4)); |
|
| 256 | + | let display = Fill::new('x', Style::default()).display(Constraint::tight(4)); |
|
| 257 | 257 | ||
| 258 | 258 | assert_eq!(display, "xxxx\n"); |
|
| 259 | 259 | } |
|
| 260 | 260 | ||
| 261 | 261 | #[test] |
| 272 | 272 | ||
| 273 | 273 | #[test] |
|
| 274 | 274 | fn clear_and_fill_widgets_cover_their_area() { |
|
| 275 | 275 | let mut buffer = Buffer::empty(Rect::new(0, 0, 5, 2)); |
|
| 276 | 276 | ||
| 277 | - | Fill::new("x", Style::default()).render(buffer.area(), &mut buffer); |
|
| 277 | + | Fill::new('x', Style::default()).render(buffer.area(), &mut buffer); |
|
| 278 | 278 | Clear.render(Rect::new(1, 0, 3, 1), &mut buffer); |
|
| 279 | 279 | ||
| 280 | 280 | assert_eq!(buffer.lines(), vec!["x x", "xxxxx"]); |
|
| 281 | 281 | } |
|
| 282 | 282 |
crates/tui-termion/src/lib.rs
+2 -2
| 196 | 196 | use tui_core::backend::Backend; |
|
| 197 | 197 | use tui_core::{Buffer, Color, Rect, Style}; |
|
| 198 | 198 | ||
| 199 | 199 | #[test] |
|
| 200 | 200 | fn termion_backend_writes_dirty_runs_with_one_based_coordinates() { |
|
| 201 | - | let cells = [tui_core::buffer::Cell::new("x", Style::default())]; |
|
| 201 | + | let cells = [tui_core::buffer::Cell::new('x', Style::default())]; |
|
| 202 | 202 | let run = DirtyRun { |
|
| 203 | 203 | x: 2, |
|
| 204 | 204 | y: 3, |
|
| 205 | 205 | cells: &cells, |
|
| 206 | 206 | }; |
| 214 | 214 | assert!(out.ends_with('x')); |
|
| 215 | 215 | } |
|
| 216 | 216 | ||
| 217 | 217 | #[test] |
|
| 218 | 218 | fn termion_backend_paint_mode_is_explicit() { |
|
| 219 | - | let cells = [tui_core::buffer::Cell::new("x", Color::Red.style())]; |
|
| 219 | + | let cells = [tui_core::buffer::Cell::new('x', Color::Red.style())]; |
|
| 220 | 220 | let run = DirtyRun { |
|
| 221 | 221 | x: 0, |
|
| 222 | 222 | y: 0, |
|
| 223 | 223 | cells: &cells, |
|
| 224 | 224 | }; |
crates/tui-widgets/src/scrollbar.rs
+6 -6
| 9 | 9 | use crate::View; |
|
| 10 | 10 | use rustik_tui_core as tui_core; |
|
| 11 | 11 | use tui_core::{Buffer, Cell, Color, Constraint, Rect, Size, Style, Widget}; |
|
| 12 | 12 | ||
| 13 | 13 | /// Default scrollbar track glyph. |
|
| 14 | - | pub const DEFAULT_TRACK: &str = "โ"; |
|
| 14 | + | pub const DEFAULT_TRACK: char = 'โ'; |
|
| 15 | 15 | /// Default scrollbar thumb glyph. |
|
| 16 | - | pub const DEFAULT_THUMB: &str = "โ"; |
|
| 16 | + | pub const DEFAULT_THUMB: char = 'โ'; |
|
| 17 | 17 | /// Default scrollbar track style. |
|
| 18 | 18 | pub const DEFAULT_TRACK_STYLE: Style = Style::new(Color::Unset); |
|
| 19 | 19 | /// Default scrollbar thumb style. |
|
| 20 | 20 | pub const DEFAULT_THUMB_STYLE: Style = Style::new(Color::Unset); |
|
| 21 | 21 |
| 156 | 156 | } |
|
| 157 | 157 | ||
| 158 | 158 | #[test] |
|
| 159 | 159 | fn scrollbar_renders_track_and_thumb_glyphs() { |
|
| 160 | 160 | let style = ScrollbarStyle::new( |
|
| 161 | - | Cell::new("ยท", Style::new(Color::Unset)), |
|
| 162 | - | Cell::new("โ", Style::new(Color::Unset)), |
|
| 161 | + | Cell::new('ยท', Style::new(Color::Unset)), |
|
| 162 | + | Cell::new('โ', Style::new(Color::Unset)), |
|
| 163 | 163 | ); |
|
| 164 | 164 | let mut scrollbar = Scrollbar::new(View::new(40, 100, 20)).style(style); |
|
| 165 | 165 | let mut buffer = Buffer::empty(Rect::new(0, 0, 1, 5)); |
|
| 166 | 166 | ||
| 167 | 167 | scrollbar.render(buffer.area(), &mut buffer); |
| 170 | 170 | } |
|
| 171 | 171 | ||
| 172 | 172 | #[test] |
|
| 173 | 173 | fn scrollbar_keeps_track_and_thumb_styles_separate() { |
|
| 174 | 174 | let style = ScrollbarStyle::new( |
|
| 175 | - | Cell::new("ยท", Style::new(Color::Fixed(236))), |
|
| 176 | - | Cell::new("โ", Style::new(Color::White)), |
|
| 175 | + | Cell::new('ยท', Style::new(Color::Fixed(236))), |
|
| 176 | + | Cell::new('โ', Style::new(Color::White)), |
|
| 177 | 177 | ); |
|
| 178 | 178 | let mut scrollbar = Scrollbar::new(View::new(2, 10, 4)).style(style); |
|
| 179 | 179 | let mut buffer = Buffer::empty(Rect::new(0, 0, 1, 4)); |
|
| 180 | 180 | ||
| 181 | 181 | scrollbar.render(buffer.area(), &mut buffer); |
crates/tui-widgets/src/scrollview.rs
+2 -2
| 345 | 345 | } |
|
| 346 | 346 | ||
| 347 | 347 | #[test] |
|
| 348 | 348 | fn scroll_view_builds_lines_at_body_width() { |
|
| 349 | 349 | let style = ScrollbarStyle::new( |
|
| 350 | - | Cell::new("|", Style::new(Color::Unset)), |
|
| 351 | - | Cell::new("#", Style::new(Color::Unset)), |
|
| 350 | + | Cell::new('|', Style::new(Color::Unset)), |
|
| 351 | + | Cell::new('#', Style::new(Color::Unset)), |
|
| 352 | 352 | ); |
|
| 353 | 353 | let area = Rect::new(0, 0, 4, 2); |
|
| 354 | 354 | let mut view = ScrollView::new(area, 1, |cols| { |
|
| 355 | 355 | (0..3) |
|
| 356 | 356 | .map(|row| Line::new(format!("{row}:{cols}"))) |
crates/tui/benches/render.rs
+7 -7
| 177 | 177 | ACTIVE_BORDER |
|
| 178 | 178 | } else { |
|
| 179 | 179 | INACTIVE_BORDER |
|
| 180 | 180 | }))) |
|
| 181 | 181 | .child(summary) |
|
| 182 | - | .divider_cell(Cell::new("โ", DIVIDER_STYLE)) |
|
| 182 | + | .divider_cell(Cell::new('โ', DIVIDER_STYLE)) |
|
| 183 | 183 | .fill(list); |
|
| 184 | 184 | let middle = VStack::default() |
|
| 185 | 185 | .border(Some(Border::new(TABLE_BORDER))) |
|
| 186 | 186 | .child(Label::new("render metrics").fg(HOT_TEXT)) |
|
| 187 | 187 | .divider() |
|
| 188 | 188 | .child_with_height(table, Length::Fixed(24)) |
|
| 189 | - | .divider_cell(Cell::new("โ", DIVIDER_STYLE)) |
|
| 189 | + | .divider_cell(Cell::new('โ', DIVIDER_STYLE)) |
|
| 190 | 190 | .child_with_height(preview, Length::Fixed(18)) |
|
| 191 | - | .fill(Fill::new("ยท", FILL_STYLE)); |
|
| 191 | + | .fill(Fill::new('ยท', FILL_STYLE)); |
|
| 192 | 192 | let right = VStack::default() |
|
| 193 | 193 | .border(Some(Border::new(INACTIVE_BORDER))) |
|
| 194 | 194 | .child(Label::new("scrolling log").fg(DIM_TEXT)) |
|
| 195 | 195 | .divider() |
|
| 196 | 196 | .fill(scroll); |
| 202 | 202 | .render(area, buffer); |
|
| 203 | 203 | } |
|
| 204 | 204 | ||
| 205 | 205 | fn scrollbar_style(track: Color, thumb: Color) -> ScrollbarStyle { |
|
| 206 | 206 | ScrollbarStyle::new( |
|
| 207 | - | Cell::new("โ", Style::new(track).bg(Color::Fixed(235))), |
|
| 208 | - | Cell::new("โ", Style::new(thumb).bg(Color::Fixed(235))), |
|
| 207 | + | Cell::new('โ', Style::new(track).bg(Color::Fixed(235))), |
|
| 208 | + | Cell::new('โ', Style::new(thumb).bg(Color::Fixed(235))), |
|
| 209 | 209 | ) |
|
| 210 | 210 | } |
|
| 211 | 211 | ||
| 212 | 212 | fn stats_table(frame: usize, selected: usize) -> Table<5, Label> { |
|
| 213 | 213 | let mut table = Table::new(TableOptions::bordered()); |
| 229 | 229 | Label::from(format!("{delta:+07}")), |
|
| 230 | 230 | state, |
|
| 231 | 231 | Label::from(format!("bucket {}", (row + selected) % 97)), |
|
| 232 | 232 | ]); |
|
| 233 | 233 | if row % 40 == 39 { |
|
| 234 | - | table.divider_with_sigil("โ"); |
|
| 234 | + | table.divider_with_sigil('โ'); |
|
| 235 | 235 | } |
|
| 236 | 236 | } |
|
| 237 | 237 | ||
| 238 | 238 | table |
|
| 239 | 239 | } |
| 246 | 246 | (0..128) |
|
| 247 | 247 | .map(|row| { |
|
| 248 | 248 | let width = 24 + (row + selected + frame) % 96; |
|
| 249 | 249 | Line::spaced([ |
|
| 250 | 250 | Label::from(format!("{row:03}")).fg(DIM_TEXT), |
|
| 251 | - | Label::new("โ").fg(INACTIVE_BORDER), |
|
| 251 | + | Label::from_char('โ').fg(INACTIVE_BORDER), |
|
| 252 | 252 | Label::from("โธ".repeat(row % 5 + 1)).fg(HOT_TEXT), |
|
| 253 | 253 | Label::from("x".repeat(width)), |
|
| 254 | 254 | ]) |
|
| 255 | 255 | }) |
|
| 256 | 256 | .collect() |