konsole tabs on steroids.

konsole with dynamic tab names.
In the category “little gems that make my life better” (which in fact is synonym for “Why
everyone would be better off with the Z-Shell”
), today I’m reading from my .zshrc file. Zsh has
those fine pre- and post-exec hooks. Those let you specify functions that are run before a command
is executed, and right after. I’m using a tabbed konsole all of the time, with some DCOP magic,
it’s easy to monitor your commands from another tab.
But let the .zshrc script speak:

# Define colors
BLACK="%{"$'\033[01;30m'"%}"
GREEN="%{"$'\033[01;32m'"%}"
RED="%{"$'\033[01;31m'"%}"
YELLOW="%{"$'\033[01;33m'"%}"
BLUE="%{"$'\033[01;34m'"%}"
BOLD="%{"$'\033[01;39m'"%}"
NORM="%{"$'\033[00m'"%}"

if [ "$KONSOLE_DCOP_SESSION" ]
then
        if [ `/usr/bin/whoami` != 'root' ]
                then
                alias ssh='konsolewrap  ssh'
                alias lftp='konsolewrap  lftp'
                alias sftp='konsolewrap  sftp'
                alias ssh='konsolewrap  ssh'
                alias su='konsolewrap su'
                alias ipy='konsolewrap ipython'
        preexec () {konsole-rename-cmd $1}
        precmd () {
            konsole-rename-path
            if [ `/bin/hostname` = 'luna' ]; then
                RPROMPT="${BLUE}[${YELLOW}%*${NORM} `battery`${BLUE}]${NORM}"
            fi
        }
        fi
fi
    

This involves two small scripts, konsole-rename-path and konsole-rename-cmd. Those basically
change the name of the tab with a dcop call. konsole-rename-path looks like this:

#!/bin/bash
# This small script renames the konsole tab to the current working directory

if [ "$KONSOLE_DCOP_SESSION" ]
then
    dcop `echo ${KONSOLE_DCOP_SESSION}` renameSession "`echo $PWD | sed s,^$HOME,~,`"
fi
    

This is konsole-rename-cmd:

#!/bin/bash
# This small script renames the konsole tab to the current command

if [ "$KONSOLE_DCOP_SESSION" ]
then
    dcop `echo ${KONSOLE_DCOP_SESSION}` renameSession "$1"
fi
    

konsolewrap is for commands like ssh, tabs get the hostname in this case:

#!/bin/sh
dcop $KONSOLE_DCOP_SESSION renameSession "$*"
exec $cmd "$@"
    

This can of course all be neatified, but it does the job pretty well. I can start a job in
one tab, switch to another, and I see the tabname change when the command is finished. This
is somewhat similar to the “Monitor for Silence / Activity” in konsole, but a bit more
flexible.

As you see, there’s also a variable set, RPROMPT. This produces a clock and the current
battery charge state. In colors and all.

battery(){
        CBS=`acpi | awk '{print $4}' | awk -F % '{print $1}'`
        if [[ ${CBS} -lt 35 ]] && [[ ${CBS} -gt 10 ]]; then
                echo "${YELLOW}${CBS}%%${NORM}"
        elif [[ ${CBS} -lt 10 ]] || [[ ${CBS} -eq 10 ]]; then
                echo "${RED}${CBS}%%${NORM}"
        else
                echo "${GREEN}${CBS}%%${NORM}"
        fi
}
    

Low battery value, and the right hand side prompt’s battery percentage goes yellow, then red.

KDE4’s konsole has a very similar feature built in, so less fiddling with scripts in the
future. :-)

Posted in KDE