Worktrunk is a CLI for Git worktree management, designed for parallel AI agent workflows
feat: Show previous worktrees in list output
Maximilian Roosd1b622600611
diff --git a/src/commands/list/collect.rs b/src/commands/list/collect.rsindex b80feb848..d0c520846 100644--- a/src/commands/list/collect.rs+++ b/src/commands/list/collect.rs@@ -463,6 +463,7 @@ pub fn collect(.cloned().unwrap_or_else(|| worktrees.main().clone());let current_worktree_path = repo.worktree_root().ok();+ let previous_branch = repo.get_switch_history().and_then(|h| h.previous);// Sort worktrees: main first, current second, then by timestamp descendinglet sorted_worktrees = sort_worktrees(@@ -574,9 +575,17 @@ pub fn collect(.worktree_path().and_then(|p| current_worktree_path.as_ref().map(|cp| p == cp)).unwrap_or(false);- layout.format_skeleton_row(item, is_current)+ let is_previous = previous_branch+ .as_deref()+ .map(|prev| item.branch.as_deref() == Some(prev))+ .unwrap_or(false);+ layout.format_skeleton_row(item, is_current, is_previous)} else {- layout.format_list_item_line(item, current_worktree_path.as_ref())+ layout.format_list_item_line(+ item,+ current_worktree_path.as_ref(),+ previous_branch.as_deref(),+ )}}).collect();@@ -711,7 +720,11 @@ pub fn collect(}// Re-render the row with caching (now includes status if computed)- let rendered = layout.format_list_item_line(item, current_worktree_path.as_ref());+ let rendered = layout.format_list_item_line(+ item,+ current_worktree_path.as_ref(),+ previous_branch.as_deref(),+ );// Compare using full line so changes beyond the clamp (e.g., CI) still refresh.if rendered != last_rendered_lines[item_idx] {@@ -735,7 +748,11 @@ pub fn collect(if table.is_tty() {// 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, current_worktree_path.as_ref());+ let rendered = layout.format_list_item_line(+ item,+ current_worktree_path.as_ref(),+ previous_branch.as_deref(),+ );if let Err(e) = table.update_row(item_idx, rendered) {log::debug!("Final row update failed: {}", e);}@@ -746,8 +763,11 @@ pub fn collect(let mut final_lines = Vec::new();final_lines.push(layout.format_header_line());for item in &all_items {- final_lines- .push(layout.format_list_item_line(item, current_worktree_path.as_ref()));+ final_lines.push(layout.format_list_item_line(+ item,+ current_worktree_path.as_ref(),+ previous_branch.as_deref(),+ ));}final_lines.push(String::new()); // Spacerfinal_lines.push(final_msg);@@ -763,9 +783,11 @@ pub fn collect(crate::output::raw_terminal(layout.format_header_line())?;for item in &all_items {- crate::output::raw_terminal(- layout.format_list_item_line(item, current_worktree_path.as_ref()),- )?;+ crate::output::raw_terminal(layout.format_list_item_line(+ item,+ current_worktree_path.as_ref(),+ previous_branch.as_deref(),+ ))?;}crate::output::raw_terminal("")?;crate::output::raw_terminal(final_msg)?;diff --git a/src/commands/list/render.rs b/src/commands/list/render.rsindex 7cf02cfbd..8d625b6db 100644--- a/src/commands/list/render.rs+++ b/src/commands/list/render.rs@@ -365,8 +365,9 @@ impl LayoutConfig {&self,item: &ListItem,current_worktree_path: Option<&std::path::PathBuf>,+ previous_branch: Option<&str>,) -> String {- let ctx = ListRowContext::new(item, current_worktree_path);+ let ctx = ListRowContext::new(item, current_worktree_path, previous_branch);let line = self.render_line(|column| {column.render_cell(&ctx,@@ -380,7 +381,12 @@ impl LayoutConfig {}/// Render a skeleton row showing known data (branch, path) with placeholders for other columns- pub fn format_skeleton_row(&self, item: &super::model::ListItem, is_current: bool) -> String {+ pub fn format_skeleton_row(+ &self,+ item: &super::model::ListItem,+ is_current: bool,+ is_previous: bool,+ ) -> String {use crate::display::shorten_path;let branch = item.branch_name();@@ -399,12 +405,15 @@ impl LayoutConfig {match col.kind {ColumnKind::Gutter => {// Show actual gutter symbol even in skeleton+ // Priority: @ (current) > ^ (main) > - (previous) > + (regular)let has_worktree = item.worktree_path().is_some();let symbol = if has_worktree {if is_current {"@ "} else if is_main {"^ "+ } else if is_previous {+ "- "} else {"+ "}@@ -453,10 +462,15 @@ struct ListRowContext<'a> {head: &'a str,text_style: Option<Style>,is_current: bool,+ is_previous: bool,}impl<'a> ListRowContext<'a> {- fn new(item: &'a ListItem, current_worktree_path: Option<&std::path::PathBuf>) -> Self {+ fn new(+ item: &'a ListItem,+ current_worktree_path: Option<&std::path::PathBuf>,+ previous_branch: Option<&str>,+ ) -> Self {let worktree_data = item.worktree_data();let counts = item.counts();let commit = item.commit_details();@@ -468,6 +482,10 @@ impl<'a> ListRowContext<'a> {.and_then(|data| current_worktree_path.map(|p| p == &data.path)).unwrap_or(false);+ let is_previous = previous_branch+ .map(|prev| item.branch.as_deref() == Some(prev))+ .unwrap_or(false);+let mut ctx = Self {item,worktree_data,@@ -478,6 +496,7 @@ impl<'a> ListRowContext<'a> {head,text_style: None,is_current,+ is_previous,};ctx.text_style = ctx.compute_text_style(current_worktree_path);@@ -533,11 +552,13 @@ impl ColumnLayout {ColumnKind::Gutter => {let mut cell = StyledLine::new();let symbol = if let Some(data) = ctx.worktree_data {- // Worktree: priority is current > main > regular+ // Priority: @ (current) > ^ (main) > - (previous) > + (regular)if ctx.is_current {"@ " // Current worktree} else if data.is_main {"^ " // Main worktree+ } else if ctx.is_previous {+ "- " // Previous worktree (wt switch -)} else {"+ " // Regular worktree}diff --git a/tests/integration_tests/list.rs b/tests/integration_tests/list.rsindex 3b27be3f2..1fd4a73a5 100644--- a/tests/integration_tests/list.rs+++ b/tests/integration_tests/list.rs@@ -220,6 +220,31 @@ fn test_list_multiple_worktrees() {snapshot_list("multiple_worktrees", &repo);}+/// Test that the `-` gutter symbol appears for the previous worktree (target of `wt switch -`)+#[test]+fn test_list_previous_worktree_gutter() {+ let mut repo = TestRepo::new();+ repo.commit("Initial commit");++ repo.add_worktree("feature", "feature");++ // Use wt switch to establish history: main -> feature -> main+ // After this, "feature" is the previous branch+ let mut cmd = wt_command();+ repo.clean_cli_env(&mut cmd);+ cmd.args(["switch", "feature"])+ .current_dir(repo.root_path());+ cmd.output().unwrap();++ let mut cmd = wt_command();+ repo.clean_cli_env(&mut cmd);+ cmd.args(["switch", "main"]).current_dir(repo.root_path());+ cmd.output().unwrap();++ // Now list should show `-` for feature (the previous worktree)+ snapshot_list("previous_worktree_gutter", &repo);+}+#[test]fn test_list_detached_head() {let repo = TestRepo::new();diff --git a/tests/snapshots/integration__integration_tests__list__previous_worktree_gutter.snap b/tests/snapshots/integration__integration_tests__list__previous_worktree_gutter.snapnew file mode 100644index 000000000..b96955596--- /dev/null+++ b/tests/snapshots/integration__integration_tests__list__previous_worktree_gutter.snap@@ -0,0 +1,29 @@+---+source: tests/integration_tests/list.rs+info:+ program: wt+ args:+ - list+ env:+ CLICOLOR_FORCE: "1"+ COLUMNS: "150"+ GIT_AUTHOR_DATE: "2025-01-01T00:00:00Z"+ GIT_COMMITTER_DATE: "2025-01-01T00:00:00Z"+ GIT_CONFIG_GLOBAL: "[TEST_GIT_CONFIG]"+ GIT_CONFIG_SYSTEM: /dev/null+ GIT_EDITOR: ""+ LANG: C+ LC_ALL: C+ SOURCE_DATE_EPOCH: "1761609600"+ WORKTRUNK_CONFIG_PATH: "[TEST_CONFIG]"+---+success: true+exit_code: 0+----- stdout -----++----- stderr -----+ [1mBranch[0m [1mStatus[0m [1mHEAD±[0m [1mmain↕[0m [1mPath[0m [1mRemote⇅[0m [1mCommit[0m [1mAge[0m [1mMessage[0m+@ [1m[35mmain[0m [2m^[0m [1m[35m./test-repo[0m [2mb834638e[0m [2m10mo[0m [2mInitial commit[0m+- [2mfeature[0m [2m∅[0m [2m./feature[0m [2mb834638e[0m [2m10mo[0m [2mInitial commit[0m++⚪ [2mShowing 2 worktrees[0m