Reputation: 11
I'm working on a syntax highlighter for the zsh ZLI, which I'm writing in rust. Essentially it consumes text from zsh, and returns text which represents the highlighted syntax. Perhaps this would be better written in zsh directly, but I'm really just doing this for fun, and to help learn Rust and IPC better.
I have a function in zsh which essentially receives the text to send as an argument, and by the the end of the function, must receive the formatted text from the external program. I am unsure of the best way to communicate this information with the rust program. My initial go uses named pipes - it writes the text to some input pipe, and consumes the formatted text from the output pipe. On the other side, the program consumes the input and writes the output to the pipe. However, I'd like this to run for each instance of zsh that the user has open.
My question has two parts.
How should I start the rust program and manage multiple zsh instances? My first thought is to run a single instance of the program as a daemon (to avoid the performance cost of having many processes). If a zsh instance is created without the rust program already running, it would launch the daemon. Each subsequent time a new instance of zsh is created, it would somehow register with the rust program, which would spawn a new thread for each instance of the shell (probably associated with tty, so nested shells don't have multiple threads). Perhaps it would have a separate thread which polls to see which zsh instances are no longer alive, so it can kill the corresponding threads.
Are named pipes the right choice for IPC? My thought was I could name the pipe "<something>_<input/output>_PID" so that there is a unique one for each zsh instance. Is this a reasonable approach, or is there something better suited for this task? Speed is of the essence because we'd like the highlighting to appear nearly instantly when the user types.
I have implemented the above approach, which works fine for a single zsh instance. However, I'd like to have multiple threads so I can retain the context of each ZLI buffer to improve performance of the highlighting aspect through incremental updating.
Upvotes: 1
Views: 52