dots

Personal dotfiles
git clone git://git.gormless.xyz/dots.git
Log | Files | Refs

gruup (5222B)


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