Last Updated: November 17, 2017
·
7.406K
· creaktive

Animated CPU chart in the Zsh prompt

Despite the fact that the rainbarf was originally intended to be used under tmux/screen, it actually works really well under the Zsh:

rainbarf + zsh

Note that the prompt itself (PS1) becomes animated, even when the shell is idle! To enable this hack, include the following lines in your ~/.zshrc (gist here):

# abort if already under tmux
[[ -n $TMUX_PANE ]] && return

# zsh-specific includes
zmodload -i zsh/datetime
zmodload -i zsh/stat

# place to store the chart
RAINBARF_OUT=~/.rainbarf.out
# update period, in seconds
TMOUT=30

# update the stored chart, avoiding running multiple rainbarf instances
rainbarf_update () {
    # check if non-existent or outdated
    if [[ \
        (! -e $RAINBARF_OUT) \
        || ($(stat +mtime $RAINBARF_OUT) -lt $(( $EPOCHSECONDS - $TMOUT ))) \
    ]]; then
        # rainbarf options should go to ~/.rainbarf.conf
        rainbarf --notmux > $RAINBARF_OUT
    fi
} 
rainbarf_update

# in-place prompt update hook
TRAPALRM () {
    rainbarf_update
    zle reset-prompt
}

# insert rainbarf chart into prompt
setopt PROMPT_SUBST
PS1="\$(cat $RAINBARF_OUT) $PS1"

4 Responses
Add your response

This is a completely useless thing. Dude, methinks that you've pimped-out your Zsh prompt just a mite to much perhaps?

over 1 year ago ·

@eddiesputz I'm absolutely sure it is useless. But it is cool, anyway :)

over 1 year ago ·

WHAT! You can animate your PS1 line!? I can already feel the ideas flooding into my brain.

over 1 year ago ·

Heh, this is awesome. Thanks for sharing.

glances at his non-animated prompt

Yep, think it's yak shaving time…

over 1 year ago ·