Swayscript: Rewrite to replaceVars.

This commit is contained in:
Dmitry Voronin 2024-12-18 11:11:25 +03:00
parent 0163b0534c
commit b05a3504a4
Signed by: voronind
SSH key fingerprint: SHA256:3kBb4iV2ahufEBNq+vFbUe4QYfHt98DHQjN7QaptY9k
17 changed files with 308 additions and 326 deletions

View file

@ -1,2 +1,23 @@
{ pkgs, util, ... }@args: {
pkgs.writeShellScriptBin "swayscript" (util.catText (util.ls ./script) args + "\${@}") __findFile,
pkgs,
util,
...
}:
let
pipewire = pkgs.pipewire;
longogg = <static/Long.ogg>;
notificationogg = <static/Notification.ogg>;
shortogg = <static/Short.ogg>;
raw = pkgs.writeText "swayscript-raw" (util.catContent (util.ls ./script));
script = pkgs.replaceVars raw {
inherit
pipewire
longogg
notificationogg
shortogg
;
};
in
pkgs.writeShellScriptBin "swayscript" (builtins.readFile script + "\n\${@}")

View file

@ -1,126 +0,0 @@
{ ... }:
{
text = ''
function monitor() {
notify_short
toggle() {
local output=$(swaymsg -t get_outputs | jq -r '.[] | select(.focused) | .name')
local state=$(swaymsg -t get_outputs | jq -r '.[] | select(.focused) | .power')
if ''${state}; then
swaymsg "output \"''${output}\" power off"
else
swaymsg "output \"''${output}\" power on"
fi
pkill -RTMIN+4 waybar
}
_sway_iterate_sockets toggle
}
function monitorreset() {
swaymsg 'output * power on'
pkill -RTMIN+4 waybar
}
function gaming() {
notify_short
toggle() {
local output=$(swaymsg -t get_outputs | jq -r '.[] | select(.focused) | .name')
local state=$(swaymsg -t get_outputs | jq -r '.[] | select(.focused) | .adaptive_sync_status')
if [[ "''${state}" = "disabled" ]]; then
swaymsg "output \"''${output}\" adaptive_sync on"
else
swaymsg "output \"''${output}\" adaptive_sync off"
fi
pkill -RTMIN+4 waybar
}
_sway_iterate_sockets toggle
}
function gamingreset() {
swaymsg 'output * adaptive_sync off'
pkill -RTMIN+4 waybar
}
function dnd() {
toggle() {
local state=$(makoctl mode)
if [[ "''${state}" = "dnd" ]]; then
makoctl mode -s default
notify_short
else
notify_short
makoctl mode -s dnd
fi
pkill -RTMIN+4 waybar
}
_sway_iterate_sockets toggle
}
# Reset the state of everything.
function displayreset() {
notify_long
[[ "''$(monitorstate)" = "Y" ]] && monitorreset
[[ "''$(gamingstate)" = "Y" ]] && gamingreset
[[ "''$(recordingstate)" = "Y" ]] && pkill wf-recorder
[[ "''$(dndstate)" = "Y" ]] && dnd
true
}
# Waybar output.
function displaywidget() {
local _monitorstate=$(monitorstate)
local _gamingstate=$(gamingstate)
local _recordingstate=$(recordingstate)
local _dndstate=$(dndstate)
local class=""
if [[ "''${_monitorstate}" = "Y" ]] || [[ "''${_gamingstate}" = "Y" ]] || [[ "''${_recordingstate}" = "Y" ]] || [[ "''${_dndstate}" = "Y" ]]; then
class="modified"
fi
printf "%s" "{\"text\": \"󰍹\", \"tooltip\": \"󰍶 ''${_dndstate}\\t󰶐 ''${_monitorstate}\\n󰡈 ''${_gamingstate}\\t󰻂 ''${_recordingstate}\", \"class\": \"''${class}\"}\n"
}
function monitorstate() {
local outputs=($(swaymsg -t get_outputs | jq -r '.[] | .power'))
for state in "''${outputs[@]}"; do
''${state} || {
printf "%s" Y
return 1
}
done
printf "%s" n
return 0
}
function recordingstate() {
[[ "$(ps cax | rg wf-recorder)" = "" ]] && printf "%s" n || printf "%s" Y
}
function dndstate() {
[[ "$(makoctl mode)" = "dnd" ]] && printf "%s" Y || printf "%s" n
}
function gamingstate() {
local outputs=($(swaymsg -t get_outputs | jq -r '.[] | .adaptive_sync_status'))
for state in "''${outputs[@]}"; do
[[ "''${state}" = "disabled" ]] || {
printf "%s" Y
return 1
}
done
printf "%s" n
return 0
}
'';
}

