commit 9da28b5
hovercats
·
2022-02-18 21:56:05 +0000 UTC
parent e51feaa
add groups.sh
M
README
+5,
-1
1@@ -43,4 +43,8 @@ Overview
2 etc, without killing the xorg-session.
3
4
5-
6+The following scripts require wmutils[0] to work:
7+ - groups.sh
8+ - workspace.sh
9+
10+[0] https://github.com/wmutils/core
+226,
-0
1@@ -0,0 +1,226 @@
2+#!/bin/sh
3+#
4+# Copyright (c) 2015 Greduan <me@greduan.com>, licensed under the WTFPL
5+# Adds group-like capabilities, sorta like those you find in CWM and such WMs
6+
7+
8+# Original, unmodified script can be found at https://github.com/wmutils/contrib
9+
10+usage() {
11+ cat << EOF
12+usage: $(basename "$0") [-hCU] [-c wid] [-s wid group] [-tmMu group]
13+ -h shows this help
14+ -c cleans WID from group files (and makes it visible)
15+ -C runs cleanup routine
16+ -s sets WID's group
17+ -t toggle group visibility state
18+ -m maps (shows) group
19+ -M maps group and unmaps all other groups
20+ -u unmaps (hides) group
21+ -U unmaps all the groups
22+EOF
23+
24+ exit 1
25+}
26+
27+# test for no arguments
28+test $# -eq 0 && usage
29+
30+# I suggest it's under /tmp or somewhere that gets cleaned up at reboot or gets
31+# cleaned up after X stops running
32+FSDIR=${FSDIR:-/tmp/groups.sh}
33+
34+# define our functions
35+
36+# clean WID ($1) from group files
37+clean_wid() {
38+ t=$(mktemp /tmp/groups.XXXXXX)
39+ for x in $(ls "$FSDIR"/group.*); do
40+ sed "/$1/d" "$x" >"$t"
41+ mv "$t" "$x"
42+ done
43+ rm -f "$t"
44+}
45+
46+# cleans group ($1) from (in)active files
47+clean_status() {
48+ t=$(mktemp /tmp/groups.XXXXXX)
49+ sed "/$1/d" "$FSDIR"/active >"$t"
50+ mv "$t" "$FSDIR"/active
51+ sed "/$1/d" "$FSDIR"/inactive >"$t"
52+ mv "$t" "$FSDIR"/inactive
53+}
54+
55+# shows all the windows in group ($1)
56+map_group() {
57+ # safety
58+ if ! grep -q "$1" < "$FSDIR"/all; then
59+ echo "Group doesn't exist"
60+ exit 1
61+ fi
62+
63+ # clean statuses
64+ clean_status "$1"
65+ # add to active
66+ echo "$1" >> "$FSDIR"/active
67+
68+ # loop through group and map windows
69+ xargs mapw -m <"$FSDIR"/group."$1"
70+}
71+
72+# hides all the windows in group ($1)
73+unmap_group() {
74+ # safety
75+ if ! grep -q "$1" < "$FSDIR"/all; then
76+ echo "Group doesn't exist"
77+ exit 1
78+ fi
79+
80+ # clean statuses
81+ clean_status "$1"
82+ # add to inactive
83+ echo "$1" >> "$FSDIR"/inactive
84+
85+ # loop through group and unmap windows
86+ while read -r line; do
87+ mapw -u "$line"
88+ done < "$FSDIR"/group."$1"
89+}
90+
91+# assigns WID ($1) to the group ($2)
92+set_group() {
93+ #so that neither grep nor ls in clean_wid complain
94+ #when group.$2 does not exist
95+ touch "$FSDIR"/group."$2"
96+
97+ # make sure we've no duplicates
98+ clean_wid "$1"
99+ clean_status "$2"
100+
101+ # insert WID into new group if not already there
102+ grep -q "$1" < "$FSDIR"/group."$2" || \
103+ echo "$1" >> "$FSDIR"/group."$2"
104+
105+ # if we can't find the group add it to groups and make it active
106+ grep -q "$2" < "$FSDIR"/all || \
107+ echo "$2" >> "$FSDIR"/all && \
108+ echo "$2" >> "$FSDIR"/active
109+
110+ # map WID if group is active
111+ grep -q "$2" < "$FSDIR"/active && \
112+ mapw -m "$1"
113+
114+ # unmap WID if group is 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+ return
125+ fi
126+
127+ # search through active groups first
128+ grep -q "$1" < "$FSDIR"/active && \
129+ unmap_group "$1" && \
130+ return
131+
132+ # search through inactive groups next
133+ grep -q "$1" < "$FSDIR"/inactive && \
134+ map_group "$1" && \
135+ return
136+}
137+
138+# removes all the unexistent WIDs from groups
139+# removes all group files that don't exist
140+# removes from 'all' file all groups that don't exist
141+cleanup_everything() {
142+ # clean WIDs that don't exist
143+ # using `cat` instead of `<` because error suppression
144+ cat "$FSDIR"/group.* 2>/dev/null | while read -r wid; do
145+ wattr "$wid" || \
146+ clean_wid "$wid"
147+ done
148+
149+ # clean group files that are empty
150+ for file in "$FSDIR"/group.*; do
151+ # is the group empty?
152+ if [ ! -s "$file" ]; then
153+ rm -f "$file"
154+ fi
155+ done
156+
157+ # remove groups that don't exist from 'all'
158+ while read -r line; do
159+ if [ ! -f "$FSDIR"/group."$line" ]; then
160+ t=$(mktemp /tmp/groups.XXXXXX)
161+ sed "/$line/d" "$FSDIR"/all >"$t"
162+ mv "$t" "$FSDIR"/all
163+ clean_status "$line"
164+ fi
165+ done < "$FSDIR"/all
166+}
167+
168+# actual run logic (including arguments and such)
169+
170+# check "$FSDIR" exists
171+test -d "$FSDIR" || mkdir -p "$FSDIR"
172+
173+# touch all the files
174+test -f "$FSDIR"/active || :> "$FSDIR"/active
175+test -f "$FSDIR"/inactive || :> "$FSDIR"/inactive
176+test -f "$FSDIR"/all || :> "$FSDIR"/all
177+
178+cleanup_everything
179+
180+# getopts yo
181+while getopts "hc:Cs:t:m:M:u:U" opt; do
182+ case $opt in
183+ h)
184+ usage
185+ ;;
186+ c)
187+ clean_wid "$OPTARG"
188+ mapw -m "$OPTARG"
189+ break
190+ ;;
191+ C)
192+ cleanup_everything
193+ break
194+ ;;
195+ s)
196+ set_group "$OPTARG" "$(eval echo "\$$OPTIND")"
197+ break
198+ ;;
199+ t)
200+ toggle_group "$OPTARG"
201+ break
202+ ;;
203+ m)
204+ map_group "$OPTARG"
205+ break
206+ ;;
207+ M)
208+ for file in "$FSDIR"/group.*; do
209+ group=${file##*.}
210+ unmap_group "$group"
211+ done
212+ map_group "$OPTARG"
213+ break
214+ ;;
215+ u)
216+ unmap_group "$OPTARG"
217+ break
218+ ;;
219+ U)
220+ for file in "$FSDIR"/group.*; do
221+ group=${file##*.}
222+ unmap_group "$group"
223+ done
224+ break
225+ ;;
226+ esac
227+done