Search code examples
pluginsterminalwindowzshoh-my-zsh

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!


Solution

  • 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.