summaryrefslogtreecommitdiff
path: root/bin/rws
blob: 198bf4b19ae4c20bf98fa5d5893927ee7d1c4772 (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
#!/bin/sh

# rebecca workspace
#
# source this from your shell rc file
# and also start rws -listen when you start sway


WSD=$XDG_RUNTIME_DIR/ws
mkdir -p $WSD

wslist () {
	# list workspaces, most-recently-used first
	swaymsg -t get_workspaces | jq -rc '.[] | .name' | while read wsn; do
		wsp=$WSD/"$(printf %s "$wsn" | base64)"
		dt="$(stat -c '%Y' $wsp 2>/dev/null)"
		if [ $? -eq 1 ]; then
			dt=1
		fi
		printf "%s\t%s\n" "$dt" "$wsn"
	done | sort -nr | cut -f2-
}

_escape() {
	printf %s "$1" | sed -e 's*\\*\\\\*g' -e 's*"*\\"*g'
}

workin () {
	# cds to the given directory, then moves focused window to workspace
	# with that name
	if [ ! -d "$1" ]; then
		printf "not a directory\n" >&2
		return 1
	else
		cd "$1"
		dirname="$(_escape "$(apwd)")"
		swaymsg "move container to workspace \"$dirname\""
		swaymsg "workspace \"$dirname\""
	fi
}


curws () {
	# prints name of current workspace
	swaymsg -t get_workspaces \
		| jq -rc '.[] | select(.focused==true) | .name'
}
gowork() {
	# cds to working directory for current workspace
	d="$(curws|sed -e "s*^~*$HOME*")"
	if [ -d "$d" ]; then
		cd "$d" || return 1
	fi
}
renws () {
	# renames current ws to current working dir
	swaymsg "rename workspace to $(apwd)"
}

queryws () {
	# uses bmenu to prompt for a workspace name, then switches to that workspace
	# or if -m given, moves focused window to that workspace instead
	wsn="$(wslist | bemenu -p workspace -l100 --fn 'Iosevka Fixed 16' --tf '#df73ff' --hf '#df73ff' --hb '#333333')"
	[ -z "$wsn" ] && return

	if [ "$1" = "-m" ]; then
		swaymsg "move container to workspace \"$(_escape "$wsn")\""
	else
		swaymsg "workspace \"$(_escape "$wsn")\""
	fi
}


_onswitch () {
	wsb=$(printf %s "$1" | base64)
	touch ${WSD}/$wsb
}
# run rws -listen to listen for workspace changes,
# and keep track of that
if [ "$1" = "-listen" ]; then
	swaymsg -rmt subscribe '[ "workspace" ]' | while read line; do
		wsn="$(printf %s "$line" | jq -rc '.current.name')"
		printf "%s\n" "$wsn"
		_onswitch "$wsn"
	done
fi

# rws -queryws does the same thing as queryws
#  because it needs to be runnable from sway cfg in order to keybind something for it
if [ "$1" = "-queryws" ]; then
	shift
	queryws $@
fi