macos: add pid and tty properties to AppleScript terminal and App Intents TerminalEntity#11922
macos: add pid and tty properties to AppleScript terminal and App Intents TerminalEntity#11922TweedBeetle wants to merge 1 commit intoghostty-org:mainfrom
Conversation
jcollie
left a comment
There was a problem hiding this comment.
Only commenting on the Zig code. The Swift code will need review by someone else.
src/apprt/embedded.zig
Outdated
| } | ||
| }; | ||
|
|
||
| // ghostty_string_s |
There was a problem hiding this comment.
This is partially duplicating source from main_c.zig. The String definition should be moved to a shared location (maybe src/c/string.zig?) and referenced from there.
Lines 62 to 102 in 0d1f77b
There was a problem hiding this comment.
We have a "standard" libghostty string type in src/lib/string.zig (we use for libghostty-vt). We should probably restandardize on that. Short of that though I agree with jcollie here.
There was a problem hiding this comment.
Addressed in 82e66aa17. I extracted the C API String into src/c/string.zig for this PR and updated main_c.zig, embedded.zig, and config/CApi.zig to share it. Agreed that any broader libghostty string restandardization would be follow-up work.
src/apprt/embedded.zig
Outdated
| /// freed by the caller via ghostty_string_free. | ||
| export fn ghostty_surface_tty_name(surface: *Surface) String { | ||
| const tty_name = surface.core_surface.getProcessInfo(.tty_name) orelse return .empty; | ||
| const copy = global_state.alloc.dupeZ(u8, tty_name) catch |err| { |
There was a problem hiding this comment.
The allocator should be available from surface.app.core_app.alloc thus making the import of global.zig above unnecessary.
There was a problem hiding this comment.
Addressed in 82e66aa17. ghostty_surface_tty_name now allocates via surface.app.core_app.alloc, and the extra global_state import in embedded.zig is gone.
src/apprt/embedded.zig
Outdated
| } | ||
| }; | ||
|
|
||
| // ghostty_string_s |
There was a problem hiding this comment.
We have a "standard" libghostty string type in src/lib/string.zig (we use for libghostty-vt). We should probably restandardize on that. Short of that though I agree with jcollie here.
0fde537 to
82e66aa
Compare
src/c/string.zig
Outdated
There was a problem hiding this comment.
Sorry actually we shouldn't create a whole new c directory for this. This should live somewhere else but I'm not sure where yet. Let me think about it.
Ghostty's AppleScript API exposes working directory but not TTY. We cross-reference window CWDs against child shell process CWDs to resolve TTYs indirectly. Windows with duplicate CWDs get tiling but no status colors. Will switch to direct tty property once Ghostty ships it (ghostty-org/ghostty#11922). Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
* feat: add Ghostty to TERMINAL_APPS set Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> * feat: add Ghostty TTY resolution via CWD matching Ghostty's AppleScript API exposes working directory but not TTY. We cross-reference window CWDs against child shell process CWDs to resolve TTYs indirectly. Windows with duplicate CWDs get tiling but no status colors. Will switch to direct tty property once Ghostty ships it (ghostty-org/ghostty#11922). Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> * fix: skip windows with unresolved TTY in tty map building Ghostty windows can have tty=None when CWD matching fails (e.g. duplicate CWDs). Guard against None TTYs flowing into _resolve_tty_cwd by checking info.get("tty") is truthy before storing in tty_map. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> * fix: use case-insensitive pgrep for Ghostty process name On macOS, Ghostty's process name is "Ghostty" (capital G) when launched from the .app bundle. Adding -i flag to pgrep ensures we find it regardless of case. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> * docs: add Ghostty to supported terminals in README Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> * fix: fix Ghostty AppleScript and process tree walking Two bugs found during live testing: 1. Ghostty windows don't have 'bounds' property — use System Events for position/size and Ghostty scripting API for working directory 2. ps -g doesn't find Ghostty child shells (different process groups) — use recursive pgrep -P to walk ghostty → login → shell tree Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> --------- Co-authored-by: coryszatkowski <213997628+coryszatkowski@users.noreply.github.com> Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
|
Happy to revert |
|
Hey @mitchellh, just wanted to check in on this. Happy to go whichever direction on the Zig file placement. As far as I understand, there are three options:
Let me know if you have thoughts. Everything else is approved 🫡 |
82e66aa to
d5e7965
Compare
|
Went ahead with option 1 to unblock: reverted |
Expose the foreground process PID and TTY device path as read-only properties on the AppleScript terminal class and App Intents TerminalEntity. This enables reliable process-to-terminal mapping for automation tools when multiple terminals share the same CWD. Closes ghostty-org#11592 Closes ghostty-org#10756 Session: 019d341c-a165-7843-a2f7-2f426114cf17
d5e7965 to
58573a2
Compare

Summary
Add
pidandttyas read-only properties to:terminalclassTerminalEntityThis enables reliable process-to-terminal mapping for automation tools when multiple terminals share the same working directory.
Changes
macos/Ghostty.sdef: addpidandttyto theterminalclassmacos/Sources/Features/AppleScript/ScriptTerminal.swift: add AppleScript gettersmacos/Sources/Ghostty/Ghostty.Surface.swift: add Swift accessors for foreground PID and PTY namesrc/apprt/embedded.zig: add C bridge exports for foreground PID and PTY nameinclude/ghostty.h: declare the new C API entry pointsmacos/Sources/Features/App Intents/Entities/TerminalEntity.swift: surface the same values in App IntentsTesting
zig build -Demit-macos-app=false/opt/homebrew/bin/nu macos/build.nu --configuration Debug --action buildswiftlint lint 'macos/Sources/Features/AppleScript/ScriptTerminal.swift'swiftlint lint 'macos/Sources/Features/App Intents/Entities/TerminalEntity.swift'swiftlint lint 'macos/Sources/Ghostty/Ghostty.Surface.swift'get {id, pid, tty, working directory} of terminal 1get pid of every terminalget tty of every terminalpid/ttyagainstps -p <pid> -o pid=,tty=,comm=andlsof -a -p <pid> -d cwd -FnAI Disclosure
This PR was developed with AI assistance (Codex). The contributor understands the changes and can explain the implementation.
Closes #11592
Closes #10756