From 2c2511670342e3deac27bdc7750081878e9926f4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=9B=B7=E7=94=B5=E8=8A=BD=E8=A1=A3?= Date: Fri, 16 Oct 2026 23:28:36 -0400 Subject: [PATCH] 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 --- AGENTS.md | 2 +- .../shell-integration/bash/elyterminal.bash | 2 ++ assets/shell-integration/pwsh/elyterminal.ps1 | 20 ++++++++---- assets/shell-integration/zsh/.zshenv | 1 + src/term/shell/tests.rs | 32 +++++++++++++++++++ tasks/acceptance.md | 2 +- 6 files changed, 50 insertions(+), 9 deletions(-) diff --git a/AGENTS.md b/AGENTS.md index 21890eb..96779b0 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -96,4 +96,4 @@ ELYTerminal. A terminal on GPUI and Ely components. `PRD.md` says what it does; - 2026-10-16: herdr 0.9 keeps its panes' OSC 7 and writes only a title, notifications, OSC 52 and bells to the terminal it runs in, so no terminal hears a folder from inside it; herdr's server knows each pane's `cwd`, the folder its shell reported, else the shell's own. On macOS and Linux each session asks twice a second which process group leads its pty's foreground (`tcgetpgrp` on a copy of the master) and what executable that group's leader runs: an exec keeps the group and swaps the executable, so `exec herdr` from a shell is a new program, and one that did not read is tried again; on Linux the ` (deleted)` mark an update leaves on a running executable is set aside. When that executable is `herdr` and its arguments start herdr's terminal client (no command, `session attach NAME` or `client`), ELYTerminal reads the client's arguments and variables (`/proc` on Linux; `KERN_PROCARGS2` on macOS, which hides only the variables of Apple's own programs) and finds its socket as herdr 0.9 does: `--session NAME`, then `HERDR_SOCKET_PATH`, then `HERDR_SESSION`, under `$XDG_CONFIG_HOME/herdr`, else `~/.config/herdr`; a relative socket lies in the client's folder, as herdr reads it (`/proc/PID/cwd`; `proc_pidinfo` on macOS). It asks that socket for `session.snapshot` twice a second, a connection each, since herdr closes one after its reply, and reports the focused pane's `cwd` as the session's folder when it changes and again each time herdr takes the foreground; an answer that lands after the shell reported a folder is dropped, since the shell reports only once herdr has left. Focus belongs to herdr's server, so clients of one server follow one focus; a client of another machine's server (`--remote`, `--machine`) is logged, not followed. A failure is logged once while it lasts. On Windows, which has no foreground group, a Toolhelp32 snapshot lists the `herdr.exe` processes at or under the session's shell, none under another herdr, since what a herdr starts, its server too, is its own; when that list changes, `sysinfo` reads their arguments and variables, nearest first, names in upper case, as Windows matches them, and the first terminal client is followed, a server or a command beside it passed by; its folder is found as herdr 0.9 finds it there, `XDG_CONFIG_HOME`, then `APPDATA`, then `USERPROFILE\AppData\Roaming`, then `HOME\.config`; and its server answers on the pipe herdr names `\\.\pipe\` and the socket's path as written, relative or not. A busy pipe is waited for, as herdr's client waits, and the pipe's server may identify ELYTerminal but never act as it. The pipe has no timeout: a herdr that never answers holds that session's follow until it answers or exits. - 2026-10-16: the main window opens at the size it was left at, which the owner asked for on macOS, Windows and Linux: `window_width` and `window_height` in the settings hold the size gpui's `window_bounds` gives 0.5 s after the bounds settle, saved through `SettingsStore::update`, which writes only a change, so a drag saves once and a move saves nothing. A maximized, full-screen or hidden window keeps the size from before; its state and size are read once they settle, since X11 marks a window maximized after its new size, gpui's X11 window then gives a maximized window's own size, and it calls a maximized window unmaximized while minimized. A window that shows again, uncovered, restored or back on its screen, is read again, so a size it took just before it hid is kept; a size it takes in the half second before it closes, or before it hides until the app quits, is lost. The window is no smaller than 100 by 100, the least the settings keep, or a smaller size the platform allows could not be kept. The window opens centered at whole points, since macOS grows a window by a point on a side whose edge falls on a half point; its place is not kept, and a kept side past the primary display's visible area shrinks to it, logged. A size outside 100 to 16384 stops the start, as any value out of range does. - 2026-10-16: the sessions and right columns resize by a drag on their border toward the terminal, which the owner asked for on macOS, Windows and Linux. Ely's `ShellColumn::resizable`, added to Ely for this, puts Ely's `resize_handle` on the border toward the filling column, the sessions column's right and the right column's left: the handle draws that border, a filled hairline, since gpui paints a border no wider than half its box, and the line it drew at first showed half a pixel on macOS and none on X11; it occludes the header under it, so a press there drags the border, not the window, on Windows too, where an unoccluded drag region takes the press as the caption. A drag sets off from the wider of the width drawn and the one kept: a header's parts may hold a column wider, and a window too narrow for both widths squeezes them, where a drag from the squeezed width jumped back against the pointer; a drag that has not moved sideways changes nothing, and one that comes back where it set off gives the width it had. A set width in Ely became a flex basis, so a column holds its header's own parts: at a set width under them, Windows' caption buttons ran out of the window. The widths, 160 to 960 pixels, show at once and are saved in `sessions_width` and `panel_width` through `SettingsStore::update` 0.5 s after a drag rests, as the window's size is; a drag in the half second before the window closes is lost, and a save the store refuses puts the columns back. A window too narrow for both yields the terminal its header's width, as Ely's set widths give way. -- 2026-10-16: prompts and commands carry OSC 133 marks, which the owner asked for, read as Ghostty reads them: A or N starts a prompt, B or I ends it, C starts a command's output, and D ends the command with its status, D's first option, none when that is no number. The tap reads any program's marks before the parser, so it never sees the cursor: A's fresh line and L, which need it, are not done, and P adds nothing to A. Each session keeps the status its last command ended with. The integration marks what the shell leaves unmarked, and the user's prompt still sees the command's status. zsh: precmd reads `$?` and sends D after a command ran, preexec sends C, and a last precmd hook wraps PS1 in A and B under `prompt_percent`; it moves to the end of `precmd_functions` at each prompt, since a theme that sets PS1 in a hook of its own loads after it, and at the first prompt `sched +0` wraps it once more, since zsh runs due events after every precmd hook; that call returns the command's status, which the prompt shows, but it would reset `$pipestatus`, so it serves the first prompt alone. bash: PROMPT_COMMAND's first entry reads `$?`, which each entry resets, and its last wraps PS1. From bash 4.4 PS0 sends C and its subscript notes that a command ran, since bash prints no PS0 for an empty line, and the first entry sends D for it; both need `promptvars`, on by default. macOS's bash 3.2 has no PS0 and nothing else between reading a line and running it: its DEBUG trap runs for a key binding's command (`bind -x`) as for a typed one, with nothing to tell the two apart, runs for no line that is only a subshell, and under `extdebug` its status decides whether a command runs. So bash 3.2 sends no C, and Ghostty's automatic integration skips bash 3.2. Its D comes from PS1 once `\#`, the command number, has moved, which neither a key binding nor an empty line does; readline sends a prompt's escapes again at each redraw, so that D goes again with it, the same status. fish 4.3 marks its own prompts and commands, fish 4.0 all but B, older ones none: the script adds what the running fish lacks, around the prompt shown, and wraps it again when the user's changes. PowerShell: the prompt reads `$?` first and sets it back for the user's prompt through `Write-Error '' -ErrorAction Ignore`, which adds nothing to `$Error`. A failure ends with `$LASTEXITCODE` when the line's last pipeline ends in a program or a script, as the line's syntax tree names it and `Get-Command` resolves it, aliases through, as the line is handed over, and that program set `$LASTEXITCODE` during the line, since each run stores a new object, equal or not, which `[object]::ReferenceEquals` tells apart; any other failure ends with 1, a cmdlet's under `-ErrorAction Ignore` too. A hook on every command lookup would name the last command run, but it slowed a loop of commands by half. `$LASTEXITCODE` is read through `Get-Variable`, since strict mode refuses an unset variable and a prompt that fails falls back to `PS>`. C goes out when PSReadLine hands over a line that is not blank. herdr passes no OSC 133 on, so a session running herdr keeps the status from before it. +- 2026-10-16: prompts and commands carry OSC 133 marks, which the owner asked for, read as Ghostty reads them: A or N starts a prompt, B or I ends it, C starts a command's output, and D ends the command with its status, D's first option, none when that is no number. The tap reads any program's marks before the parser, so it never sees the cursor: A's fresh line and L, which need it, are not done, and P adds nothing to A. Each session keeps the status its last command ended with. The integration marks what the shell leaves unmarked, and the user's prompt still sees the command's status. zsh: precmd reads `$?` and sends D after a command ran, preexec sends C, and a last precmd hook wraps PS1 in A and B under `prompt_percent`, which the first prompt says while it is off; it moves to the end of `precmd_functions` at each prompt, since a theme that sets PS1 in a hook of its own loads after it, and at the first prompt `sched +0` wraps it once more, since zsh runs due events after every precmd hook; that call returns the command's status, which the prompt shows, but it would reset `$pipestatus`, so it serves the first prompt alone. bash: PROMPT_COMMAND's first entry reads `$?`, which each entry resets, and its last wraps PS1. From bash 4.4 PS0 sends C and its subscript notes that a command ran, since bash prints no PS0 for an empty line, and the first entry sends D for it; both need `promptvars`, on by default. macOS's bash 3.2 has no PS0 and nothing else between reading a line and running it: its DEBUG trap runs for a key binding's command (`bind -x`) as for a typed one, with nothing to tell the two apart, runs for no line that is only a subshell, and under `extdebug` its status decides whether a command runs. So bash 3.2 sends no C, and Ghostty's automatic integration skips bash 3.2. Its D comes from PS1 once `\#`, the command number, has moved, which neither a key binding nor an empty line does; readline sends a prompt's escapes again at each redraw, so that D goes again with it, the same status. Every mark of a command rides an expansion, which `promptvars` turns on, so while it is off at start the script says so rather than mark nothing without a word. fish 4.3 marks its own prompts and commands, fish 4.0 all but B, older ones none: the script adds what the running fish lacks, around the prompt shown, and wraps it again when the user's changes. PowerShell: the prompt reads `$?` first and sets it back for the user's prompt through `Write-Error '' -ErrorAction Ignore`, which adds nothing to `$Error`. A failure ends with `$LASTEXITCODE` when a program set it during the line, since each run stores a new object, equal or not, which `[object]::ReferenceEquals` tells apart, unless the line surely ends in PowerShell: its syntax tree, read as PSReadLine hands the line over, ends in a cmdlet, as `Get-Command` resolves the name, aliases through, in a name not found, or in no command; such a line ends with 1, a cmdlet's failure under `-ErrorAction Ignore` too. A line that ends in a block, a function, a script or a call through a variable takes the code of a program it ran: only a hook on every command lookup would name the last command run, and it slowed a loop of commands by half. `$LASTEXITCODE` is read through `Get-Variable`, since strict mode refuses an unset variable and a prompt that fails falls back to `PS>`. C goes out when PSReadLine hands over a line that is not blank; without PSReadLine the script says it marks no command. herdr passes no OSC 133 on, so a session running herdr keeps the status from before it. diff --git a/assets/shell-integration/bash/elyterminal.bash b/assets/shell-integration/bash/elyterminal.bash index c27d018..f62917e 100644 --- a/assets/shell-integration/bash/elyterminal.bash +++ b/assets/shell-integration/bash/elyterminal.bash @@ -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 diff --git a/assets/shell-integration/pwsh/elyterminal.ps1 b/assets/shell-integration/pwsh/elyterminal.ps1 index 119dc41..608fb48 100644 --- a/assets/shell-integration/pwsh/elyterminal.ps1 +++ b/assets/shell-integration/pwsh/elyterminal.ps1 @@ -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') } diff --git a/assets/shell-integration/zsh/.zshenv b/assets/shell-integration/zsh/.zshenv index 80e77ee..be2379c 100644 --- a/assets/shell-integration/zsh/.zshenv +++ b/assets/shell-integration/zsh/.zshenv @@ -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 diff --git a/src/term/shell/tests.rs b/src/term/shell/tests.rs index 85818c5..edc1a07 100644 --- a/src/term/shell/tests.rs +++ b/src/term/shell/tests.rs @@ -280,6 +280,19 @@ fn zsh_reports_its_folder_byte_for_byte_and_its_titles() { run.finish(); } +#[test] +fn zsh_says_why_it_marks_no_prompt_without_prompt_percent() { + let mut run = Run::start(&installed("zsh"), &[(".zshrc", "unsetopt prompt_percent")]); + run.until("the warning", |run| { + run.shows("ELYTerminal: zsh marks no prompt while prompt_percent is off") + }); + run.send("false"); + let ended = [Mark::Output, Mark::Done(Some(1))]; + run.until("the command's marks", |run| run.marks.ends_with(&ended)); + assert!(!run.marks.contains(&Mark::Prompt), "{:?}", run.marks); + run.finish(); +} + #[test] fn bash_reports_its_folder_byte_for_byte_and_keeps_the_status() { let status = r#"PROMPT_COMMAND='printf "USER_STATUS=%s\n" "$?"'; shopt -s extdebug"#; @@ -325,6 +338,21 @@ bind -x '"\C-t": :'"#; run.finish(); } +#[test] +fn bash_says_why_it_marks_no_command_without_promptvars() { + let files = [ + (".bashrc", "shopt -u promptvars"), + (".bash_profile", "shopt -u promptvars"), + ]; + let mut run = Run::start(&installed("bash"), &files); + run.until("the warning", |run| { + run.shows("ELYTerminal: bash marks no command while promptvars is off") + }); + run.walk("declare -F _ely_prompt >/dev/null && echo ELY-LOAD''ED"); + run.ends("false", &[Mark::Prompt, Mark::Input]); + run.finish(); +} + #[test] fn fish_loads_its_script_and_restores_its_data_dirs() { let mut run = Run::start(&installed("fish"), &[]); @@ -368,6 +396,10 @@ fn powershell_reports_its_folder_byte_for_byte_and_keeps_the_status() { &ended(1), ); run.ends("Get-Item ./missing; sh -c 'exit 7'", &ended(7)); + // A call through a variable runs a program the line does not name. + run.send("$exe = 'sh'"); + run.ends("& $exe -c 'exit 7'", &ended(7)); + run.ends("& \"$exe\" -c 'exit 9'", &ended(9)); let native = "$PSNativeCommandUseErrorActionPreference = $true; sh -c 'exit 7'"; run.ends(native, &ended(7)); run.finish(); diff --git a/tasks/acceptance.md b/tasks/acceptance.md index 75cdf40..8ae355e 100644 --- a/tasks/acceptance.md +++ b/tasks/acceptance.md @@ -51,7 +51,7 @@ What no check reached: no real KWin drew the blur; of Wayland desktops, only Hyp |---|---|---|---| | PTY through alacritty's event loop; OSC 7 and OSC 133 scanned on the reader | T04, T34 | `term::pty`, `term::tap` tests; round 3 failed at the cap, its fix stands on a red-green test | Pass | | Shell integration for zsh, bash, fish, PowerShell; a shell without it reports nothing and the panels say so | T05 | `term::shell` tests on macOS and Linux | Pass | -| Prompt marks, OSC 133, from the shell integration and any program; each session keeps its last command's status | T34 | `term::marks` tests: each letter and option as Ghostty reads them; `term::tap` and `term::pty` tests: marks split across reads reach the session in order; `term::shell` tests: zsh, bash, fish and PowerShell send C, D;1, A and B around a failing command, and the user's prompt still shows the failure, a zsh theme's that sets PS1 in its own hook too, whose first prompt is marked before any command, and `$pipestatus` stays; bash's lone subshell ends with D;7, `$_` stays, a command runs under `extdebug` after a failure, a key binding marks nothing, the user's DEBUG trap runs, and bash 3.2 sends no C; PowerShell reads 1 for a cmdlet's failure under `-ErrorAction Ignore` after a success, a native code twice, a native failure's code under `$PSNativeCommandUseErrorActionPreference`, and on a line that mixes a program and a cmdlet the code of its last pipeline; on macOS fish 4.9 marks itself and bash 3.2 marks C through its DEBUG trap, in the Linux container fish 4.0 gets B and bash 5.2 marks C through PS0, CI's Ubuntu runs fish 3.7, which the script marks whole; by hand in a pty, PowerShell under strict mode, and a cmdlet's failure after a native one's reads 1 | Pass | +| Prompt marks, OSC 133, from the shell integration and any program; each session keeps its last command's status | T34 | `term::marks` tests: each letter and option as Ghostty reads them; `term::tap` and `term::pty` tests: marks split across reads reach the session in order; `term::shell` tests: zsh, bash, fish and PowerShell send C, D;1, A and B around a failing command, and the user's prompt still shows the failure, a zsh theme's that sets PS1 in its own hook too, whose first prompt is marked before any command, and `$pipestatus` stays; bash's lone subshell ends with D;7, `$_` stays, a command runs under `extdebug` after a failure, a key binding marks nothing, the user's DEBUG trap runs, bash 3.2 sends no C, and bash says at start why it marks nothing while `promptvars` is off; PowerShell reads 1 for a cmdlet's failure under `-ErrorAction Ignore` after a success, a native code twice, a native failure's code under `$PSNativeCommandUseErrorActionPreference`, on a line that mixes a program and a cmdlet the cmdlet's 1, and through a call by variable the program's code; on macOS fish 4.9 marks itself and bash 3.2 marks no C, in the Linux container fish 4.0 gets B and bash 5.2 marks C through PS0, CI's Ubuntu runs fish 3.7, which the script marks whole; by hand in a pty, PowerShell under strict mode, and a cmdlet's failure after a native one's reads 1 | Pass | | Inside herdr, the folder of herdr's focused pane, read from its socket, on macOS, Linux and Windows | T29, T32 | `term::herdr` tests: the socket found as herdr 0.9 finds it, only the terminal client followed and a remote one refused, the snapshot read, a socket round trip, a process's path, arguments and variables; `the_foreground_is_the_program_s_own_group`; on X11 with herdr 0.9.5 run in the session: its start showed the focused pane's folder, a cd in the pane moved the panel, the focus moved to a pane in `/etc` and back moved it each time, and the client's end gave the panel back to bash's report, then `exec herdr` from that bash, with `HERDR_SOCKET_PATH` relative to its folder, was followed through the socket there; on macOS under `shot`, herdr 0.9.5 as the program and again by `exec` from `sh` with a relative socket, its pane's cd moved the panel; on Windows, `term::herdr` tests on the CI runner: the folder under `APPDATA` and `USERPROFILE`, the `herdr.exe` nearest under the shell, this test's name, arguments and variables read through the live process list and `sysinfo`, a round trip over a named pipe; no real herdr has run on Windows yet | Partial | | Colors, styles, five underlines, wide characters, combining marks | T06 | `term::frame`, `term::ink`, `term::paint` tests | Pass | | Box drawing, blocks and powerline shapes as geometry | T07 | `term::boxes` tests | Pass |