View file

@ -0,0 +1,121 @@
function monitor() {
notify_short
toggle() {
local output=$(swaymsg -t get_outputs | jq -r '.[] | select(.focused) | .name')
local state=$(swaymsg -t get_outputs | jq -r '.[] | select(.focused) | .power')
if ${state}; then
swaymsg "output \"${output}\" power off"
else
swaymsg "output \"${output}\" power on"
fi
pkill -RTMIN+4 waybar
}
_sway_iterate_sockets toggle
}
function monitorreset() {
swaymsg 'output * power on'
pkill -RTMIN+4 waybar
}
function gaming() {
notify_short
toggle() {
local output=$(swaymsg -t get_outputs | jq -r '.[] | select(.focused) | .name')
local state=$(swaymsg -t get_outputs | jq -r '.[] | select(.focused) | .adaptive_sync_status')
if [[ ${state} == "disabled" ]]; then
swaymsg "output \"${output}\" adaptive_sync on"
else
swaymsg "output \"${output}\" adaptive_sync off"
fi
pkill -RTMIN+4 waybar
}
_sway_iterate_sockets toggle
}
function gamingreset() {
swaymsg 'output * adaptive_sync off'
pkill -RTMIN+4 waybar
}
function dnd() {
toggle() {
local state=$(makoctl mode)
if [[ ${state} == "dnd" ]]; then
makoctl mode -s default
notify_short
else
notify_short
makoctl mode -s dnd
fi
pkill -RTMIN+4 waybar
}
_sway_iterate_sockets toggle
}
# Reset the state of everything.
function displayreset() {
notify_long
[[ "$(monitorstate)" == "Y" ]] && monitorreset
[[ "$(gamingstate)" == "Y" ]] && gamingreset
[[ "$(recordingstate)" == "Y" ]] && pkill wf-recorder
[[ "$(dndstate)" == "Y" ]] && dnd
true
}
# Waybar output.
function displaywidget() {
local _monitorstate=$(monitorstate)
local _gamingstate=$(gamingstate)
local _recordingstate=$(recordingstate)
local _dndstate=$(dndstate)
local class=""
if [[ ${_monitorstate} == "Y" ]] || [[ ${_gamingstate} == "Y" ]] || [[ ${_recordingstate} == "Y" ]] || [[ ${_dndstate} == "Y" ]]; then
class="modified"
fi
printf "%s" "{\"text\": \"󰍹\", \"tooltip\": \"󰍶 ${_dndstate}\\t󰶐 ${_monitorstate}\\n󰡈 ${_gamingstate}\\t󰻂 ${_recordingstate}\", \"class\": \"${class}\"}\n"
}
function monitorstate() {
local outputs=($(swaymsg -t get_outputs | jq -r '.[] | .power'))
for state in "${outputs[@]}"; do
${state} || {
printf "%s" Y
return 1
}
done
printf "%s" n
return 0
}
function recordingstate() {
[[ "$(ps cax | rg wf-recorder)" == "" ]] && printf "%s" n || printf "%s" Y
}
function dndstate() {
[[ "$(makoctl mode)" == "dnd" ]] && printf "%s" Y || printf "%s" n
}
function gamingstate() {
local outputs=($(swaymsg -t get_outputs | jq -r '.[] | .adaptive_sync_status'))
for state in "${outputs[@]}"; do
[[ ${state} == "disabled" ]] || {
printf "%s" Y
return 1
}
done
printf "%s" n
return 0
}

View file

