Linux – Keep terminal input line at the top of the terminal

inputlinuxoutputterminal

It's really hard to explain what I am looking for exactly so I have made some example output of how I would like my terminal to behave. (each number at the start of a line represents a return on that line and > represents the input line.

>   me@computer:~$ 
5.  me@computer:~$ cat somefile                 < command
    start of file                               < output
                                                < output
    this                                        < output
    is                                          < output
    the                                         < output
    content                                     < output
    of                                          < output
    the                                         < output
    file                                        < output
                                                < output
    end of file                                 < output
4.  me@computer:~$ 
3.  me@computer:~$ 
2.  me@computer:~$ 
1.  me@computer:~$ echo this is the first line  < command
    this is the first line                      < output

in this example the input line where you type the commands that you want to execute will always be on the top. And it will push each command and it's output downwards as "events" so to speak. So if you cat a file, it won'd output everything in the reverse order, so the appearance of the output of each command is unchanged, just it's position.

Best Answer

Perhaps someone has done this (perhaps not). It would have to be done by a shell which knows how to collect the output of commands and update the screen. By itself, a regular terminal will not do this.

Supposing there were a program doing this, on each command

  • it would accept your input command (possibly multiple lines). To keep it simple, start by limiting inputs to a single line.
  • having gotten the text, the shell would run the command, collecting one line at a time from the command's standard output and error.
  • as it acquires a new line of output, it has to insert that line on the screen, pushing down the existing text. The insertion point moves down as more output is read. (Terminals provide low-level operations to help, but that's a long way from making it work).
  • if there is more output than will fit on the screen, the user likely expects the output to start scrolling up when the insertion point reaches the bottom of the screen.
  • when there is no more output (the command stops) the shell moves the cursor back to the top of the screen.

All of that is doable in a simple program. What is hard is if the command wants to take over the screen for itself. Programs which do this write — you guess it — to the standard output and the standard error. And they do not write plain text: they use escape sequences for moving around the screen.

If you limit this to the well-behaved applications which send a terminal initialization sequence, your shell could (in principle) detect this and give up for the time being, allowing the command to write to the screen. But well-behaved applications are not the majority, and you will have lots of interesting special cases to deal with.

Related Question