Skip to content

Commit d1ff234

Browse files
Copilotsawka
andcommitted
Add OSC 7 support for fish and pwsh shells
Co-authored-by: sawka <2722291+sawka@users.noreply.github.com>
1 parent 862b3bb commit d1ff234

2 files changed

Lines changed: 104 additions & 2 deletions

File tree

pkg/util/shellutil/shellintegration/fish_wavefish.sh

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,41 @@ wsh token "$WAVETERM_SWAPTOKEN" fish 2>/dev/null | source
77
set -e WAVETERM_SWAPTOKEN
88

99
# Load Wave completions
10-
wsh completion fish | source
10+
wsh completion fish | source
11+
12+
# shell integration
13+
function _waveterm_si_blocked
14+
# Check if we're in tmux or screen
15+
test -n "$TMUX" -o -n "$STY" -o "$TERM" = "tmux" -o "$TERM" = "screen"
16+
end
17+
18+
function _waveterm_si_urlencode
19+
set -l str $argv[1]
20+
# URL encode the path
21+
# Escape % first
22+
set str (string replace -a '%' '%25' -- $str)
23+
# Common reserved characters in file paths
24+
set str (string replace -a ' ' '%20' -- $str)
25+
set str (string replace -a '#' '%23' -- $str)
26+
set str (string replace -a '?' '%3F' -- $str)
27+
set str (string replace -a '&' '%26' -- $str)
28+
set str (string replace -a ';' '%3B' -- $str)
29+
set str (string replace -a '+' '%2B' -- $str)
30+
echo -n $str
31+
end
32+
33+
function _waveterm_si_osc7
34+
_waveterm_si_blocked; and return
35+
set -l encoded_pwd (_waveterm_si_urlencode $PWD)
36+
printf '\033]7;file://%s%s\007' $hostname $encoded_pwd
37+
end
38+
39+
# Hook OSC 7 to prompt and directory changes
40+
function _waveterm_si_prompt --on-event fish_prompt
41+
_waveterm_si_osc7
42+
end
43+
44+
# Also update on directory change
45+
function _waveterm_si_chpwd --on-variable PWD
46+
_waveterm_si_osc7
47+
end

pkg/util/shellutil/shellintegration/pwsh_wavepwsh.sh

Lines changed: 66 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,69 @@ Remove-Variable -Name waveterm_swaptoken_output
1010
Remove-Item Env:WAVETERM_SWAPTOKEN
1111

1212
# Load Wave completions
13-
wsh completion powershell | Out-String | Invoke-Expression
13+
wsh completion powershell | Out-String | Invoke-Expression
14+
15+
# shell integration
16+
function Global:_waveterm_si_blocked {
17+
# Check if we're in tmux or screen
18+
return ($env:TMUX -or $env:STY -or $env:TERM -like "tmux*" -or $env:TERM -like "screen*")
19+
}
20+
21+
function Global:_waveterm_si_urlencode {
22+
param([string]$str)
23+
# URL encode the path
24+
# Escape % first
25+
$str = $str -replace '%', '%25'
26+
# Common reserved characters in file paths
27+
$str = $str -replace ' ', '%20'
28+
$str = $str -replace '#', '%23'
29+
$str = $str -replace '\?', '%3F'
30+
$str = $str -replace '&', '%26'
31+
$str = $str -replace ';', '%3B'
32+
$str = $str -replace '\+', '%2B'
33+
return $str
34+
}
35+
36+
function Global:_waveterm_si_osc7 {
37+
if (_waveterm_si_blocked) { return }
38+
39+
$pwd_str = $PWD.Path
40+
41+
# Convert Windows path to file:// URL format
42+
# Replace backslashes with forward slashes
43+
$pwd_str = $pwd_str -replace '\\', '/'
44+
45+
# Ensure it starts with / for proper file:// URL format
46+
# Windows paths like C:/... need to become /C:/...
47+
if ($pwd_str -match '^[a-zA-Z]:') {
48+
$pwd_str = '/' + $pwd_str
49+
}
50+
51+
$encoded_pwd = _waveterm_si_urlencode $pwd_str
52+
$hostname = $env:COMPUTERNAME
53+
if (-not $hostname) {
54+
$hostname = hostname
55+
}
56+
57+
# OSC 7 - current directory
58+
Write-Host -NoNewline "`e]7;file://$hostname$encoded_pwd`a"
59+
}
60+
61+
# Hook OSC 7 to prompt
62+
function Global:_waveterm_si_prompt {
63+
_waveterm_si_osc7
64+
}
65+
66+
# Add the OSC 7 call to the prompt function
67+
if (Test-Path Function:\prompt) {
68+
$global:_waveterm_original_prompt = $function:prompt
69+
function Global:prompt {
70+
_waveterm_si_prompt
71+
& $global:_waveterm_original_prompt
72+
}
73+
} else {
74+
function Global:prompt {
75+
_waveterm_si_prompt
76+
"PS $($executionContext.SessionState.Path.CurrentLocation)$('>' * ($nestedPromptLevel + 1)) "
77+
}
78+
}

0 commit comments

Comments
 (0)