Worktrunk is a CLI for Git worktree management, designed for parallel AI agent workflows
Refactor ProgressiveTable: separate data mutation from rendering (#351)
Maximilian Roosa8a69cff5da7
diff --git a/src/commands/list/collect.rs b/src/commands/list/collect.rsindex 086f47182..acb8e204a 100644--- a/src/commands/list/collect.rs+++ b/src/commands/list/collect.rs@@ -1034,9 +1034,7 @@ pub fn collect(let footer_msg = format!("{INFO_SYMBOL} {dim}{footer_base} ({completed_results}/{total_results} loaded){dim:#}");- if let Err(e) = table.update_footer(footer_msg) {- log::debug!("Progressive footer update failed: {}", e);- }+ table.update_footer(footer_msg);// Re-render the row with caching (now includes status if computed)let rendered = layout.format_list_item_line(item, previous_branch.as_deref());@@ -1044,9 +1042,12 @@ pub fn collect(// Compare using full line so changes beyond the clamp (e.g., CI) still refresh.if rendered != last_rendered_lines[item_idx] {last_rendered_lines[item_idx] = rendered.clone();- if let Err(e) = table.update_row(item_idx, rendered) {- log::debug!("Progressive row update failed: {}", e);- }+ table.update_row(item_idx, rendered);+ }++ // Flush updates to terminal+ if let Err(e) = table.flush() {+ log::debug!("Progressive table flush failed: {}", e);}}},@@ -1097,9 +1098,7 @@ pub fn collect(// Interactive: do final render pass and update footer to summaryfor (item_idx, item) in all_items.iter().enumerate() {let rendered = layout.format_list_item_line(item, previous_branch.as_deref());- if let Err(e) = table.update_row(item_idx, rendered) {- log::debug!("Final row update failed: {}", e);- }+ table.update_row(item_idx, rendered);}table.finalize_tty(final_msg)?;} else {diff --git a/src/commands/list/progressive_table.rs b/src/commands/list/progressive_table.rsindex 07b8c230f..a14a7df68 100644--- a/src/commands/list/progressive_table.rs+++ b/src/commands/list/progressive_table.rs@@ -13,7 +13,7 @@ use crossterm::{cursor::{MoveToColumn, MoveUp},terminal::{Clear, ClearType},};-use std::io::{IsTerminal, Write, stderr};+use std::io::{IsTerminal, Write, stdout};use crate::display::truncate_visible;@@ -24,6 +24,9 @@ use crate::display::truncate_visible;/// - N data rows (one per worktree/branch)/// - Spacer (blank line)/// - Footer (loading status / summary)+///+/// Data mutation (`update_row`, `update_footer`) is separate from rendering (`flush`).+/// Call `flush()` after updates to write changes to the terminal.pub struct ProgressiveTable {/// Previously rendered content for each line (header + rows + spacer + footer)lines: Vec<String>,@@ -33,6 +36,8 @@ pub struct ProgressiveTable {row_count: usize,/// Whether output is going to a TTYis_tty: bool,+ /// Lines that have been modified since last flush+ dirty: Vec<usize>,}impl ProgressiveTable {@@ -51,7 +56,7 @@ impl ProgressiveTable {initial_footer: String,max_width: usize,) -> Self {- let is_tty = stderr().is_terminal();+ let is_tty = stdout().is_terminal();let row_count = skeletons.len();// Build initial lines: header + rows + spacer + footer@@ -73,10 +78,11 @@ impl ProgressiveTable {max_width,row_count,is_tty,+ dirty: Vec::new(),}}- /// Print the initial table to stderr (TTY only).+ /// Print the initial table to stdout (TTY only).pub fn render_initial(&self) -> std::io::Result<()> {if self.is_tty {self.print_all()?;@@ -84,23 +90,28 @@ impl ProgressiveTable {Ok(())}- /// Print all lines to stderr.+ /// Print all lines to stdout.fn print_all(&self) -> std::io::Result<()> {- let mut stderr = stderr();+ let mut stdout = stdout();for line in &self.lines {- writeln!(stderr, "{}", line)?;+ writeln!(stdout, "{}", line)?;}- stderr.flush()+ stdout.flush()}/// Update a data row at the given index.///+ /// This only updates the internal state. Call `flush()` to render changes.+ ////// # Arguments/// * `row_idx` - Index of the data row (0-based, not counting header)/// * `content` - New content for the row- pub fn update_row(&mut self, row_idx: usize, content: String) -> std::io::Result<()> {+ ///+ /// # Returns+ /// `true` if the content changed, `false` if unchanged or out of bounds.+ pub fn update_row(&mut self, row_idx: usize, content: String) -> bool {if row_idx >= self.row_count {- return Ok(());+ return false;}let truncated = truncate_visible(&content, self.max_width);@@ -110,20 +121,21 @@ impl ProgressiveTable {// Skip if content hasn't changedif self.lines[line_idx] == truncated {- return Ok(());+ return false;}self.lines[line_idx] = truncated;-- if self.is_tty {- self.redraw_line(line_idx)?;- }-- Ok(())+ self.dirty.push(line_idx);+ true}/// Update the footer message.- pub fn update_footer(&mut self, content: String) -> std::io::Result<()> {+ ///+ /// This only updates the internal state. Call `flush()` to render changes.+ ///+ /// # Returns+ /// `true` if the content changed, `false` if unchanged.+ pub fn update_footer(&mut self, content: String) -> bool {let truncated = truncate_visible(&content, self.max_width);// Footer is the last line@@ -131,13 +143,27 @@ impl ProgressiveTable {// Skip if content hasn't changedif self.lines[footer_idx] == truncated {- return Ok(());+ return false;}self.lines[footer_idx] = truncated;+ self.dirty.push(footer_idx);+ true+ }- if self.is_tty {- self.redraw_line(footer_idx)?;+ /// Flush pending changes to the terminal.+ ///+ /// Redraws all lines that have been modified since the last flush.+ /// No-op in non-TTY mode.+ pub fn flush(&mut self) -> std::io::Result<()> {+ if !self.is_tty || self.dirty.is_empty() {+ self.dirty.clear();+ return Ok(());+ }++ // Take ownership of dirty indices to avoid borrow conflict with redraw_line+ for line_idx in std::mem::take(&mut self.dirty) {+ self.redraw_line(line_idx)?;}Ok(())@@ -145,7 +171,7 @@ impl ProgressiveTable {/// Redraw a specific line by moving cursor up, clearing, and printing.fn redraw_line(&self, line_idx: usize) -> std::io::Result<()> {- let mut stderr = stderr();+ let mut stdout = stdout();// Calculate how many lines up from current position// Current position is after the footer (last line)@@ -153,27 +179,27 @@ impl ProgressiveTable {// Move cursor up to the target lineif lines_up > 0 {- stderr.execute(MoveUp(lines_up as u16))?;+ stdout.execute(MoveUp(lines_up as u16))?;}// Move to column 0 and clear the line- stderr.execute(MoveToColumn(0))?;- stderr.execute(Clear(ClearType::CurrentLine))?;+ stdout.execute(MoveToColumn(0))?;+ stdout.execute(Clear(ClearType::CurrentLine))?;// Print the new content- write!(stderr, "{}", self.lines[line_idx])?;+ write!(stdout, "{}", self.lines[line_idx])?;// Move cursor back to the end (after footer)// We need to move down (lines_up) lines, but since we printed one line// without newline, we need to print newlines to get backfor _ in 0..lines_up {- writeln!(stderr)?;+ writeln!(stdout)?;}- stderr.flush()+ stdout.flush()}- /// Finalize for TTY: do final render pass and leave output in place.+ /// Finalize for TTY: update footer and flush.////// # Arguments/// * `final_footer` - Final summary message to replace loading status@@ -182,10 +208,8 @@ impl ProgressiveTable {return Ok(());}- // Update footer with final summary- self.update_footer(final_footer)?;-- Ok(())+ self.update_footer(final_footer);+ self.flush()}/// Check if output is going to a TTY.@@ -204,7 +228,6 @@ mod tests {let skeletons = vec!["row0".to_string(), "row1".to_string()];let footer = "loading".to_string();- // new() no longer has I/O side effects — render_initial() must be called explicitlylet mut table =ProgressiveTable::new(header.clone(), skeletons.clone(), footer.clone(), 80);@@ -216,20 +239,20 @@ mod tests {assert!(table.lines[3].is_empty(), "spacer should be blank");assert_eq!(table.lines[4], footer);- // No-op when index out of bounds- table.update_row(5, "ignored".into()).unwrap();+ // No-op when index out of bounds (returns false)+ assert!(!table.update_row(5, "ignored".into()));// Update row content and verify it changed- table.update_row(1, "row1-updated".into()).unwrap();+ assert!(table.update_row(1, "row1-updated".into()));assert_eq!(table.lines[2], "row1-updated");- // Updating with identical content should be a no-op+ // Updating with identical content returns false (no change)let before = table.lines[2].clone();- table.update_row(1, before.clone()).unwrap();+ assert!(!table.update_row(1, before.clone()));assert_eq!(table.lines[2], before);// Footer update- table.update_footer("done".into()).unwrap();+ assert!(table.update_footer("done".into()));assert_eq!(table.lines.last().unwrap(), "done");}@@ -261,8 +284,8 @@ mod tests {// First footer should matchassert_eq!(table.lines.last().unwrap(), &footer);- // Update with same content should be a no-op- table.update_footer(footer.clone()).unwrap();+ // Update with same content returns false (no change)+ assert!(!table.update_footer(footer.clone()));assert_eq!(table.lines.last().unwrap(), &footer);}@@ -275,9 +298,8 @@ mod tests {80,);- // In test environment, stderr is typically not a TTY- // Just ensure the method can be called without panicking- let _is_tty = table.is_tty();+ // In test environment, stdout is typically not a TTY+ let _ = table.is_tty();}#[test]@@ -301,10 +323,8 @@ mod tests {80,);- // Should not panic when updating out-of-bounds row- table- .update_row(10, "should be ignored".to_string())- .unwrap();+ // Out-of-bounds update returns false+ assert!(!table.update_row(10, "should be ignored".to_string()));// Original rows should be unchangedassert_eq!(table.lines[1], "row0");@@ -312,7 +332,7 @@ mod tests {}#[test]- fn test_finalize_tty_updates_footer() {+ fn test_finalize_tty_non_tty_mode() {let mut table = ProgressiveTable::new("header".to_string(),vec!["row".to_string()],@@ -320,23 +340,36 @@ mod tests {80,);- // In non-TTY test environment, finalize_tty should still update footer internally+ // In non-TTY mode (typical test environment), finalize_tty returns early+ // without updating footer because is_tty is falsetable.finalize_tty("Complete!".to_string()).unwrap();- // Note: In non-TTY mode, finalize_tty returns early, so footer won't change- // This tests that the method doesn't panic+ // Verify no panic and no error - that's all we can reliably test+ // since is_tty depends on the test environment}#[test]- fn test_render_initial_non_tty() {- let table = ProgressiveTable::new(+ fn test_dirty_tracking() {+ let mut table = ProgressiveTable::new("header".to_string(),- vec!["row".to_string()],+ vec!["row0".to_string(), "row1".to_string()],"footer".to_string(),80,);- // In non-TTY mode, render_initial should be a no-op- table.render_initial().unwrap();+ // Initially no dirty lines+ assert!(table.dirty.is_empty());++ // Update marks line as dirty+ table.update_row(0, "updated".into());+ assert_eq!(table.dirty, vec![1]); // line_idx = row_idx + 1++ // Footer update adds to dirty list+ table.update_footer("new footer".into());+ assert_eq!(table.dirty, vec![1, 4]); // footer is last line++ // Flush clears dirty list+ table.flush().unwrap();+ assert!(table.dirty.is_empty());}}