This repository has been archived by the owner on May 27, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsway-launch
executable file
·219 lines (196 loc) · 5.98 KB
/
sway-launch
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
#!/usr/bin/env bash
# sway-launch
shopt -s xpg_echo
bold="\033[1m"
green="\033[32m"
red="\033[31m"
reset="\033[0m"
# swaymsg, fallback to i3-msg
swaymsg() {
if command -v swaymsg >/dev/null 2>&1; then
command swaymsg "$@"
else
command i3-msg "$@"
fi
}
# get ids and app_ids of all containers
# - this is used to figure out when the program has launched
get_tree_info() {
swaymsg -t get_tree | jq -r \
'recurse(.nodes[]) | recurse(.floating_nodes[]) | select(.type == "con") | "\(.id) \(.app_id) \(.window_properties.instance)"'
}
usage() {
echo "${bold}Usage:${reset} ${green}sway-launch${reset} [OPTIONS] [--] <command> [arguments...]"
echo " ${green}sway-launch${reset} [OPTIONS] [--] '<command> [arguments...]'"
echo
echo "- Launch a command. Meant to be called from a wrapper script or alias, which in turn"
echo " is called from a terminal running in the Sway/i3 scratchpad."
echo
echo "- After running it, the scratchpad is hidden. This is meant to simulate the UX of an"
echo " application launcher like rofi, but from a terminal running in the scratchpad."
echo
echo "Options:"
echo " -w, --workspace=<value> Launch the command in the given workspace."
echo " - VALUE can be \"current\" (default), \"next\", \"prev\","
echo " \"next-empty\", or a number."
echo " -n, --no-hide-scratchpad Do not hide the scratchpad after launching the command."
echo " -e, --window-name <value> Used to check for the new window's existence (by app_id"
echo " or instance). When the window exists, sway-launch exits."
echo " With no value (default), exit as soon as a new window"
echo " is created."
echo " -t, --no-wait Do not wait for a window to open. Exit immediately."
echo " -q, --quiet Do not print any output. (Useful for wrapping scripts"
echo " with their own outputs.)"
echo " -d, --debug Print debug output."
echo " -h, --help Show this help message."
echo
echo "Example:"
echo " # in .bashrc"
echo " alias firefox='sway-launch --workspace=next-empty -- firefox'"
echo
echo " <mod>- # open scratchpad"
echo " firefox<ret> # launch firefox, scratchpad is then hidden."
}
# parse options
hide_scratchpad=1
quiet=0
debug=0
wait_for_window=1
go_to_workspace="current"
window_name=""
while true; do
case "$1" in
-n|--no-hide-scratchpad)
hide_scratchpad=0
shift
;;
-q|--quiet)
quiet=1
shift
;;
-d|--debug)
debug=1
shift
;;
-h|--help)
usage
exit 0
;;
-w|--workspace)
go_to_workspace=$2
shift 2
;;
-e|--window-name)
window_name=$2
shift 2
;;
-t|--no-wait)
wait_for_window=0
shift
;;
--)
shift
break
;;
*)
break
;;
esac
done
# print usage on invalid arguments
if [[ $# -eq 0 ]]; then
usage
exit 1
fi
# handle workspace
if [[ $go_to_workspace != "current" ]]; then
sway-workspace --quiet $go_to_workspace
if [[ $? -ne 0 ]]; then
exit 1
fi
fi
# check if the scratchpad is focused at the time of calling this script.
is_scratchpad_focused=$(sway-is-scratchpad-focused)
# store current list of container IDs and 'app_id's
old_tree=$(get_tree_info)
# launch the command
[[ $quiet -eq 0 ]] && echo "${bold}sway-launch:${reset} Launching ${green}$@${reset} ..."
# prepare by cleaning latest exit code, if any
exit_code_file="/tmp/sway-launch-exit-code.${USER}"
rm -f ${exit_code_file} > /dev/null 2>&1
# parse arguments into "cmd" and "args"
if [[ "$#" -eq 1 ]]; then
# if there is only one argument, expand it into parameters with 'eval' to
# handle quoted arguments
eval "args=($1)"
cmd=${args[0]}
args=("${args[@]:1}")
else
cmd=$1
shift
args=("$@")
fi
# debug info
if [[ $debug -eq 1 ]]; then
echo sway-launch: cmd: $cmd
echo sway-launch: args: "${args[@]}"
for arg in "${args[@]}"; do
echo "sway-launch: $arg"
done
fi
# This is where the magic happens:
# - Launch in the background
# - Launch in a subshell to avoid adding to the shell's job control
# - Redirect all output to /dev/null
# - Make exit code available to this script
(
${cmd} "${args[@]}" > /dev/null 2>&1
echo $?
) > ${exit_code_file} &
# If exit code exists and is not 0, exit with that code
exit_code=$(cat ${exit_code_file})
if [[ $exit_code -ne 0 ]]; then
[[ $quiet -eq 0 ]] && echo "${bold}sway-launch:${reset} ${red}Error:${reset} Command exited with code ${bold}$exit_code${reset}"
rm -f ${exit_code_file} > /dev/null 2>&1
exit $exit_code
fi
# wait for the program to launch
# - this is done by checking if the tree has changed
# - skip if --no-wait was provided
waiting_time=0
printed_message=0
if [[ $wait_for_window -eq 1 ]]; then
while true; do
# print message after 5 seconds
if [[ $waiting_time -gt 5000 && $printed_message -eq 0 ]]; then
if [[ $quiet -eq 0 ]]; then
echo "${bold}sway-launch:${reset} The program is taking a long time to launch."
printed_message=1
fi
fi
new_tree=$(get_tree_info)
if [[ "$new_tree" != "$old_tree" ]]; then
# if "window-name" was provided, check if diff contains the string
if [[ -n "$window_name" ]]; then
diff=$(diff <(echo "$old_tree") <(echo "$new_tree"))
if [[ "$diff" == *"$window_name"* ]]; then
break
fi
else
break
fi
fi
sleep 0.001
waiting_time=$((waiting_time+1))
done
fi
[[ $quiet -eq 0 ]] && echo "${bold}sway-launch:${reset} ${green}Done!${reset}"
# If launching from the scratchpad, hide it aftrewards
# - This simulates the UX of application launchers like rofi, but for a
# terminal running in the scratchpad.
if [[ $hide_scratchpad -eq 1 && $is_scratchpad_focused == "true" ]]; then
sway-scratchpad-toggle --quiet
if [[ $? -ne 0 ]]; then
exit 1
fi
fi