dotfiles/stumpwm/bin/stumpish

197 lines
4.0 KiB
Plaintext
Raw Normal View History

#!/bin/sh
# Copyright (C) 2007 Jonathan Moore Liles
#
# Maintainer: Jonathan Moore Liles
#
# stumpish is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2, or (at your option)
# any later version.
#
# stumpish is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this software; see the file COPYING. If not, see
# <http://www.gnu.org/licenses/>.
### STUMPwm Interactive SHell.
DELAY=0.01
if ! sleep $DELAY 2>/dev/null >&2
then
DELAY=1
fi
# replace -E with -r option for old versions of GNU sed
if ! sed -E 1p /dev/null 2>/dev/null; then
sed() { shift; command sed -r "$@"; }
fi
# parse C-style backslash sequences by default
if [ "$(echo -e foo)" = foo ]; then
echo() { builtin echo -e "$@"; }
fi
wait_result ()
{
while true
do
RESULT=$(xprop -root -f STUMPWM_COMMAND_RESULT 8s \
STUMPWM_COMMAND_RESULT 2>/dev/null |
sed -E 's/\\([[:digit:]]+)/\\0\1/g')
if echo "$RESULT" | grep -v -q 'not found.$'
then
break
fi
sleep $DELAY
done
xprop -root -remove STUMPWM_COMMAND_RESULT
if echo "$RESULT" | grep -q '= $'
then
return 0
fi
echo "$RESULT" |
sed -E 's/[^"\\n]+"//
/^"[[:space:]]*$/d
s/(^|[^\\])\\n/\1\
/g
s/\\(["\\n])/\1/g
s/\^([*[:digit:]]+|[Bbn])//g'
}
send_cmd ()
{
local cmd="$1"
if [ "$cmd" = "stumpwm-quit" ]
then
cmd=quit
elif [ "$cmd" = "quit" ]
then
exit
fi
xprop -root -f STUMPWM_COMMAND 8s -set STUMPWM_COMMAND "$cmd"
wait_result
}
usage ()
{
cat <<EOF
Usage: ${0##*/} [[-e|-r] command [args...]]
StumpIsh is the StumpWM shell. Use it to interact a running StumpWM
instance. When run from a terminal with no arguments, stumpish
accepts commands interactively and prints each result. If standard
input is a pipe, stumpish executes any number of commands and prints
the concatenated results. If the '-e' option and one argument are
given on the command line, stumpish reads any number of lines from
standard input and uses them as the argument to the named command.
Otherwise, if one or more arguments are provided on the command line,
the first is considered the name of the command to execute and the
remainder is concatenated to form the argument.
Example:
echo '(group-windows (current-group))' | ${0##*/} -e eval
EOF
exit 0;
}
warn ()
{
{
tput md bold
tput AF setaf 1
echo 'WARN:\c'
tput me sgr0
echo " $*"
} >&2
}
tput ()
{
local cap1=$1 cap2=$2
shift 2
command tput $cap1 $@ 2>/dev/null ||
command tput $cap2 $@ 2>/dev/null
}
READLINE=yes
if [ "x$1" = "x-r" ]
then
READLINE=no
shift 1
fi
if [ $# -gt 0 ]
then
[ "$1" = "--help" ] && usage
if [ "$1" = "-e" ]
then
if [ $# -ne 2 ]
then
echo "'-e' requires exactly one argument!"
exit
fi
shift 1
IFS=''
ARGS=$(cat /dev/stdin)
send_cmd "$1 $ARGS"
else
IFS=' '
send_cmd "$*"
fi
else
if [ -t 0 ]
then
if ! type rlwrap 2>/dev/null >&2
then
warn rlwrap not found, command completion won\'t work
elif [ $READLINE = yes ]
then
COMMANDS="${TMPDIR:-/tmp}/stumpish.commands.$$"
echo $(send_cmd "commands") |
sed -E 's/[[:space:]]+/\
/g' |
sort > "$COMMANDS"
trap 'rm -f "$COMMANDS"' exit int term
rlwrap -b '' -f "$COMMANDS" "$0" -r
exit
fi
tput AF setaf 5
echo Welcome to the STUMPwm Interactive SHell.
tput me sgr0
echo 'Type \c'
tput AF setaf 2
echo 'commands\c'
tput me sgr0
echo \ for a list of commands.
while read -p '> ' REPLY
do
tput md bold
tput AF setaf 2
send_cmd "$REPLY"
tput me sgr0
done
else
while read REPLY
do
send_cmd "$REPLY"
done
fi
fi