Skip to content

Instantly share code, notes, and snippets.

@TauPan
Created March 31, 2011 21:50
Show Gist options
  • Save TauPan/897347 to your computer and use it in GitHub Desktop.
Save TauPan/897347 to your computer and use it in GitHub Desktop.
screen-select-or-run.sh
#!/bin/sh
# arguments:
# $1: title (mandatory)
# $2: program to run, no quotes in here! (mandatory)
# $3: explicit arguments to screen, you have to add -t title here yourself! (optional)
# $4: desired number for the new screen (optional)
# $5: screen command (!) to run, if already selected, e.g. eval 'stuff foo' (optional)
# use via e.g.:
# bind m screen screen-select-or-run mutt mutt
# bind m exec screen-select-or-run mutt mutt '-ln -t mutt' 1 q
# the 'screen' variant is faster but will open an intermediate screen
# also *if* the "stuff" actions ever work, I think they might work
# with 'exec' rather than 'screen'
title="$1"
program="$2"
args="$3"
number="$4"
action="$5"
result=0
screen -X msgminwait 0
screen -X msgwait 0
test "`screen -q -Q title`" = "$title"
selected=$?
if [ "$selected" = "0" ]
then
if [ -n "$action" ]
then
# FIXME: This doesn't work, I can't stuff or process anything while a key-binding is being executed
screen -X at "$title" stuff "$action"
result=$?
fi
else
if [ -z "$args" ]
then
args="-t $title"
fi
foundtitle=`screen -q -Q windows|sed -e 's/ /\n/g'| sed -e 's/[^ ]* *\(.*\)/\1/g'|sed -e "/^$title\$/ p; d"`
if [ "$foundtitle" != "$title" ]
then
screen $args $program
fi
screen -q -Q select "$title"
result=$?
if [ -n "$number" ]
then
screen -X number $number
fi
fi
# restore default settings of msgminwait and msgwait...
# If you've configured different defaults, you'll need to edit this script, sorry!
screen -X msgminwait 1
screen -X msgwait 5
exit $result
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment