forked from
lass.pds.lassul.us/superconfig
This repository has no description
3.6 kB
154 lines
1#!/usr/bin/env bash
2# Workspace Menu for workspace management
3
4set -euo pipefail
5
6WORKSPACES_DIR="${HOME}/workspaces"
7mkdir -p "$WORKSPACES_DIR"
8
9get_current_workspace() {
10 swaymsg -r -t get_workspaces | jq -r '.[] | select(.focused == true).name'
11}
12
13# Restore browser tabs from saved config (opens a new Firefox window with saved tabs)
14action_restore() {
15 local ws_name
16 ws_name=$(get_current_workspace)
17 local config_file="${WORKSPACES_DIR}/${ws_name}.json"
18
19 if [ ! -f "$config_file" ]; then
20 notify-send "Workspace Manager" "No config found for workspace '${ws_name}'"
21 return 1
22 fi
23
24 local tab_count
25 tab_count=$(jq '.tabs // [] | length' "$config_file" 2>/dev/null)
26
27 if [ "$tab_count" -eq 0 ]; then
28 notify-send "Workspace Manager" "No saved tabs for workspace '${ws_name}'"
29 return 0
30 fi
31
32 # Open each saved tab URL in Firefox
33 local urls
34 urls=$(jq -r '.tabs // [] | .[].url' "$config_file" 2>/dev/null)
35
36 local first=true
37 while IFS= read -r url; do
38 [ -z "$url" ] && continue
39 if [ "$first" = true ]; then
40 firefox --new-window "$url" &
41 first=false
42 sleep 1
43 else
44 firefox "$url" &
45 fi
46 done <<< "$urls"
47
48 notify-send "Workspace Manager" "Restored ${tab_count} tabs for workspace '${ws_name}'"
49}
50
51# Close workspace: kill all windows
52action_close() {
53 local ws_name
54 ws_name=$(get_current_workspace)
55
56 local confirm
57 confirm=$(printf "Yes\nNo" | menu -p "Close workspace '${ws_name}'? (kills all windows)")
58 if [ "$confirm" != "Yes" ]; then
59 return 0
60 fi
61
62 local pids
63 pids=$(swaymsg -r -t get_tree | jq --arg ws "$ws_name" '
64 [.nodes[].nodes[] |
65 select(.name == $ws) |
66 .. |
67 select(.type? == "con" and .pid? != null) |
68 .pid] | unique | .[]
69 ')
70
71 for pid in $pids; do
72 kill "$pid" 2>/dev/null || true
73 done
74
75 notify-send "Workspace Manager" "Closed workspace '${ws_name}'"
76}
77
78# Delete a workspace configuration
79action_delete() {
80 local configs
81 configs=$(find "$WORKSPACES_DIR" -name '*.json' -printf '%f\n' | sed 's/\.json$//' | sort)
82
83 if [ -z "$configs" ]; then
84 notify-send "Workspace Manager" "No workspace configs found"
85 return 0
86 fi
87
88 local selected
89 selected=$(echo "$configs" | menu -p "Delete workspace config")
90 if [ -z "$selected" ]; then
91 return 0
92 fi
93
94 local confirm
95 confirm=$(printf "Yes\nNo" | menu -p "Delete config for '${selected}'?")
96 if [ "$confirm" = "Yes" ]; then
97 rm "${WORKSPACES_DIR}/${selected}.json"
98 notify-send "Workspace Manager" "Deleted config for '${selected}'"
99 fi
100}
101
102# Edit a workspace configuration
103action_edit() {
104 local ws_name
105 ws_name=$(get_current_workspace)
106
107 local configs
108 configs=$(find "$WORKSPACES_DIR" -name '*.json' -printf '%f\n' | sed 's/\.json$//' | sort)
109
110 if ! echo "$configs" | grep -qx "$ws_name"; then
111 configs="${ws_name}"$'\n'"${configs}"
112 fi
113
114 local selected
115 selected=$(echo "$configs" | menu -p "Edit workspace config")
116 if [ -z "$selected" ]; then
117 return 0
118 fi
119
120 local config_file="${WORKSPACES_DIR}/${selected}.json"
121
122 if [ ! -f "$config_file" ]; then
123 echo '{}' > "$config_file"
124 fi
125
126 swaymsg exec "${TERMINAL:-kitty} ${EDITOR:-vim} $config_file"
127}
128
129# Main menu
130main() {
131 local ws_name
132 ws_name=$(get_current_workspace)
133
134 local choice
135 choice=$(printf "Restore tabs\nClose workspace\nEdit config\nDelete config" | \
136 menu -p "Workspace: ${ws_name}")
137
138 case "$choice" in
139 "Restore tabs")
140 action_restore
141 ;;
142 "Close workspace")
143 action_close
144 ;;
145 "Edit config")
146 action_edit
147 ;;
148 "Delete config")
149 action_delete
150 ;;
151 esac
152}
153
154main