Skip to content

Instantly share code, notes, and snippets.

@ernstki
Last active April 24, 2024 21:46
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ernstki/188e1731135874d7d69a2f1b9711f9a8 to your computer and use it in GitHub Desktop.
Save ernstki/188e1731135874d7d69a2f1b9711f9a8 to your computer and use it in GitHub Desktop.
screenrc with a decent-looking caption / hardstatus line, including LSF job status

A classy .screenrc with LSF support

Screenshot of what you get

Here's a sensible .screenrc that does the following:

  • gives you a useful and hip-looking status line
  • tries really hard to enable 256 color support for programs that support it, like Vim
  • disables the visual bell and splash screen
  • remaps window #0 to be Ctrl+A, `, which feels more natural to me
    • Ctrl+A, 0 will now select window #10, which makes more sense to me
  • prints a condensed summary of LSF's bjobs command in the status line, but only when you're on a cluster node
    • modify the value of CLUSTER_NODE_REGEXP in the helper script to suit your local setup

Usage

If your shell is Bash, here's a one-liner that will grab both the .screenrc and the supporting shell script, preserving a backup of whatever .screenrc you may already have.

# use 'wget -qO - <install-script-url>' if you don't have 'curl'
curl -sL https://gist.github.com/ernstki/188e1731135874d7d69a2f1b9711f9a8/raw/zz_install.sh | bash

Please look at what the installation script does before piping it through bash. If you don't understand it, or you don't trust it, you can do the installation manually by:

  • downloading the raw screenrc-with-lsf-support and zz_bjob_status.sh files yourself
  • renaming screenrc-with-lsf-support to ~/.screenrc
  • removing the zz_ part of the shell script's filename, making it executable with chmod a+x, then putting it somewhere in your $PATH (all the zz_ nonsense is so that the files in this gist sort in the order I want)

That said, I did test the installation script on my own user account, and everything went well.

Well, actually...

If you know already know that PATH variable additions actually probably shouldn't go in ~/.bashrc, then you probably didn't need the installation script. :)

Credits

Credit for most of the .screenrc, including the visual appearance, is due to Chris Wills. Bits and pieces of the screen config taken from other places are credited in-place.