@ -1,80 +0,0 @@
{ ... }:
{
text = ''
# Waybar output.
function networkwidget() {
local IFS=$'\n'
local internet=$(nmcli networking connectivity check)
local _ethernets=($(nmcli connection show --active | rg ethernet | sed "s/ .*//"))
local _vpns=($(nmcli connection show --active | rg vpn | sed "s/ .*//"))
local _wifis=($(nmcli connection show --active | rg wifi | sed "s/ .*//"))
local _bts_raw=($(bluetoothctl devices Connected))
local _bts=()
local _bt_lowest=100
local icon="󰖩"
local class=""
for bt in ''${_bts_raw[@]}; do
local name=$(printf "%s" ''${bt} | cut -d\ -f3)
local mac=$(printf "%s" ''${bt} | cut -d\ -f2 | sed -e "s/:/_/g")
local bat=$(dbus-send --print-reply=literal --system --dest=org.bluez /org/bluez/hci0/dev_''${mac} org.freedesktop.DBus.Properties.Get string:"org.bluez.Battery1" string:"Percentage" 2> /dev/null | cut -d\ -f12)
local btinfo="''${name}"
if [[ "''${bat}" != "" ]]; then
btinfo+=" ''${bat}%"
[[ ''${bat} -lt ''${_bt_lowest} ]] && _bt_lowest=bat
fi
_bts+=("''${btinfo}")
done
if [[ "''${_bts}" != "" ]]; then
icon="󱛃"
if [[ "''${_bt_lowest}" != "" ]] && [[ ''${_bt_lowest} -lt 21 ]]; then
class="btlow"
fi
fi
if [[ "''${_vpns}" != "" ]]; then
class="vpn"
icon="󱚿"
fi
if [[ "''${internet}" != "full" ]]; then
class="issue"
icon="󱚵"
fi
for net in ''${_vpns[@]}; do
networks+=" ''${net}\\n"
done
for net in ''${_ethernets[@]}; do
networks+=" ''${net}\\n"
done
for net in ''${_wifis[@]}; do
networks+="󰖩 ''${net}\\n"
done
for bt in ''${_bts[@]}; do
networks+="󰂯 ''${bt}\\n"
done
networks=''${networks%\\n}
printf "%s" "{\"text\": \"''${icon}\", \"tooltip\": \"''${networks}\", \"class\": \"''${class}\"}\n"
}
# Toggle network.
function network() {
notify_short
local state=$(nmcli networking)
if [[ "''${state}" = "enabled" ]]; then
nmcli networking off
else
nmcli networking on
fi
}
'';
}

View file

@ -0,0 +1,75 @@
# Waybar output.
function networkwidget() {
local IFS=$'\n'
local internet=$(nmcli networking connectivity check)
local _ethernets=($(nmcli connection show --active | rg ethernet | sed "s/ .*//"))
local _vpns=($(nmcli connection show --active | rg vpn | sed "s/ .*//"))
local _wifis=($(nmcli connection show --active | rg wifi | sed "s/ .*//"))
local _bts_raw=($(bluetoothctl devices Connected))
local _bts=()
local _bt_lowest=100
local icon="󰖩"
local class=""
for bt in ${_bts_raw[@]}; do
local name=$(printf "%s" ${bt} | cut -d\ -f3)
local mac=$(printf "%s" ${bt} | cut -d\ -f2 | sed -e "s/:/_/g")
local bat=$(dbus-send --print-reply=literal --system --dest=org.bluez /org/bluez/hci0/dev_${mac} org.freedesktop.DBus.Properties.Get string:"org.bluez.Battery1" string:"Percentage" 2>/dev/null | cut -d\ -f12)
local btinfo="${name}"
if [[ ${bat} != "" ]]; then
btinfo+=" ${bat}%"
[[ ${bat} -lt ${_bt_lowest} ]] && _bt_lowest=bat
fi
_bts+=("${btinfo}")
done
if [[ ${_bts} != "" ]]; then
icon="󱛃"
if [[ ${_bt_lowest} != "" ]] && [[ ${_bt_lowest} -lt 21 ]]; then
class="btlow"
fi
fi
if [[ ${_vpns} != "" ]]; then
class="vpn"
icon="󱚿"
fi
if [[ ${internet} != "full" ]]; then
class="issue"
icon="󱚵"
fi
for net in ${_vpns[@]}; do
networks+="${net}\\n"
done
for net in ${_ethernets[@]}; do
networks+="${net}\\n"
done
for net in ${_wifis[@]}; do
networks+="󰖩 ${net}\\n"
done
for bt in ${_bts[@]}; do
networks+="󰂯 ${bt}\\n"
done
networks=${networks%\\n}
printf "%s" "{\"text\": \"${icon}\", \"tooltip\": \"${networks}\", \"class\": \"${class}\"}\n"
}
# Toggle network.
function network() {
notify_short
local state=$(nmcli networking)
if [[ ${state} == "enabled" ]]; then
nmcli networking off
else
nmcli networking on
fi
}

View file

@ -1,19 +0,0 @@
{ __findFile, pkgs, ... }:
{
text = ''
function notify() {
[[ "''$(dndstate)" = "Y" ]] && return
${pkgs.pipewire}/bin/pw-cat -p ${<static/Notification.ogg>} &
}
function notify_short() {
[[ "''$(dndstate)" = "Y" ]] && return
${pkgs.pipewire}/bin/pw-cat -p ${<static/Short.ogg>} &
}
function notify_long() {
[[ "''$(dndstate)" = "Y" ]] && return
${pkgs.pipewire}/bin/pw-cat -p ${<static/Long.ogg>} &
}
'';
}

View file

