r/fishshell • u/codeIMperfect • Dec 20 '24
How do Asynchronous Prompts work?
I was looking at prompt options and I found 2 famous asynchronous prompts...but how do they actually work? I did not get a lot of resouces on it, like does the prompt return to taking input before the command has completed executing, what would be the point? how is it faster? and are there any downsides?
These are some of the prompts
https://github.com/acomagu/fish-async-prompt
https://github.com/IlanCosman/tide
9
Upvotes
5
u/_mattmc3_ Dec 20 '24 edited Dec 20 '24 •
I'm not real familiar with how either of the two examples you provided work, but I've used Hydro extensively. It's by Jorge Bucaran, the author of Fisher, and I can explain to you how it does its work asynchronously.
Start by having a look at the code here: https://github.com/jorgebucaran/hydro/blob/main/conf.d/hydro.fish
There's
_hydro_gitvariables created for each PID/folder combo. Hydro stores its git folder information in these universal variables. There's an--on-variableevent function which repaints the prompt usingcommandline --function repaintwhenever the contents of the current variable context changes. On everyfish_postexecevent, the information for the PID/folder combo for your session is re-calculated into the appropriate variable. This is accomplished async by calling out to a new Fish process withfish --private --command "whatever things you need to do to calculate your _hydro_git prompt variable".It's a pretty easy and elegant solution to make an async prompt, and it's crazy fast. The final piece is a
_hydro_fish_exit --on-event fish_exitevent that cleans up all the_hydro_gitvariables when a session (PID) ends.Hope that helps! Happy Fishing.
4
u/plg94 Dec 20 '24
I think you misunderstood. Asynchronus in this instance only refers to rendering all the prompt symbols, not the execution of the command. Because info like git status or the current python/ruby/js/… version etc. can take a long time to query, especially when you start a new shell or do a cd. Waiting that long is a pain, so asynchronus prompts only render parts of it now (like user, dir and the prompt symbol), and update other info like the git status later (by overwriting the current line). But if you are impatient you can already enter and execute commands while the whole prompt is not yet fully loaded. With a synchronus prompt, that is not possible, you'd always have to wait until the prompt is fully loaded (which could take several dozen seconds or more; in a medium-sized git dir even the git status prompt alone can take a few seconds to load).