For those people who want to know which font is used in the screenshot and all that (there's always one):

  • the screenshot of the terminal is PuTTY running in Wine, if that matters to you
  • the font is DejaVu Sans Mono, 11 point
  • the Vim theme is a customized version of wombat256mod

I tested in PuTTY (and XTerm) because they tend to be the lowest common denominator for color support.

License

The shell script is made available under the terms of the MIT license, ©2019, Kevin Ernst.

References:

#
# A classy .screenrc
#
# Author: Kevin Ernst <kevin.ernst ~at~ cchmc.org>, with help from many
# Date: 13 May 2019
# Source: https://gist.github.com/ernstki/188e1731135874d7d69a2f1b9711f9a8
#
# Installation:
# curl -sL https://gist.github.com/ernstki/188e1731135874d7d69a2f1b9711f9a8/raw/zz_install.sh | bash
#
# under most circumstances, this should be enough, in 2019, to get screen to
# use 256 colors. try running 'python -m fabulous.demo' after installing
# https://github.com/jart/fabulous for a test
term xterm-256color
# tell screen how to set 256 colors. AB = background, AF=foreground
# source: https://gist.github.com/ChrisWills/1337178
#
# is probably not required at all as long as your termcap/terminfo has either
# 'xterm-256color' or 'screen-256color' entries; test w/ 'infocmp <termtype>'
# termcapinfo xterm 'Co#256:AB=\E[48;5;%dm:AF=\E[38;5;%dm'
# enables use of shift-PgUp and shift-PgDn (couldn't get this to do anything
# useful; use Ctrl+A, Esc to activate scrollback, PgUp/PgDn work as expected
# termcapinfo xterm|xterms|xs|rxvt ti@:te@
# allow bold colors - doesn't seem to be necessary; run the command
# 'python -m fabulous.demo' from https://github.com/jart/fabulous for a test
#attrcolor b ".I"
# erase background with current bg color; otherwise Vim themes (and probably
# other programs that make use of background colors) will look weird
defbce "on"
# cache 30000 lines for scroll back (Ctrl+A, Esc)
defscrollback 30000
# turn off GNU screen "splash screen"
# source: https://lists.gnu.org/archive/html/screen-users/2005-04/msg00040.html
startup_message off
# turn off visual bell
vbell off
# set messages timeout to one second
msgwait 1
# it's up to the client where to display the "hardstatus"; unless otherwise
# specified, it may be capable of putting it in the title bar of the window.
# comment this out if that's what you want.
hardstatus alwayslastline
# a simple status line; '%h' means the existing 'hardstatus' of the window,
# which may be the same as what's already in your terminal's titlebar
# all the remaining "string escapes" are documented here:
# https://www.gnu.org/software/screen/manual/html_node/String-Escapes.html
#hardstatus string "%3n %t%? @%u%?%? [%h]%?%=%D %M %d %c"
# a fancier status line, gives window list, login name and current date:
# source: elementaryOS / Ubuntu default?
#hardstatus string "%{wk}%?%-Lw%?%{bw}%n*%f %t%?(%u)%?%{wk}%?%+Lw %=%{mk}@%H %{yk}%D %{ck}%M%{wk} %{ck}%d %{gk}%c"
# create an external command which can be used as part of the status line and
# store it in "slot" #101
# source: https://www.gnu.org/software/screen/manual/html_node/Backtick.html
backtick 101 30 15 $HOME/bin/bjob_status.sh
# another one; with LSF job status that is displayed conditionally based on the
# output of the ~/bin/bjob_status.sh script
# source: https://gist.github.com/ChrisWills/1337178
hardstatus string '%{= Kd} %{= Kd}%-w%{= Kr}[%{= KW}%n %t%{= Kr}]%{= Kd}%+w %-= %{KG} %H%{KW} | %?%{KY}LSF: %101`%{KW} | %?%D %M %d %Y%{= Kc} %C%A%{-}'
# bind some keys to make it easier to resize / make splits equal size
# source: elementaryOS / Ubuntu default?
bind = resize =
bind + resize +1
bind - resize -1
bind _ resize max
# bind Ctrl+A, backtick to window #0
# source: https://lists.gnu.org/archive/html/screen-users/2009-01/msg00084.html
bind ` select 0
bind 0 select 10
#!/bin/bash
#
# Print cluster job status in a format suitable for use with a screenrc's
# 'hardstatus' or 'caption' options. Rename this to 'bjob_status.sh' and
# save it in your ~/bin, or some other location that's in your $PATH.
#
# Author: Kevin Ernst <kevin.ernst ~at~ cchmc.org>
# Date: 13 May 2019
# Source: https://gist.github.com/ernstki/188e1731135874d7d69a2f1b9711f9a8
# License: MIT
#
# References:
# [1] https://gist.github.com/ChrisWills/1337178
# [2] https://www.gnu.org/software/screen/manual/html_node/Backtick.html
#
# return an error if any intermediate process in a pipe fails
set -o pipefail
# a "modern" / "extended" RE that will match whatever your cluster nodes are
CLUSTER_NODE_REGEXP='^(bmicluster|bmi-[0-9])'
# if we're NOT on a login/compute node...
if ! [[ $(hostname -s) =~ $CLUSTER_NODE_REGEXP ]]; then
#echo "n/a" # print nothing, so we can use screen's %? string escape
exit
fi
# otherwise, reformat the output of 'bjobs' so that it prints counts of all the
# job statuses from the 'STAT' (third) column
jobstatus=$(
bjobs \
| tail -n +2 \
| awk '{print $3}' \
| sort \
| uniq -c \
| tr -s ' ' \
| tr -d '\n'
)
# capture the return code
ret=$?
if [[ $ret -ne 0 ]]; then
# non-zero return code means a failure in the bjobs | tail... pipe
echo "error parsing 'bjobs'"
exit 1
elif [[ -z $jobstatus ]]; then
# if returned string is empty, no jobs were running/pending
echo "no jobs"
else
# remove that one leftover leading space that 'tr' didn't get
echo "${jobstatus/ /}"
fi
# bjob_status.sh
#!/bin/bash
#
# Installation script for GitHub gist 188e1731135874d7d69a2f1b9711f9a8
# (screenrc with a decent-looking caption / hardstatus line, including LSF
# job status)
#
# trace all operations so user can see what's happening
set -x
GIST='https://gist.githubusercontent.com/ernstki/188e1731135874d7d69a2f1b9711f9a8'
GISTRAW="$GIST/raw"
SCREENRC='screenrc-with-lsf-support'
SCRIPT='zz_bjob_status.sh'
# create ~/bin and add it to user's PATH environment variable
test -d ~/bin || mkdir ~/bin
grep -q "$HOME/bin" <<<"$PATH" || \
echo -e "\n# add ~/bin to PATH\nexport PATH=\"\$PATH:\$HOME/bin\"" >> ~/.bashrc
# make a backup of an existing ~/.screenrc (could be file or symlink)
test -r ~/.screenrc && \mv ~/.screenrc ~/.screenrc.bak-$(date +%Y%m%d)
# fetch the helper script, then the .screenrc
GETTER='curl -sLOJ'
which curl &>/dev/null || GETTER='wget'
( cd ~/bin; $GETTER $GISTRAW/$SCRIPT; \mv $SCRIPT ${SCRIPT/zz_/} )
SCRIPT=${SCRIPT/zz_/}
chmod a+x ~/bin/$SCRIPT
( cd; $GETTER $GISTRAW/$SCREENRC; \mv $SCREENRC .screenrc )
# stop tracing
set +x
if test -x ~/bin/$SCRIPT -a -f ~/.screenrc; then
source ~/.bashrc
unset GIST GISTRAW SCREENRC SCRIPT
echo -e "\nOK! Restart 'screen' (if necessary) for changes to take effect."
else
echo -e "\nOh noes. Something went wrong with the installation." >&2
fi
# end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment