commit 1a779f4
hovercats
·
2023-01-16 21:02:39 +0000 UTC
parent de08117
groups: nits
1 files changed,
+46,
-46
M
groups
M
groups
+46,
-46
1@@ -24,18 +24,18 @@ EOF
2 }
3
4 # test for no arguments
5-test $# -eq 0 && usage
6+[ $# -eq 0 ] && usage
7
8 # I suggest it's under /tmp or somewhere that gets cleaned up at reboot or gets
9 # cleaned up after X stops running
10-FSDIR=${FSDIR:-/tmp/groups.sh}
11+FSDIR=${FSDIR:-/tmp/groups}
12
13 # define our functions
14
15 # clean WID ($1) from group files
16 clean_wid() {
17 t=$(mktemp /tmp/groups.XXXXXX)
18- for x in "$FSDIR"/group.*; do
19+ for x in "${FSDIR}"/group.*; do
20 sed "/$1/d" "$x" >"$t"
21 mv "$t" "$x"
22 done
23@@ -44,92 +44,92 @@ clean_wid() {
24
25 # cleans group ($1) from (in)active files
26 clean_status() {
27- t=$(mktemp /tmp/groups.XXXXXX)
28- sed "/$1/d" "$FSDIR"/active >"$t"
29- mv "$t" "$FSDIR"/active
30- sed "/$1/d" "$FSDIR"/inactive >"$t"
31- mv "$t" "$FSDIR"/inactive
32+ t="$(mktemp /tmp/groups.XXXXXX)"
33+ sed "/$1/d" "${FSDIR}"/active >"$t"
34+ mv "$t" "${FSDIR}"/active
35+ sed "/$1/d" "${FSDIR}"/inactive >"$t"
36+ mv "$t" "${FSDIR}"/inactive
37 }
38
39 # shows all the windows in group ($1)
40 map_group() {
41 # safety
42- if ! grep -q "$1" < "$FSDIR"/all; then
43- echo "Group doesn't exist"
44+ if ! grep -q "$1" < "${FSDIR}"/all; then
45+ printf "Group doesn't exist\n"
46 exit 1
47 fi
48
49 # clean statuses
50 clean_status "$1"
51 # add to active
52- echo "$1" >> "$FSDIR"/active
53+ printf "%s\n" "$1" >> "${FSDIR}"/active
54
55 # loop through group and map windows
56- xargs mapw -m <"$FSDIR"/group."$1"
57+ xargs mapw -m <"${FSDIR}"/group."$1"
58 }
59
60 # hides all the windows in group ($1)
61 unmap_group() {
62 # safety
63- if ! grep -q "$1" < "$FSDIR"/all; then
64- echo "Group doesn't exist"
65+ if ! grep -q "$1" < "${FSDIR}"/all; then
66+ printf "Group doesn't exist\n"
67 exit 1
68 fi
69
70 # clean statuses
71 clean_status "$1"
72 # add to inactive
73- echo "$1" >> "$FSDIR"/inactive
74+ printf "%s\n" "$1" >> "${FSDIR}"/inactive
75
76 # loop through group and unmap windows
77 while read -r line; do
78 mapw -u "$line"
79- done < "$FSDIR"/group."$1"
80+ done < "${FSDIR}"/group."$1"
81 }
82
83 # assigns WID ($1) to the group ($2)
84 set_group() {
85 #so that neither grep nor ls in clean_wid complain
86 #when group.$2 does not exist
87- touch "$FSDIR"/group."$2"
88+ touch "${FSDIR}"/group."$2"
89
90 # make sure we've no duplicates
91 clean_wid "$1"
92 clean_status "$2"
93
94 # insert WID into new group if not already there
95- grep -q "$1" < "$FSDIR"/group."$2" || \
96- echo "$1" >> "$FSDIR"/group."$2"
97+ grep -q "$1" < "${FSDIR}"/group."$2" || \
98+ printf "%s\n" "$1" >> "${FSDIR}"/group."$2"
99
100 # if we can't find the group add it to groups and make it active
101- grep -q "$2" < "$FSDIR"/all || \
102- echo "$2" >> "$FSDIR"/all && \
103- echo "$2" >> "$FSDIR"/active
104+ grep -q "$2" < "${FSDIR}"/all || \
105+ printf "%s\n" "$2" >> "${FSDIR}"/all && \
106+ printf "%s\n" "$2" >> "${FSDIR}"/active
107
108 # map WID if group is active
109- grep -q "$2" < "$FSDIR"/active && \
110+ grep -q "$2" < "${FSDIR}"/active && \
111 mapw -m "$1"
112
113 # unmap WID if group is inactive
114- grep -q "$2" < "$FSDIR"/inactive && \
115+ grep -q "$2" < "${FSDIR}"/inactive && \
116 mapw -u "$1"
117 }
118
119 # toggles visibility state of all the windows in group ($1)
120 toggle_group() {
121 # safety
122- if ! grep -q "$1" < "$FSDIR"/all; then
123- echo "Group doesn't exist"
124+ if ! grep -q "$1" < "${FSDIR}"/all; then
125+ printf "Group doesn't exist\n"
126 return
127 fi
128
129 # search through active groups first
130- grep -q "$1" < "$FSDIR"/active && \
131+ grep -q "$1" < "${FSDIR}"/active && \
132 unmap_group "$1" && \
133 return
134
135 # search through inactive groups next
136- grep -q "$1" < "$FSDIR"/inactive && \
137+ grep -q "$1" < "${FSDIR}"/inactive && \
138 map_group "$1" && \
139 return
140 }
141@@ -140,41 +140,41 @@ toggle_group() {
142 cleanup_everything() {
143 # clean WIDs that don't exist
144 # using `cat` instead of `<` because error suppression
145- cat "$FSDIR"/group.* 2>/dev/null | while read -r wid; do
146+ cat "${FSDIR}"/group.* 2>/dev/null | while read -r wid; do
147 wattr "$wid" || \
148 clean_wid "$wid"
149 done
150
151 # clean group files that are empty
152- for file in "$FSDIR"/group.*; do
153+ for file in "${FSDIR}"/group.*; do
154 # is the group empty?
155 if [ ! -s "$file" ]; then
156 rm -f "$file"
157 fi
158 done
159
160- cp "$FSDIR"/all "$FSDIR"/tmpall
161+ cp "${FSDIR}"/all "${FSDIR}"/tmpall
162 # remove groups that don't exist from 'all'
163 while read -r line; do
164- if [ ! -f "$FSDIR"/group."$line" ]; then
165- t=$(mktemp /tmp/groups.XXXXXX)
166- sed "/$line/d" "$FSDIR"/tmpall >"$t"
167- mv "$t" "$FSDIR"/tmpall
168+ if [ ! -f "${FSDIR}"/group."$line" ]; then
169+ t="$(mktemp /tmp/groups.XXXXXX)"
170+ sed "/$line/d" "${FSDIR}"/tmpall >"$t"
171+ mv "$t" "${FSDIR}"/tmpall
172 clean_status "$line"
173 fi
174- done < "$FSDIR"/all
175- mv "$FSDIR"/tmpall "$FSDIR"/all
176+ done < "${FSDIR}"/all
177+ mv -f "${FSDIR}"/tmpall "${FSDIR}"/all
178 }
179
180 # actual run logic (including arguments and such)
181
182-# check "$FSDIR" exists
183-test -d "$FSDIR" || mkdir -p "$FSDIR"
184+# check "${FSDIR}" exists
185+[ -d "${FSDIR}" ] || mkdir -p "${FSDIR}"
186
187 # touch all the files
188-test -f "$FSDIR"/active || :> "$FSDIR"/active
189-test -f "$FSDIR"/inactive || :> "$FSDIR"/inactive
190-test -f "$FSDIR"/all || :> "$FSDIR"/all
191+[ -f "${FSDIR}"/active ] || :> "${FSDIR}"/active
192+[ -f "${FSDIR}"/inactive ] || :> "${FSDIR}"/inactive
193+[ -f "${FSDIR}"/all ] || :> "${FSDIR}"/all
194
195 cleanup_everything
196
197@@ -206,7 +206,7 @@ while getopts "hc:Cs:t:m:M:u:U" opt; do
198 break
199 ;;
200 M)
201- for file in "$FSDIR"/group.*; do
202+ for file in "${FSDIR}"/group.*; do
203 group=${file##*.}
204 unmap_group "$group"
205 done
206@@ -218,13 +218,13 @@ while getopts "hc:Cs:t:m:M:u:U" opt; do
207 break
208 ;;
209 U)
210- for file in "$FSDIR"/group.*; do
211- group=${file##*.}
212+ for file in "${FSDIR}"/group.*; do
213+ group="${file##*.}"
214 unmap_group "$group"
215 done
216 break
217 ;;
218 *)
219- echo "Unrecognized option. use h flag to show usage"
220+ printf "Unrecognized option. use h flag to show usage\n"
221 esac
222 done