T34: review round 3: PowerShell keeps a program's code unless the line surely ends in PowerShell, a cmdlet, a name not found or no command, so a call through a variable keeps it; bash without promptvars, zsh without prompt_percent and PowerShell without PSReadLine say at start what they cannot mark
CI / platforms (ubuntu-24.04) (push) Successful in 1m21s
CI / platforms (windows-latest) (push) Successful in 1m40s
CI / macos (push) Successful in 11m13s

This commit is contained in:
ZacharyZhang-NY committed 2026-10-16 23:28:36 -04:00
1 parent d4bc6e69fa
commit 2c25116703
6 files changed
+50 -9

No files matched your search

@@ -52,3 +52,5 @@ _ely_mark() {
fi
}
PROMPT_COMMAND="_ely_prompt${PROMPT_COMMAND:+;$PROMPT_COMMAND};_ely_mark"
# PS0 and the prompt mark a command only through expansions, which promptvars turns on.
builtin shopt -q promptvars || builtin printf 'ELYTerminal: bash marks no command while promptvars is off (shopt -s promptvars)\n' >&2
+13 -7
View File
@@ -11,7 +11,7 @@ function global:prompt {
# Each native run sets a new $LASTEXITCODE, equal or not; strict mode refuses an unset one.
$exit = Get-Variable LASTEXITCODE -Scope Global -ValueOnly -ErrorAction Ignore
$ran = $null -ne $exit -and -not [object]::ReferenceEquals($exit, $global:ElyExit)
$code = if ($global:ElyNative -and $ran -and $exit -ne 0) { $exit } else { 1 }
$code = if (-not $global:ElyShell -and $ran -and $exit -ne 0) { $exit } else { 1 }
}
[Console]::Write("$esc]133;D;$code$bel")
$global:ElyRan = $false
@@ -38,22 +38,28 @@ if (Test-Path Function:\PSConsoleHostReadLine) {
if (-not [string]::IsNullOrWhiteSpace($line)) {
$global:ElyRan = $true
$global:ElyExit = Get-Variable LASTEXITCODE -Scope Global -ValueOnly -ErrorAction Ignore
# A program's exit code is the line's when the line's last pipeline ends in it.
$global:ElyNative = $false
# A program's code is the line's, unless the line surely ends in PowerShell: a cmdlet, a name not found, or no command.
$ast = [System.Management.Automation.Language.Parser]::ParseInput($line, [ref]$null, [ref]$null)
# Strict mode refuses .Count on one statement alone, so the statements stay a list.
$statements = @(if ($ast.EndBlock) { $ast.EndBlock.Statements })
$last = if ($statements.Count) { $statements[$statements.Count - 1] }
if ($last -is [System.Management.Automation.Language.PipelineAst]) {
$elements = $last.PipelineElements
$end = $elements[$elements.Count - 1]
$name = if ($end -is [System.Management.Automation.Language.CommandAst]) { $end.GetCommandName() }
$info = if ($name) { Get-Command -Name $name -ErrorAction Ignore | Select-Object -First 1 }
$last = $elements[$elements.Count - 1]
}
$name = if ($last -is [System.Management.Automation.Language.CommandAst]) { $last.GetCommandName() }
$global:ElyShell = if ($name) {
$pattern = [System.Management.Automation.WildcardPattern]::Escape($name)
$info = Get-Command -Name $pattern -ErrorAction Ignore | Select-Object -First 1
while ($info -and $info.CommandType -eq 'Alias') { $info = $info.ResolvedCommand }
$global:ElyNative = $null -ne $info -and $info.CommandType -in 'Application', 'ExternalScript'
$null -eq $info -or $info.CommandType -eq 'Cmdlet'
} else {
$null -eq $last -or -not $last.Find({ $args[0] -is [System.Management.Automation.Language.CommandAst] }, $true)
}
[Console]::Write("$([char]27)]133;C$([char]7)")
}
$line
}
} else {
[Console]::Error.WriteLine('ELYTerminal: PowerShell marks no command without PSReadLine')
}
+1
View File
@@ -26,6 +26,7 @@ if [[ -o interactive ]]; then
precmd_functions=(${precmd_functions:#_ely_mark} _ely_mark)
if (( _ely_first )); then
_ely_first=0
[[ -o prompt_percent ]] || builtin print -ru2 'ELYTerminal: zsh marks no prompt while prompt_percent is off (setopt prompt_percent)'
builtin zmodload zsh/sched && builtin sched +0 _ely_mark $ret
fi
builtin return ret