Worktrunk is a CLI for Git worktree management, designed for parallel AI agent workflows
refactor: Improve configure shell command output
Maximilian Roosa49708ba1f6a
diff --git a/src/commands/configure_shell.rs b/src/commands/configure_shell.rsindex fa99ceb82..478dafb34 100644--- a/src/commands/configure_shell.rs+++ b/src/commands/configure_shell.rs@@ -14,6 +14,11 @@ pub struct ConfigureResult {pub config_line: String,}+pub struct ScanResult {+ pub configured: Vec<ConfigureResult>,+ pub skipped: Vec<(Shell, PathBuf)>, // Shell + first path that was checked+}+#[derive(Debug, PartialEq)]pub enum ConfigAction {Added,@@ -48,27 +53,28 @@ pub fn handle_configure_shell(shell_filter: Option<Shell>,skip_confirmation: bool,command_name: String,-) -> Result<Vec<ConfigureResult>, String> {+) -> Result<ScanResult, String> {// First, do a dry-run to see what would be changed- let preview_results = scan_shell_configs(shell_filter, true, &command_name)?;+ let preview = scan_shell_configs(shell_filter, true, &command_name)?;// If nothing to do, return early- if preview_results.is_empty() {- return Ok(vec![]);+ if preview.configured.is_empty() {+ return Ok(preview);}// Check if any changes are needed (not all are AlreadyExists)- let needs_changes = preview_results+ let needs_changes = preview+ .configured.iter().any(|r| !matches!(r.action, ConfigAction::AlreadyExists));// If nothing needs to be changed, just return the preview resultsif !needs_changes {- return Ok(preview_results);+ return Ok(preview);}// Show what will be done and ask for confirmation (unless --force flag is used)- if !skip_confirmation && !prompt_for_confirmation(&preview_results)? {+ if !skip_confirmation && !prompt_for_confirmation(&preview.configured)? {return Err("Cancelled by user".to_string());}@@ -80,7 +86,7 @@ fn scan_shell_configs(shell_filter: Option<Shell>,dry_run: bool,command_name: &str,-) -> Result<Vec<ConfigureResult>, String> {+) -> Result<ScanResult, String> {let shells = if let Some(shell) = shell_filter {vec![shell]} else {@@ -94,7 +100,7 @@ fn scan_shell_configs(};let mut results = Vec::new();- let mut checked_paths = Vec::new();+ let mut skipped = Vec::new();for shell in shells {let paths = shell.config_paths(command_name);@@ -115,9 +121,6 @@ fn scan_shell_configs(target_path.is_some()};- // Track all checked paths for better error messages- checked_paths.extend(paths.iter().map(|p| (shell, p.clone())));-// Only configure if explicitly targeting this shell OR if config file/location existslet should_configure = shell_filter.is_some() || has_config_location;@@ -140,24 +143,32 @@ fn scan_shell_configs(}}}+ } else if shell_filter.is_none() {+ // Track skipped shells (only when not explicitly filtering)+ // For Fish, we check for conf.d directory; for others, the config file+ let skipped_path = if matches!(shell, Shell::Fish) {+ paths+ .first()+ .and_then(|p| p.parent())+ .map(|p| p.to_path_buf())+ } else {+ paths.first().cloned()+ };+ if let Some(path) = skipped_path {+ skipped.push((shell, path));+ }}}- if results.is_empty() && shell_filter.is_none() {- // Provide helpful error message with checked locations- let example_paths: Vec<String> = checked_paths- .iter()- .take(3)- .map(|(_, p)| p.display().to_string())- .collect();-- return Err(format!(- "No shell config files found in $HOME. Checked: {}, and more. Create a config file or use --shell to specify a shell.",- example_paths.join(", ")- ));+ if results.is_empty() && shell_filter.is_none() && skipped.is_empty() {+ // No shells checked at all (shouldn't happen normally)+ return Err("No shell config files found".to_string());}- Ok(results)+ Ok(ScanResult {+ configured: results,+ skipped,+ })}fn configure_shell_file(diff --git a/src/main.rs b/src/main.rsindex 46e28dede..dee023a3f 100644--- a/src/main.rs+++ b/src/main.rs@@ -196,28 +196,18 @@ fn main() {// Auto-write to shell config fileshandle_configure_shell(shell, force, command_name).map_err(|e| anyhow::anyhow!("{}", e))- .map(|results| {+ .and_then(|scan_result| {use anstyle::{AnsiColor, Color};+ use worktrunk::styling::{HINT, HINT_EMOJI, format_bash_with_gutter};- // Count actual changes (not AlreadyExists)- let changes_count = results+ let changes_count = scan_result+ .configured.iter().filter(|r| !matches!(r.action, ConfigAction::AlreadyExists)).count();- if changes_count == 0 {- // All shells already configured- let green =- Style::new().fg_color(Some(Color::Ansi(AnsiColor::Green)));- println!(- "{SUCCESS_EMOJI} {green}All shells already configured{green:#}"- );- return;- }-- // Show what was done (instant operations, no progress needed)- for result in &results {- use worktrunk::styling::format_bash_with_gutter;+ // Show configured shells+ for result in &scan_result.configured {let bold = Style::new().bold();let shell = result.shell;let path = format_path_for_display(&result.path);@@ -227,24 +217,42 @@ fn main() {result.action.emoji(),result.action.description(),);- // Show config line with gutter- print!("{}", format_bash_with_gutter(&result.config_line, ""));+ // Show config line only for new additions+ if changes_count > 0 {+ print!("{}", format_bash_with_gutter(&result.config_line, ""));+ }+ }++ // Show skipped shells+ let dimmed = Style::new().dimmed();+ for (shell, path) in &scan_result.skipped {+ let path = format_path_for_display(path);+ println!("{HINT_EMOJI} {dimmed}{shell} {path} (not found){dimmed:#}");+ }++ // Exit with error if no shells configured+ if scan_result.configured.is_empty() {+ return Err(anyhow::anyhow!("No shell config files found"));}- // Success summary- println!();+ // Summarylet green = Style::new().fg_color(Some(Color::Ansi(AnsiColor::Green)));- let plural = if changes_count == 1 { "" } else { "s" };- println!(- "{SUCCESS_EMOJI} {green}Configured {changes_count} shell{plural}{green:#}"- );-- // Show hint about restarting shell- println!();- use worktrunk::styling::{HINT, HINT_EMOJI};- println!(- "{HINT_EMOJI} {HINT}Restart your shell or run: source <config-file>{HINT:#}"- );+ if changes_count > 0 {+ println!();+ let plural = if changes_count == 1 { "" } else { "s" };+ println!(+ "{SUCCESS_EMOJI} {green}Configured {changes_count} shell{plural}{green:#}"+ );+ println!();+ println!(+ "{HINT_EMOJI} {HINT}Restart your shell or run: source <config-file>{HINT:#}"+ );+ } else {+ println!(+ "{SUCCESS_EMOJI} {green}All shells already configured{green:#}"+ );+ }+ Ok(())})}},diff --git a/tests/integration_tests/configure_shell.rs b/tests/integration_tests/configure_shell.rsindex 296810127..f2eccc9f5 100644--- a/tests/integration_tests/configure_shell.rs+++ b/tests/integration_tests/configure_shell.rs@@ -30,6 +30,8 @@ fn test_configure_shell_with_yes() {----- stdout -----β Added [1mzsh[0m ~/.zshrc[107m [0m [1m[35mif[0m [1m[34mcommand[0m [36m-v[0m wt [36m>[0m/dev/null [33m2[0m>&1; [1m[35mthen[0m [1m[34meval[0m [32m"$([1m[34mcommand[0m wt config shell init zsh)"[0m; [1m[35mfi[0m[0m+ π‘ [2mbash ~/.bash_profile (not found)[0m+ π‘ [2mfish ~/.config/fish/conf.d (not found)[0mβ [32mConfigured 1 shell[0m@@ -116,6 +118,7 @@ fn test_configure_shell_already_exists() {success: trueexit_code: 0----- stdout -----+ βͺ Already configured [1mzsh[0m ~/.zshrcβ [32mAll shells already configured[0m----- stderr -----@@ -200,7 +203,10 @@ fn test_configure_shell_no_files() {success: falseexit_code: 1----- stdout ------ β [31mNo shell config files found in $HOME. Checked: [TEMP_HOME]/.bash_profile, [TEMP_HOME]/.zshrc, and more. Create a config file or use --shell to specify a shell.[0m+ π‘ [2mbash ~/.bash_profile (not found)[0m+ π‘ [2mzsh ~/.zshrc (not found)[0m+ π‘ [2mfish ~/.config/fish/conf.d (not found)[0m+ β [31mNo shell config files found[0m----- stderr -----");@@ -238,6 +244,7 @@ fn test_configure_shell_multiple_configs() {[107m [0m [1m[35mif[0m [1m[34mcommand[0m [36m-v[0m wt [36m>[0m/dev/null [33m2[0m>&1; [1m[35mthen[0m [1m[34meval[0m [32m"$([1m[34mcommand[0m wt config shell init bash)"[0m; [1m[35mfi[0m[0mβ Added [1mzsh[0m ~/.zshrc[107m [0m [1m[35mif[0m [1m[34mcommand[0m [36m-v[0m wt [36m>[0m/dev/null [33m2[0m>&1; [1m[35mthen[0m [1m[34meval[0m [32m"$([1m[34mcommand[0m wt config shell init zsh)"[0m; [1m[35mfi[0m[0m+ π‘ [2mfish ~/.config/fish/conf.d (not found)[0mβ [32mConfigured 2 shells[0m@@ -298,6 +305,7 @@ fn test_configure_shell_mixed_states() {[107m [0m [1m[35mif[0m [1m[34mcommand[0m [36m-v[0m wt [36m>[0m/dev/null [33m2[0m>&1; [1m[35mthen[0m [1m[34meval[0m [32m"$([1m[34mcommand[0m wt config shell init bash)"[0m; [1m[35mfi[0m[0mβ Added [1mzsh[0m ~/.zshrc[107m [0m [1m[35mif[0m [1m[34mcommand[0m [36m-v[0m wt [36m>[0m/dev/null [33m2[0m>&1; [1m[35mthen[0m [1m[34meval[0m [32m"$([1m[34mcommand[0m wt config shell init zsh)"[0m; [1m[35mfi[0m[0m+ π‘ [2mfish ~/.config/fish/conf.d (not found)[0mβ [32mConfigured 1 shell[0m