Skip to content

Commit 4d259ba

Browse files
CopilotDRSDavidSoft
andcommitted
Consolidate terminal conditionals and add shell integration for bash and cmd.exe
Co-authored-by: DRSDavidSoft <4673812+DRSDavidSoft@users.noreply.github.com>
1 parent bbd7507 commit 4d259ba

File tree

3 files changed

+57
-25
lines changed

3 files changed

+57
-25
lines changed

vendor/git-prompt.sh

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,34 @@ then
4848
fi
4949
else
5050
# Source: github.com/git-for-windows/build-extra/blob/main/git-extra/git-prompt.sh
51+
52+
# Setup OSC 133 shell integration for Windows Terminal
53+
if [ -n "$WT_SESSION" ]; then
54+
__cmder_prompt_command() {
55+
local exit_code=$?
56+
# OSC 133;D - Mark end of command execution with exit code
57+
printf '\e]133;D;%s\a' "$exit_code"
58+
# OSC 133;A - Mark start of prompt
59+
printf '\e]133;A\a'
60+
return $exit_code
61+
}
62+
63+
# OSC 133;C - Mark start of command output (emitted right before command execution)
64+
__cmder_preexec() {
65+
printf '\e]133;C\a'
66+
}
67+
68+
# Append to PROMPT_COMMAND to emit sequences before each prompt
69+
if [ -z "$PROMPT_COMMAND" ]; then
70+
PROMPT_COMMAND="__cmder_prompt_command"
71+
else
72+
PROMPT_COMMAND="__cmder_prompt_command;$PROMPT_COMMAND"
73+
fi
74+
75+
# Use DEBUG trap to emit OSC 133;C before command execution
76+
trap '__cmder_preexec' DEBUG
77+
fi
78+
5179
PS1='\[\033]0;${TITLEPREFIX:+$TITLEPREFIX:}${PWD//[^[:ascii:]]/?}\007\]' # set window title to TITLEPREFIX (if set) and current working directory
5280
# PS1="$PS1"'\n' # new line (disabled)
5381
PS1="$PS1"'\[\033[32m\]' # change to green and bold
@@ -80,6 +108,11 @@ else
80108
PS1="$PS1"'\[\033[30;1m\]' # change color to grey in bold
81109
PS1="$PS1"'λ ' # prompt: Cmder uses λ
82110
PS1="$PS1"'\[\033[0m\]' # reset color
111+
112+
# OSC 133;B - Mark start of command input (Windows Terminal only)
113+
if [ -n "$WT_SESSION" ]; then
114+
PS1="$PS1"'\[\e]133;B\a\]'
115+
fi
83116
fi
84117

85118
MSYS2_PS1="$PS1" # for detection by MSYS2 SDK's bash.basrc

vendor/init.bat

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,12 @@ goto :SKIP_CLINK
221221
chcp 65001>nul
222222

223223
:: Revert back to plain cmd.exe prompt without clink
224-
prompt $E[1;32;49m$P$S$_$E[1;30;49mλ$S$E[0m
224+
:: With Windows Terminal shell integration support (OSC 133 sequences)
225+
if defined WT_SESSION (
226+
prompt $e]133;D$e\$e]133;A$e\$e]9;9;$P$e\$E[1;32;49m$P$S$_$E[1;30;49mλ$S$E[0m$e]133;B$e\
227+
) else (
228+
prompt $E[1;32;49m$P$S$_$E[1;30;49mλ$S$E[0m
229+
)
225230

226231
chcp %cp%>nul
227232

vendor/profile.ps1

Lines changed: 18 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -221,28 +221,23 @@ if ( $(Get-Command prompt).Definition -match 'PS \$\(\$executionContext.SessionS
221221
$lastSUCCESS = $?
222222
$realLastExitCode = $LastExitCode
223223

224-
# Emit OSC 133;D sequence for Windows Terminal shell integration
225-
# This marks the end of command execution with the exit code
226-
# Must be emitted before OSC 133;A (start of next prompt)
227-
# Only active in Windows Terminal ($env:WT_SESSION)
228-
if ($env:WT_SESSION) {
229-
Microsoft.PowerShell.Utility\Write-Host -NoNewline "$([char]0x1B)]133;D;$realLastExitCode$([char]7)"
230-
}
231-
232-
# Emit OSC 9;9 sequence for Windows Terminal directory tracking
233-
# This enables "Duplicate Tab" and "Split Pane" to preserve the working directory
234-
# Only active in Windows Terminal ($env:WT_SESSION) or ConEmu ($env:ConEmuPID)
235-
$loc = $executionContext.SessionState.Path.CurrentLocation
236-
if (($env:WT_SESSION -or $env:ConEmuPID) -and $loc.Provider.Name -eq "FileSystem") {
237-
Microsoft.PowerShell.Utility\Write-Host -NoNewline "$([char]0x1B)]9;9;`"$($loc.ProviderPath)`"$([char]0x1B)\"
238-
}
239-
240-
# Emit OSC 133;A sequence for Windows Terminal shell integration
241-
# This marks the start of the prompt
242-
# Enables features like command navigation, selection, and visual separators
243-
# Only active in Windows Terminal ($env:WT_SESSION)
244-
if ($env:WT_SESSION) {
245-
Microsoft.PowerShell.Utility\Write-Host -NoNewline "$([char]0x1B)]133;A$([char]7)"
224+
# Emit terminal-specific escape sequences for Windows Terminal and ConEmu
225+
if ($env:WT_SESSION -or $env:ConEmuPID) {
226+
# OSC 133;D - Mark end of command execution with exit code (Windows Terminal only)
227+
if ($env:WT_SESSION) {
228+
Microsoft.PowerShell.Utility\Write-Host -NoNewline "$([char]0x1B)]133;D;$realLastExitCode$([char]7)"
229+
}
230+
231+
# OSC 9;9 - Enable directory tracking for "Duplicate Tab" and "Split Pane"
232+
$loc = $executionContext.SessionState.Path.CurrentLocation
233+
if ($loc.Provider.Name -eq "FileSystem") {
234+
Microsoft.PowerShell.Utility\Write-Host -NoNewline "$([char]0x1B)]9;9;`"$($loc.ProviderPath)`"$([char]0x1B)\"
235+
}
236+
237+
# OSC 133;A - Mark start of prompt (Windows Terminal only)
238+
if ($env:WT_SESSION) {
239+
Microsoft.PowerShell.Utility\Write-Host -NoNewline "$([char]0x1B)]133;A$([char]7)"
240+
}
246241
}
247242

248243
$host.UI.RawUI.WindowTitle = Microsoft.PowerShell.Management\Split-Path $pwd.ProviderPath -Leaf
@@ -254,8 +249,7 @@ if ( $(Get-Command prompt).Definition -match 'PS \$\(\$executionContext.SessionS
254249
CmderPrompt
255250
PostPrompt | Microsoft.PowerShell.Utility\Write-Host -NoNewline
256251

257-
# Emit OSC 133;B sequence for Windows Terminal shell integration
258-
# This marks the start of command input (after prompt, before user types)
252+
# OSC 133;B - Mark start of command input (Windows Terminal only)
259253
if ($env:WT_SESSION) {
260254
Microsoft.PowerShell.Utility\Write-Host -NoNewline "$([char]0x1B)]133;B$([char]7)"
261255
}

0 commit comments

Comments
 (0)