Worktrunk is a CLI for Git worktree management, designed for parallel AI agent workflows
fix: progressive table garbled when output exceeds terminal height (#981)
Maximilian Roos8cfab7e1d74e
diff --git a/src/commands/list/collect/mod.rs b/src/commands/list/collect/mod.rsindex 5b2008a48..1449d52dd 100644--- a/src/commands/list/collect/mod.rs+++ b/src/commands/list/collect/mod.rs@@ -818,11 +818,11 @@ pub fn collect(if table.is_tty() {// Interactive: do final render pass and update footer to summary- for (item_idx, item) in all_items.iter().enumerate() {- let rendered = layout.format_list_item_line(item);- table.update_row(item_idx, rendered);- }- table.finalize(final_msg)?;+ let rows: Vec<String> = all_items+ .iter()+ .map(|item| layout.format_list_item_line(item))+ .collect();+ table.finalize(rows, final_msg)?;} else {// Non-TTY: output to stdout (same as buffered mode)// Progressive skeleton was suppressed; now output the final tablediff --git a/src/commands/list/progressive_table.rs b/src/commands/list/progressive_table.rsindex 43faafc64..89b0b73f6 100644--- a/src/commands/list/progressive_table.rs+++ b/src/commands/list/progressive_table.rs@@ -32,8 +32,11 @@ pub struct ProgressiveTable {lines: Vec<String>,/// Maximum width for content (terminal width - safety margin)max_width: usize,- /// Number of data rows (not counting header, spacer, footer)+ /// Number of data rows visible in skeleton (not counting header, spacer, footer).+ /// May be less than `total_row_count` when terminal is too short.row_count: usize,+ /// Total number of data rows (including those not shown in skeleton)+ total_row_count: usize,/// Whether output is going to a TTYis_tty: bool,/// Lines that have been modified since last flush@@ -57,15 +60,39 @@ impl ProgressiveTable {skeletons: Vec<String>,initial_footer: String,max_width: usize,+ ) -> Self {+ // Only check terminal height when stdout is a TTY. terminal_size() falls+ // back to stderr/stdin, so it can return Some even for piped stdout.+ let term_height = if stdout().is_terminal() {+ terminal_size::terminal_size().map(|(_, h)| h.0 as usize)+ } else {+ None+ };+ Self::new_with_height(header, skeletons, initial_footer, max_width, term_height)+ }++ fn new_with_height(+ header: String,+ skeletons: Vec<String>,+ initial_footer: String,+ max_width: usize,+ terminal_height: Option<usize>,) -> Self {let is_tty = stdout().is_terminal();- let row_count = skeletons.len();+ let total_row_count = skeletons.len();++ // Limit visible rows to fit in terminal: header + rows + spacer + footer = rows + 3+ // Reserve one extra line for the cursor position after printing.+ // Only limit when we have height info — None means non-TTY or unknown.+ let visible_row_count = terminal_height+ .map(|h| total_row_count.min(h.saturating_sub(4)))+ .unwrap_or(total_row_count);- // Build initial lines: header + rows + spacer + footer- let mut lines = Vec::with_capacity(row_count + 3);+ // Build initial lines: header + visible rows + spacer + footer+ let mut lines = Vec::with_capacity(visible_row_count + 3);lines.push(truncate_visible(&header, max_width));- for skeleton in skeletons {+ for skeleton in skeletons.into_iter().take(visible_row_count) {lines.push(truncate_visible(&skeleton, max_width));}@@ -78,7 +105,8 @@ impl ProgressiveTable {Self {lines,max_width,- row_count,+ row_count: visible_row_count,+ total_row_count,is_tty,dirty: Vec::new(),rendered: false,@@ -217,15 +245,46 @@ impl ProgressiveTable {stdout.flush()}- /// Finalize: update footer and flush.+ /// Finalize the table with final row content and footer.///- /// Updates the footer content. If we rendered initially, also redraws the footer.- ///- /// # Arguments- /// * `final_footer` - Final summary message to replace loading status- pub fn finalize(&mut self, final_footer: String) -> std::io::Result<()> {- self.update_footer(final_footer);- self.flush()+ /// For the normal case, updates rows in-place and redraws the footer.+ /// When overflowing (skeleton showed a subset of rows), erases the skeleton+ /// and prints the complete table — the output scrolls naturally, avoiding+ /// the `MoveUp`-into-scrollback problem.+ pub fn finalize(+ &mut self,+ final_rows: Vec<String>,+ final_footer: String,+ ) -> std::io::Result<()> {+ if self.row_count < self.total_row_count {+ // Overflow: erase skeleton, print complete table (scrolls naturally)+ debug_assert!(+ self.rendered,+ "overflow finalize should only be called after render_skeleton"+ );+ let mut stdout = stdout();+ stdout.execute(MoveUp(self.lines.len() as u16))?;+ stdout.execute(MoveToColumn(0))?;+ stdout.execute(Clear(ClearType::FromCursorDown))?;+ writeln!(stdout, "{}", self.lines[0])?; // header (unchanged)+ for row in &final_rows {+ writeln!(stdout, "{}", truncate_visible(row, self.max_width))?;+ }+ writeln!(stdout)?;+ writeln!(+ stdout,+ "{}",+ truncate_visible(&final_footer, self.max_width)+ )?;+ stdout.flush()+ } else {+ // Normal: update rows in-place + footer+ for (idx, row) in final_rows.into_iter().enumerate() {+ self.update_row(idx, row);+ }+ self.update_footer(final_footer);+ self.flush()+ }}/// Check if output is going to a TTY.@@ -328,6 +387,8 @@ mod tests {);assert_eq!(table.row_count, 3);+ assert_eq!(table.total_row_count, 3);+ assert_eq!(table.row_count, table.total_row_count);}#[test]@@ -357,10 +418,13 @@ mod tests {);// Without render_skeleton(), finalize updates footer but doesn't print- table.finalize("Complete!".to_string()).unwrap();+ table+ .finalize(vec!["row-final".to_string()], "Complete!".to_string())+ .unwrap();// Footer IS updated (the data changes), but no output since not renderedassert_eq!(table.lines.last().unwrap(), "Complete!");+ assert_eq!(table.lines[1], "row-final");}#[test]@@ -411,4 +475,134 @@ mod tests {table.update_footer("new footer".into());assert_eq!(table.dirty, vec![1, 4]); // footer is last line}++ #[test]+ fn overflow_limits_visible_rows() {+ // 10 rows, terminal height 8 → visible = 8 - 4 = 4+ let skeletons: Vec<String> = (0..10).map(|i| format!("row{i}")).collect();+ let table = ProgressiveTable::new_with_height(+ "header".into(),+ skeletons,+ "loading".into(),+ 80,+ Some(8),+ );++ assert_eq!(table.row_count, 4);+ assert_eq!(table.total_row_count, 10);+ assert!(table.row_count < table.total_row_count);+ // header + 4 visible rows + spacer + footer = 7 lines+ assert_eq!(table.lines.len(), 7);+ assert_eq!(table.lines[0], "header");+ assert_eq!(table.lines[1], "row0");+ assert_eq!(table.lines[4], "row3");+ assert_eq!(table.lines[5], ""); // spacer+ assert_eq!(table.lines[6], "loading");+ }++ #[test]+ fn no_overflow_when_fits() {+ // 3 rows, terminal height 20 → visible = 3 (fits easily)+ let skeletons = vec!["a".into(), "b".into(), "c".into()];+ let table = ProgressiveTable::new_with_height(+ "header".into(),+ skeletons,+ "loading".into(),+ 80,+ Some(20),+ );++ assert_eq!(table.row_count, 3);+ assert_eq!(table.total_row_count, 3);+ assert_eq!(table.row_count, table.total_row_count);+ }++ #[test]+ fn overflow_boundary_exact_fit() {+ // 5 rows need height 5+4=9, terminal height 9 → fits exactly, no overflow+ let skeletons: Vec<String> = (0..5).map(|i| format!("row{i}")).collect();+ let table = ProgressiveTable::new_with_height(+ "header".into(),+ skeletons,+ "loading".into(),+ 80,+ Some(9),+ );++ assert_eq!(table.row_count, 5);+ assert_eq!(table.row_count, table.total_row_count);+ }++ #[test]+ fn overflow_boundary_one_short() {+ // 5 rows need height 5+4=9, terminal height 8 → overflow, visible = 4+ let skeletons: Vec<String> = (0..5).map(|i| format!("row{i}")).collect();+ let table = ProgressiveTable::new_with_height(+ "header".into(),+ skeletons,+ "loading".into(),+ 80,+ Some(8),+ );++ assert_eq!(table.row_count, 4);+ assert_eq!(table.total_row_count, 5);+ assert!(table.row_count < table.total_row_count);+ }++ #[test]+ fn overflow_hidden_rows_are_noop() {+ let skeletons: Vec<String> = (0..10).map(|i| format!("row{i}")).collect();+ let mut table = ProgressiveTable::new_with_height(+ "header".into(),+ skeletons,+ "loading".into(),+ 80,+ Some(8),+ );++ // Can update visible rows (0..4)+ assert!(table.update_row(0, "updated0".into()));+ assert_eq!(table.lines[1], "updated0");++ // Hidden rows (4..10) are no-ops+ assert!(!table.update_row(4, "should-be-ignored".into()));+ assert!(!table.update_row(9, "should-be-ignored".into()));+ }++ #[test]+ fn overflow_very_small_terminal() {+ // Terminal too small for any rows: height 3 → visible = 0+ let skeletons: Vec<String> = (0..5).map(|i| format!("row{i}")).collect();+ let table = ProgressiveTable::new_with_height(+ "header".into(),+ skeletons,+ "loading".into(),+ 80,+ Some(3),+ );++ assert_eq!(table.row_count, 0);+ assert_eq!(table.total_row_count, 5);+ assert!(table.row_count < table.total_row_count);+ // header + 0 rows + spacer + footer = 3 lines+ assert_eq!(table.lines.len(), 3);+ }++ #[test]+ fn no_height_info_shows_all_rows() {+ // No terminal height info → show all rows (non-TTY or unknown)+ let skeletons: Vec<String> = (0..10).map(|i| format!("row{i}")).collect();+ let table = ProgressiveTable::new_with_height(+ "header".into(),+ skeletons,+ "loading".into(),+ 80,+ None,+ );++ assert_eq!(table.row_count, 10);+ assert_eq!(table.total_row_count, 10);+ assert_eq!(table.row_count, table.total_row_count);+ }}diff --git a/tests/integration_tests/list_progressive.rs b/tests/integration_tests/list_progressive.rsindex 1880a0349..627c0e155 100644--- a/tests/integration_tests/list_progressive.rs+++ b/tests/integration_tests/list_progressive.rs@@ -98,6 +98,49 @@ fn test_list_progressive_api(mut repo: TestRepo) {);}+/// Tests overflow mode: when worktrees exceed terminal height, the skeleton shows a subset,+/// then finalize erases and prints the complete table (scrolls naturally).+#[rstest]+fn test_list_progressive_overflow(mut repo: TestRepo) {+ // Create enough worktrees to overflow a 10-row terminal.+ // With height=10: visible_rows = 10 - 4 (header + spacer + footer + cursor) = 6+ // 10 worktrees + main = 11 rows, well above the 6-row limit.+ for i in 1..=10 {+ repo.add_worktree(&format!("overflow-{:02}", i));+ }++ let mut opts = ProgressiveCaptureOptions::with_byte_interval(500);+ opts.terminal_size = (10, 150); // Short terminal triggers overflow++ let output = capture_progressive_output(&repo, "list", &["--full", "--branches"], opts);++ assert_eq!(output.exit_code, 0);++ // The overflow finalize path erases the skeleton and prints the complete table,+ // which scrolls naturally. The vt100 parser (10 rows, no scrollback) only captures+ // the visible tail. Verify: the footer and later branches are visible, confirming+ // the overflow finalize path executed and printed the full table.+ let final_text = output.final_output();++ // Footer should be visible at the bottom+ assert!(+ final_text.contains("Showing"),+ "Footer should be visible after overflow finalize.\nFinal output:\n{final_text}"+ );++ // Later branches should be visible (earlier ones scrolled off the 10-row viewport)+ assert!(+ final_text.contains("overflow-10"),+ "Last branch should be visible.\nFinal output:\n{final_text}"+ );++ // No placeholder dots should remain — finalize printed real data+ assert!(+ !final_text.contains('⋯'),+ "No placeholder dots should remain after finalize.\nFinal output:\n{final_text}"+ );+}+/// Tests progressive rendering with no worktrees (fast path).#[rstest]fn test_list_progressive_fast_command(repo: TestRepo) {