-
-
Notifications
You must be signed in to change notification settings - Fork 20
/
plumb
executable file
·189 lines (157 loc) · 5.16 KB
/
plumb
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
#!/bin/sh
# Documentation ────────────────────────────────────────────────────────────────
if test $# -eq 0; then
cat <<'EOF' > /dev/stderr
Description
‾‾‾‾‾‾‾‾‾‾‾
Plumber from Plan 9.
Use (wl-clipboard|xclip|pb{copy,paste}) (or $CLIPBOARD_COPY and $CLIPBOARD_PASTE if set) to access the clipboard and jq for JSON.
dmenu (or $DMENU if set) is used for validating execution.
Dependencies
‾‾‾‾‾‾‾‾‾‾‾‾
– Clipboard
– Wayland: wl-clipboard (https://github.com/bugaevc/wl-clipboard)
– X11: xclip (https://github.com/astrand/xclip)
– jq (https://stedolan.github.io/jq/)
– dmenu (https://tools.suckless.org/dmenu/)
Usage
‾‾‾‾‾
$ plumb [commands]
Allowed commands are passed through the command-line, and a validation step
(with a dmenu filter program) is required before proceeding.
Primary selection (JSON) format:
❯ [<command>, <arguments>]
❯ ["input", <input>, <command>, <arguments>]
The primary selection is used as input for the command (in a JSON array of strings)
and will be escaped suitable (list of single-quoted strings, separated by spaces)
for use in a command-line for a POSIX shell. The output of the command will be
copied to the clipboard.
Examples
‾‾‾‾‾‾‾‾
$ plumb mpv pandoc xdg-open
Primary selection contents:
❯ ["mpv", "https://youtu.be/dP1xVpMPn8M"]
❯ ["input", "<p>Tchou</p>", "pandoc", "--from", "html", "--to", "markdown"]
A Tour of the Acme Editor: https://research.swtch.com/acme
Configuration
‾‾‾‾‾‾‾‾‾‾‾‾‾
Change the dmenu command:
$ export DMENU=dmenu
Change the clipboard copy and paste commands:
$ export CLIPBOARD_COPY=wl-copy
$ export CLIPBOARD_PASTE=wl-paste
EOF
exit 1
fi
# Environment variables ────────────────────────────────────────────────────────
OS=$(uname -s)
# Options ──────────────────────────────────────────────────────────────────────
# Validate input with a dmenu filter program.
if test -z "$DMENU"; then
DMENU=dmenu
fi
# Copy content to the clipboard.
if test -z "$CLIPBOARD_COPY"; then
case "$OS" in
Darwin) CLIPBOARD_COPY='pbcopy' ;;
Linux|FreeBSD|OpenBSD)
case "$XDG_SESSION_TYPE" in
wayland) CLIPBOARD_COPY='wl-copy' ;;
x11) CLIPBOARD_COPY='xclip -filter -selection clipboard | xclip' ;;
esac
;;
esac
fi
# Paste content from the clipboard.
if test -z "$CLIPBOARD_PASTE"; then
case "$OS" in
Darwin) CLIPBOARD_PASTE='pbpaste' ;;
Linux|FreeBSD|OpenBSD)
case "$XDG_SESSION_TYPE" in
wayland) CLIPBOARD_PASTE='wl-paste' ;;
x11) CLIPBOARD_PASTE='xclip -out' ;;
esac
;;
esac
fi
# Main ─────────────────────────────────────────────────────────────────────────
main() {
# List of allowed commands
PLUMBERS=$@
# Get message from the primary selection
script=$(
sh -c "$CLIPBOARD_PASTE" |
jq --raw-output '
@sh "
set -- \(.)
"
'
)
# Abort if invalid JSON
if test $? != 0; then
notify-send --urgency critical 'Plumb' 'Invalid JSON'
exit 1
fi
# Get the argument list – now it is safe to proceed.
eval "$script"
# Input handler
if test "$1" = 'input'; then
exec <<EOF
$2
EOF
shift 2
fi
# Use `xdg_open` wrapper function
if test "$1" = xdg-open; then
for plumber in $PLUMBERS; do
if test "$plumber" = xdg-open; then
PLUMBERS=xdg_open
shift
set -- xdg_open "$@"
break
fi
done
fi
plumb "$@"
}
# Functions ────────────────────────────────────────────────────────────────────
plumb() {
command=$1
plumbed=no
# Iterate allowed commands
for plumber in $PLUMBERS; do
if test "$plumber" = "$command"; then
# Validate command:
if ! validate_command "$@"; then
exit 1
fi
# Execute the command and copy its output to the clipboard
"$@" | sh -c "$CLIPBOARD_COPY"
notify-send 'Plumb' "Command finished: $command"
plumbed=yes
break
fi
done
# Reject other commands
if test "$plumbed" = no; then
notify-send --urgency critical 'Plumb' "Command not allowed: $command"
exit 1
fi
}
# Requires validation before proceeding commands.
validate_command() {
# Display one item per argument.
# Separator for dmenu: '\0'
# Newline display: '\t'
# Note: '' could be nice, but `tr` only supports ASCII characters.
printf '%s\0' "$@" |
tr '\n\0' '\t\n' |
sh -c "$DMENU" > /dev/null 2>&1
}
# Wrapper function to open multiple URLs.
xdg_open() {
for url do
xdg-open "$url" < /dev/null > /dev/null 2>&1 &
done
}
main "$@"