Scripting API for Ghostty #2353
Replies: 25 comments 21 replies
-
Just dropping down some thoughts. I'm leaning towards a single-line text protocol in the format of memcached/redis/etc. This has various benefits:
|
Beta Was this translation helpful? Give feedback.
-
I know GitHub links it, but I want to explicitly note @LordMZTE's proposal for dynamically linked plugins in #1358. See my comment there, I don't think these are mutually exclusive, but I did want to centralize all potential plugin discussion in one place for now until we spin out actual working on some of these. And I'm not ready to commit to working on these yet. |
Beta Was this translation helpful? Give feedback.
-
I'd like to propose another way to interact which are escape sequences, we could make another standard which specifies how it is used. The way it would work is simple, you just echo stuff in your terminal and Ghostty (and other standard implementors) parse it and apply the settings. I don't know how the Kitty keyboard protocol works, but it would be similar, just print your desired setting, and it gets applied: echo $"($escape_start)background($escape_sep)#800000($escape_end)" This would be really useful over SSH, and could prevent you from accidentally deleting the primary database instead of the backup one because you selected the wrong terminal emulator window. This isn't a replacement for the socket way, btw. Another idea is that the background-color and other common settings could be documented in the standard, so all terminal emulators that implement the standard work with a single shell command when added to the shellrc. For emulators that don't support it, the escape sequences could be picked carefully so it doesn't change output and just shows as a normal echo statement. Like |
Beta Was this translation helpful? Give feedback.
-
Just building on the conversation about making a text protocol. Would it be cursed to do something like A few problems/questions that this kind of syntax raises:
I chose something overly simple because like mentioned above, it can be easily debugged and building an API client with it is as simple as building a parser in the language and moving forward with normal TCP connections. I think at the very least following the naming scheme of keybinds is the best choice because then you don't need to reinvent the wheel and there's consistency between these two. The one downside is we already run into consistency issues because in a keybind you would do |
Beta Was this translation helpful? Give feedback.
-
Why invent a new protocol? Why not use HTTP & JSON over Unix sockets? Every language worth mentioning has HTTP & JSON support already (although it may take a bit of work to talk to a Unix socket) and it's basically infinitely extensible. That also means that we don't need to spend weeks bikeshedding a new protocol. Even cURL works over Unix sockets.
|
Beta Was this translation helpful? Give feedback.
-
I understand the use of JSON here for a uniform and simple standard, but HTTP doesn't seem logical to me, mostly because we could just use a streaming JSON parser (Zig can be made to do this IIRC) to directly transfer JSON over the socket. It would also seem to me that most HTTP clients aren't exactly easy to convince to connect to a Unix socket, as it's simply not something HTTP was ever intended to do, as further made evident by the lack of a way to express this in a URL. Your example could translate to something as simple as:
(Note: the JSON here is obviously just a placeholder and isn't meant to suggest a possible way a request could look.) |
Beta Was this translation helpful? Give feedback.
-
JSON makes sense, honestly it wasn't my first choice because I was looking at redis, which is just raw commands. Need to confirm, but is there an overhead to using JSON parsing? Do we need the power of JSON either? I think following the keybind syntax is a good place to start because it's simple and shares a mental model in 2 different places. Introducing JSON basically opens up the can of worms for super complicated API bodies which I personally think is just a sign of bad design. |
Beta Was this translation helpful? Give feedback.
-
Everyone hates JSON, because everyone has had to deal with JSON. It's the LCD for passing data between systems. I think that the overhead of JSON parsing is irrelevant unless you're expecting hundreds of API calls per second. Yeah JSON does open you up to the possibility of some truly horrific data structures, but the same could be said for trying to cram everything into a bespoke REDIS-like API. The point of using HTTP & JSON is that the best code is the code that you don't have to write. Each language's HTTP & JSON code is thoroughly vetted by a large community whereas a bespoke protocol will probably only ever be looked at by a very small number of people. Plus cURL & wget mean that you can access the API from a simple shell script as well. |
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
The use of HTTP makes things simpler as it's a very common design pattern for such things. I suggested using Unix sockets as a security measure but binding to a random port on localhost is an option as well. On Linux it would theoretically be possible to use CGroups and network namespaces to futher limit access to the localhost port but that might be more complication than necessary. |
Beta Was this translation helpful? Give feedback.
-
Why not use D-Bus? |
Beta Was this translation helpful? Give feedback.
-
This needs to be cross platform. Additionally, I’ve found dbus pretty complicated to use compared to a simple text proto. |
Beta Was this translation helpful? Give feedback.
-
Technically, we're already using D-Bus since it's required by GTK. I wouldn't recommend it as the primary API endpoint. At some point we'll need to make more use of D-Bus to achieve deeper integration with Gnome (such as being able to link into the Gnome search interface) so at that point it may make sense to expose the Ghostty API over D-Bus in some way. |
Beta Was this translation helpful? Give feedback.
-
Is the plan also to expose this functionality via Ghostty's cli? I find myself wanting to port stuff like this wezterm/helix integration to Ghostty - i.e. making it easy to use the API from scripts would be great. |
Beta Was this translation helpful? Give feedback.
-
I would start with simple text API over Unix socket. Question is whether it should be stream socket or datagram socket. It could also be exposed within Ghostty session itself via environment variable like Usage of Unix sockets would also allow us to share extra data via |
Beta Was this translation helpful? Give feedback.
-
If it's desired, you could resume #1701, I no longer have the time to finish it unfortunately because of work but I'd be more than happy to help pass it on to someone else. |
Beta Was this translation helpful? Give feedback.
-
Thanks @tale ❤️ |
Beta Was this translation helpful? Give feedback.
-
In the spirit of Ghostty aiming to feel native on every platform, it seems to me that a platform-native API (per-platform) approach isn't unreasonable, that way that part of the code can also be platform-specific (rather than in libghostty). This would mean dbus on Linux, AppleScript on MacOS, and so on; none of them conflicting. I'm going to give adding AppleScript support a shot just to see what it's like (I've been waiting for an opportunity to try out Swift anyway). Worst case there's no community interest and it never gets merged. How do you feel about that approach @mitchellh? To wit, I already have some core dictionary items defined (e.g. you can tell Ghostty to open a binary/script; it's the default behavior but it's now documented in the Script Editor dictionary), so I'll see how far I can get from there. |
Beta Was this translation helpful? Give feedback.
-
It would definitely be a bonus for my setup to be able to launch Ghostty via a As it is, I have a It's not the end of the world, and this is definitely the most basic version of the feature being discussed here, but we have a lot of repos in our workplace setup and sometimes I get bored typing Footnotes
|
Beta Was this translation helpful? Give feedback.
-
I know this approach wouldn’t really be an API per sé, but for my own purposes in scripting ghostty, what I'd most prefer is that various actions like creating new windows, tabs, splits, sending text to a pane, etc. were built into the CLI so that you could hit them as commands from shell scripts in arbitrary shells / languages. I have some elaborate AppleScripts for iTerm that set up my working environment with a lot of tabs for different remote hosts, and each tab has a horizontal split and a different command run in each of the panes. Setting this up manually every time would take 5 minutes. Having the script do it takes 30 seconds and i just have to watch. Using AppleScript is clunky but manageable - I only had to write those scripts once. But I’d sure rather write them in fish or bash or python if I could. Not having any kind of scriptability is the only thing that’s currently keeping me from switching from iTerm to Ghostty and I am looking forward to being able to switch because iTerm is feeling a bit bloated and ghostty feels nice and light and snappy. |
Beta Was this translation helpful? Give feedback.
-
Agreed with @pjv here. A command line interface seems like the most straightforward approach. Additionally, it seems like the CLI needs to be updated, since it documents that actions like |
Beta Was this translation helpful? Give feedback.
-
Just adding a use-case here: on tmux and then on WezTerm I have been using a similar (cli) interface to get the content of the active "pane", read the last line of it, and display it in my status bar (waybar). This has been super useful when running long tasks, as I can move to other work and hide the terminal completely, and yet see what's the latest in regard to the command I ran. The WezTerm way was |
Beta Was this translation helpful? Give feedback.
-
Out of curiosity, don't the needs of programmatically controlling windows, tabs, and splits align with #1935? While digging into the actual spec, I observed that Control Mode also has notifications, meaning the integration could be bidirectional if one wished (e.g. Ghostty could inform listeners of external changes) Pro
Con
|
Beta Was this translation helpful? Give feedback.
-
This discussion has been marked as an answer to #4870 which asks for a programmatic capability to access the scrollback buffer. Particularly thinking of tools like kitty-scrollback. Because the title of the discussion here only references windows/tabs/splits but the substance is actually about remote communication for ghostty more generally, I didn't spot it previously. Perhaps this discussion could be renamed? |
Beta Was this translation helpful? Give feedback.
-
Hi, this thread was marked as an answer to #3795 . |
Beta Was this translation helpful? Give feedback.
-
Some inspiration:
Beta Was this translation helpful? Give feedback.
All reactions