dotfiles

custom linux config files managed with gnu stow

dotfiles

wmutils/bin/groups.sh


#!/bin/sh
#
# Copyright (c) 2015 Greduan <me@greduan.com>, licensed under the WTFPL
# Adds group-like capabilities, sorta like those you find in CWM and such WMs

usage() {
    cat << EOF
usage: $(basename {{&blob}}) [-hCU] [-c wid] [-s wid group] [-tmMu group]
       -h shows this help
       -c cleans WID from group files (and makes it visible)
       -C runs cleanup routine
       -s sets WID's group
       -t toggle group visibility state
       -m maps (shows) group
       -M maps group and unmaps all other groups
       -u unmaps (hides) group
       -U unmaps all the groups
EOF

    exit 1
}

# test for no arguments
test $# -eq 0 && usage

# I suggest it's under /tmp or somewhere that gets cleaned up at reboot or gets
# cleaned up after X stops running
FSDIR=${FSDIR:-/tmp/groups.sh}

# define our functions

# clean WID () from group files
clean_wid() {
    # TODO: make POSIX compatible, -i is a GNU-ism
    sed -i "//d" $FSDIR/group.*
}

# cleans group () from (in)active files
clean_status() {
    # TODO: make POSIX compatible, -i is a GNU-ism
    sed -i "//d" $FSDIR/active
    sed -i "//d" $FSDIR/inactive
}

# shows all the windows in group ()
map_group() {
    # safety
    if ! grep -q  < $FSDIR/all; then
        echo "Group doesn't exist"
        exit 1
    fi

    # clean statuses
    clean_status 
    # add to active
    echo  >> $FSDIR/active

    # loop through group and map windows
    while read line; do
        mapw -m $line
    done < $FSDIR/group.
}

# hides all the windows in group ()
unmap_group() {
    # safety
    if ! grep -q  < $FSDIR/all; then
        echo "Group doesn't exist"
        exit 1
    fi

    # clean statuses
    clean_status 
    # add to inactive
    echo  >> $FSDIR/inactive

    # loop through group and unmap windows
    while read line; do
        mapw -u $line
    done < $FSDIR/group.
}

# assigns WID () to the group ()
set_group() {
    # make sure we've no duplicates
    clean_wid 
    clean_status 

    # insert WID into new group if not already there
    grep -q  < $FSDIR/group. || \
    echo  >> $FSDIR/group.

    # if we can't find the group add it to groups and make it active
    grep -q  < $FSDIR/all || \
    echo  >> $FSDIR/all && \
    echo  >> $FSDIR/active

    # map WID if group is active
    grep -q  < $FSDIR/active && \
    mapw -m 

    # unmap WID if group is inactive
    grep -q  < $FSDIR/inactive && \
    mapw -u 
}

# toggles visibility state of all the windows in group ()
toggle_group() {
    # safety
    if ! grep -q  < $FSDIR/all; then
        echo "Group doesn't exist"
        return
    fi

    # search through active groups first
    grep -q  < $FSDIR/active && \
    unmap_group  && \
    return

    # search through inactive groups next
    grep -q  < $FSDIR/inactive && \
    map_group  && \
    return
}

# removes all the unexistent WIDs from groups
# removes all group files that don't exist
# removes from 'all' file all groups that don't exist
cleanup_everything() {
    # clean WIDs that don't exist
    # using `cat` instead of `<` because error suppression
    cat $FSDIR/group.* 2>/dev/null | while read wid; do
        wattr $wid || \
        clean_wid $wid
    done

    # clean group files that are empty
    for file in $FSDIR/group.*; do
        # is the group empty?
        if [ ! -s $file ]; then
            rm -f $file
        fi
    done

    # remove groups that don't exist from 'all'
    while read line; do
        if [ ! -f $FSDIR/group.$line ]; then
            # TODO: make POSIX compatible, -i is a GNU-ism
            sed -i "/$line/d" $FSDIR/all
            clean_status $line
        fi
    done < $FSDIR/all
}

# actual run logic (including arguments and such)

# check $FSDIR exists
test -d $FSDIR || mkdir -p $FSDIR

# touch all the files
test -f $FSDIR/active || :> $FSDIR/active
test -f $FSDIR/inactive || :> $FSDIR/inactive
test -f $FSDIR/all || :> $FSDIR/all

cleanup_everything

# getopts yo
while getopts "hc:Cs:t:m:M:u:U" opt; do
    case $opt in
        h)
            usage
            ;;
        c)
            clean_wid $OPTARG
            mapw -m $OPTARG
            break
            ;;
        C)
            cleanup_everything
            break
            ;;
        s)
            set_group $OPTARG $(eval echo "$$OPTIND")
            break
            ;;
        t)
            toggle_group $OPTARG
            break
            ;;
        m)
            map_group $OPTARG
            break
            ;;
        M)
            for file in $FSDIR/group.*; do
                group=${file##*.}
                unmap_group $group
            done
            map_group $OPTARG
            break
            ;;
        u)
            unmap_group $OPTARG
            break
            ;;
        U)
            for file in $FSDIR/group.*; do
                group=${file##*.}
                unmap_group $group
            done
            break
            ;;
    esac
done

Download

raw zip tar