-
Notifications
You must be signed in to change notification settings - Fork 0
/
.shellfishrc
executable file
·449 lines (392 loc) · 12.2 KB
/
.shellfishrc
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
# include this from .bashrc, .zshrc or
# another shell startup file with:
# source $HOME/.shellfishrc
#
# Running from Secure ShellFish
# this gives access to:
# openUrl to open pages or deeps links
# pbcopy to copy text to iOS clipboard
# pbpaste to paste from iOS clipboard
# quicklook to preview files
# runShortcut to run Shortcuts
# setbarcolor to change toolbar color
# sharesheet to invoke iOS share sheet
# snip to add new snippets
# textastic to edit files with Textastic
#
# In any terminal use:
# thumbnail to write exif thumbnails
# notify to post notifications
# widget to change lock/home screen
# widgets & apple watch complications
#
#
# this part does nothing outside ShellFish
if [[ "$LC_TERMINAL" = "ShellFish" ]]; then
ios_printURIComponent() {
awk 'BEGIN {while (y++ < 125) z[sprintf("%c", y)] = y
while (y = substr(ARGV[1], ++j, 1))
q = y ~ /[a-zA-Z0-9]/ ? q y : q sprintf("%%%02X", z[y])
printf("%s", q)}' "$1"
}
ios_printBase64Component() {
echo -n "$1" | base64
}
which printf > /dev/null
ios_hasPrintf=$?
ios_printf() {
if [ $ios_hasPrintf ]; then
printf "$1"
else
awk "BEGIN {printf \"$1\"}"
fi
}
ios_sequence() {
if [[ -n "$TMUX" ]]; then
OUTPUT=$(
ios_printf '\033Ptmux;\033\033]'
echo -n "$1" | tr -d '[:space:]'
ios_printf '\a\033\\' )
else
OUTPUT=$(
ios_printf '\033]'
echo -n "$1" | tr -d '[:space:]'
ios_printf '\a' )
fi
if [ -t 1 ] ; then
echo -n $OUTPUT
elif [[ -n "$SSH_TTY" ]]; then
echo -n $OUTPUT > $SSH_TTY
else
echo >&2 'Standard output is not tty and there is no $SSH_TTY'
fi
}
ios_sequence_spaced() {
if [[ -n "$TMUX" ]]; then
OUTPUT=$(
ios_printf '\033Ptmux;\033\033]'
echo -n "$1"
ios_printf '\a\033\\' )
else
OUTPUT=$(
ios_printf '\033]'
echo -n "$1"
ios_printf '\a' )
fi
if [ -t 1 ] ; then
echo -n $OUTPUT
elif [[ -n "$SSH_TTY" ]]; then
echo -n $OUTPUT > $SSH_TTY
else
echo >&2 'Standard output is not tty and there is no $SSH_TTY'
fi
}
# prepare fifo for communicating result back to shell
ios_prepareResult() {
FIFO=$(mktemp)
rm -f $FIFO
mkfifo $FIFO
echo $FIFO
}
# wait for terminal to complete action
ios_handleResult() {
FIFO=$1
if [ -n "$FIFO" ]; then
read <$FIFO -s
rm -f $FIFO
if [[ $REPLY = error* ]]; then
echo "${REPLY#error=}" | base64 >&2 -d
return 1
fi
if [[ $REPLY = result* ]]; then
echo "${REPLY#result=}" | base64 -d
fi
fi
}
sharesheet() {
if [[ $# -eq 0 ]]; then
if tty -s; then
cat <<EOF
Usage: sharesheet [FILE]...
Present share sheet for files and directories. Alternatively you can pipe in text and call it without arguments.
If arguments exist inside the Finder or Files app changes made are written back to the server.
EOF
return 0
fi
fi
FIFO=$(ios_prepareResult)
OUTPUT=$(
awk 'BEGIN {printf "6;sharesheet://?ver=2&respond="}'
ios_printBase64Component "$FIFO"
awk 'BEGIN {printf "&pwd="}'
ios_printBase64Component "$PWD"
awk 'BEGIN {printf "&home="}'
ios_printBase64Component "$HOME"
for var in "$@"
do
awk 'BEGIN {printf "&path="}'
ios_printBase64Component "$var"
done
if [[ $# -eq 0 ]]; then
text=$(cat -)
awk 'BEGIN {printf "&text="}'
ios_printBase64Component "$text"
fi
)
ios_sequence "$OUTPUT"
ios_handleResult "$FIFO"
}
quicklook() {
if [[ $# -eq 0 ]]; then
if tty -s; then
cat <<EOF
Usage: quicklook [FILE]...
Show QuickLook preview for files and directories. Alternatively you can pipe in text and call it without arguments.
EOF
return 0
fi
fi
FIFO=$(ios_prepareResult)
OUTPUT=$(
awk 'BEGIN {printf "6;quicklook://?ver=2&respond="}'
ios_printBase64Component "$FIFO"
awk 'BEGIN {printf "&pwd="}'
ios_printBase64Component "$PWD"
awk 'BEGIN {printf "&home="}'
ios_printBase64Component "$HOME"
for var in "$@"
do
awk 'BEGIN {printf "&path="}'
ios_printBase64Component "$var"
done
if [[ $# -eq 0 ]]; then
text=$(cat -)
awk 'BEGIN {printf "&text="}'
ios_printBase64Component "$text"
fi
)
ios_sequence "$OUTPUT"
ios_handleResult "$FIFO"
}
textastic() {
if [[ $# -eq 0 ]]; then
cat <<EOF
Usage: textastic <text-file>
Open in Textastic 9.5 or later.
File must be in directory represented in the Files app to allow writing back edits.
EOF
else
if [ ! -e "$1" ]; then
touch "$1"
fi
OUTPUT=$(
awk 'BEGIN {printf "6;textastic://?ver=2&pwd="}'
ios_printBase64Component "$PWD"
awk 'BEGIN {printf "&home="}'
ios_printBase64Component "$HOME"
awk 'BEGIN {printf "&path="}'
ios_printBase64Component "$1"
)
ios_sequence "$OUTPUT"
fi
}
setbarcolor() {
if [[ $# -eq 0 ]]; then
cat <<EOF
Usage: setbarcolor <css-style color>
Set color of terminal toolbar color with values such as
red, #f00, #ff0000, rgb(255,0,0), color(p3 1.0 0.0 0.0)
EOF
else
OUTPUT=$(
awk 'BEGIN {printf "6;settoolbar://?ver=2&color="}'
ios_printBase64Component "$1"
)
ios_sequence "$OUTPUT"
fi
}
openUrl() {
if [[ $# -eq 0 ]]; then
cat <<EOF
Usage: openUrl <url>
Open URL on iOS.
EOF
else
FIFO=$(ios_prepareResult)
OUTPUT=$(
awk 'BEGIN {printf "6;open://?ver=2&respond="}'
ios_printBase64Component "$FIFO"
awk 'BEGIN {printf "&url="}'
ios_printBase64Component "$1"
)
ios_sequence "$OUTPUT"
ios_handleResult "$FIFO"
fi
}
runShortcut() {
local baseUrl="shortcuts://run-shortcut"
if [[ $1 == "--x-callback" ]]; then
local baseUrl="shortcuts://x-callback-url/run-shortcut"
shift
fi
if [[ $# -eq 0 ]]; then
cat <<EOF
Usage: runShortcut [--x-callback] <shortcut-name> [input-for-shortcut]
Run in Shortcuts app bringing back results if --x-callback is included.
EOF
else
local name=$(ios_printURIComponent "$1")
shift
if [[ $* == "-" ]]; then
local text=$(cat -)
local input=$(ios_printURIComponent "$text")
else
local input=$(ios_printURIComponent "$*")
fi
openUrl "$baseUrl?name=$name&input=$input"
fi
}
# copy standard input or arguments to iOS clipboard
pbcopy() {
OUTPUT=$(
awk 'BEGIN {printf "52;c;"} '
if [ $# -eq 0 ]; then
base64 | tr -d '\n'
else
echo -n "$@" | base64 | tr -d '\n'
fi
)
ios_sequence "$OUTPUT"
}
# paste from iOS device clipboard to standard output
pbpaste() {
FIFO=$(ios_prepareResult)
OUTPUT=$(
awk 'BEGIN {printf "6;pbpaste://?ver=2&respond="}'
ios_printBase64Component "$FIFO"
)
ios_sequence "$OUTPUT"
ios_handleResult "$FIFO"
}
# create new snippets
snip() {
if [[ $# -eq 0 ]]; then
cat <<EOF
Usage: snip <text for snippet>
EOF
else
OUTPUT=$(
awk 'BEGIN {printf "6;addsnippet://?ver=2&text="}'
ios_printBase64Component "$*"
)
ios_sequence "$OUTPUT"
fi
}
# Secure ShellFish supports 24-bit colors
export COLORTERM=truecolor
# We need to pass through escape sequences through tmux
if [[ -n "$TMUX" ]]; then
# ignore error from old versions of tmux without this command
tmux 2> /dev/null set -g allow-passthrough on || true
fi
if [[ -z "$INSIDE_EMACS" && $- = *i* ]]; then
# tmux mouse mode enables scrolling with
# swipe and mouse wheel
if [[ -n "$TMUX" ]]; then
tmux set -g mouse on
fi
# send the current directory using OSC 7 when showing prompt to
# make filename detection work better for interactive shell
update_terminal_cwd() {
ios_sequence $(
awk "BEGIN {printf \"7;%s\", \"file://$HOSTNAME\"}"
ios_printURIComponent "$PWD"
)
}
if [ -n "$ZSH_VERSION" ]; then
precmd() { update_terminal_cwd; }
elif [[ $PROMPT_COMMAND != *"update_terminal_cwd"* ]]; then
PROMPT_COMMAND="update_terminal_cwd${PROMPT_COMMAND:+; $PROMPT_COMMAND}"
fi
fi
fi
# this part works in any context
thumbnail() {
if [[ $# -eq 0 ]]; then
cat <<EOF
Usage: thumbnail <image-file> [image-file-2] ...
Add Exif thumnails to image files using ImageMagick convert and exiftool.
EOF
else
# make sure ImageMagick and exiftool are available
convert -version 1>/dev/null 2>/dev/null || { echo "ImageMagick convert needs to be installed"; return 1; }
exiftool -v 1>/dev/null 2>/dev/null || { echo "exiftool needs to be installed"; return 1; }
THUMBNAIL=/tmp/thumbnail.jpg
for arg in "$@"
do
echo "$arg"
convert "$arg" -thumbnail 160x120^ "$THUMBNAIL"
exiftool -q -overwrite_original "-thumbnailimage<=$THUMBNAIL" "$arg"
rm -f "$THUMBNAIL"
done
fi
}
# Updates Terminal Data widget in Secure ShellFish
#
# This command sends encrypted data through push notifications such
# that it doesn't need to run from a Secure ShellFish terminal.
widget() {
if [[ $# -eq 0 ]]; then
cat <<EOF
Usage: widget [target] <data> ...
Update widget on device from which this function was installed with a number of content parameters that can be string, progress, icon, target, color, url or shortcut.
Each argument type is derived from input, where the first argument is assumed to be a target if it matches a target configured on the widget.
Progress has the form: 50% or 110/220
Icon must match valid SF Symbol name such as globe or terminal.fill
Colors must be hex colours such as #000 #ff00ff where the color is used for later content and 'foreground' switches back to default colour
Target is used to send different content to different widgets after configuring the widgets with different target identifiers which requires the pro unlock. The target parameter is never assumed unless --target is used and is effective until next --target parameter allowing updates of several widgets with a single command
URL is used when tapping the widget and is assumed for arguments starting with https:// and other schemes are supported by using --url
Shortcut works like URL running the Shortcut with the given name and is never assumed without --shortcut
String is the fallback type if nothing else matches, but content type can be forced for next parameter with --progress, --icon, --color, --text or --target with something like:
widget --text "50/100"
You can update several widgets at once by using --target to send all parameters until the next --target to a particular widget. Updating several widgets at once allows more total updates per day.
EOF
return 0
fi
local key=88850f3a88bfd0c1e361efd864763bf78300b5ca867609678fbf830eaab7e832
local user=ArMDQHmWakfnQMcNPBStmwEj4okqqjjNzycr8X2p
local iv=ab5bbeb426015da7eedcee8bee3dffb7
local plain=$(
echo Secure ShellFish Widget 2.0
for var in "$@"
do
echo -ne "$var" | base64
done)
local base64=$(echo "$plain" | openssl enc -aes-256-cbc -base64 -K $key -iv $iv)
curl -sS -X POST -H "Content-Type: text/plain" --data "$base64" "https://secureshellfish.app/push/?user=$user"
}
# Shows notification on your device with Secure ShellFish installed
# optionally opening URL or running Shortcut when notification is
# opened.
#
# This command sends encrypted data through push notifications such
# that it doesn't need to run from a Secure ShellFish terminal.
notify() {
if [[ $# -eq 0 ]]; then
cat <<EOF
Usage: notify [--shortcut NameOfShortcut] [--url https://url.to.open/] [title] <body> ...
EOF
return 0
fi
local key=88850f3a88bfd0c1e361efd864763bf78300b5ca867609678fbf830eaab7e832
local user=ArMDQHmWakfnQMcNPBStmwEj4okqqjjNzycr8X2p
local iv=ab5bbeb426015da7eedcee8bee3dffb7
local plain=$(
echo Secure ShellFish Notify 2.0
for var in "$@"
do
echo -ne "$var" | base64
done)
local base64=$(echo "$plain" | openssl enc -aes-256-cbc -base64 -K $key -iv $iv)
curl -sS -X POST -H "Content-Type: text/plain" --data "$base64" "https://secureshellfish.app/push/?user=$user&mutable"
}