-
-
Notifications
You must be signed in to change notification settings - Fork 24
/
eval-helper
executable file
Β·222 lines (195 loc) Β· 5.7 KB
/
eval-helper
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
#!/usr/bin/env bash
function eval-helper() (
source "$DOROTHY/sources/bash.bash"
source "$DOROTHY/sources/tty.bash"
# =====================================
# Arguments
function help {
cat <<-EOF >/dev/stderr
ABOUT:
Helpers for working with commands.
USAGE:
eval-helper [...options] -- ...<command>
OPTIONS:
--[no-]wrap
Wrap the output of the command in the command itself.
Enabled by default.
--[no-]confirm
Confirm execution of the command.
Disabled by default.
--[no-]quiet
If enabled, only latest message will be kept, and command output will be cleared if successful.
If disabled, all messages and command output will be kept.
--[no-]shapeshifter
Workaround for commands that clear themselves.
Disabled by default.
--pending=<pending>
Message to display while the command is executing.
--success=<success>
Message to display if the command succeeded.
--failure=<failure>
Message to display if the command failed.
EOF
if test "$#" -ne 0; then
echo-error "$@"
fi
return 22 # Invalid argument
}
# process
local item cmd option_wrap option_confirm option_quiet option_pending option_success option_failure
cmd=()
option_wrap='yes'
option_confirm='no'
option_quiet="$(echo-quiet -- "$@")"
option_shapeshifter='no'
option_pending=''
option_success=''
option_failure=''
while test "$#" -ne 0; do
item="$1"
shift
case "$item" in
'--help' | '-h') help ;;
'--no-quiet'* | '--quiet'* | '--no-verbose'* | '--verbose'*) ;; # handled by echo-quiet
'--pending='*) option_pending="${item#*--pending=}" ;;
'--success='*) option_success="${item#*--success=}" ;;
'--failure='*) option_failure="${item#*--failure=}" ;;
'--no-confirm'* | '--confirm'*)
option_confirm="$(
get-flag-value confirm --missing="$option_confirm" -- "$item" | echo-affirmative
)"
;;
'--no-shapeshifter'* | '--shapeshifter'*)
option_shapeshifter="$(
get-flag-value shapeshifter --missing="$option_shapeshifter" -- "$item" | echo-affirmative
)"
;;
'--no-wrap'* | '--wrap'*)
option_wrap="$(
get-flag-value wrap --missing="$option_wrap" -- "$item" | echo-affirmative
)"
;;
'--')
cmd+=("$@")
shift $#
break
;;
'--'*) help "An unrecognised flag was provided: $item" ;;
*) help "An unrecognised argument was provided: $item" ;;
esac
done
# =====================================
# Action
# prepare
local title
title="$(echo-quote "${cmd[@]}" | echo-join ' ')"
# confirm
if test "$option_confirm" = 'yes' && ! confirm --positive --ppid=$$ -- 'Confirm execution of the command that is below:' "$(echo-style --code="$title")"; then
echo-style --notice='Skipped execution of:' ' ' --code="$title"
return 0
fi
# output everything if already inside a revolving door, or if in verbose mode
local ec=0
if test "${INSIDE_REVOLVING_DOOR-}" = 'yes' -o "$option_quiet" = 'no'; then
# headers
if test -n "$option_pending"; then
echo "$option_pending"
fi
if test "$option_wrap" = 'yes'; then
echo-element --open="$title"
fi
# body
ec=0 && ("${cmd[@]}") || ec="$?"
# footers
if test "$option_wrap" = 'yes'; then
echo-element --close="$title" --status="$ec"
fi
if test "$ec" -eq 0; then
if test -n "$option_success"; then
echo "$option_success"
fi
else
if test -n "$option_failure"; then
echo "$option_failure"
fi
fi
else
# not inside a revolving door, and not in verbose mode
local headers body footer=''
headers="$(mktemp)"
body="$(mktemp)"
# headers
if test -n "$option_pending"; then
echo "$option_pending" | tee -a "$headers"
fi
if test "$option_wrap" = 'yes'; then
echo-element --open="$title" | tee -a "$headers"
fi
# body
# NOTE |& is bash v4 only, and this script must work on Bash v3
# https://github.com/bevry/dorothy/discussions/151
local INSIDE_REVOLVING_DOOR_original="${INSIDE_REVOLVING_DOOR:-"no"}"
export INSIDE_REVOLVING_DOOR='yes' # use export, as env doesn't work if cmd[0] was a function
if test "$option_shapeshifter" = 'yes'; then
# this is used if the command writes to TTY
# in which case echo-revolving-door fails to clear
tty_start
cat "$headers" # redo headers inside alt tty while its active
ec=0 && ("${cmd[@]}" 2>&1 | tee "$body") || ec="$?"
tty_finish
else
ec=0 && ("${cmd[@]}" 2>&1 | tee "$body" | echo-revolving-door) || ec="$?"
fi
export INSIDE_REVOLVING_DOOR="$INSIDE_REVOLVING_DOOR_original"
# clear headers, we can re-add them later if needed
echo-clear-lines <"$headers"
# generate footer
if test "$ec" -eq 0; then
if test -n "$option_success"; then
footer+="$option_success"$'\n'
fi
else
if test -n "$option_failure"; then
footer+="$option_failure"$'\n'
fi
fi
# if quiet and successful, dump footer and exit
if test "$option_quiet" = 'yes' -a "$ec" -eq 0; then
if test -n "$footer"; then
echo -n "$footer"
fi
return "$ec"
fi
# if it didn't output anything, output self closing wrap, then footer, then exit
if test -z "$(cat "$body")"; then
echo-element --openclose="$title" --status="$ec"
if test -n "$footer"; then
echo -n "$footer"
fi
return "$ec"
fi
# the command outputted things
# output wrap header
if test "$option_wrap" = 'yes'; then
echo-element --open="$title"
fi
# if verbose, or failure, output body
if test "$option_quiet" = 'no' -o "$ec" -ne 0; then
cat "$body"
fi
# outpout wrap footer
if test "$option_wrap" = 'yes'; then
echo-element --close="$title" --status="$ec"
fi
# output footer
if test -n "$footer"; then
echo -n "$footer"
fi
fi
# done
return "$ec"
)
# fire if invoked standalone
if test "$0" = "${BASH_SOURCE[0]}"; then
eval-helper "$@"
fi