Worktrunk is a CLI for Git worktree management, designed for parallel AI agent workflows
Feat: Improve shell integration hint for `switch` and `remove`
Maximilian Roos20da62fa128c
diff --git a/src/commands/worktree.rs b/src/commands/worktree.rsindex 4e45ca77f..6a5dbe024 100644--- a/src/commands/worktree.rs+++ b/src/commands/worktree.rs@@ -70,8 +70,25 @@ use std::io::{self, Write};use std::path::PathBuf;use worktrunk::config::{ProjectConfig, WorktrunkConfig};use worktrunk::git::{GitError, Repository};+use worktrunk::shell::Shell;use worktrunk::styling::{format_error, format_error_with_bold, format_hint};+/// Generate hint message for shell integration setup+///+/// Returns a hint message based on whether shell integration is configured:+/// - If configured but not active: provides exact source command+/// - If not configured: suggests running configure-shell+fn shell_integration_hint() -> String {+ if let Some(config_path) = Shell::is_integration_configured() {+ format!(+ "\n\nShell integration configured. Restart your shell or run: source {}",+ config_path.display()+ )+ } else {+ "\n\nTo enable automatic cd, run: wt configure-shell".to_string()+ }+}+/// Result of a worktree switch operationpub enum SwitchResult {/// Switched to existing worktree at the given path@@ -102,10 +119,8 @@ impl SwitchResult {path.display())};- Some(format!(- "{}\n\nTo enable automatic cd, run: wt configure-shell",- msg- ))++ Some(format!("{}{}", msg, shell_integration_hint()))}}}@@ -154,8 +169,9 @@ impl RemoveResult {Some(format!("Already on default branch '{}'", branch))}RemoveResult::RemovedWorktree { primary_path } => Some(format!(- "Moved to primary worktree and removed worktree\nPath: {}\n\nTo enable automatic cd, run: wt configure-shell",- primary_path.display()+ "Moved to primary worktree and removed worktree\nPath: {}{}",+ primary_path.display(),+ shell_integration_hint())),RemoveResult::SwitchedToDefault(branch) => {Some(format!("Switched to default branch '{}'", branch))@@ -369,41 +385,6 @@ fn check_worktree_conflicts(Ok(())}-/// Execute a command in the specified worktree directory-fn execute_command_in_worktree(- worktree_path: &std::path::Path,- command: &str,-) -> Result<(), GitError> {- use std::process::Command;-- // Use platform-specific shell- #[cfg(target_os = "windows")]- let (shell, shell_arg) = ("cmd", "/C");- #[cfg(not(target_os = "windows"))]- let (shell, shell_arg) = ("sh", "-c");-- let output = Command::new(shell)- .arg(shell_arg)- .arg(command)- .current_dir(worktree_path)- .output()- .map_err(|e| GitError::CommandFailed(format!("Failed to execute command: {}", e)))?;-- // Forward stdout/stderr to user- std::io::stdout().write_all(&output.stdout).ok();- std::io::stderr().write_all(&output.stderr).ok();-- if !output.status.success() {- return Err(GitError::CommandFailed(format!(- "Command '{}' exited with status: {}",- command,- output.status.code().unwrap_or(-1)- )));- }-- Ok(())-}-/// Prompt the user to approve a command for executionfn prompt_for_approval(command: &str, project_id: &str) -> io::Result<bool> {eprintln!();diff --git a/src/shell.rs b/src/shell.rsindex 6fd3de616..c379d0735 100644--- a/src/shell.rs+++ b/src/shell.rs@@ -135,6 +135,86 @@ impl Shell {}}}++ /// Check if shell integration is configured in any shell's config file+ ///+ /// Returns the path to the first config file with integration if found.+ /// This helps detect the "configured but not restarted shell" state.+ ///+ /// This function is prefix-agnostic - it detects integration patterns regardless+ /// of what cmd_prefix was used during configuration (wt, worktree, etc).+ pub fn is_integration_configured() -> Option<PathBuf> {+ use std::fs;+ use std::io::{BufRead, BufReader};++ let home = PathBuf::from(std::env::var("HOME").unwrap_or_else(|_| ".".to_string()));++ // Check common shell config files for integration patterns+ let config_files = vec![+ // Bash+ home.join(".bashrc"),+ home.join(".bash_profile"),+ home.join(".profile"),+ // Zsh+ home.join(".zshrc"),+ std::env::var("ZDOTDIR")+ .map(PathBuf::from)+ .unwrap_or_else(|_| home.clone())+ .join(".zshrc"),+ // Nushell+ home.join(".config/nushell/config.nu"),+ // Elvish+ home.join(".config/elvish/rc.elv"),+ // Xonsh+ home.join(".xonshrc"),+ // Powershell+ home.join(".config/powershell/Microsoft.PowerShell_profile.ps1"),+ ];++ // Check standard config files for eval pattern (any prefix)+ for path in config_files {+ if !path.exists() {+ continue;+ }++ if let Ok(file) = fs::File::open(&path) {+ let reader = BufReader::new(file);+ for line in reader.lines().map_while(Result::ok) {+ let trimmed = line.trim();+ // Match: eval "$(anything init bash/zsh/etc)"+ // This catches both wt and custom prefixes+ if (trimmed.starts_with("eval \"$(") || trimmed.starts_with("eval '$("))+ && trimmed.contains(" init ")+ {+ return Some(path);+ }+ }+ }+ }++ // Check Fish conf.d directory for any .fish files (Fish integration)+ let fish_conf_d = home.join(".config/fish/conf.d");+ if fish_conf_d.exists()+ && let Ok(entries) = fs::read_dir(&fish_conf_d)+ {+ for entry in entries.flatten() {+ let path = entry.path();+ if path.extension().and_then(|s| s.to_str()) == Some("fish")+ && let Ok(content) = fs::read_to_string(&path)+ {+ // Look for function definitions with switch/remove/merge handling+ if content.contains("function ")+ && content.contains("switch")+ && content.contains("__WORKTRUNK_CD__")+ {+ return Some(path);+ }+ }+ }+ }++ None+ }}/// Shell integration configuration