-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfirebox_dmenu
More file actions
executable file
·204 lines (172 loc) · 4.68 KB
/
firebox_dmenu
File metadata and controls
executable file
·204 lines (172 loc) · 4.68 KB
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
#!/bin/sh
COMMAND="firebox"
TERMINAL=${TERMINAL:-"xterm"}
profile_dir="$("$COMMAND" config print_profile_dir)"
sep="$("$COMMAND" config print_separator)"
sep_esc="$("$COMMAND" config print_separator --escaped)"
if [ -d "$profile_dir" ]; then
options="service;workspace;explorer"
else
options="init"
fi
dmenu_cmd() {
dmenu -i "$@"
}
print_log() {
echo "$1" >&2
}
split_options() {
OLD_IFS="$IFS"
export IFS=";"
for i in $1; do
# call your procedure/other scripts here below
printf "%s\n" "$i"
done
export IFS="$OLD_IFS"
}
select_stuff() {
action="$1"
shift
prompt="Select $action(s):"
if [ "$action" = "custom" ]; then
options="$1"
prompt="Select services:"
shift
else
if [ "$action" = "workspace" ]; then
options="$("$COMMAND" workspace list --all --raw | sed "s/$sep.*//g")"
else
options="$("$COMMAND" service list --raw | sed "s/$sep.*//g")"
fi
fi
if [ "$1" = "--once" ]; then
once="--once"
shift
else
options="== Confirm ==;$options"
fi
selection=""
if [ -n "$options" ]; then
while true; do
current_selection="$(split_options "$options" | dmenu_cmd -p "$prompt" "$@")"
if [ -z "$current_selection" ]; then
selection=""
break
fi
if [ "$current_selection" != "== Confirm ==" ]; then
selection_name=$(echo "$current_selection" | sed 's/^* //')
if echo "$selection" | grep -q " '$selection_name'\s*"; then
options=$(split_options "$options" | sed "/* $selection_name/{s/^* //}")
selection=$(split_options "$selection" | sed "s/'$selection_name'//")
else
options=$(split_options "$options" | sed "/$current_selection/{s/^/* /}")
selection="$selection '$current_selection'"
fi
else
break
fi
[ "$once" = "--once" ] && break
done
fi
echo "$selection" | sed 's/^\s*//'
}
launch_stuff() {
action="$1"
shift
selection=$(select_stuff "$action" "$@")
[ -n "$selection" ] && echo "$selection" | xargs "$COMMAND" "$action" launch
}
explorer() {
"$TERMINAL" sh -c "$COMMAND explorer && printf '\n\nPress any key to close' && read -r confirm"
}
service() {
options="list;launch"
option=$(split_options "$options" | dmenu_cmd -p "Firebox Services:" "$@")
case "$option" in
list)
"$TERMINAL" sh -c "printf 'Available services:\n' && $COMMAND service list && printf '\n\nPress any key to close' && read -r confirm"
;;
launch)
launch_stuff service "$@"
;;
*)
echo "Invalid option" &&
exit 1
;;
esac
}
add_to_workspace() {
[ -n "$1" ] && services=$(select_stuff service)
{ [ -n "$1" ] && [ -n "$services" ]; } &&
echo "$1" "$services" | xargs "$COMMAND" workspace add
}
workspace() {
workspaces=$("$COMMAND" workspace list --all)
if [ -z "$workspaces" ]; then
options="create"
else
options="create;list;launch;add;remove;delete"
fi
option=$(split_options "$options" | dmenu_cmd -p "Firebox Services:" "$@")
case "$option" in
create)
workspace_name=$(dmenu_cmd -p "Enter service name:" <&-)
echo "$workspace_name"
"$COMMAND" workspace create "$workspace_name" &&
add_to_workspace "$workspace_name"
;;
list)
workspace_name=$(select_stuff custom "== ALL ==;$("$COMMAND" workspace list --all --raw | sed "s/$sep.*//")" --once)
[ "$workspace_name" = "'== ALL =='" ] && workspace_name="--all"
"$TERMINAL" sh -c "printf 'Available services:\n' && $COMMAND workspace list $workspace_name && printf '\n\nPress any key to close' && read -r confirm"
;;
launch)
launch_stuff workspace --once
;;
add)
print_log "add"
if [ "$1" = "--workspace" ]; then
print_log "waka waka"
workspace_name="$2" &&
shift && shift
else
workspace_name=$(select_stuff workspace --once)
fi
add_to_workspace "$workspace_name"
;;
remove)
workspace_name=$(select_stuff workspace --once)
if [ -n "$workspace_name" ]; then
available_services=$(echo "$workspace_name" | xargs -I "{}" "$COMMAND" workspace list "{}" --raw | grep -iEo "$sep_esc.*\$" | sed "s/$sep//;s/$sep/\n/g;")
if [ -n "$available_services" ]; then
services=$(select_stuff custom "$available_services")
else
echo "Workspace Empty"
fi
[ -n "$services" ] &&
echo "$workspace_name" "$services" | xargs "$COMMAND" workspace remove
fi
;;
delete)
workspace_name=$(select_stuff workspace --once)
[ -n "$workspace_name" ] && "$TERMINAL" sh -c "$COMMAND workspace delete $workspace_name && printf '\n\nPress any key to close' && read -r confirm"
;;
*)
echo "Invalid option" &&
exit 1
;;
esac
}
option=$(split_options "$options" | dmenu_cmd -p "Firebox" "$@")
init() {
"$TERMINAL" sh -c "$COMMAND init && printf '\n\nPress any key to close' && read -r confirm"
}
case "$option" in
init | explorer | service | workspace)
"$option" "$@"
;;
*)
echo "Invalid option" &&
exit 1
;;
esac