Improvements to `tui-*` crates
41390c541f2ac2db229caf67ba74c6824bfdafd5
These are needed for the proper implementation of the upcoming `diff` crate.
1 parent
07cf7af7
crates/tui-core/src/buffer.rs
+37 -0
| 303 | 303 | .iter() |
|
| 304 | 304 | .map(|cell| cell.glyph()) |
|
| 305 | 305 | .collect::<String>() |
|
| 306 | 306 | } |
|
| 307 | 307 | ||
| 308 | + | /// Returns one styled rendered line by local row index. |
|
| 309 | + | pub fn styled_line(&self, row: usize) -> Line { |
|
| 310 | + | if row >= self.area.rows { |
|
| 311 | + | return Line::blank(); |
|
| 312 | + | } |
|
| 313 | + | let start = row * self.area.cols; |
|
| 314 | + | let end = start + self.area.cols; |
|
| 315 | + | let mut line = Line::blank(); |
|
| 316 | + | ||
| 317 | + | for cell in &self.cells[start..end] { |
|
| 318 | + | if !cell.is_continuation() { |
|
| 319 | + | line.push(Label::from(cell.paint())); |
|
| 320 | + | } |
|
| 321 | + | } |
|
| 322 | + | line |
|
| 323 | + | } |
|
| 324 | + | ||
| 308 | 325 | /// Computes dirty runs using a cell predicate. |
|
| 309 | 326 | fn runs(&self, mut dirty: impl FnMut(usize, &Cell) -> bool) -> Vec<DirtyRun<'_>> { |
|
| 310 | 327 | let mut runs = Vec::new(); |
|
| 311 | 328 | ||
| 312 | 329 | for row in 0..self.area.rows { |
| 474 | 491 | buffer.set_line(0, 1, &Line::new("ok")); |
|
| 475 | 492 | ||
| 476 | 493 | assert_eq!(buffer.lines(), vec![" hi ", "ok "]); |
|
| 477 | 494 | } |
|
| 478 | 495 | ||
| 496 | + | #[test] |
|
| 497 | + | fn styled_line_preserves_cell_styles() { |
|
| 498 | + | let mut buffer = Buffer::empty(Rect::new(0, 0, 3, 1)); |
|
| 499 | + | ||
| 500 | + | buffer.set_str(0, 0, "r", Style::new(crate::Color::Red)); |
|
| 501 | + | buffer.set_str(1, 0, "b", Style::new(crate::Color::Blue)); |
|
| 502 | + | ||
| 503 | + | let line = buffer.styled_line(0); |
|
| 504 | + | ||
| 505 | + | assert_eq!(line.to_string(), "rb "); |
|
| 506 | + | assert_eq!( |
|
| 507 | + | line.labels()[0].paint().style().fg_color(), |
|
| 508 | + | crate::Color::Red |
|
| 509 | + | ); |
|
| 510 | + | assert_eq!( |
|
| 511 | + | line.labels()[1].paint().style().fg_color(), |
|
| 512 | + | crate::Color::Blue |
|
| 513 | + | ); |
|
| 514 | + | } |
|
| 515 | + | ||
| 479 | 516 | #[test] |
|
| 480 | 517 | fn new_buffer_starts_dirty_and_reset_clears_dirty_marks() { |
|
| 481 | 518 | let mut buffer = Buffer::empty(Rect::new(2, 3, 4, 2)); |
|
| 482 | 519 | let runs = buffer.dirty_runs(); |
|
| 483 | 520 |
crates/tui-core/src/hstack.rs
+15 -0
| 208 | 208 | .child(vec![Line::new("aa"), Line::new("bb")]) |
|
| 209 | 209 | .child(Line::new("cc")); |
|
| 210 | 210 | ||
| 211 | 211 | assert_eq!(stack.display(Constraint::tight(10)), "aacc\nbb \n"); |
|
| 212 | 212 | } |
|
| 213 | + | ||
| 214 | + | #[test] |
|
| 215 | + | fn lines_zip_columns_for_multi_row_children() { |
|
| 216 | + | let stack = HStack::default() |
|
| 217 | + | .child(vec![Line::new("aa"), Line::new("bb")]) |
|
| 218 | + | .child(Line::new("cc")); |
|
| 219 | + | ||
| 220 | + | let lines = stack |
|
| 221 | + | .lines(Constraint::tight(10)) |
|
| 222 | + | .into_iter() |
|
| 223 | + | .map(|line| line.to_string()) |
|
| 224 | + | .collect::<Vec<_>>(); |
|
| 225 | + | ||
| 226 | + | assert_eq!(lines, vec!["aacc", "bb "]); |
|
| 227 | + | } |
|
| 213 | 228 | } |
crates/tui-core/src/label.rs
+8 -0
| 80 | 80 | } |
|
| 81 | 81 | ||
| 82 | 82 | fn render(&self, area: crate::Rect, buf: &mut crate::Buffer) { |
|
| 83 | 83 | Line::renders([Line::new(self.clone())], area, buf); |
|
| 84 | 84 | } |
|
| 85 | + | ||
| 86 | + | fn lines(&self, _parent: Constraint) -> Vec<Line> { |
|
| 87 | + | vec![Line::new(self.clone())] |
|
| 88 | + | } |
|
| 89 | + | ||
| 90 | + | fn to_line(&self, _parent: Constraint) -> Line { |
|
| 91 | + | Line::new(self.clone()) |
|
| 92 | + | } |
|
| 85 | 93 | } |
|
| 86 | 94 | ||
| 87 | 95 | impl fmt::Display for Label { |
|
| 88 | 96 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
|
| 89 | 97 | fmt::Display::fmt(&self.0, f) |
crates/tui-core/src/primitive.rs
+12 -0
| 262 | 262 | } |
|
| 263 | 263 | ||
| 264 | 264 | fn render(&self, area: crate::Rect, buf: &mut crate::Buffer) { |
|
| 265 | 265 | Line::renders([self.clone()], area, buf); |
|
| 266 | 266 | } |
|
| 267 | + | ||
| 268 | + | fn lines(&self, _parent: Constraint) -> Vec<Line> { |
|
| 269 | + | vec![self.clone()] |
|
| 270 | + | } |
|
| 271 | + | ||
| 272 | + | fn to_line(&self, _parent: Constraint) -> Line { |
|
| 273 | + | self.clone() |
|
| 274 | + | } |
|
| 267 | 275 | } |
|
| 268 | 276 | ||
| 269 | 277 | impl Widget for Vec<Line> { |
|
| 270 | 278 | fn size(&self, _parent: Constraint) -> Size { |
|
| 271 | 279 | let width = self.iter().map(Line::width).max().unwrap_or_default(); |
| 275 | 283 | } |
|
| 276 | 284 | ||
| 277 | 285 | fn render(&self, area: crate::Rect, buf: &mut crate::Buffer) { |
|
| 278 | 286 | Line::renders(self.clone(), area, buf); |
|
| 279 | 287 | } |
|
| 288 | + | ||
| 289 | + | fn lines(&self, _parent: Constraint) -> Vec<Line> { |
|
| 290 | + | self.clone() |
|
| 291 | + | } |
|
| 280 | 292 | } |
|
| 281 | 293 | ||
| 282 | 294 | impl fmt::Display for Line { |
|
| 283 | 295 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
|
| 284 | 296 | for item in &self.items { |
crates/tui-core/src/table.rs
+86 -28
| 74 | 74 | #[derive(Debug)] |
|
| 75 | 75 | enum Row<const W: usize, T> { |
|
| 76 | 76 | /// Header row. |
|
| 77 | 77 | Header([T; W]), |
|
| 78 | 78 | /// Data row. |
|
| 79 | - | Data([T; W]), |
|
| 79 | + | Data { |
|
| 80 | + | /// Row cells. |
|
| 81 | + | cells: [T; W], |
|
| 82 | + | /// Optional row-wide style applied to spacing and trailing pad cells. |
|
| 83 | + | style: Option<Style>, |
|
| 84 | + | }, |
|
| 80 | 85 | /// Horizontal divider row. |
|
| 81 | 86 | Divider { |
|
| 82 | 87 | /// Horizontal line sigil used for this divider. |
|
| 83 | 88 | sigil: &'static str, |
|
| 84 | 89 | }, |
| 123 | 128 | ||
| 124 | 129 | fn render(&self, area: crate::Rect, buf: &mut crate::Buffer) { |
|
| 125 | 130 | let size = area.size(); |
|
| 126 | 131 | Line::renders(self.lines(Constraint::new(size, size)), area, buf); |
|
| 127 | 132 | } |
|
| 133 | + | ||
| 134 | + | fn lines(&self, parent: Constraint) -> Vec<Line> { |
|
| 135 | + | Table::lines(self, parent) |
|
| 136 | + | } |
|
| 128 | 137 | } |
|
| 129 | 138 | ||
| 130 | 139 | impl<const W: usize, T: Text> Table<W, T> |
|
| 131 | 140 | where |
|
| 132 | 141 | T::Padded: Into<Line>, |
| 151 | 160 | ||
| 152 | 161 | for row in &self.rows { |
|
| 153 | 162 | if lines.len() == max_rows { |
|
| 154 | 163 | break; |
|
| 155 | 164 | } |
|
| 156 | - | let mut line = Line::default(); |
|
| 157 | - | ||
| 158 | 165 | match row { |
|
| 159 | - | Row::Header(cells) | Row::Data(cells) => { |
|
| 160 | - | if let Some(border) = border { |
|
| 161 | - | line.push(border.left(1)); |
|
| 162 | - | } |
|
| 163 | - | for (i, cell) in cells.iter().enumerate() { |
|
| 164 | - | let pad = if i == cells.len() - 1 { |
|
| 165 | - | 0 |
|
| 166 | - | } else { |
|
| 167 | - | self.widths[i] + self.opts.spacing |
|
| 168 | - | }; |
|
| 169 | - | line = line.extend( |
|
| 170 | - | cell.pad(pad) |
|
| 171 | - | .into() |
|
| 172 | - | .style(Style::default().bg(cell.background())), |
|
| 173 | - | ); |
|
| 174 | - | } |
|
| 175 | - | Line::pad(&mut line, cols); |
|
| 176 | - | Line::truncate(&mut line, cols, "…"); |
|
| 177 | - | ||
| 178 | - | if let Some(border) = border { |
|
| 179 | - | line.push(border.right(1)); |
|
| 180 | - | } |
|
| 181 | - | lines.push(line); |
|
| 166 | + | Row::Header(cells) => lines.push(self.cells_line(cells, None, border, cols)), |
|
| 167 | + | Row::Data { cells, style } => { |
|
| 168 | + | lines.push(self.cells_line(cells, *style, border, cols)); |
|
| 182 | 169 | } |
|
| 183 | 170 | Row::Divider { sigil } => { |
|
| 184 | 171 | if let Some(border) = border { |
|
| 185 | 172 | lines.push(border.divider(cols, sigil)); |
|
| 186 | 173 | } else { |
| 194 | 181 | { |
|
| 195 | 182 | lines.push(border.bottom(cols)); |
|
| 196 | 183 | } |
|
| 197 | 184 | lines |
|
| 198 | 185 | } |
|
| 186 | + | ||
| 187 | + | /// Renders one header or data row as a styled line. |
|
| 188 | + | /// |
|
| 189 | + | /// When `style` is `Some`, the row-wide style is applied to the whole line |
|
| 190 | + | /// so trailing pad cells inherit the background. When `None`, each cell |
|
| 191 | + | /// keeps its own background (the header / unstyled data path). |
|
| 192 | + | fn cells_line( |
|
| 193 | + | &self, |
|
| 194 | + | cells: &[T; W], |
|
| 195 | + | style: Option<Style>, |
|
| 196 | + | border: Option<Border>, |
|
| 197 | + | cols: usize, |
|
| 198 | + | ) -> Line { |
|
| 199 | + | let mut line = Line::default(); |
|
| 200 | + | if let Some(border) = border { |
|
| 201 | + | line.push(border.left(1)); |
|
| 202 | + | } |
|
| 203 | + | for (i, cell) in cells.iter().enumerate() { |
|
| 204 | + | let pad = if i + 1 == W { |
|
| 205 | + | 0 |
|
| 206 | + | } else { |
|
| 207 | + | self.widths[i] + self.opts.spacing |
|
| 208 | + | }; |
|
| 209 | + | let cell_style = style.unwrap_or_else(|| Style::default().bg(cell.background())); |
|
| 210 | + | line = line.extend(cell.pad(pad).into().style(cell_style)); |
|
| 211 | + | } |
|
| 212 | + | Line::pad(&mut line, cols); |
|
| 213 | + | Line::truncate(&mut line, cols, "…"); |
|
| 214 | + | if let Some(row_style) = style { |
|
| 215 | + | line = line.style(row_style); |
|
| 216 | + | } |
|
| 217 | + | if let Some(border) = border { |
|
| 218 | + | line.push(border.right(1)); |
|
| 219 | + | } |
|
| 220 | + | line |
|
| 221 | + | } |
|
| 199 | 222 | } |
|
| 200 | 223 | ||
| 201 | 224 | impl<const W: usize, T: Text> Table<W, T> { |
|
| 202 | 225 | /// Creates an empty table with explicit options. |
|
| 203 | 226 | pub fn new(opts: TableOptions) -> Self { |
| 235 | 258 | /// Adds a data row. |
|
| 236 | 259 | pub fn push(&mut self, row: [T; W]) { |
|
| 237 | 260 | for (i, cell) in row.iter().enumerate() { |
|
| 238 | 261 | self.widths[i] = self.widths[i].max(cell.width()); |
|
| 239 | 262 | } |
|
| 240 | - | self.rows.push(Row::Data(row)); |
|
| 263 | + | self.rows.push(Row::Data { |
|
| 264 | + | cells: row, |
|
| 265 | + | style: None, |
|
| 266 | + | }); |
|
| 267 | + | } |
|
| 268 | + | ||
| 269 | + | /// Adds a data row with a row-wide style applied to spacing and trailing pad cells. |
|
| 270 | + | pub fn push_styled(&mut self, row: [T; W], style: Style) { |
|
| 271 | + | for (i, cell) in row.iter().enumerate() { |
|
| 272 | + | self.widths[i] = self.widths[i].max(cell.width()); |
|
| 273 | + | } |
|
| 274 | + | self.rows.push(Row::Data { |
|
| 275 | + | cells: row, |
|
| 276 | + | style: Some(style), |
|
| 277 | + | }); |
|
| 241 | 278 | } |
|
| 242 | 279 | ||
| 243 | 280 | /// Adds a header row. |
|
| 244 | 281 | pub fn header(&mut self, row: [T; W]) { |
|
| 245 | 282 | for (i, cell) in row.iter().enumerate() { |
| 248 | 285 | self.rows.push(Row::Header(row)); |
|
| 249 | 286 | } |
|
| 250 | 287 | ||
| 251 | 288 | /// Returns whether the table contains no data rows. |
|
| 252 | 289 | pub fn is_empty(&self) -> bool { |
|
| 253 | - | !self.rows.iter().any(|r| matches!(r, Row::Data(..))) |
|
| 290 | + | !self.rows.iter().any(|r| matches!(r, Row::Data { .. })) |
|
| 254 | 291 | } |
|
| 255 | 292 | ||
| 256 | 293 | /// Returns the table size inside any border. |
|
| 257 | 294 | fn inner(&self, c: Constraint) -> Size { |
|
| 258 | 295 | let mut outer = self.outer(c); |
| 573 | 610 | assert_eq!(opts.border, Some(Border::new(colors::FAINT))); |
|
| 574 | 611 | assert_eq!(opts.spacing, 3); |
|
| 575 | 612 | assert!(!opts.overflow); |
|
| 576 | 613 | } |
|
| 577 | 614 | ||
| 615 | + | #[test] |
|
| 616 | + | fn push_styled_applies_row_background_to_padding() { |
|
| 617 | + | let mut t = Table::<2, _>::new(TableOptions { |
|
| 618 | + | spacing: 2, |
|
| 619 | + | ..TableOptions::default() |
|
| 620 | + | }); |
|
| 621 | + | ||
| 622 | + | t.push_styled(["a", "b"], Style::new(Color::Unset).bg(Color::Red)); |
|
| 623 | + | let lines = t.lines(Constraint::tight(8)); |
|
| 624 | + | ||
| 625 | + | assert_eq!(lines.len(), 1); |
|
| 626 | + | let line = &lines[0]; |
|
| 627 | + | assert_eq!(line.to_string(), "a b "); |
|
| 628 | + | assert!( |
|
| 629 | + | line.labels() |
|
| 630 | + | .iter() |
|
| 631 | + | .all(|label| label.paint().style().bg_color() == Color::Red), |
|
| 632 | + | "all labels (including spacing/padding) take the row background", |
|
| 633 | + | ); |
|
| 634 | + | } |
|
| 635 | + | ||
| 578 | 636 | #[test] |
|
| 579 | 637 | fn divider_sigil_options_replace_horizontal_lines() { |
|
| 580 | 638 | let mut t = Table::new(TableOptions { |
|
| 581 | 639 | border: Some(Border::new(Color::Unset)), |
|
| 582 | 640 | spacing: 3, |
crates/tui-core/src/widget.rs
+23 -0
| 39 | 39 | self.render(area, &mut buffer); |
|
| 40 | 40 | ||
| 41 | 41 | buffer |
|
| 42 | 42 | } |
|
| 43 | 43 | ||
| 44 | + | /// Returns this widget rendered as styled lines, one per row. |
|
| 45 | + | /// |
|
| 46 | + | /// The default implementation renders into a private buffer and reads each |
|
| 47 | + | /// row back as a styled [`Line`]. Composite widgets that already produce |
|
| 48 | + | /// row-shaped output should override this to skip the buffer round-trip. |
|
| 49 | + | fn lines(&self, constraints: Constraint) -> Vec<Line> { |
|
| 50 | + | let buffer = self.to_buffer(constraints); |
|
| 51 | + | let rows = buffer.area().rows; |
|
| 52 | + | ||
| 53 | + | (0..rows).map(|row| buffer.styled_line(row)).collect() |
|
| 54 | + | } |
|
| 55 | + | ||
| 56 | + | /// Returns the first rendered row of this widget, or a blank line. |
|
| 57 | + | /// |
|
| 58 | + | /// Convenience for callers that compose single-row widgets and want a |
|
| 59 | + | /// `Line` rather than a one-element `Vec<Line>`. |
|
| 60 | + | fn to_line(&self, constraints: Constraint) -> Line { |
|
| 61 | + | self.lines(constraints) |
|
| 62 | + | .into_iter() |
|
| 63 | + | .next() |
|
| 64 | + | .unwrap_or_default() |
|
| 65 | + | } |
|
| 66 | + | ||
| 44 | 67 | /// Returns a plain string representation of this widget. |
|
| 45 | 68 | #[must_use] |
|
| 46 | 69 | fn display(&self, constraints: Constraint) -> String { |
|
| 47 | 70 | let buffer = self.to_buffer(constraints); |
|
| 48 | 71 | let mut out = String::new(); |
crates/tui-widgets/src/scrollbar.rs
+6 -6
| 75 | 75 | self.style = style; |
|
| 76 | 76 | self |
|
| 77 | 77 | } |
|
| 78 | 78 | ||
| 79 | 79 | /// Returns the thumb row range for a rendered height. |
|
| 80 | + | /// |
|
| 81 | + | /// Returns an empty range when the view is not scrollable, so callers see |
|
| 82 | + | /// only the track. The thumb appears once scrolling is actually possible. |
|
| 80 | 83 | pub fn thumb_range(&self, rows: usize) -> Range<usize> { |
|
| 81 | - | if rows == 0 { |
|
| 84 | + | if rows == 0 || !self.view.is_scrollable() { |
|
| 82 | 85 | return 0..0; |
|
| 83 | 86 | } |
|
| 84 | - | if !self.view.is_scrollable() { |
|
| 85 | - | return 0..rows; |
|
| 86 | - | } |
|
| 87 | 87 | let thumb_rows = |
|
| 88 | 88 | proportional(rows, self.view.visible_rows, self.view.content_rows).clamp(1, rows); |
|
| 89 | 89 | let travel = rows - thumb_rows; |
|
| 90 | 90 | let start = if self.view.max_offset() == 0 { |
|
| 91 | 91 | 0 |
| 136 | 136 | use super::*; |
|
| 137 | 137 | use rustik_tui_core as tui_core; |
|
| 138 | 138 | use tui_core::{Color, Style}; |
|
| 139 | 139 | ||
| 140 | 140 | #[test] |
|
| 141 | - | fn thumb_range_fills_area_when_content_fits() { |
|
| 141 | + | fn thumb_range_is_empty_when_content_fits() { |
|
| 142 | 142 | let scrollbar = Scrollbar::new(View::new(0, 3, 5)); |
|
| 143 | 143 | ||
| 144 | - | assert_eq!(scrollbar.thumb_range(5), 0..5); |
|
| 144 | + | assert_eq!(scrollbar.thumb_range(5), 0..0); |
|
| 145 | 145 | } |
|
| 146 | 146 | ||
| 147 | 147 | #[test] |
|
| 148 | 148 | fn thumb_range_tracks_scroll_position() { |
|
| 149 | 149 | let top = Scrollbar::new(View::new(0, 100, 10)); |
crates/tui-widgets/src/scrollview.rs
+81 -27
| 39 | 39 | /// A scrollable collection of lines. |
|
| 40 | 40 | #[derive(Debug, Clone)] |
|
| 41 | 41 | pub struct ScrollView { |
|
| 42 | 42 | lines: Vec<Line>, |
|
| 43 | 43 | offset: usize, |
|
| 44 | + | sticky: Vec<Line>, |
|
| 44 | 45 | scrollbar: ScrollbarStyle, |
|
| 45 | 46 | } |
|
| 46 | 47 | ||
| 47 | 48 | impl ScrollView { |
|
| 49 | + | /// Returns the body width inside the reserved scrollbar column. |
|
| 50 | + | /// |
|
| 51 | + | /// Callers that pre-build content lines should size them to this width so |
|
| 52 | + | /// they exactly fit the body next to the scrollbar. |
|
| 53 | + | pub const fn body_cols(area: Rect) -> usize { |
|
| 54 | + | if area.cols > 2 { |
|
| 55 | + | area.cols - 1 |
|
| 56 | + | } else { |
|
| 57 | + | area.cols |
|
| 58 | + | } |
|
| 59 | + | } |
|
| 60 | + | ||
| 48 | 61 | /// Creates a scroll view from prebuilt render lines. |
|
| 49 | 62 | pub fn from_lines(lines: impl IntoIterator<Item = Line>) -> Self { |
|
| 50 | 63 | Self { |
|
| 51 | 64 | lines: lines.into_iter().collect(), |
|
| 52 | 65 | offset: 0, |
|
| 66 | + | sticky: Vec::new(), |
|
| 53 | 67 | scrollbar: ScrollbarStyle::default(), |
|
| 54 | 68 | } |
|
| 55 | 69 | } |
|
| 56 | 70 | ||
| 57 | 71 | /// Builds a scroll view for an area from a width-sensitive line producer. |
|
| 58 | 72 | /// |
|
| 59 | - | /// The producer is called again with one fewer column when a vertical |
|
| 60 | - | /// scrollbar is needed. |
|
| 73 | + | /// The producer is called once with the body width that ScrollView will use |
|
| 74 | + | /// (see [`body_cols`](Self::body_cols)). |
|
| 61 | 75 | pub fn new(area: Rect, offset: usize, mut lines: impl FnMut(usize) -> Vec<Line>) -> Self { |
|
| 62 | - | let rendered = if area.cols > 2 { |
|
| 63 | - | let rendered = lines(area.cols - 1); |
|
| 64 | - | if rendered.len() > area.rows { |
|
| 65 | - | rendered |
|
| 66 | - | } else { |
|
| 67 | - | lines(area.cols) |
|
| 68 | - | } |
|
| 69 | - | } else { |
|
| 70 | - | lines(area.cols) |
|
| 71 | - | }; |
|
| 72 | - | ||
| 73 | 76 | Self { |
|
| 74 | - | lines: rendered, |
|
| 77 | + | lines: lines(Self::body_cols(area)), |
|
| 75 | 78 | offset, |
|
| 79 | + | sticky: Vec::new(), |
|
| 76 | 80 | scrollbar: ScrollbarStyle::default(), |
|
| 77 | 81 | } |
|
| 78 | 82 | } |
|
| 79 | 83 | ||
| 80 | 84 | /// Sets the first visible row. |
|
| 81 | 85 | pub const fn offset(mut self, offset: usize) -> Self { |
|
| 82 | 86 | self.offset = offset; |
|
| 83 | 87 | self |
|
| 84 | 88 | } |
|
| 85 | 89 | ||
| 90 | + | /// Sets sticky rows pinned at the top of the view above the scrolled body. |
|
| 91 | + | pub fn sticky(mut self, lines: impl IntoIterator<Item = Line>) -> Self { |
|
| 92 | + | self.sticky = lines.into_iter().collect(); |
|
| 93 | + | self |
|
| 94 | + | } |
|
| 95 | + | ||
| 86 | 96 | /// Sets the vertical scrollbar style. |
|
| 87 | 97 | pub fn scrollbar(mut self, style: ScrollbarStyle) -> Self { |
|
| 88 | 98 | self.scrollbar = style; |
|
| 89 | 99 | self |
|
| 90 | 100 | } |
|
| 91 | 101 | ||
| 92 | 102 | /// Returns metrics for a rendered area. |
|
| 93 | 103 | pub fn metrics(&self, area: Rect) -> ScrollMetrics { |
|
| 94 | 104 | let (text_area, _) = self.areas(area); |
|
| 95 | - | let text_rows = text_area.rows; |
|
| 96 | - | let view = View::new(self.offset, self.lines.len(), text_rows); |
|
| 105 | + | let body_rows = text_area.rows.saturating_sub(self.sticky.len()); |
|
| 106 | + | let view = View::new(self.offset, self.lines.len(), body_rows); |
|
| 97 | 107 | ||
| 98 | 108 | ScrollMetrics { |
|
| 99 | 109 | content_rows: self.lines.len(), |
|
| 100 | - | visible_rows: text_rows, |
|
| 110 | + | visible_rows: body_rows, |
|
| 101 | 111 | view, |
|
| 102 | 112 | } |
|
| 103 | 113 | } |
|
| 104 | 114 | ||
| 105 | 115 | /// Splits the render area into text and scrollbar rectangles. |
|
| 106 | 116 | /// |
|
| 107 | - | /// The rightmost column is reserved whenever content overflows vertically |
|
| 108 | - | /// and the area is wide enough to spare it. |
|
| 117 | + | /// The rightmost column is always reserved for the scrollbar when the area |
|
| 118 | + | /// is wide enough to spare it; the scrollbar paints only its track when the |
|
| 119 | + | /// view is not scrollable, so reserving has no thumb-shaped visual cost. |
|
| 109 | 120 | fn areas(&self, area: Rect) -> (Rect, Option<Rect>) { |
|
| 110 | - | if self.lines.len() > area.rows && area.cols > 2 { |
|
| 121 | + | if area.cols > 2 { |
|
| 111 | 122 | ( |
|
| 112 | 123 | Rect::new(area.x, area.y, area.cols - 1, area.rows), |
|
| 113 | 124 | Some(Rect::new(area.right() - 1, area.y, 1, area.rows)), |
|
| 114 | 125 | ) |
|
| 115 | 126 | } else { |
| 126 | 137 | fn render(&self, area: Rect, buf: &mut Buffer) { |
|
| 127 | 138 | if area.is_empty() { |
|
| 128 | 139 | return; |
|
| 129 | 140 | } |
|
| 130 | 141 | let (text_area, scrollbar_area) = self.areas(area); |
|
| 131 | - | let view = View::new(self.offset, self.lines.len(), text_area.rows); |
|
| 132 | - | ||
| 133 | - | view.render_lines(&self.lines, text_area, buf); |
|
| 142 | + | let sticky_rows = self.sticky.len().min(text_area.rows); |
|
| 143 | + | let body_area = Rect::new( |
|
| 144 | + | text_area.x, |
|
| 145 | + | text_area.y + sticky_rows, |
|
| 146 | + | text_area.cols, |
|
| 147 | + | text_area.rows.saturating_sub(sticky_rows), |
|
| 148 | + | ); |
|
| 149 | + | let view = View::new(self.offset, self.lines.len(), body_area.rows); |
|
| 150 | + | ||
| 151 | + | if sticky_rows > 0 { |
|
| 152 | + | let sticky_area = Rect::new(text_area.x, text_area.y, text_area.cols, sticky_rows); |
|
| 153 | + | Line::renders( |
|
| 154 | + | self.sticky.iter().take(sticky_rows).cloned(), |
|
| 155 | + | sticky_area, |
|
| 156 | + | buf, |
|
| 157 | + | ); |
|
| 158 | + | } |
|
| 159 | + | view.render_lines(&self.lines, body_area, buf); |
|
| 134 | 160 | ||
| 135 | 161 | if let Some(area) = scrollbar_area { |
|
| 136 | 162 | Scrollbar::new(view) |
|
| 137 | 163 | .style(self.scrollbar.clone()) |
|
| 138 | 164 | .render(area, buf); |
| 144 | 170 | mod tests { |
|
| 145 | 171 | use super::*; |
|
| 146 | 172 | use rustik_tui_core::{Cell, Color, Style}; |
|
| 147 | 173 | ||
| 148 | 174 | #[test] |
|
| 149 | - | fn scroll_view_rebuilds_width_sensitive_lines_when_scrollbar_is_needed() { |
|
| 175 | + | fn scrollbar_column_shows_track_only_when_content_fits() { |
|
| 176 | + | let view = ScrollView::from_lines([Line::new("hello")]); |
|
| 177 | + | let mut buffer = Buffer::empty(Rect::new(0, 0, 7, 2)); |
|
| 178 | + | ||
| 179 | + | view.render(buffer.area(), &mut buffer); |
|
| 180 | + | ||
| 181 | + | // 6-col body + 1-col scrollbar; non-scrollable view paints track only. |
|
| 182 | + | assert_eq!(buffer.lines(), vec!["hello │", " │"]); |
|
| 183 | + | } |
|
| 184 | + | ||
| 185 | + | #[test] |
|
| 186 | + | fn sticky_rows_pin_to_top_above_scrolled_body() { |
|
| 187 | + | let lines = (0..6) |
|
| 188 | + | .map(|i| Line::new(format!("row{i}"))) |
|
| 189 | + | .collect::<Vec<_>>(); |
|
| 190 | + | let view = ScrollView::from_lines(lines) |
|
| 191 | + | .offset(2) |
|
| 192 | + | .sticky([Line::new("HEAD")]); |
|
| 193 | + | let mut buffer = Buffer::empty(Rect::new(0, 0, 5, 3)); |
|
| 194 | + | ||
| 195 | + | view.render(buffer.area(), &mut buffer); |
|
| 196 | + | ||
| 197 | + | assert_eq!(buffer.lines(), vec!["HEAD│", "row2┃", "row3│"]); |
|
| 198 | + | } |
|
| 199 | + | ||
| 200 | + | #[test] |
|
| 201 | + | fn scroll_view_builds_lines_at_body_width() { |
|
| 150 | 202 | let style = ScrollbarStyle::new( |
|
| 151 | 203 | Cell::new("|", Style::new(Color::Unset)), |
|
| 152 | 204 | Cell::new("#", Style::new(Color::Unset)), |
|
| 153 | 205 | ); |
|
| 154 | - | let view = ScrollView::new(Rect::new(0, 0, 4, 2), 1, |cols| { |
|
| 206 | + | let area = Rect::new(0, 0, 4, 2); |
|
| 207 | + | let view = ScrollView::new(area, 1, |cols| { |
|
| 155 | 208 | (0..3) |
|
| 156 | 209 | .map(|row| Line::new(format!("{row}:{cols}"))) |
|
| 157 | 210 | .collect() |
|
| 158 | 211 | }) |
|
| 159 | 212 | .scrollbar(style); |
|
| 160 | - | let metrics = view.metrics(Rect::new(0, 0, 4, 2)); |
|
| 161 | - | let mut buffer = Buffer::empty(Rect::new(0, 0, 4, 2)); |
|
| 213 | + | let metrics = view.metrics(area); |
|
| 214 | + | let mut buffer = Buffer::empty(area); |
|
| 162 | 215 | ||
| 163 | 216 | view.render(buffer.area(), &mut buffer); |
|
| 164 | 217 | ||
| 218 | + | assert_eq!(ScrollView::body_cols(area), 3); |
|
| 165 | 219 | assert_eq!(metrics.content_rows, 3); |
|
| 166 | 220 | assert_eq!(metrics.visible_rows, 2); |
|
| 167 | 221 | assert_eq!(buffer.lines(), vec!["1:3|", "2:3#"]); |
|
| 168 | 222 | } |
|
| 169 | 223 | } |