Monday, December 14, 2009

Gnu Screen theme cycle with keybinding

SkyHi @ Monday, December 14, 2009

A co-worker of mine did this and shared with me, so i thought I’d continue spreading it around. If you have multiple gnu screen sessions going at once, wouldn’t it be nice to visually tell them apart without spending time to read the session name of each or trying to see what was going on within each?

The best way would be to color code the caption and hardlinestatus lines and be able to easily select a “color” per screen session and that’s what he and I did. Using C-g, we/you can easily cycle through various “themes”. This could also be useful if you wanted to cycle through mutiple hardstatus lines that displayed different information too if that’s the route you’d like to go. Anyways here it goes.

The following is my ~/.screenrc (the bolded is the keybinding)

#terminfo and termcap for nice 256 color terminal
# allow bold colors – necessary for some reason
attrcolor b “.I”
# tell screen how to set colors. AB = background, AF=foreground
termcapinfo xterm ‘Co#256:AB=\E[48;5;%dm:AF=\E[38;5;%dm'
# erase background with current bg color
defbce "on"

startup_message off

bindkey ^G screen $HOME/bin/screen_theme.sh

caption always "%{= KW}%-w%{= gW}%n %t%{-}%+w %-="

hardstatus alwayslastline "%{= kW} %-= %{= kG}Session:%u%{= kW} %5` | %{= kG}Host:%{= kW} %H | %{= kG} MEM:%{= kW} %2`MB |%{= kG} SW: %{= kW}%3`MB | %{= kG}Unread: %{= kW} %4` | %m/%d %C %A"

vbell off

#Backticks to display information in status bar
backtick 1 60 60 $HOME/bin/get_uptime
backtick 2 60 60 $HOME/bin/get_freemem
backtick 3 60 60 $HOME/bin/get_freeswap
backtick 4 60 60 $HOME/bin/get_gmail
backtick 5 60 60 $HOME/bin/get_sessionname

defscrollback 5000
screen -t root 0 sudo -s
screen -t shell 1 bash
screen -M -t irc 8 irssi
screen -t home 9 ssh home
select 1

This script resides in ~/bin which is in my path. screen_theme.sh:

#!/bin/bash

INDEXFILE="$HOME/bin/themes/theme_index"

# if this is the first time then set
# the index to 0

if [[ ! -e $INDEXFILE ]]
then
echo 0 > $INDEXFILE
fi

THEMENO=`cat $INDEXFILE`

THEMEMAX=5

if [[ $THEMENO -eq $THEMEMAX ]]
then
THEMENO=0
else
THEMENO=`expr $THEMENO + 1`
fi

echo $THEMENO > $INDEXFILE

THEMEFILE=$HOME/bin/themes/theme${THEMENO}

if [[ -e $THEMEFILE ]]
then
bash $THEMEFILE $STY
else

# reset the index back to zero if broken

echo 0 > $INDEXFILE
fi

then I created a directory ~/bin/themes. Within that directory I have the following files:

$ ls themes/
theme0 theme1 theme2 theme3 theme4 theme5 theme_index

Each theme file is another color theme, the index just holds a count of the number of themes.

An example of theme0 ( remember when doing yours to escape your backticks, otherwise it’ll break).

#!/bin/bash

# yellow

SESSION=$1

screen -S $SESSION -X caption always “%{= KW}%-w%{= yk}%n %t%{-}%+w %-=”
screen -S $SESSION -X hardstatus alwayslastline “%{= kW} %-= %{= ky}Session: %u%{= kW}%5\` | %{= ky}Host:%{= kW} %H | %{= ky} \
MEM:%{= kW} %2\`MB |%{= ky} SW: %{= kW}%3\`MB | %{= ky}Unread: %{= kW}%4\` | %m/%d %C %A”

Here’s how my screen started:


After C-g

After C-g again

and so on.

If copy and pasting the above is not working for you, here’s my take on the script and my theme files.   Note the theme files in here don’t have all the crazy backtick stuff going on in the examples above.

Download

http://tuxtraining.com/files/screen_theme.tar.gz

Note: this tutorial goes well with this one:  Automagically set titles of windows in Gnu Screen to boxes you ssh in screen.

Reference: http://tuxtraining.com/2009/10/12/gnu-screen-theme-cycle-with-keybinding