[go: up one dir, main page]

Skip to content

Commit

Permalink
group: add smart toggle
Browse files Browse the repository at this point in the history
  • Loading branch information
lwilletts committed Jul 4, 2020
1 parent 48804f7 commit ea188d3
Showing 1 changed file with 33 additions and 10 deletions.
43 changes: 33 additions & 10 deletions group
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ Usage:
$ $base -A | scr \$group : Add all windows on screen to \$group.
$ $base -d | del \$wid : Delete \$wid from all groups.
$ $base -t | tog \$group : Toggle \$group visibility.
$ $base -s | smart \$group : Toggle \$group visibility smartly.
$ $base -l | list : List all windows in groups.
$ $base -h | help : Show this help.
EOF
Expand All @@ -31,6 +32,9 @@ sort_groups() {
}

show_group() {
# focus window in group that is at the top
PFW="$(list_groups | grep "GROUP_$1" | tail -n 1 | cut -d\ -f 2)"

for wid in $(lsw -u); do
wg=$(atomx GROUP "$wid")
[ -z "$wg" ] && continue
Expand All @@ -40,8 +44,7 @@ show_group() {
}
done

# focus window in group that is at the top
focus -w "$(list_groups | grep "GROUP_$1" | tail -n 1 | cut -d\ -f 2)"
focus -w "$PFW"

# set group to active
atomx GROUP_"$1"=on "$ROOT" > /dev/null
Expand All @@ -65,7 +68,26 @@ toggle_group() {
case $(atomx GROUP_"$1" "$ROOT") in
on) hide_group "$1" ;;
off) show_group "$1" ;;
*) atomx GROUP_"$1"=on "$ROOT" > /dev/null ;;
esac
}

smart_toggle_group() {
case $(atomx GROUP_"$1" "$ROOT") in
on)
windows="$(list_groups | grep "GROUP_$1" | cut -d\ -f 2)"

for wid in $windows; do
if [ "$wid" = "$PFW" ]; then
hide_group "$1"
return
fi
done

focus -w "$(printf '%s\n' "$windows" | tail -n 1)"
;;
off)
show_group "$1"
;;
esac
}

Expand Down Expand Up @@ -96,13 +118,14 @@ main() {
ROOT="$(lsw -r)"

case $1 in
-a|--add|add) add_group "$2" "$3" ;;
-A|--scr|scr) add_screen "$2" ;;
-d|--del|del) atomx -d GROUP "$2" ;;
-t|--tog|tog) toggle_group "$2" ;;
-l|--list|list) sort_groups ;;
-h|--help|help) usage 0 ;;
*) usage 1 ;;
-a|--add|add) add_group "$2" "$3" ;;
-A|--scr|scr) add_screen "$2" ;;
-d|--del|del) atomx -d GROUP "$2" ;;
-t|--tog|tog) toggle_group "$2" ;;
-s|--smart|smart) smart_toggle_group "$2" ;;
-l|--list|list) sort_groups ;;
-h|--help|help) usage 0 ;;
*) usage 1 ;;
esac
}

Expand Down

0 comments on commit ea188d3

Please sign in to comment.