Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
31 changes: 20 additions & 11 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 7 additions & 5 deletions shellenv_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
}
Expand Down
Loading