diff --git a/README.md b/README.md index 7f41084..e3d61d7 100644 --- a/README.md +++ b/README.md @@ -54,7 +54,7 @@ sudo cp bin/wt /usr/local/bin/ ### Shell Integration (Optional but Recommended) -Add this to the **END** of your `~/.bashrc` or `~/.zshrc`: +Add this to the **END** of your `~/.bashrc` or `~/.zshrc` (auto-cd requires the `script` command, available by default on macOS and most Linux distros): ```bash source <(wt shellenv) diff --git a/main.go b/main.go index 085b8fb..1ef1b22 100644 --- a/main.go +++ b/main.go @@ -723,17 +723,26 @@ Register-ArgumentCompleter -CommandName wt -ScriptBlock { // Bash/Zsh integration for Unix systems fmt.Print(`wt() { - # All commands (including interactive) need output capture for auto-cd - local output - output=$(command wt "$@") - local exit_code=$? - echo "$output" - if [ $exit_code -eq 0 ]; then - local cd_path=$(echo "$output" | grep "^TREE_ME_CD:" | cut -d: -f2-) - [ -n "$cd_path" ] && cd "$cd_path" - fi - return $exit_code -} + if ! command -v script >/dev/null 2>&1; then + command wt "$@" + return $? + fi + + local wt_log exit_code cd_path + wt_log=$(mktemp -t wt.XXXXXX) + + script -q "$wt_log" /bin/sh -c 'command wt "$@"' wt "$@" + exit_code=$? + + cd_path=$(grep '^TREE_ME_CD:' "$wt_log" | tail -1 | cut -d: -f2-) + rm -f "$wt_log" + cd_path=${cd_path%$'\r'} + + if [ $exit_code -eq 0 ] && [ -n "$cd_path" ]; then + cd "$cd_path" + fi + return $exit_code + } # Bash completion if [ -n "$BASH_VERSION" ]; then diff --git a/shellenv_test.go b/shellenv_test.go index e987311..66fb58e 100644 --- a/shellenv_test.go +++ b/shellenv_test.go @@ -40,13 +40,15 @@ func TestShellenvInteractiveModeOutputCapture(t *testing.T) { "EXPECTED: Remove the special case and let all commands use the same output capture logic.") } - // Verify the fix: should have output capture - if !strings.Contains(shellenv, "output=$(command wt \"$@\")") { - t.Error("Shell function must capture command output to support auto-cd") + if strings.Contains(shellenv, "output=$(command wt \"$@\")") { + t.Error("Shell function should not rely on basic command substitution; interactive prompts would hang") } - // Verify the fix: should extract cd_path from output - if !strings.Contains(shellenv, "cd_path=$(echo \"$output\" | grep \"^TREE_ME_CD:\"") { + if !strings.Contains(shellenv, "script -q \"$wt_log\"") { + t.Error("Shell function must use script(1) to support interactive prompts") + } + + if !strings.Contains(shellenv, "grep '^TREE_ME_CD:'") { t.Error("Shell function must extract cd_path from TREE_ME_CD marker") } }