toggles: refactor to have config

Config is in $XDG_CONFIG_HOME/caelestia/scripts.json
Selector is a jq selector using hyprland client data
Spawn is a command passed to uwsm app
Action is either spawn, move, or both
Extra cond is an extra shell command to check whether to do action (for easy disabling just put false or empty action)
This commit is contained in:
2 * r + 2 * t
2025-04-03 21:41:26 +11:00
parent 9464e34727
commit f8a6e84aa9
8 changed files with 107 additions and 40 deletions
+51
View File
@@ -0,0 +1,51 @@
{
"toggles": {
"communication": {
"apps": [
{
"selector": ".class == \"equibop\"",
"spawn": "equibop",
"action": "spawn move"
},
{
"selector": ".class == \"whatsapp\"",
"spawn": "firefox --name whatsapp -P whatsapp 'https://web.whatsapp.com'",
"action": "move",
"extraCond": "grep -q 'Name=whatsapp' ~/.mozilla/firefox/profiles.ini"
}
]
},
"music": {
"apps": [
{
"selector": ".class == \"Spotify\" or .initialTitle == \"Spotify\" or .initialTitle == \"Spotify Free\"",
"spawn": "spotify-adblock.desktop",
"action": "spawn move"
},
{
"selector": ".class == \"feishin\"",
"spawn": "feishin",
"action": "move"
}
]
},
"sysmon": {
"apps": [
{
"selector": ".class == \"btop\" and .title == \"btop\" and .workspace.name == \"special:sysmon\"",
"spawn": "foot -a 'btop' -T 'btop' -- btop",
"action": "spawn"
}
]
},
"todo": {
"apps": [
{
"selector": ".class == \"Todoist\"",
"spawn": "todoist",
"action": "spawn move"
}
]
}
}
}
+11 -1
View File
@@ -20,7 +20,17 @@ end
if test "$argv[1]" = toggle
set -l valid_toggles communication music sysmon specialws todo
contains -- "$argv[2]" $valid_toggles && $src/toggles/$argv[2].fish || error "Invalid toggle: $argv[2]"
if contains -- "$argv[2]" $valid_toggles
if $argv[2] = specialws
$src/toggles/specialws.fish
else
. $src/toggles/util.fish
toggle-workspace $argv[2]
end
else
error "Invalid toggle: $argv[2]"
end
exit
end
-12
View File
@@ -1,12 +0,0 @@
#!/bin/fish
. (dirname (status filename))/util.fish
spawn-or-move '.class == "equibop"' communication equibop
# Has whatsapp firefox profile
if grep -q 'Name=whatsapp' ~/.mozilla/firefox/profiles.ini
spawn-or-move '.class == "whatsapp"' communication firefox --name whatsapp -P whatsapp 'https://web.whatsapp.com'
end
hyprctl dispatch togglespecialworkspace communication
-8
View File
@@ -1,8 +0,0 @@
#!/bin/fish
. (dirname (status filename))/util.fish
move-client '.class == "feishin"' music
move-client '.class == "Spotify" or .initialTitle == "Spotify" or .initialTitle == "Spotify Free"' music
hyprctl dispatch togglespecialworkspace music
-7
View File
@@ -1,7 +0,0 @@
#!/bin/fish
. (dirname (status filename))/util.fish
spawn-client '.class == "btop" and .title == "btop" and .workspace.name == "special:sysmon"' foot -a 'btop' -T 'btop' -- btop
hyprctl dispatch togglespecialworkspace sysmon
-7
View File
@@ -1,7 +0,0 @@
#!/bin/fish
. (dirname (status filename))/util.fish
spawn-or-move '.class == "Todoist"' todo todoist
hyprctl dispatch togglespecialworkspace todo
+26 -5
View File
@@ -1,4 +1,6 @@
function move-client -a selector -a workspace
. (dirname (status filename))/../util.fish
function move-client -a selector workspace
if hyprctl -j clients | jq -e 'first(.[] | select('$selector')).workspace.name != "special:'$workspace'"' > /dev/null
# Window not in correct workspace
set -l window_addr (hyprctl -j clients | jq -r 'first(.[] | select('$selector')).address')
@@ -6,16 +8,35 @@ function move-client -a selector -a workspace
end
end
function spawn-client -a selector
function spawn-client -a selector spawn
# Spawn if doesn't exist
hyprctl -j clients | jq -e "first(.[] | select($selector))" > /dev/null
set -l stat $status
if test $stat != 0
uwsm app -- $argv[2..] & disown
eval "uwsm app -- $spawn & disown"
end
test $stat != 0 # Exit 1 if already exists
end
function spawn-or-move -a selector -a workspace
spawn-client $selector $argv[3..] || move-client $selector $workspace
function jq-var -a op json
jq -rn --argjson json "$json" "\$json | $op"
end
function toggle-workspace -a workspace
set -l apps (get-config "toggles.$workspace.apps")
for i in (seq 0 (math (jq-var 'length' "$apps") - 1))
set -l app (jq-var ".[$i]" "$apps")
set -l action (jq-var '.action' "$app")
set -l selector (jq-var '.selector' "$app")
set -l extra_cond (jq-var '.extraCond' "$app")
test $extra_cond = null && set -l extra_cond true
if eval $extra_cond
string match -qe -- 'spawn' $action && spawn-client $selector (jq-var '.spawn' "$app")
string match -qe -- 'move' $action && move-client $selector $workspace
end
end
hyprctl dispatch togglespecialworkspace $workspace
end
+19
View File
@@ -22,11 +22,30 @@ function input -a text
_out blue INPUT $text $argv[2..]
end
function get-config -a key
test -f $C_CONFIG_FILE && set -l value (jq -r ".$key" $C_CONFIG_FILE)
test -n "$value" -a "$value" != null && echo $value || jq -r ".$key" (dirname (status filename))/data/config.json
end
function set-config -a key value
if test -f $C_CONFIG_FILE
set -l tmp (mktemp)
cp $C_CONFIG_FILE $tmp
jq -e ".$key = $value" $tmp > $C_CONFIG_FILE || cp $tmp $C_CONFIG_FILE
rm $tmp
else
jq -en ".$key = $value" > $C_CONFIG_FILE || rm $C_CONFIG_FILE
end
end
set -q XDG_DATA_HOME && set C_DATA $XDG_DATA_HOME/caelestia || set C_DATA $HOME/.local/share/caelestia
set -q XDG_STATE_HOME && set C_STATE $XDG_STATE_HOME/caelestia || set C_STATE $HOME/.local/state/caelestia
set -q XDG_CACHE_HOME && set C_CACHE $XDG_CACHE_HOME/caelestia || set C_CACHE $HOME/.cache/caelestia
set -q XDG_CONFIG_HOME && set CONFIG $XDG_CONFIG_HOME || set CONFIG $HOME/.config
set C_CONFIG $CONFIG/caelestia
set C_CONFIG_FILE $C_CONFIG/scripts.json
mkdir -p $C_DATA
mkdir -p $C_STATE
mkdir -p $C_CACHE
mkdir -p $C_CONFIG