Posts Tagged ‘screen’

Often I find myself working on multiple source code clones or views simultaneously. And I like to have a separate screen session for each view/clone. In this post, I attempt to achieve two things.

  • Make it easier to correct the DISPLAY variable after reconnecting to an existing screen session.
  • Display current clone name on screen status line.

Screen shot of screen status line

First we decide on a suitable environment variable name to hold the view/clone name. CLONE_NAME is good enough. Now let’s define a bash function that will set the view/clone for us. Give this command to change the view/clone.

setclone ()
{
  if [ "$1" ]; then
    export CLONE_NAME=$1;
    SERVER_CLONE=`hostname`.$CLONE_NAME;
    export DISPLAY_FILE="$HOME/displays/$SERVER_CLONE";
    fi;
    echo clone=$CLONE_NAME;
}

Setclone exports two variables. CLONE_NAME is the name of the view/clone that we are going to work on. DISPLAY_FILE is the name of an ASCII file where we will store the current $DISPLAY variable before starting screen. We will use this later, from within screen. Now we define another function – clview (short for clone view) which actually starts the screen session.

clview ()
{
  if [ "$1" ]; then
    setclone $1;
  fi;
  rm -f $DISPLAY_FILE;
  echo $DISPLAY > $DISPLAY_FILE;
  screen -xR -S $CLONE_NAME;
}

This function writes the current DISPLAY variable to $DISPLAY_FILE (which was set by setclone). If we had started a screen session before and are reconnecting to it now, the DISPLAY variable in the existing screen session may have changed. So we have to set it up again. Instead of detaching from screen, finding the DISPLAY variable, reconnecting to screen and manually updating the DISPLAY variable, the clview function will put the correct value for DISPLAY variable in the file $DISPLAY_FILE. Now we write another function that will correct the DISPLAY variable for the screen sessions.

display ()
{
  echo old DISPLAY=$DISPLAY;
  export DISPLAY=`cat $DISPLAY_FILE`;
  echo new DISPLAY=$DISPLAY;
}

The screen options used are

       -x   Attach to a not detached screen session. (Multi display mode).
       -R   attempts  to resume the first detached screen session it finds.  If successful,
            all other command-line options are ignored.  If no detached session exists,
            starts a new session using the specified options, just as if -R had not been
            specified. The option is set by default if screen is run as a login-shell
            (actually screen uses "-xRR" in that case).
       -S sessionname
            When creating a new session, this option can be used to specify a meaningful
            name for the session. This name identifies  the  session  for  "screen -list"
            and "screen -r" actions. It substitutes the default [tty.host] suffix.

One thing that can be useful is for screen to display the current view/clone name on the status bar. Put the following the the screenrc file for a nice status line. The backtick command can query the shell and display its output in the status line. The query interval is set to 3600 seconds.

backtick 1 3600 3600 /bin/echo $CLONE_NAME
hardstatus alwayslastline "%{ck}%H: %{gk}%1` %?%{wk}%-Lw%?%{Yk}[%n*%f %t]%?%{kk}(%u)%?%?%{wk}%+Lw%?%=%{gk}%C %A %{Bk}%D, %M %d, %Y"

One side effect of the above status line is that it display the current time. So if we use the mouse to scroll back in history and the minute changes, it needs to be redisplayed. As part of the redisplay, we end up back at the prompt. You can disable this from the status line by removing “%{gk}%C %A ” (set green foreground, black background, time in 12 HR format, AM or PM).

Advertisements