.cargo/
crates/
brain/
diff/
files/
highlight/
repo/
signals/
tui/
tui-core/
src/
ansi/
aligned.rs
2.3 KiB
ansi.rs
2.3 KiB
app.rs
4.2 KiB
backend.rs
3.9 KiB
border.rs
6.5 KiB
buffer.rs
20.6 KiB
cell.rs
7.2 KiB
colors.rs
4.2 KiB
event.rs
3.4 KiB
frame.rs
3.7 KiB
hstack.rs
6.5 KiB
label.rs
5.5 KiB
layout.rs
11.2 KiB
lib.rs
2.4 KiB
primitive.rs
19.4 KiB
table.rs
23.5 KiB
terminal.rs
5.2 KiB
text.rs
3.0 KiB
textarea.rs
6.4 KiB
vstack.rs
16.4 KiB
widget.rs
11.0 KiB
Cargo.toml
298 B
README.md
521 B
tui-termion/
tui-widgets/
scripts/
src/
.gitignore
30 B
.gitsigners
198 B
AGENTS.md
1.9 KiB
CONTRIBUTING
1.7 KiB
Cargo.lock
5.4 KiB
Cargo.toml
1.9 KiB
LICENSE
1.1 KiB
README.md
245 B
STYLE
3.7 KiB
clippy.toml
213 B
crates/tui-core/src/buffer.rs
raw
| 1 | //! Styled terminal cell buffer. |
| 2 | //! |
| 3 | //! `Buffer` is the render artifact for the core crate: widgets write styled |
| 4 | //! [`Cell`] values into it, and the terminal flushes only the cells that changed |
| 5 | //! since the previous frame. It also handles wide glyph continuation cells so |
| 6 | //! higher-level widgets can write glyphs without manual cleanup. |
| 7 | |
| 8 | use std::borrow::Cow; |
| 9 | use std::{slice, str}; |
| 10 | |
| 11 | use crate::cell::{Text as _, char_width}; |
| 12 | use crate::layout::{Point, Rect}; |
| 13 | use crate::{Label, Line, Paint, Size, Style}; |
| 14 | |
| 15 | /// A styled terminal cell. |
| 16 | #[derive(Clone, Debug, Default, PartialEq, Eq, Hash)] |
| 17 | pub struct Cell { |
| 18 | /// Cell glyph. |
| 19 | glyph: CellGlyph, |
| 20 | /// Cell style. |
| 21 | style: Style, |
| 22 | } |
| 23 | |
| 24 | /// Renderable cell glyph. |
| 25 | #[derive(Clone, Debug, Default, PartialEq, Eq, Hash)] |
| 26 | enum CellGlyph { |
| 27 | /// A blank terminal cell. |
| 28 | #[default] |
| 29 | Blank, |
| 30 | /// A single ASCII terminal glyph. |
| 31 | Ascii(u8), |
| 32 | /// A single non-ASCII terminal glyph. |
| 33 | Unicode(char), |
| 34 | /// A heap-backed multi-scalar terminal glyph. |
| 35 | Grapheme(String), |
| 36 | /// Continuation cell occupied by a wide leading glyph. |
| 37 | Continuation, |
| 38 | } |
| 39 | |
| 40 | impl Cell { |
| 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 { |
| 51 | Self { |
| 52 | glyph: normalize_glyph(glyph.as_ref()), |
| 53 | style, |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | /// Creates a blank cell. |
| 58 | pub fn blank() -> Self { |
| 59 | Self::default() |
| 60 | } |
| 61 | |
| 62 | /// Returns the cell glyph. |
| 63 | pub fn glyph(&self) -> Cow<'_, str> { |
| 64 | match &self.glyph { |
| 65 | CellGlyph::Blank => Cow::Borrowed(" "), |
| 66 | CellGlyph::Ascii(glyph) => { |
| 67 | Cow::Borrowed(str::from_utf8(slice::from_ref(glyph)).unwrap_or(" ")) |
| 68 | } |
| 69 | CellGlyph::Unicode(glyph) => Cow::Owned(glyph.to_string()), |
| 70 | CellGlyph::Grapheme(glyph) => Cow::Borrowed(glyph), |
| 71 | CellGlyph::Continuation => Cow::Borrowed(""), |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | /// Returns the cell style. |
| 76 | pub const fn style(&self) -> Style { |
| 77 | self.style |
| 78 | } |
| 79 | |
| 80 | /// Returns whether this cell is a wide-glyph continuation. |
| 81 | pub fn is_continuation(&self) -> bool { |
| 82 | matches!(self.glyph, CellGlyph::Continuation) |
| 83 | } |
| 84 | |
| 85 | /// Returns this cell as styled display text. |
| 86 | pub fn paint(&self) -> Paint<Cow<'_, str>> { |
| 87 | Paint::new(self.glyph()).with_style(self.style) |
| 88 | } |
| 89 | |
| 90 | /// Returns this cell with a different style. |
| 91 | pub fn with_style(mut self, style: Style) -> Self { |
| 92 | self.style = style; |
| 93 | self |
| 94 | } |
| 95 | |
| 96 | /// Creates a continuation cell. |
| 97 | fn continuation(style: Style) -> Self { |
| 98 | Self { |
| 99 | glyph: CellGlyph::Continuation, |
| 100 | style, |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | /// Creates a cell from one printable ASCII byte. |
| 105 | fn from_printable_ascii(byte: u8, style: Style) -> Self { |
| 106 | Self { |
| 107 | glyph: if byte == b' ' { |
| 108 | CellGlyph::Blank |
| 109 | } else { |
| 110 | CellGlyph::Ascii(byte) |
| 111 | }, |
| 112 | style, |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | /// Returns the terminal width of this cell's leading glyph. |
| 117 | fn width(&self) -> usize { |
| 118 | match &self.glyph { |
| 119 | CellGlyph::Blank | CellGlyph::Ascii(_) => 1, |
| 120 | CellGlyph::Unicode(glyph) => char_width(*glyph), |
| 121 | CellGlyph::Grapheme(glyph) => glyph.width(), |
| 122 | CellGlyph::Continuation => 0, |
| 123 | } |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | /// A contiguous run of dirty cells. |
| 128 | #[derive(Debug, Clone, Copy)] |
| 129 | pub struct DirtyRun<'a> { |
| 130 | /// Start column. |
| 131 | pub x: usize, |
| 132 | /// Row. |
| 133 | pub y: usize, |
| 134 | /// Dirty cells. |
| 135 | pub cells: &'a [Cell], |
| 136 | } |
| 137 | |
| 138 | /// A rectangular styled cell buffer. |
| 139 | #[derive(Clone, Debug, PartialEq, Eq)] |
| 140 | pub struct Buffer { |
| 141 | area: Rect, |
| 142 | cells: Vec<Cell>, |
| 143 | dirty: Vec<bool>, |
| 144 | } |
| 145 | |
| 146 | impl Buffer { |
| 147 | /// Creates an empty buffer for an area. |
| 148 | pub fn empty(area: Rect) -> Self { |
| 149 | let len = area.cols.saturating_mul(area.rows); |
| 150 | Self { |
| 151 | area, |
| 152 | cells: vec![Cell::blank(); len], |
| 153 | dirty: vec![true; len], |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | /// Returns the covered area. |
| 158 | pub const fn area(&self) -> Rect { |
| 159 | self.area |
| 160 | } |
| 161 | |
| 162 | /// Returns the buffer size. |
| 163 | pub const fn size(&self) -> Size { |
| 164 | self.area.size() |
| 165 | } |
| 166 | |
| 167 | /// Returns whether the buffer has no cells. |
| 168 | pub fn is_empty(&self) -> bool { |
| 169 | self.cells.is_empty() |
| 170 | } |
| 171 | |
| 172 | /// Resizes the buffer and marks all cells dirty. |
| 173 | pub fn resize(&mut self, area: Rect) { |
| 174 | if self.area == area { |
| 175 | return; |
| 176 | } |
| 177 | *self = Self::empty(area); |
| 178 | } |
| 179 | |
| 180 | /// Clears the buffer to blank cells. |
| 181 | pub fn clear(&mut self) { |
| 182 | for (cell, dirty) in self.cells.iter_mut().zip(&mut self.dirty) { |
| 183 | if *cell != Cell::blank() { |
| 184 | *cell = Cell::blank(); |
| 185 | *dirty = true; |
| 186 | } |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | /// Marks all cells clean. |
| 191 | pub fn reset_dirty(&mut self) { |
| 192 | self.dirty.fill(false); |
| 193 | } |
| 194 | |
| 195 | /// Returns a cell by absolute position. |
| 196 | pub fn get(&self, x: usize, y: usize) -> Option<&Cell> { |
| 197 | self.index(x, y).map(|index| &self.cells[index]) |
| 198 | } |
| 199 | |
| 200 | /// Sets a cell by absolute position. |
| 201 | pub fn set(&mut self, x: usize, y: usize, cell: Cell) { |
| 202 | if cell.is_continuation() { |
| 203 | self.set_exact(x, y, cell); |
| 204 | return; |
| 205 | } |
| 206 | let width = cell.width().max(1); |
| 207 | self.clear_glyph_at(x, y); |
| 208 | |
| 209 | if width > self.area.right().saturating_sub(x) { |
| 210 | return; |
| 211 | } |
| 212 | let style = cell.style(); |
| 213 | self.set_exact(x, y, cell); |
| 214 | |
| 215 | for dx in 1..width { |
| 216 | self.set_exact(x + dx, y, Cell::continuation(style)); |
| 217 | } |
| 218 | } |
| 219 | |
| 220 | /// Sets a cell without adjusting neighboring wide cells. |
| 221 | fn set_exact(&mut self, x: usize, y: usize, cell: Cell) { |
| 222 | let Some(index) = self.index(x, y) else { |
| 223 | return; |
| 224 | }; |
| 225 | if self.cells[index] != cell { |
| 226 | self.cells[index] = cell; |
| 227 | self.dirty[index] = true; |
| 228 | } |
| 229 | } |
| 230 | |
| 231 | /// Sets one styled glyph by absolute position. |
| 232 | pub fn set_glyph(&mut self, x: usize, y: usize, glyph: impl AsRef<str>, style: 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)); |
| 239 | } |
| 240 | |
| 241 | /// Writes styled text by absolute position. |
| 242 | pub fn set_str(&mut self, x: usize, y: usize, text: &str, style: Style) { |
| 243 | if text.is_ascii() |
| 244 | && text.bytes().all(|byte| !byte.is_ascii_control()) |
| 245 | && self.area.contains(Point::new(x, y)) |
| 246 | { |
| 247 | self.set_ascii_str(x, y, text, style); |
| 248 | return; |
| 249 | } |
| 250 | |
| 251 | let mut cursor = x; |
| 252 | for ch in text.chars() { |
| 253 | if cursor >= self.area.right() { |
| 254 | break; |
| 255 | } |
| 256 | let width = char_width(ch); |
| 257 | if width == 0 { |
| 258 | continue; |
| 259 | } |
| 260 | if width > self.area.right().saturating_sub(cursor) { |
| 261 | break; |
| 262 | } |
| 263 | self.set(cursor, y, Cell::new(ch, style)); |
| 264 | cursor += width; |
| 265 | } |
| 266 | } |
| 267 | |
| 268 | /// Writes printable ASCII text by absolute position. |
| 269 | fn set_ascii_str(&mut self, x: usize, y: usize, text: &str, style: Style) { |
| 270 | let visible = text.len().min(self.area.right() - x); |
| 271 | if visible == 0 { |
| 272 | return; |
| 273 | } |
| 274 | self.clear_glyph_at(x, y); |
| 275 | if x + visible < self.area.right() |
| 276 | && self.get(x + visible, y).is_some_and(Cell::is_continuation) |
| 277 | { |
| 278 | self.clear_glyph_at(x + visible, y); |
| 279 | } |
| 280 | |
| 281 | let start = (y - self.area.y) * self.area.cols + (x - self.area.x); |
| 282 | for (offset, byte) in text.bytes().take(visible).enumerate() { |
| 283 | let cell = Cell::from_printable_ascii(byte, style); |
| 284 | let index = start + offset; |
| 285 | |
| 286 | if self.cells[index] != cell { |
| 287 | self.cells[index] = cell; |
| 288 | self.dirty[index] = true; |
| 289 | } |
| 290 | } |
| 291 | } |
| 292 | |
| 293 | /// Writes a label by absolute position. |
| 294 | pub fn set_label(&mut self, x: usize, y: usize, label: &Label) { |
| 295 | if label.is_ascii() && self.area.contains(Point::new(x, y)) { |
| 296 | self.set_ascii_str(x, y, label.content(), label.paint.style()); |
| 297 | } else { |
| 298 | self.set_str(x, y, label.content(), label.paint.style()); |
| 299 | } |
| 300 | } |
| 301 | |
| 302 | /// Writes a styled line by absolute position. |
| 303 | pub fn set_line(&mut self, x: usize, y: usize, line: &Line) { |
| 304 | let mut cursor = x; |
| 305 | for label in line.labels() { |
| 306 | self.set_label(cursor, y, label); |
| 307 | cursor += label.width(); |
| 308 | if cursor >= self.area.right() { |
| 309 | break; |
| 310 | } |
| 311 | } |
| 312 | } |
| 313 | |
| 314 | /// Fills an area with a cell. |
| 315 | pub fn fill(&mut self, area: Rect, cell: Cell) { |
| 316 | let area = self.area.intersection(area); |
| 317 | for y in area.y..area.bottom() { |
| 318 | let mut x = area.x; |
| 319 | while x < area.right() { |
| 320 | let width = cell.width().max(1); |
| 321 | if width > area.right().saturating_sub(x) { |
| 322 | break; |
| 323 | } |
| 324 | self.set(x, y, cell.clone()); |
| 325 | x += width; |
| 326 | } |
| 327 | } |
| 328 | } |
| 329 | |
| 330 | /// Returns dirty runs based on explicit dirty marks. |
| 331 | pub fn dirty_runs(&self) -> Vec<DirtyRun<'_>> { |
| 332 | self.runs(|index, _| self.dirty[index]) |
| 333 | } |
| 334 | |
| 335 | /// Returns dirty runs by diffing against `previous`. |
| 336 | pub fn diff(&self, previous: &Self) -> Vec<DirtyRun<'_>> { |
| 337 | if self.area != previous.area { |
| 338 | return self.runs(|_, _| true); |
| 339 | } |
| 340 | self.runs(|index, cell| previous.cells[index] != *cell) |
| 341 | } |
| 342 | |
| 343 | /// Returns plain rendered lines. |
| 344 | pub fn lines(&self) -> Vec<String> { |
| 345 | (0..self.area.rows).map(|row| self.line(row)).collect() |
| 346 | } |
| 347 | |
| 348 | /// Returns one plain rendered line by local row index. |
| 349 | pub fn line(&self, row: usize) -> String { |
| 350 | if row >= self.area.rows { |
| 351 | return String::new(); |
| 352 | } |
| 353 | let start = row * self.area.cols; |
| 354 | let end = start + self.area.cols; |
| 355 | |
| 356 | self.cells[start..end] |
| 357 | .iter() |
| 358 | .map(|cell| cell.glyph()) |
| 359 | .collect::<String>() |
| 360 | } |
| 361 | |
| 362 | /// Returns one styled rendered line by local row index. |
| 363 | pub fn styled_line(&self, row: usize) -> Line { |
| 364 | if row >= self.area.rows { |
| 365 | return Line::blank(); |
| 366 | } |
| 367 | let start = row * self.area.cols; |
| 368 | let end = start + self.area.cols; |
| 369 | let mut line = Line::blank(); |
| 370 | let mut content = String::new(); |
| 371 | let mut style = None; |
| 372 | |
| 373 | for cell in &self.cells[start..end] { |
| 374 | if cell.is_continuation() { |
| 375 | continue; |
| 376 | } |
| 377 | let cell_style = cell.style(); |
| 378 | if style.is_some_and(|style| style != cell_style) && !content.is_empty() { |
| 379 | line.push(Label::raw(content, style.unwrap_or_default())); |
| 380 | content = String::new(); |
| 381 | } |
| 382 | style = Some(cell_style); |
| 383 | content.push_str(&cell.glyph()); |
| 384 | } |
| 385 | if !content.is_empty() { |
| 386 | line.push(Label::raw(content, style.unwrap_or_default())); |
| 387 | } |
| 388 | line |
| 389 | } |
| 390 | |
| 391 | /// Computes dirty runs using a cell predicate. |
| 392 | fn runs(&self, mut dirty: impl FnMut(usize, &Cell) -> bool) -> Vec<DirtyRun<'_>> { |
| 393 | let mut runs = Vec::new(); |
| 394 | |
| 395 | for row in 0..self.area.rows { |
| 396 | let start = row * self.area.cols; |
| 397 | let end = start + self.area.cols; |
| 398 | let mut index = start; |
| 399 | |
| 400 | while index < end { |
| 401 | while index < end && !dirty(index, &self.cells[index]) { |
| 402 | index += 1; |
| 403 | } |
| 404 | if index == end { |
| 405 | break; |
| 406 | } |
| 407 | let run_start = index; |
| 408 | while index < end && dirty(index, &self.cells[index]) { |
| 409 | index += 1; |
| 410 | } |
| 411 | runs.push(DirtyRun { |
| 412 | x: self.area.x + run_start - start, |
| 413 | y: self.area.y + row, |
| 414 | cells: &self.cells[run_start..index], |
| 415 | }); |
| 416 | } |
| 417 | } |
| 418 | runs |
| 419 | } |
| 420 | |
| 421 | /// Returns an index for an absolute position. |
| 422 | fn index(&self, x: usize, y: usize) -> Option<usize> { |
| 423 | if !self.area.contains(Point::new(x, y)) { |
| 424 | return None; |
| 425 | } |
| 426 | let local_x = x - self.area.x; |
| 427 | let local_y = y - self.area.y; |
| 428 | |
| 429 | Some(local_y * self.area.cols + local_x) |
| 430 | } |
| 431 | |
| 432 | /// Clears the leading glyph that covers a position. |
| 433 | fn clear_glyph_at(&mut self, x: usize, y: usize) { |
| 434 | if self.index(x, y).is_none() { |
| 435 | return; |
| 436 | } |
| 437 | let mut lead = x; |
| 438 | |
| 439 | while lead > self.area.x { |
| 440 | let Some(cell) = self.get(lead, y) else { |
| 441 | break; |
| 442 | }; |
| 443 | if !cell.is_continuation() { |
| 444 | break; |
| 445 | } |
| 446 | lead -= 1; |
| 447 | } |
| 448 | let width = self.get(lead, y).map_or(1, |cell| cell.width().max(1)); |
| 449 | |
| 450 | for dx in 0..width { |
| 451 | self.set_exact(lead + dx, y, Cell::blank()); |
| 452 | } |
| 453 | } |
| 454 | } |
| 455 | |
| 456 | /// Normalizes arbitrary input into one terminal glyph. |
| 457 | fn normalize_glyph(input: &str) -> CellGlyph { |
| 458 | let mut chars = input.chars(); |
| 459 | let Some(first) = chars.next() else { |
| 460 | return CellGlyph::Blank; |
| 461 | }; |
| 462 | if first.is_control() { |
| 463 | return CellGlyph::Blank; |
| 464 | } |
| 465 | if first.is_ascii() { |
| 466 | return ascii_glyph(first); |
| 467 | } |
| 468 | if !matches!(chars.clone().next(), Some('\u{fe0f}' | '\u{fe0e}')) { |
| 469 | return normalize_char(first); |
| 470 | } |
| 471 | let mut glyph = String::from(first); |
| 472 | |
| 473 | if let Some(second @ ('\u{fe0f}' | '\u{fe0e}')) = chars.next() { |
| 474 | glyph.push(second); |
| 475 | |
| 476 | for ch in chars { |
| 477 | if !matches!(ch, '\u{fe0f}' | '\u{fe0e}') { |
| 478 | break; |
| 479 | } |
| 480 | glyph.push(ch); |
| 481 | } |
| 482 | } |
| 483 | if glyph.width() == 0 { |
| 484 | CellGlyph::Blank |
| 485 | } else { |
| 486 | CellGlyph::Grapheme(glyph) |
| 487 | } |
| 488 | } |
| 489 | |
| 490 | /// Normalizes one character into one terminal glyph. |
| 491 | fn normalize_char(ch: char) -> CellGlyph { |
| 492 | if ch.is_control() || char_width(ch) == 0 { |
| 493 | CellGlyph::Blank |
| 494 | } else if ch.is_ascii() { |
| 495 | ascii_glyph(ch) |
| 496 | } else { |
| 497 | CellGlyph::Unicode(ch) |
| 498 | } |
| 499 | } |
| 500 | |
| 501 | /// Normalizes one non-control ASCII character. |
| 502 | fn ascii_glyph(ch: char) -> CellGlyph { |
| 503 | if ch == ' ' { |
| 504 | CellGlyph::Blank |
| 505 | } else { |
| 506 | CellGlyph::Ascii(ch as u8) |
| 507 | } |
| 508 | } |
| 509 | |
| 510 | #[cfg(test)] |
| 511 | #[allow(clippy::unwrap_used)] |
| 512 | mod tests { |
| 513 | use super::*; |
| 514 | |
| 515 | #[test] |
| 516 | fn cell_normalizes_to_one_terminal_glyph() { |
| 517 | let style = Style::new(crate::Color::Red); |
| 518 | |
| 519 | let cell = Cell::from_glyph("ab", style); |
| 520 | let control = Cell::from_glyph("\n", style); |
| 521 | |
| 522 | assert_eq!(cell.glyph(), "a"); |
| 523 | assert_eq!(cell.style(), style); |
| 524 | assert_eq!(control.glyph(), " "); |
| 525 | assert!(!cell.is_continuation()); |
| 526 | } |
| 527 | |
| 528 | #[test] |
| 529 | fn cell_uses_inline_storage_for_blank_and_ascii_glyphs() { |
| 530 | let style = Style::new(crate::Color::Blue); |
| 531 | |
| 532 | assert!(matches!(Cell::blank().glyph, CellGlyph::Blank)); |
| 533 | assert!(matches!( |
| 534 | Cell::from_glyph(" ", style).glyph, |
| 535 | CellGlyph::Blank |
| 536 | )); |
| 537 | assert!(matches!( |
| 538 | Cell::from_glyph("x", style).glyph, |
| 539 | CellGlyph::Ascii(b'x') |
| 540 | )); |
| 541 | assert!(matches!( |
| 542 | Cell::from_glyph("╭", style).glyph, |
| 543 | CellGlyph::Unicode('╭') |
| 544 | )); |
| 545 | assert!(matches!( |
| 546 | Cell::from_glyph("❤️", style).glyph, |
| 547 | CellGlyph::Grapheme(glyph) if glyph == "❤️" |
| 548 | )); |
| 549 | assert!(matches!( |
| 550 | Cell::from_glyph("λ", style).glyph, |
| 551 | CellGlyph::Unicode('λ') |
| 552 | )); |
| 553 | } |
| 554 | |
| 555 | #[test] |
| 556 | fn buffer_writes_plain_and_styled_text() { |
| 557 | let mut buffer = Buffer::empty(Rect::new(0, 0, 8, 2)); |
| 558 | |
| 559 | buffer.set_str(1, 0, "hi", Style::default()); |
| 560 | buffer.set_line(0, 1, &Line::new("ok")); |
| 561 | |
| 562 | assert_eq!(buffer.lines(), vec![" hi ", "ok "]); |
| 563 | } |
| 564 | |
| 565 | #[test] |
| 566 | fn styled_line_preserves_cell_styles() { |
| 567 | let mut buffer = Buffer::empty(Rect::new(0, 0, 3, 1)); |
| 568 | |
| 569 | buffer.set_str(0, 0, "r", Style::new(crate::Color::Red)); |
| 570 | buffer.set_str(1, 0, "b", Style::new(crate::Color::Blue)); |
| 571 | |
| 572 | let line = buffer.styled_line(0); |
| 573 | |
| 574 | assert_eq!(line.to_string(), "rb "); |
| 575 | assert_eq!(line.labels()[0].paint.style().fg_color(), crate::Color::Red); |
| 576 | assert_eq!( |
| 577 | line.labels()[1].paint.style().fg_color(), |
| 578 | crate::Color::Blue |
| 579 | ); |
| 580 | } |
| 581 | |
| 582 | #[test] |
| 583 | fn ascii_text_clears_wide_glyph_boundaries() { |
| 584 | let mut buffer = Buffer::empty(Rect::new(0, 0, 6, 1)); |
| 585 | |
| 586 | buffer.set_str(0, 0, "🍍🍍", Style::default()); |
| 587 | buffer.set_str(1, 0, "ab", Style::default()); |
| 588 | |
| 589 | assert_eq!(buffer.line(0), " ab "); |
| 590 | } |
| 591 | |
| 592 | #[test] |
| 593 | fn new_buffer_starts_dirty_and_reset_clears_dirty_marks() { |
| 594 | let mut buffer = Buffer::empty(Rect::new(2, 3, 4, 2)); |
| 595 | let runs = buffer.dirty_runs(); |
| 596 | |
| 597 | assert_eq!(runs.len(), 2); |
| 598 | assert_eq!(runs[0].x, 2); |
| 599 | assert_eq!(runs[0].y, 3); |
| 600 | assert_eq!(runs[0].cells.len(), 4); |
| 601 | |
| 602 | buffer.reset_dirty(); |
| 603 | |
| 604 | assert!(buffer.dirty_runs().is_empty()); |
| 605 | } |
| 606 | |
| 607 | #[test] |
| 608 | fn buffer_tracks_dirty_runs() { |
| 609 | let mut previous = Buffer::empty(Rect::new(0, 0, 6, 1)); |
| 610 | previous.reset_dirty(); |
| 611 | let mut next = previous.clone(); |
| 612 | |
| 613 | next.set_str(1, 0, "abc", Style::default()); |
| 614 | |
| 615 | let runs = next.diff(&previous); |
| 616 | assert_eq!(runs.len(), 1); |
| 617 | assert_eq!(runs[0].x, 1); |
| 618 | assert_eq!(runs[0].cells.len(), 3); |
| 619 | } |
| 620 | |
| 621 | #[test] |
| 622 | fn setting_same_cell_after_reset_does_not_mark_dirty() { |
| 623 | let mut buffer = Buffer::empty(Rect::new(0, 0, 3, 1)); |
| 624 | |
| 625 | buffer.reset_dirty(); |
| 626 | buffer.set(0, 0, Cell::blank()); |
| 627 | |
| 628 | assert!(buffer.dirty_runs().is_empty()); |
| 629 | } |
| 630 | |
| 631 | #[test] |
| 632 | fn fill_clips_to_buffer_area_and_out_of_bounds_writes_are_ignored() { |
| 633 | let mut buffer = Buffer::empty(Rect::new(1, 1, 4, 2)); |
| 634 | |
| 635 | buffer.reset_dirty(); |
| 636 | buffer.fill( |
| 637 | Rect::new(3, 0, 4, 4), |
| 638 | Cell::new('x', Style::new(crate::Color::Blue)), |
| 639 | ); |
| 640 | buffer.set(0, 0, Cell::new('z', Style::default())); |
| 641 | |
| 642 | assert_eq!(buffer.lines(), vec![" xx", " xx"]); |
| 643 | assert_eq!(buffer.dirty_runs().len(), 2); |
| 644 | } |
| 645 | |
| 646 | #[test] |
| 647 | fn resize_reallocates_cells_and_marks_new_area_dirty() { |
| 648 | let mut buffer = Buffer::empty(Rect::new(0, 0, 2, 1)); |
| 649 | |
| 650 | buffer.reset_dirty(); |
| 651 | buffer.resize(Rect::new(0, 0, 3, 2)); |
| 652 | |
| 653 | assert_eq!(buffer.size(), Size::new(3, 2)); |
| 654 | assert_eq!(buffer.lines(), vec![" ", " "]); |
| 655 | assert_eq!(buffer.dirty_runs().len(), 2); |
| 656 | } |
| 657 | |
| 658 | #[test] |
| 659 | fn wide_glyphs_occupy_continuation_cells_without_extra_text() { |
| 660 | let mut buffer = Buffer::empty(Rect::new(0, 0, 4, 1)); |
| 661 | |
| 662 | buffer.reset_dirty(); |
| 663 | buffer.set_str(0, 0, "🍍a", Style::default()); |
| 664 | |
| 665 | assert_eq!(buffer.line(0), "🍍a "); |
| 666 | assert_eq!(buffer.get(0, 0).unwrap().glyph(), "🍍"); |
| 667 | assert!(buffer.get(1, 0).unwrap().is_continuation()); |
| 668 | assert_eq!(buffer.get(2, 0).unwrap().glyph(), "a"); |
| 669 | assert_eq!(buffer.dirty_runs()[0].cells.len(), 3); |
| 670 | } |
| 671 | |
| 672 | #[test] |
| 673 | fn overwriting_inside_wide_glyph_clears_the_leading_cell() { |
| 674 | let mut buffer = Buffer::empty(Rect::new(0, 0, 4, 1)); |
| 675 | |
| 676 | buffer.set_str(0, 0, "🍍x", Style::default()); |
| 677 | buffer.reset_dirty(); |
| 678 | buffer.set_str(1, 0, "a", Style::default()); |
| 679 | |
| 680 | assert_eq!(buffer.line(0), " ax "); |
| 681 | assert_eq!(buffer.get(0, 0).unwrap().glyph(), " "); |
| 682 | assert_eq!(buffer.get(1, 0).unwrap().glyph(), "a"); |
| 683 | assert!(!buffer.get(1, 0).unwrap().is_continuation()); |
| 684 | } |
| 685 | |
| 686 | #[test] |
| 687 | fn wide_glyph_that_cannot_fit_is_not_written() { |
| 688 | let mut buffer = Buffer::empty(Rect::new(0, 0, 3, 1)); |
| 689 | |
| 690 | buffer.set_str(2, 0, "🍍", Style::default()); |
| 691 | |
| 692 | assert_eq!(buffer.line(0), " "); |
| 693 | } |
| 694 | |
| 695 | #[test] |
| 696 | fn wide_fill_tiles_by_visual_width() { |
| 697 | let mut buffer = Buffer::empty(Rect::new(0, 0, 5, 1)); |
| 698 | |
| 699 | buffer.fill(buffer.area(), Cell::new('🍍', Style::default())); |
| 700 | |
| 701 | assert_eq!(buffer.line(0), "🍍🍍 "); |
| 702 | assert!(buffer.get(1, 0).unwrap().is_continuation()); |
| 703 | assert!(buffer.get(3, 0).unwrap().is_continuation()); |
| 704 | } |
| 705 | |
| 706 | #[test] |
| 707 | fn line_returns_empty_string_for_out_of_bounds_rows() { |
| 708 | let buffer = Buffer::empty(Rect::new(0, 0, 2, 1)); |
| 709 | |
| 710 | assert_eq!(buffer.line(99), ""); |
| 711 | } |
| 712 | } |