-
Notifications
You must be signed in to change notification settings - Fork 14
/
i3-exec
executable file
·67 lines (61 loc) · 1.75 KB
/
i3-exec
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
#!/bin/sh -ue
# i3-exec COMMAND [ARG]..
#
# Tell i3 to start a command. The environment and other process state is inherited from i3, not i3-exec.
#
# The command is *not* a shell command, and thus redirections, globs, or other shell features will not work. If you want that, run "i3-msg exec". If you want your head to explode instead, run "i3-exec sh -c 'command'".
#
# Options:
# --startup-id command uses $DESKTOP_STARTUP_ID
fatal() {
printf %s\\n "$(basename "$0"): $2" >&2
exit $1
}
startup_id=--no-startup-id
handle_option() {
case "$1" in
startup-id) [ $# = 1 ] || fatal 64 "unexpected --$1 value"
startup_id= ;;
no-startup-id) [ $# = 1 ] || fatal 64 "unexpected --$1 value"
startup_id=--no-startup-id ;;
*) fatal 64 "unknown option: $1" ;;
esac
}
while [ $# -gt 0 ]; do
case "$1" in
--)
shift
break;;
--*=*)
x="${1#--}"
handle_option "${x%%=*}" "${x#*=}"
shift;;
--*)
handle_option "${1#--}"
shift;;
-?*)
if [ ${#1} = 2 ]; then
handle_option "${1#-}"
else
v="${1#??}"
x="${1%"$v"}"
handle_option "${x#-}" "$v"
fi
shift;;
*)
break;;
esac
done
if [ $# = 0 ]; then
fatal 64 'missing command'
fi
i3_exec_quote() {
# for sh, wrap in single quotes with escaped existing single quote
# for i3, escape double quotes
printf %s "$1"x | sed "s/'/'\\\\''/g; 1s/^/'/; \$s/x\$/'/; s/\"/\\\\\"/g"
}
cmd=exec
for arg; do
cmd="$cmd $(i3_exec_quote "$arg")"
done
exec i3-msg -q "exec $startup_id \"cd $(i3_exec_quote "$PWD") && $cmd\""