@ -0,0 +1,14 @@
function notify() {
[[ "$(dndstate)" == "Y" ]] && return
@pipewire@/bin/pw-cat -p @notificationogg@ &
}
function notify_short() {
[[ "$(dndstate)" == "Y" ]] && return
@pipewire@/bin/pw-cat -p @shortogg@ &
}
function notify_long() {
[[ "$(dndstate)" == "Y" ]] && return
@pipewire@/bin/pw-cat -p @longogg@ &
}

View file

@ -1,31 +0,0 @@
{ ... }:
{
text = ''
# Reload.
function reload() {
# notify_long
re() {
# Sway.
swaymsg reload
# Waybar.
pkill waybar
swaymsg exec waybar
# Tmux.
tmux source-file ~/.config/tmux/tmux.conf
# Bash.
pkill -SIGUSR1 bash
# Notifications.
makoctl reload
# Reset displays.
displayreset
}
_sway_iterate_sockets re
}
'';
}

View file

@ -0,0 +1,26 @@
# Reload.
function reload() {
# notify_long
re() {
# Sway.
swaymsg reload
# Waybar.
pkill waybar
swaymsg exec waybar
# Tmux.
tmux source-file ~/.config/tmux/tmux.conf
# Bash.
pkill -SIGUSR1 bash
# Notifications.
makoctl reload
# Reset displays.
displayreset
}
_sway_iterate_sockets re
}

View file

@ -1,15 +0,0 @@
{ ... }:
{
text = ''
# Kill all windows in Sway scratchpad.
function scratchpad_kill() {
# notify_short
kill() {
for window in $(swaymsg -t get_tree | jq -r 'recurse(.nodes[]?) | select(.name == "__i3_scratch").floating_nodes[].id'); do
swaymsg [ con_id="$window" ] kill
done
}
_sway_iterate_sockets kill
}
'';
}

View file

@ -0,0 +1,10 @@
# Kill all windows in Sway scratchpad.
function scratchpad_kill() {
# notify_short
kill() {
for window in $(swaymsg -t get_tree | jq -r 'recurse(.nodes[]?) | select(.name == "__i3_scratch").floating_nodes[].id'); do
swaymsg [ con_id="$window" ] kill
done
}
_sway_iterate_sockets kill
}

View file

@ -1,18 +0,0 @@
{ ... }:
{
text = ''
function sound_output_cycle() {
notify_short
local IFS=$'\n'
local current=$(pactl get-default-sink)
local all=($(pactl list short sinks | cut -f2))
local i_current=$(_index_of ''${current} ''${all[@]})
local i_total=''${#all[@]}; ((i_total--))
local i_target=0
[[ "''${i_current}" -lt "''${i_total}" ]] && i_target=$((i_current+1))
pactl set-default-sink ''${all[''${i_target}]}
}
'';
}

View file

@ -0,0 +1,14 @@
function sound_output_cycle() {
notify_short
local IFS=$'\n'
local current=$(pactl get-default-sink)
local all=($(pactl list short sinks | cut -f2))
local i_current=$(_index_of ${current} ${all[@]})
local i_total=${#all[@]}
((i_total--))
local i_target=0
[[ ${i_current} -lt ${i_total} ]] && i_target=$((i_current + 1))
pactl set-default-sink ${all[${i_target}]}
}

View file

@ -1,16 +0,0 @@
{ __findFile, ... }:
{
text = ''
# Find currently active SWAYSOCK paths.
function _sway_find_sockets() {
ls /run/user/''${UID}/sway-ipc.''${UID}.*.sock
}
function _sway_iterate_sockets() {
local IFS=$'\n'
for socket in $(_sway_find_sockets); do
SWAYSOCK="''${socket}" ''${1}
done
}
'';
}

View file

@ -0,0 +1,11 @@
# Find currently active SWAYSOCK paths.
function _sway_find_sockets() {
ls /run/user/${UID}/sway-ipc.${UID}.*.sock
}
function _sway_iterate_sockets() {
local IFS=$'\n'
for socket in $(_sway_find_sockets); do
SWAYSOCK="${socket}" ${1}
done
}

View file

@ -1,19 +0,0 @@
{ ... }:
{
text = ''
# Toggle vpn.
function vpn() {
notify_short
if [[ "$(_vpn)" = "on" ]]; then
nmcli connection down Vpn
else
nmcli connection up Vpn
fi
}
function _vpn() {
local state=$(nmcli connection show Vpn | rg -i state.*activated)
[ "''${state}" != "" ] && printf on || printf off
}
'';
}

View file

@ -0,0 +1,14 @@
# Toggle vpn.
function vpn() {
notify_short
if [[ "$(_vpn)" == "on" ]]; then
nmcli connection down Vpn
else
nmcli connection up Vpn
fi
}
function _vpn() {
local state=$(nmcli connection show Vpn | rg -i state.*activated)
[ "${state}" != "" ] && printf on || printf off
}