-
Notifications
You must be signed in to change notification settings - Fork 0
/
wm-print-tree
executable file
·192 lines (159 loc) · 5.18 KB
/
wm-print-tree
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
#! /usr/bin/env bash
# SCRIPT_DIR is set to the directory where this script is localed. It it was
# run by a symlink, it will be resolved to the directory of the target script
# (that is, the actual script directory).
SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
if [[ -L "${BASH_SOURCE[0]}" ]]; then
SCRIPT_DIR="$(readlink "${BASH_SOURCE[0]}")"
SCRIPT_DIR="$( cd "$( dirname "${SCRIPT_DIR}" )" >/dev/null 2>&1 && pwd )"
fi
# get platform info, populates 'platform' and 'apple_silicon' variables
source "${SCRIPT_DIR}"/lib/platform.sh
# print usage on -h/--help
if [[ "$1" == "-h" || "$1" == "--help" ]]; then
echo "Prints the current sway tree"
echo " - Currently only works on Linux"
exit 0
fi
if [[ "$platform" == "macos" ]]; then
echo "This function is only available on Linux"
exit 1
fi
# ansi colors
bold='\033[1m'
red='\033[0;31m'
green='\033[0;32m'
yellow='\033[1;33m'
blue='\033[0;34m'
reset='\033[0m'
r=$((16#58));
g=$((16#5b));
b=$((16#70));
grey="\x1b[38;2;${r};${g};${b}m"
vertical_line="| "
leaf="\\_"
result=""
# print an output line
print_output() {
local indent="$2"
result="${result}${green}${indent}output $1${reset}\n"
}
# print a workspace line
print_workspace() {
local indent="$2"
result="${result}${grey}${indent}${leaf}${reset} ${yellow}workspace $1${reset}\n"
}
# print a container or an application line
print_container() {
local name="$1"
local pid="$2"
local type="$3"
local floating="$4"
local indent="$5"
local focused="$6"
if [[ "$floating" == "true" ]]; then
floating=" ${reset}(floating)${red}"
else
floating=""
fi
if [[ "$focused" == "true" ]]; then
focused=" ${reset}${bold}<< you are here!"
else
focused=""
fi
if [[ "$pid" != "null" ]]; then
result="${result}${grey}${indent}${leaf}${red}${floating} $name (pid: $pid)${focused}${reset}\n"
else
result="${result}${grey}${indent}${leaf}${blue}${floating} container $type${focused}${reset}\n"
fi
}
traverse_tree() {
local nodes="$1"
local indent="$2"
local is_last="$3"
local count
count=$(grep -c "^ {" <<< "$nodes")
local i
for (( i=0; i < $count; i++ )); do
local last_arg="$is_last"
if [[ $i -ne $((count-1)) ]]; then
last_arg="false"
else
last_arg="true"
fi
# build prefix for child nodes
local new_prefix="$indent"
if [[ "$last_arg" == "true" ]]; then
new_prefix="${new_prefix} "
else
new_prefix="${new_prefix}${vertical_line} "
fi
local child
child=$(jq --unbuffered ".[$i]" <<< "$nodes")
local type
type="${child#*\"type\": \"}"
type="${type%%\"*}"
local name
name="${child#*\"name\": \"}"
name="${name%%\"*}"
case "$type" in
output)
print_output "$name" "$indent"
traverse_tree "$(jq --unbuffered '.nodes' <<< "$child")" "$new_prefix" "$last_arg"
;;
workspace)
print_workspace "$name" "$indent"
traverse_tree "$(jq --unbuffered '.floating_nodes + .nodes' <<< "$child")" "$new_prefix" "$last_arg"
;;
con|floating_con)
local app_id
app_id=${child#*\"app_id\": \"}
app_id=${app_id%%\"*}
local app_name
if [[ "$app_id" != "null" ]]; then
app_name="$app_id"
else
app_name="$name"
fi
# have to use jq on this one due to nesting
local pid
pid=$(echo "$child" | jq --unbuffered '.pid')
local layout
layout="${child#*\"layout\": \"}"
layout="${layout%%\"*}"
local floating_con
floating_con="${child#*\"type\": \"}"
floating_con="${floating_con%%\"*}"
if [[ "$floating_con" == "floating_con" ]]; then
floating_con="true"
else
floating_con="false"
fi
local focused
focused="${child#*\"focused\": }"
focused="${focused%%,*}"
if [[ "$focused" == "true" ]]; then
focused="true"
else
focused="false"
fi
print_container "$app_name" "$pid" "$layout" "$floating_con" "$indent" "$focused"
traverse_tree "$(jq --unbuffered '.floating_nodes + .nodes' <<< "$child")" "$new_prefix" "$last_arg"
;;
esac
done
}
main() {
local outputs=$(swaymsg -t get_tree | jq --unbuffered '.nodes')
local count=$(grep -c "^ {" <<< "$outputs")
local i
for (( i=0; i < $count; i++ )); do
local output=$(jq --unbuffered ".[$i]" <<< "$outputs")
local name="${output#*\"name\": \"}"
name="${name%%\"*}"
print_output "$name" ""
traverse_tree "$(jq --unbuffered '.nodes' <<< "$output")" " " "true"
done
}
main
echo -ne "$result"