r/commandline • • 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?

16 Upvotes

19 comments sorted by

11

u/Pille1842 Jan 22 '18

You should look into job control because your shell can already do this.

3

u/Spikey8D Jan 22 '18

Thanks for you comment! I know about job control and I use it all the time. What I'm thinking of here is more "cosmetic" I guess. It would be like backgrounding with ctrl-z, but stdout still appears and you have a full prompt at the bottom of the screen. Also, using ctrl-z is "forking" rather than "queuing", so if the second command relied on the first command, backgrounding with ctrl-z wouldn't work right?

4

u/Pille1842 Jan 22 '18

Well, then another possibility would be to just run two shells in tmux or screen.

(Sorry, didn't want to sound condescending with my comment about job control.)

1

u/Spikey8D Jan 22 '18

Right, I'm also a heavy tmux user. That is more like what I'm thinking. It would be something like making a small horizontal split at the bottom after each command, and then somehow rejoining the splits after the first command finishes.

3

u/Pille1842 Jan 22 '18

Since tmux is scriptable, you could certainly make a script that does just that: launch a tmux session, split the screen in two, run a command in the top window. Exiting might be a little bit harder, but perhaps someone has an idea.

3

u/attrigh Jan 22 '18

As an example of this sort of thing see fzf-tmux.

If you are using zsh you can rebind the enter key (accept-line) and then get it to run tmux split-pane. You might then want to run another zsh with a patched accept line that just copies the command (you can see my library zshnip for examples of this sort of thing).

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

u/zreeon Jan 22 '18

The Emacs shell already allows this. :-)

1

u/Spikey8D Jan 22 '18

How can I try this out? Any good links for how to do this in emacs shell?

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

u/AyrA_ch Jan 22 '18

iirc you can also end a command with & to achieve this.

1

u/TotesMessenger Jan 22 '18

I'm a bot, bleep, bloop. Someone has linked to this thread from another place on reddit:

 If you follow any of the above links, please respect the rules of reddit and don't vote in the other threads. (Info / ^Contact)

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.