summaryrefslogtreecommitdiffstats
path: root/files/.bin/stumpish
blob: 9e95c0c2a9ceec7d0dfb16adf90746b25c408c44 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
#!/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