Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fixed line on top of zsh in terminal

I'm trying to find if it's possible (and how to do it, if it is) to put a fixed line on top of the terminal using zsh. I would like to put into this line some system's information (for example cpu usage, disk free space, ram's usage, etc) preserving the classic "scrolling behaviour".

I would use it in windowed terminals in my desktop.

Something like this

Any idea/suggestion would be very apreciated, thank you!

like image 234
SDp Avatar asked Oct 16 '14 23:10

SDp


2 Answers

Instead of trying to do this in zsh I would suggest using tmux for that. If you set the following in your ~/tmux.conf it should have the desired effect:

set -g status-position top
set -g status-right '#(system-information-command)'
set -g status-intervall 10

It will put a status bar on top, print the first line of system-information-command's output in it and update every 10 seconds.

Reasoning:

While it is quite easy to print something in the first line of a terminal from within zsh and - if wanted - updating it constantly is not that much harder, preserving scrolling behaviour will be very hard, if not impossible with just zsh.

The reason for that is that the scroll back buffer is maintained by the terminal and not by zsh. Any output to the terminal is done at the current position of the cursor. If anything is already printed at or after the cursor position, it will be overwritten. There is no way to insert anything.

Unless the cursor is explicitly set to a position it will be at the end of the last output, most of the time that is at the very bottom of the terminal. As said putting the cursor in the first line, writing something and resetting at to its previous position is quite easy. But anything that was in the first line before printing the status line will be overwritten. zsh can only write to the terminal, it cannot read from the previous output. So whatever the status bar overwrites it cannot be restored.

If you scroll up in the terminal the previously printed status line will move down together with the rest of the terminal's content. If the output of a command is higher than the terminal, at least one line will be overwritten.

tmux is a terminal multiplexer. It essentially runs a terminal (or more of them) inside your terminal. zsh and any program started in it would then run in the 'inner' terminal. Therewhile status line would be printed to the 'outer' terminal and not interfere with the shell's output.

like image 126
Adaephon Avatar answered Nov 15 '22 10:11

Adaephon


You can abuse your prompt to display a fixed line at the top of the screen. It makes use of several ANSI escape codes for manipulating the position of the cursor.

  • \e[s - save the cursor position
  • \e[u - restore the cursor position
  • \e[H - move the cursor to the top-left corner of the screen

Be sure to enclose any such escape sequences, as well as the text for the top line itself, in %{ ... %} so that zsh doesn't count them towards the size of your prompt.

PS1=$'%{\e[s\e[HTOPLINE\e[u%}%# '

Note that unlike bash, zsh does not treat \e specially in a prompt, so use $'...'-style quoting instead. If you can't (or don't want to) use them, you can try typing a literal escape character with Control-V Esc (which will show up as ^[):

PS1='^[[s^[[HTOPLINE^[[u%# '
like image 39
chepner Avatar answered Nov 15 '22 10:11

chepner