commit e51feaa

hovercats  ·  2022-02-18 21:35:47 +0000 UTC
parent 3965975
add workspace.sh
1 files changed,  +110, -0
+110, -0
  1@@ -0,0 +1,110 @@
  2+#!/bin/sh
  3+
  4+# Original, unmodified script can be found at https://github.com/wmutils/contrib
  5+
  6+# Constructs and groups windows into workspaces to switch between using your favorite keybinder.
  7+
  8+NUM_WS=9
  9+
 10+help() {
 11+	cat << EOF
 12+usage: $(basename "$0") [-hinp] [-g ws_num] [-m ws_num]
 13+	-h: Displays this message
 14+	-i: Initialize workspaces. Should be called once in a startup script.
 15+	-n: Move up one workspace
 16+	-p: Move down one workspace
 17+	-g, <ws_num>: go to the workspace specified by <ws_num>
 18+	-m, <ws_num>: move the currently focused window to the worskpace specified by <ws_num>
 19+EOF
 20+	exit 1
 21+}
 22+# Initializes us in workspace 0.
 23+ws_init() {
 24+	mkdir -p /tmp/workspaces/
 25+	i=0
 26+	while [ $i -le $NUM_WS ]; do
 27+		:> /tmp/workspaces/ws"$i"
 28+		i=$(echo $i + 1 | bc)
 29+	done
 30+	echo 0 > /tmp/workspaces/curr
 31+}
 32+# Saves all mapped windows to the current workspace.
 33+save_ws() {
 34+	curr=$(cat /tmp/workspaces/curr)
 35+	lsw > /tmp/workspaces/ws"$curr"
 36+}
 37+move_to_ws() {
 38+	ws_num=$1
 39+	if [ "$ws_num" -gt $NUM_WS ] || [ "$ws_num" -lt 0 ]; then
 40+		echo "Workspace not found"
 41+		return
 42+	fi
 43+	curr_ws=$(cat /tmp/workspaces/curr);
 44+	if [ "$ws_num" = "$curr_ws" ]; then
 45+		# same workspace. ignore flicker
 46+		return
 47+	fi
 48+	save_ws
 49+	curr_windows=$(lsw)
 50+	if [ "$curr_windows" ]; then
 51+		mapw -u "$(lsw)"
 52+	fi
 53+	new_windows="$(cat /tmp/workspaces/ws"$ws_num")"
 54+	if [ "$new_windows" ]; then
 55+		mapw -m "$new_windows"
 56+	fi
 57+	echo "$ws_num" > /tmp/workspaces/curr
 58+}
 59+next_ws() {
 60+	# Get what ws we're currently in.
 61+	curr=$(cat /tmp/workspaces/curr)
 62+	curr=$(echo "$curr" + 1 | bc)
 63+
 64+	# Take care of loopback.
 65+	if [ "$curr" -gt $NUM_WS ]; then
 66+		curr=0
 67+	fi
 68+
 69+	move_to_ws $curr
 70+}
 71+prev_ws() {
 72+	# Get what ws we're currently in.
 73+	curr=$(cat /tmp/workspaces/curr)
 74+	curr=$(echo "$curr" - 1 | bc)
 75+
 76+	# Take care of loopback.
 77+	if [ "$curr" -lt 0 ]; then
 78+		curr=$NUM_WS
 79+	fi
 80+
 81+	move_to_ws "$curr"
 82+}
 83+move_focused_window() {
 84+	ws_num=$1
 85+	if [ "$ws_num" -gt "$NUM_WS" ] || [ "$ws_num" -lt 0 ]; then
 86+		echo "Workspace not found"
 87+		return
 88+	fi
 89+	wid=$(pfw)
 90+	curr_ws=$(cat /tmp/workspaces/curr);
 91+	if [ "$ws_num" != "$curr_ws" ]; then
 92+		pfw >> /tmp/workspaces/ws"$1"
 93+		mapw -u "$wid"
 94+	fi
 95+}
 96+while getopts ":m:g:npi" opt; do
 97+	case $opt in
 98+		n)
 99+			next_ws;;
100+		p)
101+			prev_ws;;
102+		g)
103+			move_to_ws "$OPTARG";;
104+		m)
105+			move_focused_window "$OPTARG";;
106+		i)
107+			ws_init;;
108+		\?)
109+			help;;
110+	esac
111+done