Fixed line over zsh in terminal
I am trying to find if possible (and how to do it if it is) to put a fixed string on top of the terminal using zsh. I would like to include some system information in this line (eg cpu usage, free disk space, ram usage, etc.) while keeping the classic "scrolling behavior".
I would use it in window terminals on my desktop.
Any idea / suggestion would be much appreciated, thanks!
source to share
Instead of trying to do it in zsh
, I would suggest using tmux
. If you set it to the ~/tmux.conf
following, 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 place the status bar on top, print the first line in it, system-information-command
and update every 10 seconds.
Rationale:
While it's pretty easy to type something on the first line of the terminal from zsh
and - if necessary - updating it constantly isn't much more difficult, maintaining the scrolling behavior would be very hard, if not impossible, with only zsh
.
The reason is that the scrollback buffer is supported by the terminal and not zsh
. Any output to the terminal is done at the current cursor position. If anything is already typed at or after the cursor position, it will be overwritten. Unable to insert anything.
If the cursor is not explicitly positioned, it will be at the end of the last output, most of the time at the very end of the terminal. As said to put the cursor on the first line, writing something and reloading to its previous position is pretty easy. But whatever was on the first line before printing the status bar will be overwritten. zsh
can only write to terminal, it cannot read from previous output. So no matter what the status bar is overwritten, it cannot be restored.
If you scroll up in the terminal, the previously printed status bar will move along with the rest of the terminal content. If the output of the command is higher than terminal, at least one line will be overwritten.
tmux
is a terminal multiplexer. It essentially launches a terminal (or more) inside your terminal. zsh
, and any program launched in it will run in the internal terminal. In the meantime, the status line will be printed on the "external" terminal and will not interfere with the shell's exit.
source to share
You can use your prompt to display a fixed line at the top of the screen. It uses multiple ANSI escape codes to control cursor position.
-
\e[s
- save cursor position -
\e[u
- restore cursor position -
\e[H
- move the cursor to the upper left corner of the screen.
Be sure to include any such escape sequences, as well as the text for the top-most line in %{ ... %}
, so that it zsh
doesn't account for the size of your invitation.
PS1=$'%{\e[s\e[HTOPLINE\e[u%}%# '
Note that unlike bash
, zsh
does not refer \e
specifically to the prompt, so use $'...'
-style quoting. If you can't (or don't want to) use them, you can try typing an alphabetic escape character with Control- V Esc(which will display as ^[
):
PS1='^[[s^[[HTOPLINE^[[u%# '
source to share