r/commandline • u/Spikey8D • Jan 22 '18
Async shell idea, is it possible?
I have been looking at Zsh prompts like Pure, Geometry and Spaceship, which are able to utilise asynchronous execution.
But how about showing the prompt and allowing the user to type while the previous command is still executing? (I suppose this command would then be queued up and run directly after if the exit status is 0).
You can get similar behaviour already by typing the next command while the previous one is still running, but the input gets mixed up with any stdout of the command. The difference here is there would be a full prompt.
Is it possible in Zsh? Or would it require a whole new shell, or even a new terminal emulator?
3
u/ciawal Jan 22 '18
What's the difference vs just running the command in the background, e.g. with ls &?
1
u/Spikey8D Jan 23 '18
Actually this is pretty close. If only the stdout wasn't jumbled and commands were queued.
3
u/AdamK117 Jan 23 '18
As others have pointed out, there's standard CLI job control for for forking multiple processes from a single shell.
If you're asking if it's possible to split outputs from separate background tasks such that they join up nicely in the UI, there is nothing at a technical level stopping a UI designer from doing this. Under the hood, terminal emulators are just fork(2) and exec(2)ing processes. Those subprocesses usually inherit their stdout/stderr from the terminal emulator, which is why outputs from multiple applications are mixed together in the UI.
Alternative terminal emulators could redirect a subprocess's output into separate streams and render those streams according to the design you're specifying; however, as you identified, there are design decisions that need to be made, for example:
Are calls queued up or concurrent?
Are outputs from concurrent processes retrospectively rearranged in the UI based on exit times?
How is standard-input handled for these "async" processes? The processes synchronously read the standard input, which may be provided by the user (rather than via a pipe). Is your next command going to be the standard input for an existing async process or a command to boot a new process?
I think this is an interesting idea but may only work for a subset of CLI applications. It may also increase the complexity of terminal emulators. For the sake of being able to write your next command while the previous one (or many) execute, it may be more straightforward to use the existing job control system (with redirects), scripts, or boot another terminal.
1
u/Spikey8D Jan 23 '18
Thanks for your considered response, these are good points. In broader terms, it surprises me that terminals have remained the same for so long. I know of the maths program Maple, which has a REPL interface where output appears below each command/equation. It allows you to edit previous commands in-place, and run them again with the output still appearing below the command. I'm surprised features like this aren't available in some terminal emulators. I guess they are held back by design decisions like the points you have raised.
5
u/sime Jan 22 '18
As a point of interest, the Commodore Amiga from decades ago had a CLI/shell which did allow you to type ahead and enter future commands while the current command was still running.
Fitting this kind of feature into the current unix style terminal+shell architecture that we have now is non-trivial. You would need a mix of terminal and shell support. But it is something I might one day try to implement and experiment with in my terminal project Extraterm.
1
3
u/johnydoe666 Jan 22 '18
I came across https://github.com/Nukesor/pueue a few weeks ago in /r/coolgithubprojects. Maybe that can solve some of your problems with wanting to queue commands that rely on each other?
3
u/anderslanglands Jan 22 '18
Ctrl-Z pauses the current task and returns you to an interactive prompt. Then typing ‘bg’ will run the paused task in the background. This is standard across all(?) shells.
3
1
u/TotesMessenger Jan 22 '18
1
u/knobbysideup Jan 22 '18
On a similar note, look into gnu parallel. I use this all the time when managing multiple systems via ssh.
1
u/soullessroentgenium Jan 22 '18
This is a good idea. You might have some trouble with things that want to use the terminal not in an append only mode.
11
u/Pille1842 Jan 22 '18
You should look into job control because your shell can already do this.