diff: Virtualize scroll rendering
07ead6bd84450af13838ed64d6af341ad9a66095
Renders only visible scroll content while keeping sticky rows and viewport behavior intact.
1 parent
7c1e02d6
crates/diff/src/bin/rustik-diff.rs
+0 -0
+0 -0
+0 -0
a.rs
+0 -0
crates/diff/src/highlight.rs
+1 -1
| 259 | 259 | assert!( |
|
| 260 | 260 | result |
|
| 261 | 261 | .line |
|
| 262 | 262 | .labels() |
|
| 263 | 263 | .iter() |
|
| 264 | - | .any(|label| label.paint().style().fg_color() != Color::Unset) |
|
| 264 | + | .any(|label| label.paint.style().fg_color() != Color::Unset) |
|
| 265 | 265 | ); |
|
| 266 | 266 | } |
|
| 267 | 267 | } |
crates/diff/src/renderer.rs
+311 -468
| 4 | 4 | use std::path::PathBuf; |
|
| 5 | 5 | ||
| 6 | 6 | use rustik_tui as tui; |
|
| 7 | 7 | use tui::{ |
|
| 8 | 8 | Buffer, Cell, Color, Constraint, HStack, Label, Line, Placement, Rect, Scroll, ScrollView, |
|
| 9 | - | Scrollbar, ScrollbarStyle, Size, Style, Text, View, Viewport, Widget, colors, |
|
| 9 | + | ScrollbarStyle, Size, Style, Text, View, Viewport, Widget, colors, |
|
| 10 | 10 | }; |
|
| 11 | 11 | ||
| 12 | 12 | use crate::highlight::{HighlightKeyInput, HighlightResult}; |
|
| 13 | 13 | use crate::{ |
|
| 14 | 14 | DiffDocument, DiffLine, DiffLineKind, DiffSide, FileDiff, FileId, FileKind, HighlightKey, |
|
| 15 | - | HighlightRequest, HunkId, RowId, |
|
| 15 | + | HighlightRequest, Hunk, HunkId, RowId, |
|
| 16 | 16 | }; |
|
| 17 | 17 | ||
| 18 | 18 | /// Minimum width before the renderer attempts table-style diff rows. |
|
| 19 | 19 | const MIN_RENDER_COLS: usize = 12; |
|
| 20 | 20 | /// Width of old and new line number cells. |
| 118 | 118 | state: DiffState, |
|
| 119 | 119 | /// Highlighted row cache. |
|
| 120 | 120 | highlights: BTreeMap<HighlightKey, Line>, |
|
| 121 | 121 | /// Requests already handed to a worker. |
|
| 122 | 122 | pending_highlights: BTreeSet<HighlightKey>, |
|
| 123 | - | /// Cached full render rows keyed by render body width. |
|
| 124 | - | rows_cache: BTreeMap<usize, Vec<RenderRow>>, |
|
| 125 | 123 | } |
|
| 126 | 124 | ||
| 127 | 125 | /// One rendered row before viewport projection. |
|
| 128 | 126 | #[derive(Clone, Debug)] |
|
| 129 | 127 | struct RenderRow { |
|
| 130 | 128 | /// Renderable line. |
|
| 131 | 129 | line: Line, |
|
| 132 | - | /// Row role used by scrolling, focus, and highlighting. |
|
| 133 | - | meta: RowMeta, |
|
| 130 | + | /// Source-row metadata when this is a diff source row. |
|
| 131 | + | diff: Option<DiffLineMeta>, |
|
| 134 | 132 | } |
|
| 135 | 133 | ||
| 136 | - | /// Metadata attached to one rendered row. |
|
| 137 | - | #[derive(Clone, Debug, Eq, PartialEq)] |
|
| 138 | - | enum RowMeta { |
|
| 139 | - | /// Preamble row. |
|
| 140 | - | Preamble, |
|
| 141 | - | /// File-card top border. |
|
| 142 | - | FileTop(FileId), |
|
| 143 | - | /// File-card header. |
|
| 144 | - | FileHeader(FileId), |
|
| 145 | - | /// File-card header divider. |
|
| 146 | - | FileDivider(FileId), |
|
| 147 | - | /// File-card hunk divider. |
|
| 148 | - | HunkDivider(FileId), |
|
| 149 | - | /// Hunk header. |
|
| 150 | - | HunkHeader(FileId, HunkId), |
|
| 151 | - | /// Diff source row. |
|
| 152 | - | DiffLine(DiffLineMeta), |
|
| 153 | - | /// File-card bottom border. |
|
| 154 | - | FileBottom(FileId), |
|
| 134 | + | /// Render location of a row inside one file card. |
|
| 135 | + | #[derive(Clone, Copy, Debug, Eq, PartialEq)] |
|
| 136 | + | struct FileRowLocation<'a> { |
|
| 137 | + | /// File containing the row. |
|
| 138 | + | file: &'a FileDiff, |
|
| 139 | + | /// Global row where the file card starts. |
|
| 140 | + | start: usize, |
|
| 141 | + | /// Global row one past the end of the file card. |
|
| 142 | + | end: usize, |
|
| 143 | + | /// Row offset inside the file card. |
|
| 144 | + | local: usize, |
|
| 155 | 145 | } |
|
| 156 | 146 | ||
| 157 | 147 | /// Metadata for one source row. |
|
| 158 | 148 | #[derive(Clone, Debug, Eq, PartialEq)] |
|
| 159 | 149 | struct DiffLineMeta { |
| 190 | 180 | document, |
|
| 191 | 181 | options, |
|
| 192 | 182 | state: DiffState::default(), |
|
| 193 | 183 | highlights: BTreeMap::new(), |
|
| 194 | 184 | pending_highlights: BTreeSet::new(), |
|
| 195 | - | rows_cache: BTreeMap::new(), |
|
| 196 | 185 | } |
|
| 197 | 186 | } |
|
| 198 | 187 | ||
| 199 | 188 | /// Returns the viewed document. |
|
| 200 | 189 | pub const fn document(&self) -> &DiffDocument { |
| 206 | 195 | &self.state |
|
| 207 | 196 | } |
|
| 208 | 197 | ||
| 209 | 198 | /// Replaces the viewer state. |
|
| 210 | 199 | pub fn set_state(&mut self, state: DiffState) { |
|
| 211 | - | if self.state.collapsed_files != state.collapsed_files { |
|
| 212 | - | self.invalidate_rows(); |
|
| 213 | - | } |
|
| 214 | 200 | self.state = state; |
|
| 215 | 201 | } |
|
| 216 | 202 | ||
| 217 | 203 | /// Sets the first visible rendered row. |
|
| 218 | 204 | pub fn set_scroll(&mut self, scroll: usize) { |
| 224 | 210 | self.state.focus = focus; |
|
| 225 | 211 | } |
|
| 226 | 212 | ||
| 227 | 213 | /// Sets the collapsed file sections. |
|
| 228 | 214 | pub fn set_collapsed_files(&mut self, collapsed_files: BTreeSet<FileId>) { |
|
| 229 | - | if self.state.collapsed_files != collapsed_files { |
|
| 230 | - | self.invalidate_rows(); |
|
| 231 | - | } |
|
| 232 | 215 | self.state.collapsed_files = collapsed_files; |
|
| 233 | 216 | } |
|
| 234 | 217 | ||
| 235 | 218 | /// Renders the current frame into `buf`. |
|
| 236 | 219 | pub fn render(&mut self, area: Rect, buf: &mut Buffer) { |
|
| 237 | 220 | if area.is_empty() { |
|
| 238 | 221 | return; |
|
| 239 | 222 | } |
|
| 240 | 223 | let body_cols = ScrollView::body_cols(area); |
|
| 241 | - | self.ensure_rows(body_cols); |
|
| 242 | - | let Some(rows) = self.cached_rows(body_cols) else { |
|
| 243 | - | return; |
|
| 244 | - | }; |
|
| 245 | - | let sticky = Self::sticky_rows(rows, self.state.scroll, area.rows, body_cols); |
|
| 224 | + | let scroll = self.state.scroll; |
|
| 225 | + | let content_rows = self.row_count(); |
|
| 226 | + | let sticky = self.sticky_rows(scroll, area.rows, body_cols); |
|
| 227 | + | // Body offset shifts past sticky so the file chrome is not repeated. |
|
| 228 | + | let body_offset = scroll.saturating_add(sticky.len().min(area.rows)); |
|
| 246 | 229 | ||
| 247 | - | Self::render_visible_rows(rows, self.state.scroll, sticky, area, buf); |
|
| 230 | + | ScrollView::virtualized(content_rows, body_offset, |row, cols| { |
|
| 231 | + | self.render_row(row, cols) |
|
| 232 | + | .map(|row| row.line) |
|
| 233 | + | .unwrap_or_default() |
|
| 234 | + | }) |
|
| 235 | + | .sticky(sticky) |
|
| 236 | + | .scrollbar(Self::scrollbar_style()) |
|
| 237 | + | .render(area, buf); |
|
| 248 | 238 | } |
|
| 249 | 239 | ||
| 250 | 240 | /// Returns the number of rendered rows at a given terminal width. |
|
| 251 | - | pub fn content_rows(&self, cols: usize) -> usize { |
|
| 252 | - | let _ = cols; |
|
| 241 | + | pub fn content_rows(&self, _cols: usize) -> usize { |
|
| 253 | 242 | self.row_count() |
|
| 254 | 243 | } |
|
| 255 | 244 | ||
| 256 | 245 | /// Applies an action to the viewer state. |
|
| 257 | 246 | pub fn apply(&mut self, action: DiffAction, area: Rect) -> ViewChange { |
|
| 258 | 247 | let before = self.state.clone(); |
|
| 259 | - | let body_cols = ScrollView::body_cols(area); |
|
| 260 | 248 | ||
| 261 | 249 | match action { |
|
| 262 | 250 | DiffAction::Scroll(scroll) => { |
|
| 263 | 251 | let content_rows = self.row_count(); |
|
| 264 | 252 | let mut viewport = Viewport::from_offset(self.state.scroll); |
| 269 | 257 | let content_rows = self.row_count(); |
|
| 270 | 258 | let mut viewport = Viewport::from_offset(self.state.scroll); |
|
| 271 | 259 | viewport.clamp(content_rows, area.rows); |
|
| 272 | 260 | self.state.scroll = viewport.offset; |
|
| 273 | 261 | } |
|
| 274 | - | DiffAction::FocusNext => self.focus(body_cols, area.rows, 1, FocusFilter::File), |
|
| 275 | - | DiffAction::FocusPrevious => self.focus(body_cols, area.rows, -1, FocusFilter::File), |
|
| 276 | - | DiffAction::NextHunk => self.focus(body_cols, area.rows, 1, FocusFilter::Hunk), |
|
| 277 | - | DiffAction::PreviousHunk => self.focus(body_cols, area.rows, -1, FocusFilter::Hunk), |
|
| 262 | + | DiffAction::FocusNext => { |
|
| 263 | + | self.focus(area.rows, 1, FocusFilter::File); |
|
| 264 | + | } |
|
| 265 | + | DiffAction::FocusPrevious => { |
|
| 266 | + | self.focus(area.rows, -1, FocusFilter::File); |
|
| 267 | + | } |
|
| 268 | + | DiffAction::NextHunk => { |
|
| 269 | + | self.focus(area.rows, 1, FocusFilter::Hunk); |
|
| 270 | + | } |
|
| 271 | + | DiffAction::PreviousHunk => { |
|
| 272 | + | self.focus(area.rows, -1, FocusFilter::Hunk); |
|
| 273 | + | } |
|
| 278 | 274 | DiffAction::ToggleFocusedFile => { |
|
| 279 | 275 | self.toggle_focused_file(); |
|
| 280 | 276 | } |
|
| 281 | 277 | DiffAction::CollapseFocusedFile => { |
|
| 282 | 278 | self.collapse_focused_file(); |
|
| 283 | 279 | } |
|
| 284 | 280 | } |
|
| 285 | 281 | ||
| 286 | 282 | if self.state.collapsed_files != before.collapsed_files { |
|
| 287 | - | let changed = self |
|
| 288 | - | .state |
|
| 289 | - | .collapsed_files |
|
| 290 | - | .symmetric_difference(&before.collapsed_files) |
|
| 291 | - | .copied() |
|
| 292 | - | .collect::<BTreeSet<_>>(); |
|
| 293 | - | self.refresh_cached_files(&changed); |
|
| 294 | - | self.clamp_scroll(area); |
|
| 283 | + | self.state.scroll = View::new(self.state.scroll, self.row_count(), area.rows).offset; |
|
| 295 | 284 | } |
|
| 296 | 285 | if self.state == before { |
|
| 297 | 286 | ViewChange::Unchanged |
|
| 298 | 287 | } else { |
|
| 299 | 288 | ViewChange::Changed |
|
| 300 | 289 | } |
|
| 301 | 290 | } |
|
| 302 | 291 | ||
| 303 | 292 | /// Returns highlight requests for visible source rows plus configured lookahead. |
|
| 304 | 293 | pub fn visible_highlight_requests(&mut self, area: Rect) -> Vec<HighlightRequest> { |
|
| 305 | - | self.ensure_rows(ScrollView::body_cols(area)); |
|
| 306 | 294 | let take = area.rows.saturating_add(self.options.highlight_lookahead); |
|
| 307 | - | let visible = { |
|
| 308 | - | let body_cols = ScrollView::body_cols(area); |
|
| 309 | - | let Some(rows) = self.cached_rows(body_cols) else { |
|
| 310 | - | return Vec::new(); |
|
| 311 | - | }; |
|
| 312 | - | let view = View::new(self.state.scroll, rows.len(), area.rows); |
|
| 313 | - | let start = |
|
| 314 | - | view.offset |
|
| 315 | - | .saturating_add(Self::sticky_row_count(rows, view.offset, area.rows)); |
|
| 316 | - | ||
| 317 | - | rows.iter() |
|
| 318 | - | .skip(start) |
|
| 319 | - | .take(take) |
|
| 320 | - | .filter_map(|row| match &row.meta { |
|
| 321 | - | RowMeta::DiffLine(meta) => Some(meta.clone()), |
|
| 322 | - | _ => None, |
|
| 323 | - | }) |
|
| 324 | - | .collect::<Vec<_>>() |
|
| 325 | - | }; |
|
| 295 | + | let view = View::new(self.state.scroll, self.row_count(), area.rows); |
|
| 296 | + | let start = view |
|
| 297 | + | .offset |
|
| 298 | + | .saturating_add(self.sticky_row_count(view.offset, area.rows)); |
|
| 299 | + | let visible = (start..start.saturating_add(take)) |
|
| 300 | + | .filter_map(|row| self.render_row(row, ScrollView::body_cols(area))?.diff) |
|
| 301 | + | .collect::<Vec<_>>(); |
|
| 326 | 302 | ||
| 327 | 303 | let mut requests = Vec::new(); |
|
| 328 | 304 | for meta in visible { |
|
| 329 | 305 | let key = HighlightKey::new(HighlightKeyInput { |
|
| 330 | 306 | file: meta.file, |
| 350 | 326 | } |
|
| 351 | 327 | ||
| 352 | 328 | /// Merges highlighted rows and returns whether a redraw is needed. |
|
| 353 | 329 | pub fn apply_highlights(&mut self, results: impl IntoIterator<Item = HighlightResult>) -> bool { |
|
| 354 | 330 | let mut changed = false; |
|
| 355 | - | let mut files = BTreeSet::new(); |
|
| 356 | 331 | for result in results { |
|
| 357 | 332 | self.pending_highlights.remove(&result.key); |
|
| 358 | - | files.insert(result.key.file); |
|
| 359 | 333 | self.highlights.insert(result.key, result.line); |
|
| 360 | 334 | changed = true; |
|
| 361 | 335 | } |
|
| 362 | - | if changed { |
|
| 363 | - | self.refresh_cached_files(&files); |
|
| 364 | - | } |
|
| 365 | 336 | changed |
|
| 366 | 337 | } |
|
| 367 | 338 | ||
| 368 | - | /// Ensures rendered rows are cached for a width. |
|
| 369 | - | fn ensure_rows(&mut self, cols: usize) { |
|
| 370 | - | let cols = cols.max(MIN_RENDER_COLS); |
|
| 371 | - | if !self.rows_cache.contains_key(&cols) { |
|
| 372 | - | self.rows_cache.insert(cols, self.render_rows(cols)); |
|
| 373 | - | } |
|
| 374 | - | } |
|
| 375 | - | ||
| 376 | - | /// Returns cached render rows for a width. |
|
| 377 | - | fn cached_rows(&self, cols: usize) -> Option<&[RenderRow]> { |
|
| 378 | - | self.rows_cache |
|
| 379 | - | .get(&cols.max(MIN_RENDER_COLS)) |
|
| 380 | - | .map(Vec::as_slice) |
|
| 381 | - | } |
|
| 382 | - | ||
| 383 | - | /// Clears cached rendered rows. |
|
| 384 | - | fn invalidate_rows(&mut self) { |
|
| 385 | - | self.rows_cache.clear(); |
|
| 386 | - | } |
|
| 387 | - | ||
| 388 | - | /// Borrows cached rows and dispatches a focus walk against the live state. |
|
| 389 | - | fn focus(&mut self, body_cols: usize, area_rows: usize, delta: isize, filter: FocusFilter) { |
|
| 390 | - | let body_cols = body_cols.max(MIN_RENDER_COLS); |
|
| 391 | - | self.ensure_rows(body_cols); |
|
| 392 | - | let Some(rows) = self.rows_cache.remove(&body_cols) else { |
|
| 393 | - | return; |
|
| 394 | - | }; |
|
| 395 | - | Self::focus_relative(&mut self.state, &rows, area_rows, delta, filter); |
|
| 396 | - | self.rows_cache.insert(body_cols, rows); |
|
| 397 | - | } |
|
| 398 | - | ||
| 399 | 339 | /// Returns the content row count without building render rows. |
|
| 400 | 340 | fn row_count(&self) -> usize { |
|
| 401 | 341 | self.document.preamble.len() |
|
| 402 | 342 | + self |
|
| 403 | 343 | .document |
| 407 | 347 | .sum::<usize>() |
|
| 408 | 348 | } |
|
| 409 | 349 | ||
| 410 | 350 | /// Returns the row count for one file card. |
|
| 411 | 351 | fn file_row_count(&self, file: &FileDiff) -> usize { |
|
| 412 | - | let collapsed = self.state.collapsed_files.contains(&file.id); |
|
| 413 | - | let base_rows = 3; |
|
| 414 | - | if collapsed || file.hunks.is_empty() { |
|
| 415 | - | return base_rows; |
|
| 416 | - | } |
|
| 417 | - | base_rows |
|
| 418 | - | + 1 |
|
| 419 | - | + file.hunks.len() |
|
| 420 | - | + file |
|
| 421 | - | .hunks |
|
| 422 | - | .iter() |
|
| 423 | - | .map(|hunk| hunk.lines.len()) |
|
| 424 | - | .sum::<usize>() |
|
| 425 | - | + file.hunks.len().saturating_sub(1) |
|
| 352 | + | file.rendered_rows(self.state.collapsed_files.contains(&file.id)) |
|
| 426 | 353 | } |
|
| 427 | 354 | ||
| 428 | - | /// Clamps scroll after a row-count-changing action. |
|
| 429 | - | fn clamp_scroll(&mut self, area: Rect) { |
|
| 430 | - | let content_rows = self.row_count(); |
|
| 431 | - | self.state.scroll = View::new(self.state.scroll, content_rows, area.rows).offset; |
|
| 355 | + | /// Returns one rendered row by global row index. |
|
| 356 | + | fn render_row(&self, row: usize, cols: usize) -> Option<RenderRow> { |
|
| 357 | + | let cols = cols.max(MIN_RENDER_COLS); |
|
| 358 | + | if let Some(line) = self.document.preamble.get(row) { |
|
| 359 | + | return Some(RenderRow { |
|
| 360 | + | line: Line::new(line.clone()).style(Style::default().fg(colors::rgb::DIM)), |
|
| 361 | + | diff: None, |
|
| 362 | + | }); |
|
| 363 | + | } |
|
| 364 | + | ||
| 365 | + | self.file_row_location(row) |
|
| 366 | + | .map(|location| self.render_file_row(location, cols)) |
|
| 432 | 367 | } |
|
| 433 | 368 | ||
| 434 | - | /// Refreshes cached rows for files whose highlighted lines changed. |
|
| 435 | - | fn refresh_cached_files(&mut self, files: &BTreeSet<FileId>) { |
|
| 436 | - | if self.rows_cache.is_empty() { |
|
| 437 | - | return; |
|
| 369 | + | /// Returns one rendered row inside a file card. |
|
| 370 | + | fn render_file_row(&self, location: FileRowLocation<'_>, cols: usize) -> RenderRow { |
|
| 371 | + | let file = location.file; |
|
| 372 | + | let border = Self::file_border(); |
|
| 373 | + | let inner_cols = cols.saturating_sub(2); |
|
| 374 | + | let child_cols = cols.saturating_sub(4); |
|
| 375 | + | let collapsed = self.state.collapsed_files.contains(&file.id); |
|
| 376 | + | let local = location.local; |
|
| 377 | + | let bottom = location.end - location.start - 1; |
|
| 378 | + | let wrap = |line| Self::file_child_line(line, child_cols, border); |
|
| 379 | + | ||
| 380 | + | if local == 0 { |
|
| 381 | + | return RenderRow { |
|
| 382 | + | line: border.top(inner_cols), |
|
| 383 | + | diff: None, |
|
| 384 | + | }; |
|
| 438 | 385 | } |
|
| 439 | - | let cols = self.rows_cache.keys().copied().collect::<Vec<_>>(); |
|
| 440 | - | let replacements = cols |
|
| 441 | - | .iter() |
|
| 442 | - | .copied() |
|
| 443 | - | .map(|cols| { |
|
| 444 | - | let files = files |
|
| 445 | - | .iter() |
|
| 446 | - | .filter_map(|file| { |
|
| 447 | - | let document_file = |
|
| 448 | - | self.document.files.iter().find(|item| item.id == *file)?; |
|
| 449 | - | let mut rows = Vec::new(); |
|
| 450 | - | self.push_file_rows(document_file, cols, &mut rows); |
|
| 451 | - | Some((*file, rows)) |
|
| 452 | - | }) |
|
| 453 | - | .collect::<Vec<_>>(); |
|
| 454 | - | (cols, files) |
|
| 455 | - | }) |
|
| 456 | - | .collect::<Vec<_>>(); |
|
| 457 | - | ||
| 458 | - | for (cols, files) in replacements { |
|
| 459 | - | let Some(cache) = self.rows_cache.get_mut(&cols) else { |
|
| 460 | - | continue; |
|
| 386 | + | if local == 1 { |
|
| 387 | + | return RenderRow { |
|
| 388 | + | line: wrap(file.header_line(child_cols)), |
|
| 389 | + | diff: None, |
|
| 461 | 390 | }; |
|
| 462 | - | for (file, rows) in files { |
|
| 463 | - | let Some(start) = cache |
|
| 464 | - | .iter() |
|
| 465 | - | .position(|row| row.meta == RowMeta::FileTop(file)) |
|
| 466 | - | else { |
|
| 467 | - | continue; |
|
| 391 | + | } |
|
| 392 | + | if local == bottom { |
|
| 393 | + | return RenderRow { |
|
| 394 | + | line: border.bottom(inner_cols), |
|
| 395 | + | diff: None, |
|
| 396 | + | }; |
|
| 397 | + | } |
|
| 398 | + | if !file.has_body(collapsed) { |
|
| 399 | + | return RenderRow { |
|
| 400 | + | line: border.bottom(inner_cols), |
|
| 401 | + | diff: None, |
|
| 402 | + | }; |
|
| 403 | + | } |
|
| 404 | + | if local == 2 { |
|
| 405 | + | return RenderRow { |
|
| 406 | + | line: border.divider(inner_cols, DIVIDER), |
|
| 407 | + | diff: None, |
|
| 408 | + | }; |
|
| 409 | + | } |
|
| 410 | + | let mut header_rows = file.hunk_header_rows(collapsed).peekable(); |
|
| 411 | + | while let Some(&(hunk, header_row)) = header_rows.peek() { |
|
| 412 | + | let body_end = header_row + 1 + hunk.lines.len(); |
|
| 413 | + | if local < header_row { |
|
| 414 | + | return RenderRow { |
|
| 415 | + | line: wrap(Self::hunk_gap_line()), |
|
| 416 | + | diff: None, |
|
| 468 | 417 | }; |
|
| 469 | - | let end = cache |
|
| 470 | - | .iter() |
|
| 471 | - | .enumerate() |
|
| 472 | - | .skip(start + 1) |
|
| 473 | - | .find_map(|(row, candidate)| match candidate.meta { |
|
| 474 | - | RowMeta::FileBottom(candidate) if candidate == file => Some(row + 1), |
|
| 475 | - | _ => None, |
|
| 476 | - | }) |
|
| 477 | - | .unwrap_or(start + 1); |
|
| 478 | - | ||
| 479 | - | cache.splice(start..end, rows); |
|
| 480 | 418 | } |
|
| 419 | + | if local == header_row { |
|
| 420 | + | return RenderRow { |
|
| 421 | + | line: wrap(hunk.header_line()), |
|
| 422 | + | diff: None, |
|
| 423 | + | }; |
|
| 424 | + | } |
|
| 425 | + | if local < body_end { |
|
| 426 | + | let row = self.hunk_row_at(file, hunk, local - header_row - 1, child_cols); |
|
| 427 | + | return RenderRow { |
|
| 428 | + | line: wrap(row.line), |
|
| 429 | + | diff: row.diff, |
|
| 430 | + | }; |
|
| 431 | + | } |
|
| 432 | + | header_rows.next(); |
|
| 481 | 433 | } |
|
| 482 | - | } |
|
| 483 | - | ||
| 484 | - | /// Builds all render rows for a width. |
|
| 485 | - | fn render_rows(&self, cols: usize) -> Vec<RenderRow> { |
|
| 486 | - | let cols = cols.max(MIN_RENDER_COLS); |
|
| 487 | - | let mut rows = Vec::new(); |
|
| 488 | - | ||
| 489 | - | for line in &self.document.preamble { |
|
| 490 | - | rows.push(RenderRow { |
|
| 491 | - | line: Line::new(line.clone()).style(Style::default().fg(colors::rgb::DIM)), |
|
| 492 | - | meta: RowMeta::Preamble, |
|
| 493 | - | }); |
|
| 494 | - | } |
|
| 495 | - | for file in &self.document.files { |
|
| 496 | - | self.push_file_rows(file, cols, &mut rows); |
|
| 434 | + | RenderRow { |
|
| 435 | + | line: border.bottom(inner_cols), |
|
| 436 | + | diff: None, |
|
| 497 | 437 | } |
|
| 498 | - | rows |
|
| 499 | 438 | } |
|
| 500 | 439 | ||
| 501 | - | /// Adds one file card to render rows. |
|
| 502 | - | fn push_file_rows(&self, file: &FileDiff, cols: usize, rows: &mut Vec<RenderRow>) { |
|
| 503 | - | let border = Self::file_border(); |
|
| 504 | - | let collapsed = self.state.collapsed_files.contains(&file.id); |
|
| 505 | - | let child_cols = cols.saturating_sub(4); |
|
| 506 | - | ||
| 507 | - | rows.push(RenderRow { |
|
| 508 | - | line: border.top(cols.saturating_sub(2)), |
|
| 509 | - | meta: RowMeta::FileTop(file.id), |
|
| 510 | - | }); |
|
| 511 | - | rows.push(RenderRow { |
|
| 512 | - | line: Self::file_child_line(file.header_line(child_cols), child_cols, border), |
|
| 513 | - | meta: RowMeta::FileHeader(file.id), |
|
| 514 | - | }); |
|
| 515 | - | if !collapsed && !file.hunks.is_empty() { |
|
| 516 | - | rows.push(RenderRow { |
|
| 517 | - | line: border.divider(cols.saturating_sub(2), DIVIDER), |
|
| 518 | - | meta: RowMeta::FileDivider(file.id), |
|
| 519 | - | }); |
|
| 520 | - | for (index, hunk) in file.hunks.iter().enumerate() { |
|
| 521 | - | if index > 0 { |
|
| 522 | - | rows.push(RenderRow { |
|
| 523 | - | line: Self::file_child_line(Self::hunk_gap_line(), child_cols, border), |
|
| 524 | - | meta: RowMeta::HunkDivider(file.id), |
|
| 525 | - | }); |
|
| 526 | - | } |
|
| 527 | - | rows.push(RenderRow { |
|
| 528 | - | line: Self::file_child_line(hunk.header_line(), child_cols, border), |
|
| 529 | - | meta: RowMeta::HunkHeader(file.id, hunk.id), |
|
| 440 | + | /// Returns the file location for a global rendered row. |
|
| 441 | + | fn file_row_location(&self, row: usize) -> Option<FileRowLocation<'_>> { |
|
| 442 | + | let mut start = self.document.preamble.len(); |
|
| 443 | + | for file in &self.document.files { |
|
| 444 | + | let end = start + self.file_row_count(file); |
|
| 445 | + | if (start..end).contains(&row) { |
|
| 446 | + | return Some(FileRowLocation { |
|
| 447 | + | file, |
|
| 448 | + | start, |
|
| 449 | + | end, |
|
| 450 | + | local: row - start, |
|
| 530 | 451 | }); |
|
| 531 | - | for row in self.hunk_rows(file, hunk, child_cols) { |
|
| 532 | - | rows.push(RenderRow { |
|
| 533 | - | line: Self::file_child_line(row.line, child_cols, border), |
|
| 534 | - | meta: row.meta, |
|
| 535 | - | }); |
|
| 536 | - | } |
|
| 537 | 452 | } |
|
| 453 | + | start = end; |
|
| 538 | 454 | } |
|
| 539 | - | rows.push(RenderRow { |
|
| 540 | - | line: border.bottom(cols.saturating_sub(2)), |
|
| 541 | - | meta: RowMeta::FileBottom(file.id), |
|
| 542 | - | }); |
|
| 455 | + | None |
|
| 543 | 456 | } |
|
| 544 | 457 | ||
| 545 | 458 | /// Wraps one file-card child row with padding and borders. |
|
| 546 | 459 | fn file_child_line(mut line: Line, child_cols: usize, border: tui::Border) -> Line { |
|
| 547 | 460 | Line::truncate(&mut line, child_cols, "…"); |
|
| 548 | 461 | Line::pad(&mut line, child_cols); |
|
| 549 | 462 | Line::new(border.left(1)).extend(line).item(border.right(1)) |
|
| 550 | 463 | } |
|
| 551 | 464 | ||
| 552 | 465 | /// Returns sticky file-header rows for the current offset. |
|
| 553 | - | fn sticky_rows( |
|
| 554 | - | rows: &[RenderRow], |
|
| 555 | - | scroll: usize, |
|
| 556 | - | visible_rows: usize, |
|
| 557 | - | cols: usize, |
|
| 558 | - | ) -> Vec<Line> { |
|
| 559 | - | Self::sticky_start(rows, scroll, visible_rows) |
|
| 466 | + | fn sticky_rows(&self, scroll: usize, visible_rows: usize, cols: usize) -> Vec<Line> { |
|
| 467 | + | self.sticky_start(scroll, visible_rows) |
|
| 560 | 468 | .map(|start| { |
|
| 561 | - | rows.iter() |
|
| 562 | - | .skip(start) |
|
| 563 | - | .take(STICKY_HEADER_ROWS) |
|
| 564 | - | .enumerate() |
|
| 565 | - | .map(|(index, row)| { |
|
| 469 | + | (0..STICKY_HEADER_ROWS) |
|
| 470 | + | .map(|index| { |
|
| 566 | 471 | if index == STICKY_HEADER_ROWS - 1 { |
|
| 567 | 472 | Self::sticky_divider(cols) |
|
| 568 | 473 | } else { |
|
| 569 | - | row.line.clone() |
|
| 474 | + | self.render_row(start + index, cols) |
|
| 475 | + | .map(|row| row.line) |
|
| 476 | + | .unwrap_or_default() |
|
| 570 | 477 | } |
|
| 571 | 478 | }) |
|
| 572 | 479 | .collect() |
|
| 573 | 480 | }) |
|
| 574 | 481 | .unwrap_or_default() |
|
| 575 | 482 | } |
|
| 576 | 483 | ||
| 577 | 484 | /// Returns the number of sticky rows for the current offset. |
|
| 578 | - | fn sticky_row_count(rows: &[RenderRow], scroll: usize, visible_rows: usize) -> usize { |
|
| 579 | - | Self::sticky_start(rows, scroll, visible_rows).map_or(0, |_| STICKY_HEADER_ROWS) |
|
| 485 | + | fn sticky_row_count(&self, scroll: usize, visible_rows: usize) -> usize { |
|
| 486 | + | self.sticky_start(scroll, visible_rows) |
|
| 487 | + | .map_or(0, |_| STICKY_HEADER_ROWS) |
|
| 580 | 488 | } |
|
| 581 | 489 | ||
| 582 | 490 | /// Returns the source row for the sticky header at the current offset. |
|
| 583 | - | fn sticky_start(rows: &[RenderRow], scroll: usize, visible_rows: usize) -> Option<usize> { |
|
| 584 | - | let scroll = scroll.min(rows.len().saturating_sub(visible_rows)); |
|
| 491 | + | fn sticky_start(&self, scroll: usize, visible_rows: usize) -> Option<usize> { |
|
| 492 | + | let rows = self.row_count(); |
|
| 493 | + | let scroll = scroll.min(rows.saturating_sub(visible_rows)); |
|
| 585 | 494 | if scroll == 0 { |
|
| 586 | 495 | return None; |
|
| 587 | 496 | } |
|
| 588 | - | let file = rows.get(scroll).and_then(|row| row.meta.file())?; |
|
| 589 | - | let start = rows |
|
| 590 | - | .iter() |
|
| 591 | - | .position(|row| row.meta == RowMeta::FileTop(file))?; |
|
| 592 | - | if scroll <= start { |
|
| 497 | + | let location = self.file_row_location(scroll)?; |
|
| 498 | + | if scroll <= location.start { |
|
| 593 | 499 | return None; |
|
| 594 | 500 | } |
|
| 595 | - | let file_bottom = rows |
|
| 596 | - | .iter() |
|
| 597 | - | .enumerate() |
|
| 598 | - | .skip(start + 1) |
|
| 599 | - | .find_map(|(row, candidate)| match candidate.meta { |
|
| 600 | - | RowMeta::FileBottom(candidate) if candidate == file => Some(row), |
|
| 601 | - | _ => None, |
|
| 602 | - | }) |
|
| 603 | - | .unwrap_or(rows.len()); |
|
| 501 | + | let file_bottom = location.end.saturating_sub(1); |
|
| 604 | 502 | if file_bottom < scroll.saturating_add(visible_rows) { |
|
| 605 | 503 | return None; |
|
| 606 | 504 | } |
|
| 607 | - | Some(start) |
|
| 505 | + | Some(location.start) |
|
| 608 | 506 | } |
|
| 609 | 507 | ||
| 610 | 508 | /// Focuses a target relative to the current focus. |
|
| 611 | - | fn focus_relative( |
|
| 612 | - | state: &mut DiffState, |
|
| 613 | - | rows: &[RenderRow], |
|
| 614 | - | visible_rows: usize, |
|
| 615 | - | delta: isize, |
|
| 616 | - | filter: FocusFilter, |
|
| 617 | - | ) { |
|
| 618 | - | let targets = filter.targets(rows); |
|
| 509 | + | fn focus(&mut self, visible_rows: usize, delta: isize, filter: FocusFilter) { |
|
| 510 | + | let targets = self.focus_targets(filter); |
|
| 619 | 511 | if targets.is_empty() { |
|
| 620 | 512 | return; |
|
| 621 | 513 | } |
|
| 622 | - | let current = state |
|
| 514 | + | let current = self |
|
| 515 | + | .state |
|
| 623 | 516 | .focus |
|
| 624 | 517 | .and_then(|focus| targets.iter().position(|target| target.focus == focus)); |
|
| 625 | 518 | let next = match (current, delta.is_negative()) { |
|
| 626 | 519 | (Some(index), true) => index.saturating_sub(1), |
|
| 627 | 520 | (Some(index), false) => index.saturating_add(1).min(targets.len() - 1), |
| 630 | 523 | }; |
|
| 631 | 524 | let Some(target) = targets.get(next) else { |
|
| 632 | 525 | return; |
|
| 633 | 526 | }; |
|
| 634 | 527 | ||
| 635 | - | state.focus = Some(target.focus); |
|
| 528 | + | self.state.focus = Some(target.focus); |
|
| 636 | 529 | if filter == FocusFilter::File { |
|
| 637 | - | state.scroll = View::new(target.row, rows.len(), visible_rows).offset; |
|
| 530 | + | self.state.scroll = View::new(target.row, self.row_count(), visible_rows).offset; |
|
| 638 | 531 | return; |
|
| 639 | 532 | } |
|
| 640 | 533 | ||
| 641 | - | let mut viewport = Viewport::from_offset(state.scroll); |
|
| 534 | + | let mut viewport = Viewport::from_offset(self.state.scroll); |
|
| 642 | 535 | let margin = visible_rows / 4; |
|
| 643 | 536 | viewport.scroll_to_row( |
|
| 644 | 537 | target.row, |
|
| 645 | - | rows.len(), |
|
| 538 | + | self.row_count(), |
|
| 646 | 539 | visible_rows, |
|
| 647 | 540 | Placement::Margin { |
|
| 648 | - | before: margin.max(Self::sticky_row_count(rows, state.scroll, visible_rows)), |
|
| 541 | + | before: margin.max(self.sticky_row_count(self.state.scroll, visible_rows)), |
|
| 649 | 542 | after: margin, |
|
| 650 | 543 | }, |
|
| 651 | 544 | ); |
|
| 652 | - | state.scroll = viewport.offset; |
|
| 545 | + | self.state.scroll = viewport.offset; |
|
| 546 | + | } |
|
| 547 | + | ||
| 548 | + | /// Returns focusable targets in rendered row order. |
|
| 549 | + | fn focus_targets(&self, filter: FocusFilter) -> Vec<FocusTarget> { |
|
| 550 | + | let mut targets = Vec::new(); |
|
| 551 | + | let mut row = self.document.preamble.len(); |
|
| 552 | + | ||
| 553 | + | for file in &self.document.files { |
|
| 554 | + | let collapsed = self.state.collapsed_files.contains(&file.id); |
|
| 555 | + | if filter == FocusFilter::File { |
|
| 556 | + | targets.push(FocusTarget { |
|
| 557 | + | focus: DiffFocus::File(file.id), |
|
| 558 | + | row, |
|
| 559 | + | }); |
|
| 560 | + | } |
|
| 561 | + | if filter == FocusFilter::Hunk { |
|
| 562 | + | for (hunk, local) in file.hunk_header_rows(collapsed) { |
|
| 563 | + | targets.push(FocusTarget { |
|
| 564 | + | focus: DiffFocus::Hunk(hunk.id), |
|
| 565 | + | row: row + local, |
|
| 566 | + | }); |
|
| 567 | + | } |
|
| 568 | + | } |
|
| 569 | + | row += file.rendered_rows(collapsed); |
|
| 570 | + | } |
|
| 571 | + | ||
| 572 | + | targets |
|
| 653 | 573 | } |
|
| 654 | 574 | ||
| 655 | 575 | /// Toggles the file containing the current focus. |
|
| 656 | 576 | fn toggle_focused_file(&mut self) { |
|
| 657 | 577 | let Some(file) = self.focused_file() else { |
| 683 | 603 | .find(|file| file.hunks.iter().any(|candidate| candidate.id == hunk)) |
|
| 684 | 604 | .map(|file| file.id), |
|
| 685 | 605 | } |
|
| 686 | 606 | } |
|
| 687 | 607 | ||
| 688 | - | /// Returns hunk table rows. |
|
| 689 | - | fn hunk_rows(&self, file: &FileDiff, hunk: &crate::Hunk, inner: usize) -> Vec<RenderRow> { |
|
| 690 | - | let mut rows = Vec::with_capacity(hunk.lines.len()); |
|
| 608 | + | /// Returns one rendered row inside a hunk. |
|
| 609 | + | fn hunk_row_at( |
|
| 610 | + | &self, |
|
| 611 | + | file: &FileDiff, |
|
| 612 | + | hunk: &crate::Hunk, |
|
| 613 | + | line_index: usize, |
|
| 614 | + | inner: usize, |
|
| 615 | + | ) -> RenderRow { |
|
| 616 | + | let source_cols = inner.saturating_sub(LINE_NUMBER_WIDTH * 2 + MARKER_WIDTH); |
|
| 691 | 617 | let mut old_line = hunk.header.old_line_no; |
|
| 692 | 618 | let mut new_line = hunk.header.new_line_no; |
|
| 693 | - | let source_cols = inner.saturating_sub(LINE_NUMBER_WIDTH * 2 + MARKER_WIDTH); |
|
| 694 | - | ||
| 695 | - | for line in &hunk.lines { |
|
| 696 | - | rows.push(self.hunk_row( |
|
| 697 | - | file, |
|
| 698 | - | hunk.id, |
|
| 699 | - | line, |
|
| 700 | - | &mut old_line, |
|
| 701 | - | &mut new_line, |
|
| 702 | - | source_cols, |
|
| 703 | - | )); |
|
| 619 | + | for line in &hunk.lines[..line_index] { |
|
| 620 | + | old_line = old_line.saturating_add(line.kind.old_len()); |
|
| 621 | + | new_line = new_line.saturating_add(line.kind.new_len()); |
|
| 704 | 622 | } |
|
| 705 | 623 | ||
| 706 | - | rows |
|
| 624 | + | self.hunk_row( |
|
| 625 | + | file, |
|
| 626 | + | hunk.id, |
|
| 627 | + | &hunk.lines[line_index], |
|
| 628 | + | &mut old_line, |
|
| 629 | + | &mut new_line, |
|
| 630 | + | source_cols, |
|
| 631 | + | ) |
|
| 707 | 632 | } |
|
| 708 | 633 | ||
| 709 | 634 | /// Returns one rendered hunk row. |
|
| 710 | 635 | fn hunk_row( |
|
| 711 | 636 | &self, |
| 714 | 639 | line: &DiffLine, |
|
| 715 | 640 | old_line: &mut u32, |
|
| 716 | 641 | new_line: &mut u32, |
|
| 717 | 642 | source_cols: usize, |
|
| 718 | 643 | ) -> RenderRow { |
|
| 719 | - | match line.kind { |
|
| 720 | - | DiffLineKind::Addition => { |
|
| 721 | - | let source_line = *new_line; |
|
| 722 | - | let meta = DiffLineMeta::new(file, hunk, line, DiffSide::New, source_line); |
|
| 723 | - | let rendered = self.hunk_line( |
|
| 724 | - | [ |
|
| 725 | - | Self::blank_number(colors::rgb::POSITIVE), |
|
| 726 | - | Self::number_cell( |
|
| 727 | - | source_line, |
|
| 728 | - | colors::rgb::POSITIVE_LIGHT, |
|
| 729 | - | colors::rgb::POSITIVE, |
|
| 730 | - | ), |
|
| 731 | - | Self::marker_cell(" + ", colors::rgb::POSITIVE_LIGHT), |
|
| 732 | - | ], |
|
| 733 | - | self.highlighted_or_plain(&meta, source_cols), |
|
| 734 | - | source_cols, |
|
| 735 | - | Style::default().bg(colors::rgb::POSITIVE_DARK), |
|
| 736 | - | ); |
|
| 737 | - | *new_line = new_line.saturating_add(1); |
|
| 738 | - | RenderRow { |
|
| 739 | - | line: rendered, |
|
| 740 | - | meta: RowMeta::DiffLine(meta), |
|
| 741 | - | } |
|
| 742 | - | } |
|
| 743 | - | DiffLineKind::Deletion => { |
|
| 744 | - | let source_line = *old_line; |
|
| 745 | - | let meta = DiffLineMeta::new(file, hunk, line, DiffSide::Old, source_line); |
|
| 746 | - | let rendered = self.hunk_line( |
|
| 747 | - | [ |
|
| 748 | - | Self::number_cell( |
|
| 749 | - | source_line, |
|
| 750 | - | colors::rgb::NEGATIVE_LIGHT, |
|
| 751 | - | colors::rgb::NEGATIVE, |
|
| 752 | - | ), |
|
| 753 | - | Self::blank_number(colors::rgb::NEGATIVE), |
|
| 754 | - | Self::marker_cell(" - ", colors::rgb::NEGATIVE_LIGHT), |
|
| 755 | - | ], |
|
| 756 | - | self.highlighted_or_plain(&meta, source_cols), |
|
| 757 | - | source_cols, |
|
| 758 | - | Style::default().bg(colors::rgb::NEGATIVE_DARK).dim(), |
|
| 759 | - | ); |
|
| 760 | - | *old_line = old_line.saturating_add(1); |
|
| 761 | - | RenderRow { |
|
| 762 | - | line: rendered, |
|
| 763 | - | meta: RowMeta::DiffLine(meta), |
|
| 764 | - | } |
|
| 765 | - | } |
|
| 766 | - | DiffLineKind::Context => { |
|
| 767 | - | let source_line = *new_line; |
|
| 768 | - | let meta = DiffLineMeta::new(file, hunk, line, DiffSide::New, source_line); |
|
| 769 | - | let rendered = self.hunk_line( |
|
| 770 | - | [ |
|
| 771 | - | Self::number_cell(*old_line, colors::rgb::DIM, colors::rgb::FAINT), |
|
| 772 | - | Self::number_cell(source_line, colors::rgb::DIM, colors::rgb::FAINT), |
|
| 773 | - | Self::marker_cell(" ", Color::Unset), |
|
| 774 | - | ], |
|
| 775 | - | self.highlighted_or_plain(&meta, source_cols), |
|
| 776 | - | source_cols, |
|
| 777 | - | Style::default(), |
|
| 778 | - | ); |
|
| 779 | - | *old_line = old_line.saturating_add(1); |
|
| 780 | - | *new_line = new_line.saturating_add(1); |
|
| 781 | - | RenderRow { |
|
| 782 | - | line: rendered, |
|
| 783 | - | meta: RowMeta::DiffLine(meta), |
|
| 784 | - | } |
|
| 785 | - | } |
|
| 786 | - | DiffLineKind::NoNewline => { |
|
| 787 | - | let meta = DiffLineMeta::new(file, hunk, line, DiffSide::New, *new_line); |
|
| 788 | - | RenderRow { |
|
| 789 | - | line: self.hunk_line( |
|
| 790 | - | [ |
|
| 791 | - | Line::blank().padded(LINE_NUMBER_WIDTH), |
|
| 792 | - | Line::blank().padded(LINE_NUMBER_WIDTH), |
|
| 793 | - | Self::marker_cell(" ", Color::Unset), |
|
| 794 | - | ], |
|
| 795 | - | Line::new(NO_NEWLINE_MARKER).style(Style::default().fg(colors::rgb::DIM)), |
|
| 796 | - | source_cols, |
|
| 797 | - | Style::default(), |
|
| 644 | + | let side = match line.kind { |
|
| 645 | + | DiffLineKind::Deletion => DiffSide::Old, |
|
| 646 | + | _ => DiffSide::New, |
|
| 647 | + | }; |
|
| 648 | + | let source_line = match side { |
|
| 649 | + | DiffSide::Old => *old_line, |
|
| 650 | + | DiffSide::New => *new_line, |
|
| 651 | + | }; |
|
| 652 | + | let meta = DiffLineMeta::new(file, hunk, line, side, source_line); |
|
| 653 | + | let (cells, source, style) = match line.kind { |
|
| 654 | + | DiffLineKind::Addition => ( |
|
| 655 | + | [ |
|
| 656 | + | Self::blank_number(colors::rgb::POSITIVE), |
|
| 657 | + | Self::number_cell( |
|
| 658 | + | source_line, |
|
| 659 | + | colors::rgb::POSITIVE_LIGHT, |
|
| 660 | + | colors::rgb::POSITIVE, |
|
| 798 | 661 | ), |
|
| 799 | - | meta: RowMeta::DiffLine(meta), |
|
| 800 | - | } |
|
| 801 | - | } |
|
| 662 | + | Self::marker_cell(" + ", colors::rgb::POSITIVE_LIGHT), |
|
| 663 | + | ], |
|
| 664 | + | self.highlighted_or_plain(&meta, source_cols), |
|
| 665 | + | Style::default().bg(colors::rgb::POSITIVE_DARK), |
|
| 666 | + | ), |
|
| 667 | + | DiffLineKind::Deletion => ( |
|
| 668 | + | [ |
|
| 669 | + | Self::number_cell( |
|
| 670 | + | source_line, |
|
| 671 | + | colors::rgb::NEGATIVE_LIGHT, |
|
| 672 | + | colors::rgb::NEGATIVE, |
|
| 673 | + | ), |
|
| 674 | + | Self::blank_number(colors::rgb::NEGATIVE), |
|
| 675 | + | Self::marker_cell(" - ", colors::rgb::NEGATIVE_LIGHT), |
|
| 676 | + | ], |
|
| 677 | + | self.highlighted_or_plain(&meta, source_cols), |
|
| 678 | + | Style::default().bg(colors::rgb::NEGATIVE_DARK).dim(), |
|
| 679 | + | ), |
|
| 680 | + | DiffLineKind::Context => ( |
|
| 681 | + | [ |
|
| 682 | + | Self::number_cell(*old_line, colors::rgb::DIM, colors::rgb::FAINT), |
|
| 683 | + | Self::number_cell(source_line, colors::rgb::DIM, colors::rgb::FAINT), |
|
| 684 | + | Self::marker_cell(" ", Color::Unset), |
|
| 685 | + | ], |
|
| 686 | + | self.highlighted_or_plain(&meta, source_cols), |
|
| 687 | + | Style::default(), |
|
| 688 | + | ), |
|
| 689 | + | DiffLineKind::NoNewline => ( |
|
| 690 | + | [ |
|
| 691 | + | Line::blank().padded(LINE_NUMBER_WIDTH), |
|
| 692 | + | Line::blank().padded(LINE_NUMBER_WIDTH), |
|
| 693 | + | Self::marker_cell(" ", Color::Unset), |
|
| 694 | + | ], |
|
| 695 | + | Line::new(NO_NEWLINE_MARKER).style(Style::default().fg(colors::rgb::DIM)), |
|
| 696 | + | Style::default(), |
|
| 697 | + | ), |
|
| 698 | + | }; |
|
| 699 | + | let rendered = self.hunk_line(cells, source, source_cols, style); |
|
| 700 | + | *old_line = old_line.saturating_add(line.kind.old_len()); |
|
| 701 | + | *new_line = new_line.saturating_add(line.kind.new_len()); |
|
| 702 | + | RenderRow { |
|
| 703 | + | line: rendered, |
|
| 704 | + | diff: Some(meta), |
|
| 802 | 705 | } |
|
| 803 | 706 | } |
|
| 804 | 707 | ||
| 805 | 708 | /// Returns a fixed-column hunk row line. |
|
| 806 | 709 | fn hunk_line( |
| 880 | 783 | ScrollbarStyle::new( |
|
| 881 | 784 | Cell::new(SCROLLBAR_TRACK, Style::default().fg(colors::FAINT)), |
|
| 882 | 785 | Cell::new(SCROLLBAR_THUMB, Style::default().fg(SCROLLBAR_THUMB_COLOR)), |
|
| 883 | 786 | ) |
|
| 884 | 787 | } |
|
| 885 | - | ||
| 886 | - | /// Renders the visible viewport from cached full rows. |
|
| 887 | - | fn render_visible_rows( |
|
| 888 | - | rows: &[RenderRow], |
|
| 889 | - | scroll: usize, |
|
| 890 | - | sticky: Vec<Line>, |
|
| 891 | - | area: Rect, |
|
| 892 | - | buf: &mut Buffer, |
|
| 893 | - | ) { |
|
| 894 | - | let (text_area, scrollbar_area) = Self::viewport_areas(area); |
|
| 895 | - | let sticky_rows = sticky.len().min(text_area.rows); |
|
| 896 | - | let body_area = Rect::new( |
|
| 897 | - | text_area.x, |
|
| 898 | - | text_area.y + sticky_rows, |
|
| 899 | - | text_area.cols, |
|
| 900 | - | text_area.rows.saturating_sub(sticky_rows), |
|
| 901 | - | ); |
|
| 902 | - | let view = View::new(scroll, rows.len(), text_area.rows); |
|
| 903 | - | let body_offset = view.offset.saturating_add(sticky_rows); |
|
| 904 | - | ||
| 905 | - | if sticky_rows > 0 { |
|
| 906 | - | let sticky_area = Rect::new(text_area.x, text_area.y, text_area.cols, sticky_rows); |
|
| 907 | - | Line::renders(sticky.into_iter().take(sticky_rows), sticky_area, buf); |
|
| 908 | - | } |
|
| 909 | - | Line::renders( |
|
| 910 | - | rows.iter() |
|
| 911 | - | .skip(body_offset) |
|
| 912 | - | .take(body_area.rows) |
|
| 913 | - | .map(|row| row.line.clone()), |
|
| 914 | - | body_area, |
|
| 915 | - | buf, |
|
| 916 | - | ); |
|
| 917 | - | ||
| 918 | - | if let Some(area) = scrollbar_area { |
|
| 919 | - | Scrollbar::new(view) |
|
| 920 | - | .style(Self::scrollbar_style()) |
|
| 921 | - | .render(area, buf); |
|
| 922 | - | } |
|
| 923 | - | } |
|
| 924 | - | ||
| 925 | - | /// Splits a viewport into body text and scrollbar areas. |
|
| 926 | - | fn viewport_areas(area: Rect) -> (Rect, Option<Rect>) { |
|
| 927 | - | if area.cols > 2 { |
|
| 928 | - | ( |
|
| 929 | - | Rect::new(area.x, area.y, area.cols - 1, area.rows), |
|
| 930 | - | Some(Rect::new(area.right() - 1, area.y, 1, area.rows)), |
|
| 931 | - | ) |
|
| 932 | - | } else { |
|
| 933 | - | (area, None) |
|
| 934 | - | } |
|
| 935 | - | } |
|
| 936 | 788 | } |
|
| 937 | 789 | ||
| 938 | 790 | impl Widget for DiffViewer { |
|
| 939 | 791 | fn size(&self, parent: Constraint) -> Size { |
|
| 940 | 792 | let cols = if parent.max.cols == usize::MAX { |
| 944 | 796 | }; |
|
| 945 | 797 | Size::new(cols, self.content_rows(cols)).constrain(parent) |
|
| 946 | 798 | } |
|
| 947 | 799 | ||
| 948 | 800 | fn render(&mut self, area: Rect, buf: &mut Buffer) { |
|
| 949 | - | if area.is_empty() { |
|
| 950 | - | return; |
|
| 951 | - | } |
|
| 952 | - | let body_cols = ScrollView::body_cols(area); |
|
| 953 | - | self.ensure_rows(body_cols); |
|
| 954 | - | let Some(rows) = self.cached_rows(body_cols) else { |
|
| 955 | - | return; |
|
| 956 | - | }; |
|
| 957 | - | let sticky = Self::sticky_rows(rows, self.state.scroll, area.rows, body_cols); |
|
| 958 | - | ||
| 959 | - | Self::render_visible_rows(rows, self.state.scroll, sticky, area, buf); |
|
| 801 | + | DiffViewer::render(self, area, buf); |
|
| 960 | 802 | } |
|
| 961 | 803 | ||
| 962 | 804 | fn to_buffer(&mut self, constraints: Constraint) -> Buffer { |
|
| 963 | 805 | let size = self.size(constraints); |
|
| 964 | 806 | let area = Rect::from_size(size); |
|
| 965 | 807 | let mut buffer = Buffer::empty(area); |
|
| 966 | - | let body_cols = ScrollView::body_cols(area); |
|
| 967 | - | let rows = self.render_rows(body_cols); |
|
| 968 | - | let sticky = Self::sticky_rows(&rows, self.state.scroll, area.rows, body_cols); |
|
| 969 | 808 | ||
| 970 | - | Self::render_visible_rows(&rows, self.state.scroll, sticky, area, &mut buffer); |
|
| 809 | + | DiffViewer::render(self, area, &mut buffer); |
|
| 971 | 810 | ||
| 972 | 811 | buffer |
|
| 973 | 812 | } |
|
| 974 | 813 | } |
|
| 975 | 814 | ||
| 976 | - | impl RowMeta { |
|
| 977 | - | /// Returns the containing file for row kinds that belong to a file card. |
|
| 978 | - | fn file(&self) -> Option<FileId> { |
|
| 979 | - | match self { |
|
| 980 | - | Self::FileTop(file) |
|
| 981 | - | | Self::FileHeader(file) |
|
| 982 | - | | Self::FileDivider(file) |
|
| 983 | - | | Self::HunkDivider(file) |
|
| 984 | - | | Self::HunkHeader(file, _) |
|
| 985 | - | | Self::FileBottom(file) => Some(*file), |
|
| 986 | - | Self::DiffLine(meta) => Some(meta.file), |
|
| 987 | - | Self::Preamble => None, |
|
| 988 | - | } |
|
| 989 | - | } |
|
| 990 | - | } |
|
| 991 | - | ||
| 992 | 815 | /// Focus target with its rendered row. |
|
| 993 | 816 | #[derive(Clone, Copy, Debug, Eq, PartialEq)] |
|
| 994 | 817 | struct FocusTarget { |
|
| 995 | 818 | /// Focus value. |
|
| 996 | 819 | focus: DiffFocus, |
| 1005 | 828 | File, |
|
| 1006 | 829 | /// Hunk targets only. |
|
| 1007 | 830 | Hunk, |
|
| 1008 | 831 | } |
|
| 1009 | 832 | ||
| 1010 | - | impl FocusFilter { |
|
| 1011 | - | /// Returns focusable targets from render rows. |
|
| 1012 | - | fn targets(self, rows: &[RenderRow]) -> Vec<FocusTarget> { |
|
| 1013 | - | rows.iter() |
|
| 833 | + | /// Local row of the first hunk header (top border + file header + divider). |
|
| 834 | + | const HUNK_BODY_START: usize = 3; |
|
| 835 | + | /// Total card height when the file has no visible body (just borders + header). |
|
| 836 | + | const EMPTY_CARD_ROWS: usize = 3; |
|
| 837 | + | ||
| 838 | + | impl FileDiff { |
|
| 839 | + | /// Returns whether this file card should render hunk rows. |
|
| 840 | + | fn has_body(&self, collapsed: bool) -> bool { |
|
| 841 | + | !collapsed && !self.hunks.is_empty() |
|
| 842 | + | } |
|
| 843 | + | ||
| 844 | + | /// Returns the rendered row count for this file's card. |
|
| 845 | + | fn rendered_rows(&self, collapsed: bool) -> usize { |
|
| 846 | + | if !self.has_body(collapsed) { |
|
| 847 | + | return EMPTY_CARD_ROWS; |
|
| 848 | + | } |
|
| 849 | + | let hunks = self.hunks.len(); |
|
| 850 | + | let body: usize = self.hunks.iter().map(|hunk| hunk.lines.len()).sum(); |
|
| 851 | + | // top + header + divider + (hunk header + body) per hunk + gaps + bottom. |
|
| 852 | + | HUNK_BODY_START + hunks + body + hunks.saturating_sub(1) + 1 |
|
| 853 | + | } |
|
| 854 | + | ||
| 855 | + | /// Returns each hunk together with the local row of its header. |
|
| 856 | + | fn hunk_header_rows(&self, collapsed: bool) -> impl Iterator<Item = (&Hunk, usize)> + '_ { |
|
| 857 | + | let has_body = self.has_body(collapsed); |
|
| 858 | + | self.hunks |
|
| 859 | + | .iter() |
|
| 1014 | 860 | .enumerate() |
|
| 1015 | - | .filter_map(move |(row, render_row)| match render_row.meta { |
|
| 1016 | - | RowMeta::FileTop(file) if self == Self::File => Some(FocusTarget { |
|
| 1017 | - | focus: DiffFocus::File(file), |
|
| 1018 | - | row, |
|
| 1019 | - | }), |
|
| 1020 | - | RowMeta::HunkHeader(_, hunk) if self == Self::Hunk => Some(FocusTarget { |
|
| 1021 | - | focus: DiffFocus::Hunk(hunk), |
|
| 1022 | - | row, |
|
| 1023 | - | }), |
|
| 1024 | - | _ => None, |
|
| 861 | + | .scan(HUNK_BODY_START, move |local, (index, hunk)| { |
|
| 862 | + | if !has_body { |
|
| 863 | + | return None; |
|
| 864 | + | } |
|
| 865 | + | if index > 0 { |
|
| 866 | + | *local += 1; |
|
| 867 | + | } |
|
| 868 | + | let row = *local; |
|
| 869 | + | *local += 1 + hunk.lines.len(); |
|
| 870 | + | Some((hunk, row)) |
|
| 1025 | 871 | }) |
|
| 1026 | - | .collect() |
|
| 1027 | 872 | } |
|
| 1028 | - | } |
|
| 1029 | 873 | ||
| 1030 | - | impl FileDiff { |
|
| 1031 | 874 | /// Returns the source path for a file side. |
|
| 1032 | 875 | fn source_path(&self, side: DiffSide) -> Option<PathBuf> { |
|
| 1033 | 876 | match side { |
|
| 1034 | 877 | DiffSide::Old => self |
|
| 1035 | 878 | .header |
a.rs
+0 -0
a.rs
+0 -0
b.rs
+0 -0
a.rs
+0 -0
crates/tui-core/src/border.rs
+1 -1
| 185 | 185 | ||
| 186 | 186 | assert_eq!(line.to_string(), "╭───╮"); |
|
| 187 | 187 | assert!( |
|
| 188 | 188 | line.labels() |
|
| 189 | 189 | .iter() |
|
| 190 | - | .all(|label| { label.paint().style().fg_color() == Color::Red }) |
|
| 190 | + | .all(|label| { label.paint.style().fg_color() == Color::Red }) |
|
| 191 | 191 | ); |
|
| 192 | 192 | assert_eq!(border.divider(2, "┄").to_string(), "├┄┄┤"); |
|
| 193 | 193 | assert_eq!(border.bottom(1).to_string(), "╰─╯"); |
|
| 194 | 194 | } |
|
| 195 | 195 |
crates/tui-core/src/buffer.rs
+5 -6
| 265 | 265 | let visible = text.len().min(self.area.right() - x); |
|
| 266 | 266 | if visible == 0 { |
|
| 267 | 267 | return; |
|
| 268 | 268 | } |
|
| 269 | 269 | self.clear_glyph_at(x, y); |
|
| 270 | - | if x + visible < self.area.right() { |
|
| 270 | + | if x + visible < self.area.right() |
|
| 271 | + | && self.get(x + visible, y).is_some_and(Cell::is_continuation) |
|
| 272 | + | { |
|
| 271 | 273 | self.clear_glyph_at(x + visible, y); |
|
| 272 | 274 | } |
|
| 273 | 275 | ||
| 274 | 276 | let start = (y - self.area.y) * self.area.cols + (x - self.area.x); |
|
| 275 | 277 | for (offset, byte) in text.bytes().take(visible).enumerate() { |
| 560 | 562 | buffer.set_str(1, 0, "b", Style::new(crate::Color::Blue)); |
|
| 561 | 563 | ||
| 562 | 564 | let line = buffer.styled_line(0); |
|
| 563 | 565 | ||
| 564 | 566 | assert_eq!(line.to_string(), "rb "); |
|
| 567 | + | assert_eq!(line.labels()[0].paint.style().fg_color(), crate::Color::Red); |
|
| 565 | 568 | assert_eq!( |
|
| 566 | - | line.labels()[0].paint().style().fg_color(), |
|
| 567 | - | crate::Color::Red |
|
| 568 | - | ); |
|
| 569 | - | assert_eq!( |
|
| 570 | - | line.labels()[1].paint().style().fg_color(), |
|
| 569 | + | line.labels()[1].paint.style().fg_color(), |
|
| 571 | 570 | crate::Color::Blue |
|
| 572 | 571 | ); |
|
| 573 | 572 | } |
|
| 574 | 573 | ||
| 575 | 574 | #[test] |
crates/tui-core/src/table.rs
+1 -1
| 626 | 626 | let line = &lines[0]; |
|
| 627 | 627 | assert_eq!(line.to_string(), "a b "); |
|
| 628 | 628 | assert!( |
|
| 629 | 629 | line.labels() |
|
| 630 | 630 | .iter() |
|
| 631 | - | .all(|label| label.paint().style().bg_color() == Color::Red), |
|
| 631 | + | .all(|label| label.paint.style().bg_color() == Color::Red), |
|
| 632 | 632 | "all labels (including spacing/padding) take the row background", |
|
| 633 | 633 | ); |
|
| 634 | 634 | } |
|
| 635 | 635 | ||
| 636 | 636 | #[test] |
crates/tui-core/src/vstack.rs
+5 -0
| 101 | 101 | pub fn child(mut self, child: impl Widget + 'a) -> Self { |
|
| 102 | 102 | self.push(child); |
|
| 103 | 103 | self |
|
| 104 | 104 | } |
|
| 105 | 105 | ||
| 106 | + | /// Boxes this stack as a [`Widget`]. |
|
| 107 | + | pub fn boxed(self) -> Box<dyn Widget + 'a> { |
|
| 108 | + | Box::new(self) |
|
| 109 | + | } |
|
| 110 | + | ||
| 106 | 111 | /// Add a widget with an explicit row height policy. |
|
| 107 | 112 | pub fn child_with_height(mut self, child: impl Widget + 'a, height: Length) -> Self { |
|
| 108 | 113 | self.push_with_height(child, height); |
|
| 109 | 114 | self |
|
| 110 | 115 | } |
crates/tui-widgets/src/lib.rs
+1 -1
| 18 | 18 | /// Scroll viewport primitives. |
|
| 19 | 19 | pub mod viewport; |
|
| 20 | 20 | ||
| 21 | 21 | pub use list::{List, Selection}; |
|
| 22 | 22 | pub use scrollbar::{Scrollbar, ScrollbarStyle}; |
|
| 23 | - | pub use scrollview::{ScrollMetrics, ScrollView}; |
|
| 23 | + | pub use scrollview::{ScrollLayout, ScrollMetrics, ScrollView, VirtualScrollView, scroll_layout}; |
|
| 24 | 24 | pub use viewport::{Placement, Scroll, View, Viewport}; |
crates/tui-widgets/src/scrollview.rs
+219 -41
| 1 | 1 | //! Scrollable line widget. |
|
| 2 | 2 | //! |
|
| 3 | - | //! `ScrollView` renders a slice of width-sensitive lines for a given offset and |
|
| 4 | - | //! draws a scrollbar when content exceeds the visible area. It is the |
|
| 5 | - | //! widget-level bridge between viewport math and actual line rendering. |
|
| 3 | + | //! `ScrollView` renders scrollable line collections and draws a scrollbar when |
|
| 4 | + | //! content exceeds the visible area. It is the widget-level bridge between |
|
| 5 | + | //! viewport math and actual line rendering. |
|
| 6 | 6 | ||
| 7 | 7 | use crate::{Scrollbar, ScrollbarStyle, View}; |
|
| 8 | 8 | use rustik_tui_core as tui_core; |
|
| 9 | 9 | use tui_core::{Buffer, Constraint, Line, Rect, Size, Widget}; |
|
| 10 | 10 |
| 43 | 43 | offset: usize, |
|
| 44 | 44 | sticky: Vec<Line>, |
|
| 45 | 45 | scrollbar: ScrollbarStyle, |
|
| 46 | 46 | } |
|
| 47 | 47 | ||
| 48 | + | /// A scrollable collection that renders rows on demand. |
|
| 49 | + | pub struct VirtualScrollView<F> { |
|
| 50 | + | /// Number of rows in the full content. |
|
| 51 | + | content_rows: usize, |
|
| 52 | + | /// First visible content row before clamping. |
|
| 53 | + | offset: usize, |
|
| 54 | + | /// Sticky rows pinned at the top of the view. |
|
| 55 | + | sticky: Vec<Line>, |
|
| 56 | + | /// Scrollbar style. |
|
| 57 | + | scrollbar: ScrollbarStyle, |
|
| 58 | + | /// Width-sensitive row producer. |
|
| 59 | + | line: F, |
|
| 60 | + | } |
|
| 61 | + | ||
| 62 | + | /// Layout rectangles for a scroll-view render pass. |
|
| 63 | + | #[derive(Clone, Copy, Debug)] |
|
| 64 | + | pub struct ScrollLayout { |
|
| 65 | + | /// Text area excluding the scrollbar. |
|
| 66 | + | pub text: Rect, |
|
| 67 | + | /// Optional scrollbar area. |
|
| 68 | + | pub scrollbar: Option<Rect>, |
|
| 69 | + | /// Sticky rows that fit in the text area. |
|
| 70 | + | pub sticky_rows: usize, |
|
| 71 | + | /// Body area for scrolled content rows. |
|
| 72 | + | pub body: Rect, |
|
| 73 | + | } |
|
| 74 | + | ||
| 75 | + | /// Layout information for one scroll-view render pass. |
|
| 76 | + | #[derive(Clone, Copy, Debug)] |
|
| 77 | + | struct ScrollFrame { |
|
| 78 | + | /// Area rectangles for the render pass. |
|
| 79 | + | layout: ScrollLayout, |
|
| 80 | + | /// Clamped view for the scrolled body. |
|
| 81 | + | view: View, |
|
| 82 | + | } |
|
| 83 | + | ||
| 48 | 84 | impl ScrollView { |
|
| 49 | 85 | /// Returns the body width inside the reserved scrollbar column. |
|
| 50 | 86 | /// |
|
| 51 | 87 | /// Callers that pre-build content lines should size them to this width so |
|
| 52 | 88 | /// they exactly fit the body next to the scrollbar. |
| 79 | 115 | sticky: Vec::new(), |
|
| 80 | 116 | scrollbar: ScrollbarStyle::default(), |
|
| 81 | 117 | } |
|
| 82 | 118 | } |
|
| 83 | 119 | ||
| 120 | + | /// Creates a scroll view that renders only visible rows. |
|
| 121 | + | pub fn virtualized<F>(content_rows: usize, offset: usize, line: F) -> VirtualScrollView<F> |
|
| 122 | + | where |
|
| 123 | + | F: FnMut(usize, usize) -> Line, |
|
| 124 | + | { |
|
| 125 | + | VirtualScrollView::new(content_rows, offset, line) |
|
| 126 | + | } |
|
| 127 | + | ||
| 84 | 128 | /// Sets the first visible row. |
|
| 85 | 129 | pub const fn offset(mut self, offset: usize) -> Self { |
|
| 86 | 130 | self.offset = offset; |
|
| 87 | 131 | self |
|
| 88 | 132 | } |
| 99 | 143 | self |
|
| 100 | 144 | } |
|
| 101 | 145 | ||
| 102 | 146 | /// Returns metrics for a rendered area. |
|
| 103 | 147 | pub fn metrics(&self, area: Rect) -> ScrollMetrics { |
|
| 104 | - | let (text_area, _) = self.areas(area); |
|
| 105 | - | let body_rows = text_area.rows.saturating_sub(self.sticky.len()); |
|
| 106 | - | let view = View::new(self.offset, self.lines.len(), body_rows); |
|
| 148 | + | let frame = scroll_frame(area, self.offset, self.lines.len(), self.sticky.len()); |
|
| 107 | 149 | ||
| 108 | 150 | ScrollMetrics { |
|
| 109 | 151 | content_rows: self.lines.len(), |
|
| 110 | - | visible_rows: body_rows, |
|
| 111 | - | view, |
|
| 152 | + | visible_rows: frame.layout.body.rows, |
|
| 153 | + | view: frame.view, |
|
| 112 | 154 | } |
|
| 113 | 155 | } |
|
| 156 | + | } |
|
| 114 | 157 | ||
| 115 | - | /// Splits the render area into text and scrollbar rectangles. |
|
| 116 | - | /// |
|
| 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. |
|
| 120 | - | fn areas(&self, area: Rect) -> (Rect, Option<Rect>) { |
|
| 121 | - | if area.cols > 2 { |
|
| 122 | - | ( |
|
| 123 | - | Rect::new(area.x, area.y, area.cols - 1, area.rows), |
|
| 124 | - | Some(Rect::new(area.right() - 1, area.y, 1, area.rows)), |
|
| 125 | - | ) |
|
| 126 | - | } else { |
|
| 127 | - | (area, None) |
|
| 158 | + | impl<F> VirtualScrollView<F> |
|
| 159 | + | where |
|
| 160 | + | F: FnMut(usize, usize) -> Line, |
|
| 161 | + | { |
|
| 162 | + | /// Creates a virtual scroll view from a row count and row producer. |
|
| 163 | + | pub fn new(content_rows: usize, offset: usize, line: F) -> Self { |
|
| 164 | + | Self { |
|
| 165 | + | content_rows, |
|
| 166 | + | offset, |
|
| 167 | + | sticky: Vec::new(), |
|
| 168 | + | scrollbar: ScrollbarStyle::default(), |
|
| 169 | + | line, |
|
| 170 | + | } |
|
| 171 | + | } |
|
| 172 | + | ||
| 173 | + | /// Sets sticky rows pinned at the top of the view above the scrolled body. |
|
| 174 | + | pub fn sticky(mut self, lines: impl IntoIterator<Item = Line>) -> Self { |
|
| 175 | + | self.sticky = lines.into_iter().collect(); |
|
| 176 | + | self |
|
| 177 | + | } |
|
| 178 | + | ||
| 179 | + | /// Sets the vertical scrollbar style. |
|
| 180 | + | pub fn scrollbar(mut self, style: ScrollbarStyle) -> Self { |
|
| 181 | + | self.scrollbar = style; |
|
| 182 | + | self |
|
| 183 | + | } |
|
| 184 | + | ||
| 185 | + | /// Returns metrics for a rendered area. |
|
| 186 | + | pub fn metrics(&self, area: Rect) -> ScrollMetrics { |
|
| 187 | + | let frame = scroll_frame(area, self.offset, self.content_rows, self.sticky.len()); |
|
| 188 | + | ||
| 189 | + | ScrollMetrics { |
|
| 190 | + | content_rows: self.content_rows, |
|
| 191 | + | visible_rows: frame.layout.body.rows, |
|
| 192 | + | view: frame.view, |
|
| 128 | 193 | } |
|
| 129 | 194 | } |
|
| 130 | 195 | } |
|
| 131 | 196 | ||
| 197 | + | impl<F> std::fmt::Debug for VirtualScrollView<F> { |
|
| 198 | + | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
|
| 199 | + | f.debug_struct("VirtualScrollView") |
|
| 200 | + | .field("content_rows", &self.content_rows) |
|
| 201 | + | .field("offset", &self.offset) |
|
| 202 | + | .field("sticky", &self.sticky) |
|
| 203 | + | .field("scrollbar", &self.scrollbar) |
|
| 204 | + | .finish_non_exhaustive() |
|
| 205 | + | } |
|
| 206 | + | } |
|
| 207 | + | ||
| 132 | 208 | impl Widget for ScrollView { |
|
| 133 | 209 | fn size(&self, parent: Constraint) -> Size { |
|
| 134 | 210 | Size::new(parent.max.cols, parent.max.rows).constrain(parent) |
|
| 135 | 211 | } |
|
| 136 | 212 | ||
| 137 | 213 | fn render(&mut self, area: Rect, buf: &mut Buffer) { |
|
| 138 | 214 | if area.is_empty() { |
|
| 139 | 215 | return; |
|
| 140 | 216 | } |
|
| 141 | - | let (text_area, scrollbar_area) = self.areas(area); |
|
| 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 | - | ); |
|
| 217 | + | let frame = scroll_frame(area, self.offset, self.lines.len(), self.sticky.len()); |
|
| 218 | + | ||
| 219 | + | render_sticky(&self.sticky, frame.layout, buf); |
|
| 220 | + | frame.view.render_lines(&self.lines, frame.layout.body, buf); |
|
| 221 | + | ||
| 222 | + | if let Some(area) = frame.layout.scrollbar { |
|
| 223 | + | Scrollbar::new(frame.view) |
|
| 224 | + | .style(self.scrollbar.clone()) |
|
| 225 | + | .render(area, buf); |
|
| 158 | 226 | } |
|
| 159 | - | view.render_lines(&self.lines, body_area, buf); |
|
| 227 | + | } |
|
| 228 | + | } |
|
| 160 | 229 | ||
| 161 | - | if let Some(area) = scrollbar_area { |
|
| 162 | - | Scrollbar::new(view) |
|
| 230 | + | impl<F> Widget for VirtualScrollView<F> |
|
| 231 | + | where |
|
| 232 | + | F: FnMut(usize, usize) -> Line + Send, |
|
| 233 | + | { |
|
| 234 | + | fn size(&self, parent: Constraint) -> Size { |
|
| 235 | + | Size::new(parent.max.cols, self.content_rows).constrain(parent) |
|
| 236 | + | } |
|
| 237 | + | ||
| 238 | + | fn render(&mut self, area: Rect, buf: &mut Buffer) { |
|
| 239 | + | if area.is_empty() { |
|
| 240 | + | return; |
|
| 241 | + | } |
|
| 242 | + | let frame = scroll_frame(area, self.offset, self.content_rows, self.sticky.len()); |
|
| 243 | + | ||
| 244 | + | render_sticky(&self.sticky, frame.layout, buf); |
|
| 245 | + | ||
| 246 | + | let lines = frame |
|
| 247 | + | .view |
|
| 248 | + | .range() |
|
| 249 | + | .map(|row| (self.line)(row, frame.layout.body.cols)); |
|
| 250 | + | Line::renders(lines, frame.layout.body, buf); |
|
| 251 | + | ||
| 252 | + | if let Some(area) = frame.layout.scrollbar { |
|
| 253 | + | Scrollbar::new(frame.view) |
|
| 163 | 254 | .style(self.scrollbar.clone()) |
|
| 164 | 255 | .render(area, buf); |
|
| 165 | 256 | } |
|
| 166 | 257 | } |
|
| 167 | 258 | } |
|
| 168 | 259 | ||
| 260 | + | /// Renders sticky lines into the top of a scroll layout's text area. |
|
| 261 | + | fn render_sticky(sticky: &[Line], layout: ScrollLayout, buf: &mut Buffer) { |
|
| 262 | + | if layout.sticky_rows == 0 { |
|
| 263 | + | return; |
|
| 264 | + | } |
|
| 265 | + | let sticky_area = Rect::new( |
|
| 266 | + | layout.text.x, |
|
| 267 | + | layout.text.y, |
|
| 268 | + | layout.text.cols, |
|
| 269 | + | layout.sticky_rows, |
|
| 270 | + | ); |
|
| 271 | + | Line::renders( |
|
| 272 | + | sticky.iter().take(layout.sticky_rows).cloned(), |
|
| 273 | + | sticky_area, |
|
| 274 | + | buf, |
|
| 275 | + | ); |
|
| 276 | + | } |
|
| 277 | + | ||
| 278 | + | /// Returns the area rectangles for a scroll view with `sticky_rows` pinned. |
|
| 279 | + | /// |
|
| 280 | + | /// Splits `area` into a text column (excluding the rightmost scrollbar column |
|
| 281 | + | /// when there is room for one), then divides the text area into a sticky |
|
| 282 | + | /// header band of `sticky_rows` rows and a body for scrolled content. |
|
| 283 | + | pub fn scroll_layout(area: Rect, sticky_rows: usize) -> ScrollLayout { |
|
| 284 | + | let (text, scrollbar) = if area.cols > 2 { |
|
| 285 | + | let text = Rect::new(area.x, area.y, area.cols - 1, area.rows); |
|
| 286 | + | let scrollbar = Some(Rect::new(area.right() - 1, area.y, 1, area.rows)); |
|
| 287 | + | ||
| 288 | + | (text, scrollbar) |
|
| 289 | + | } else { |
|
| 290 | + | (area, None) |
|
| 291 | + | }; |
|
| 292 | + | let sticky_rows = sticky_rows.min(text.rows); |
|
| 293 | + | let body = Rect::new( |
|
| 294 | + | text.x, |
|
| 295 | + | text.y + sticky_rows, |
|
| 296 | + | text.cols, |
|
| 297 | + | text.rows.saturating_sub(sticky_rows), |
|
| 298 | + | ); |
|
| 299 | + | ||
| 300 | + | ScrollLayout { |
|
| 301 | + | text, |
|
| 302 | + | scrollbar, |
|
| 303 | + | sticky_rows, |
|
| 304 | + | body, |
|
| 305 | + | } |
|
| 306 | + | } |
|
| 307 | + | ||
| 308 | + | /// Returns layout rectangles and view state for one scroll-view render pass. |
|
| 309 | + | fn scroll_frame(area: Rect, offset: usize, content_rows: usize, sticky_rows: usize) -> ScrollFrame { |
|
| 310 | + | let layout = scroll_layout(area, sticky_rows); |
|
| 311 | + | let view = View::new(offset, content_rows, layout.body.rows); |
|
| 312 | + | ||
| 313 | + | ScrollFrame { layout, view } |
|
| 314 | + | } |
|
| 315 | + | ||
| 169 | 316 | #[cfg(test)] |
|
| 170 | 317 | mod tests { |
|
| 171 | 318 | use super::*; |
|
| 172 | 319 | use rustik_tui_core::{Buffer, Cell, Color, Style}; |
|
| 173 | 320 |
| 218 | 365 | assert_eq!(ScrollView::body_cols(area), 3); |
|
| 219 | 366 | assert_eq!(metrics.content_rows, 3); |
|
| 220 | 367 | assert_eq!(metrics.visible_rows, 2); |
|
| 221 | 368 | assert_eq!(buffer.lines(), vec!["1:3|", "2:3#"]); |
|
| 222 | 369 | } |
|
| 370 | + | ||
| 371 | + | #[test] |
|
| 372 | + | fn virtual_scroll_view_renders_only_visible_rows() { |
|
| 373 | + | let mut rendered = Vec::new(); |
|
| 374 | + | let mut view = ScrollView::virtualized(10, 4, |row, cols| { |
|
| 375 | + | rendered.push(row); |
|
| 376 | + | Line::new(format!("{row}:{cols}")) |
|
| 377 | + | }); |
|
| 378 | + | let mut buffer = Buffer::empty(Rect::new(0, 0, 5, 3)); |
|
| 379 | + | ||
| 380 | + | view.render(buffer.area(), &mut buffer); |
|
| 381 | + | ||
| 382 | + | assert_eq!(rendered, vec![4, 5, 6]); |
|
| 383 | + | assert_eq!(buffer.lines(), vec!["4:4 │", "5:4 ┃", "6:4 │"]); |
|
| 384 | + | } |
|
| 385 | + | ||
| 386 | + | #[test] |
|
| 387 | + | fn virtual_scroll_view_keeps_sticky_rows_out_of_source() { |
|
| 388 | + | let mut rendered = Vec::new(); |
|
| 389 | + | let mut view = ScrollView::virtualized(10, 4, |row, cols| { |
|
| 390 | + | rendered.push(row); |
|
| 391 | + | Line::new(format!("{row}:{cols}")) |
|
| 392 | + | }) |
|
| 393 | + | .sticky([Line::new("HEAD")]); |
|
| 394 | + | let mut buffer = Buffer::empty(Rect::new(0, 0, 5, 3)); |
|
| 395 | + | ||
| 396 | + | view.render(buffer.area(), &mut buffer); |
|
| 397 | + | ||
| 398 | + | assert_eq!(rendered, vec![4, 5]); |
|
| 399 | + | assert_eq!(buffer.lines(), vec!["HEAD│", "4:4 ┃", "5:4 │"]); |
|
| 400 | + | } |
|
| 223 | 401 | } |