summaryrefslogtreecommitdiffstats
path: root/files/.bin/stumpish
diff options
context:
space:
mode:
Diffstat (limited to 'files/.bin/stumpish')
-rwxr-xr-xfiles/.bin/stumpish196
1 files changed, 196 insertions, 0 deletions
diff --git a/files/.bin/stumpish b/files/.bin/stumpish
new file mode 100755
index 0000000..9e95c0c
--- /dev/null
+++ b/files/.bin/stumpish
@@ -0,0 +1,196 @@
1#!/bin/sh
2
3# Copyright (C) 2007 Jonathan Moore Liles
4#
5# Maintainer: Jonathan Moore Liles
6#
7# stumpish is free software; you can redistribute it and/or modify
8# it under the terms of the GNU General Public License as published by
9# the Free Software Foundation; either version 2, or (at your option)
10# any later version.
11#
12# stumpish is distributed in the hope that it will be useful,
13# but WITHOUT ANY WARRANTY; without even the implied warranty of
14# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15# GNU General Public License for more details.
16#
17# You should have received a copy of the GNU General Public License
18# along with this software; see the file COPYING. If not, see
19# <http://www.gnu.org/licenses/>.
20
21### STUMPwm Interactive SHell.
22
23DELAY=0.01
24
25if ! sleep $DELAY 2>/dev/null >&2
26then
27 DELAY=1
28fi
29
30# replace -E with -r option for old versions of GNU sed
31if ! sed -E 1p /dev/null 2>/dev/null; then
32 sed() { shift; command sed -r "$@"; }
33fi
34
35# parse C-style backslash sequences by default
36if [ "$(echo -e foo)" = foo ]; then
37 echo() { builtin echo -e "$@"; }
38fi
39
40wait_result ()
41{
42 while true
43 do
44 RESULT=$(xprop -root -f STUMPWM_COMMAND_RESULT 8s \
45 STUMPWM_COMMAND_RESULT 2>/dev/null |
46 sed -E 's/\\([[:digit:]]+)/\\0\1/g')
47 if echo "$RESULT" | grep -v -q 'not found.$'
48 then
49 break
50 fi
51
52 sleep $DELAY
53 done
54
55 xprop -root -remove STUMPWM_COMMAND_RESULT
56
57 if echo "$RESULT" | grep -q '= $'
58 then
59 return 0
60 fi
61
62 echo "$RESULT" |
63 sed -E 's/[^"\\n]+"//
64 /^"[[:space:]]*$/d
65 s/(^|[^\\])\\n/\1\
66/g
67 s/\\(["\\n])/\1/g
68 s/\^([*[:digit:]]+|[Bbn])//g'
69}
70
71send_cmd ()
72{
73 local cmd="$1"
74
75 if [ "$cmd" = "stumpwm-quit" ]
76 then
77 cmd=quit
78 elif [ "$cmd" = "quit" ]
79 then
80 exit
81 fi
82
83 xprop -root -f STUMPWM_COMMAND 8s -set STUMPWM_COMMAND "$cmd"
84
85 wait_result
86}
87
88usage ()
89{
90 cat <<EOF
91Usage: ${0##*/} [[-e|-r] command [args...]]
92
93StumpIsh is the StumpWM shell. Use it to interact a running StumpWM
94instance. When run from a terminal with no arguments, stumpish
95accepts commands interactively and prints each result. If standard
96input is a pipe, stumpish executes any number of commands and prints
97the concatenated results. If the '-e' option and one argument are
98given on the command line, stumpish reads any number of lines from
99standard input and uses them as the argument to the named command.
100Otherwise, if one or more arguments are provided on the command line,
101the first is considered the name of the command to execute and the
102remainder is concatenated to form the argument.
103
104Example:
105 echo '(group-windows (current-group))' | ${0##*/} -e eval
106EOF
107 exit 0;
108}
109
110warn ()
111{
112 {
113 tput md bold
114 tput AF setaf 1
115 echo 'WARN:\c'
116 tput me sgr0
117 echo " $*"
118 } >&2
119}
120
121tput ()
122{
123 local cap1=$1 cap2=$2
124 shift 2
125
126 command tput $cap1 $@ 2>/dev/null ||
127 command tput $cap2 $@ 2>/dev/null
128}
129
130READLINE=yes
131
132if [ "x$1" = "x-r" ]
133then
134 READLINE=no
135 shift 1
136fi
137
138if [ $# -gt 0 ]
139then
140 [ "$1" = "--help" ] && usage
141 if [ "$1" = "-e" ]
142 then
143 if [ $# -ne 2 ]
144 then
145 echo "'-e' requires exactly one argument!"
146 exit
147 fi
148 shift 1
149 IFS=''
150 ARGS=$(cat /dev/stdin)
151 send_cmd "$1 $ARGS"
152 else
153 IFS=' '
154 send_cmd "$*"
155 fi
156else
157 if [ -t 0 ]
158 then
159 if ! type rlwrap 2>/dev/null >&2
160 then
161 warn rlwrap not found, command completion won\'t work
162 elif [ $READLINE = yes ]
163 then
164 COMMANDS="${TMPDIR:-/tmp}/stumpish.commands.$$"
165 echo $(send_cmd "commands") |
166 sed -E 's/[[:space:]]+/\
167/g' |
168 sort > "$COMMANDS"
169 trap 'rm -f "$COMMANDS"' exit int term
170 rlwrap -b '' -f "$COMMANDS" "$0" -r
171 exit
172 fi
173
174 tput AF setaf 5
175 echo Welcome to the STUMPwm Interactive SHell.
176 tput me sgr0
177 echo 'Type \c'
178 tput AF setaf 2
179 echo 'commands\c'
180 tput me sgr0
181 echo \ for a list of commands.
182
183 while read -p '> ' REPLY
184 do
185 tput md bold
186 tput AF setaf 2
187 send_cmd "$REPLY"
188 tput me sgr0
189 done
190 else
191 while read REPLY
192 do
193 send_cmd "$REPLY"
194 done
195 fi
196fi