aboutsummaryrefslogtreecommitdiff
path: root/wmutils/bin/wgrp
blob: c24226017d673af23497b5a4a49e13495054969c (plain)
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
#!/bin/sh
#           
# ███     ██  █████  ██████ ██████ 
#░░██  █ ░██ ██░░░██░░██░░█░██░░░██
# ░██ ███░██░██  ░██ ░██ ░ ░██  ░██
# ░████░████░░██████ ░██   ░██████ 
# ███░ ░░░██ ░░░░░██░███   ░██░░░  
#░░░    ░░░  █████ ░░░     ░██     
#           ░░░░░          ░░     

# tmp dir
wmutilsdir=${wmutilsdir:-"/tmp/wmutils"}
dir="$wmutilsdir/groups"
# create if it doesnt exist
if [ ! -d "$dir" ]
then
  mkdir -p "$dir"
  printf "1\n" > "$dir/active"
  cat "/dev/null" > "$dir/1"
fi

# get current group
active=`cat "$dir/active"`

# help
usage() {
  say "usage: `basename $0` [-lbsmdarnpRA <gid>]\n\
  -l list groups\n\
  -b status bar info\n\
  -s show all\n\
  -m make group\n\
  -d delete group\n\
  -n next group\n\
  -p previous group\n\
  -a add current window\n\
  -A add by wid\n\
  -r remove current window\n\
  -R remove by wid"
}

say() {
  printf "$@\n" >&2
  exit 1
}

show() {
  while read wid
  do
    mapw -m "$wid"
  done <"$1"
}

hide() {
  while read wid
  do
    mapw -u "$wid"
  done <"$1"
}

bar() {
  b=" "
  for grp in `ls "$dir" | grep -E '[0-9]'`
  do
    if [ "$grp" == "$active" ]
    then
      b="$b ◆"
    else
      b="$b ◇"
    fi
  done
  printf "$b \n"
}

ingroup() {
  grep -q "$1" "$dir/$active"
}

pushpop() {
  if [ "$1" == "add" ]
  then
    if ingroup $2
    then
      say "error: window already exists in group"
    else
      printf "$2\n" >> "$dir/$active"
    fi
  else
    if ingroup $2
    then
      sed -i "/$2/d" "$dir/$active"
    else
      say "error: window does not exist in group"
    fi
  fi
}

while getopts "lbsmdarnpiR:A:" opt
do
  case $opt in
    # list
    l)
      tree $dir
    ;;
    # bar
    b)
      bar
    ;;
    # show all
    s)
      lsw -u | while read wid
      do
        mapw -m "$wid"
      done
    ;;
    # make group
    m)
      last=`ls -r "$dir" | grep -E '[0-9]' | head -n 1`
      next=$((last+1))
      cat "/dev/null" > "$dir/$next"
    ;;
    # delete group
    d)
      if [ `ls -r "$dir" | grep -E '[0-9]' | wc -l` -gt 1 ]
      then
        rm "$dir/$active"
        ls "$dir" | grep -E '[0-9]' | head -n 1 > "$dir/active"
      else
        say "there must be at least one group"
      fi
    ;;
    # next group
    n)
      hide "$dir/$active"
      for grp in `ls "$dir" | grep -E '[0-9]'`
      do
        if [ "$grp" -gt "$active" ]
        then
          printf "$grp\n" > "$dir/active"
          break
        fi
      done
      if [ "$grp" -eq "$active" ]
      then
        grp=`ls "$dir" | grep -E '[0-9]' | head -n 1`
        printf "$grp\n" > "$dir/active"
      fi
      active=`cat "$dir/active"`
      show "$dir/$active"
    ;;
    # previous group
    p)
      hide "$dir/$active"
      for grp in `ls -r "$dir" | grep -E '[0-9]'`
      do
        if [ "$grp" -lt "$active" ]
        then
          printf "$grp\n" > "$dir/active"
          break
        fi
      done
      if [ "$grp" -eq "$active" ]
      then
        grp=`ls -r "$dir" | grep -E '[0-9]' | head -n 1`
        printf "$grp\n" > "$dir/active"
      fi
      active=`cat "$dir/active"`
      show "$dir/$active"
    ;;
    # add window to group
    a)
      pushpop "add" `pfw`
    ;;
    # add window to group by id
    A)
      pushpop "add" $OPTARG
    ;;
    # remove window from group
    r)
      pushpop "rm" `pfw`
    ;;
    # remove window from group
    R)
      pushpop "rm" $OPTARG
    ;;
    # unknown option
    *)
      usage
    ;;
  esac
done

[ $# -eq 0 ] && usage