diff --git a/package/swayscript/default.nix b/package/swayscript/default.nix index 51a7ef09..6c300850 100644 --- a/package/swayscript/default.nix +++ b/package/swayscript/default.nix @@ -1,2 +1,23 @@ -{ pkgs, util, ... }@args: -pkgs.writeShellScriptBin "swayscript" (util.catText (util.ls ./script) args + "\${@}") +{ + __findFile, + pkgs, + util, + ... +}: +let + pipewire = pkgs.pipewire; + longogg = ; + notificationogg = ; + shortogg = ; + + 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\${@}") diff --git a/package/swayscript/script/DisplayWidget.nix b/package/swayscript/script/DisplayWidget.nix deleted file mode 100644 index c3fefeb5..00000000 --- a/package/swayscript/script/DisplayWidget.nix +++ /dev/null @@ -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 - } - ''; -} diff --git a/package/swayscript/script/DisplayWidget.sh b/package/swayscript/script/DisplayWidget.sh new file mode 100644 index 00000000..d322c020 --- /dev/null +++ b/package/swayscript/script/DisplayWidget.sh @@ -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 +} diff --git a/package/swayscript/script/NetworkWidget.nix b/package/swayscript/script/NetworkWidget.nix deleted file mode 100644 index a31d5273..00000000 --- a/package/swayscript/script/NetworkWidget.nix +++ /dev/null @@ -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 - } - ''; -} diff --git a/package/swayscript/script/NetworkWidget.sh b/package/swayscript/script/NetworkWidget.sh new file mode 100644 index 00000000..e7962b41 --- /dev/null +++ b/package/swayscript/script/NetworkWidget.sh @@ -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 +} diff --git a/package/swayscript/script/Notify.nix b/package/swayscript/script/Notify.nix deleted file mode 100644 index c10783cb..00000000 --- a/package/swayscript/script/Notify.nix +++ /dev/null @@ -1,19 +0,0 @@ -{ __findFile, pkgs, ... }: -{ - text = '' - function notify() { - [[ "''$(dndstate)" = "Y" ]] && return - ${pkgs.pipewire}/bin/pw-cat -p ${} & - } - - function notify_short() { - [[ "''$(dndstate)" = "Y" ]] && return - ${pkgs.pipewire}/bin/pw-cat -p ${} & - } - - function notify_long() { - [[ "''$(dndstate)" = "Y" ]] && return - ${pkgs.pipewire}/bin/pw-cat -p ${} & - } - ''; -} diff --git a/package/swayscript/script/Notify.sh b/package/swayscript/script/Notify.sh new file mode 100644 index 00000000..26ea6e12 --- /dev/null +++ b/package/swayscript/script/Notify.sh @@ -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@ & +} diff --git a/package/swayscript/script/Reload.nix b/package/swayscript/script/Reload.nix deleted file mode 100644 index 341f9c94..00000000 --- a/package/swayscript/script/Reload.nix +++ /dev/null @@ -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 - } - ''; -} diff --git a/package/swayscript/script/Reload.sh b/package/swayscript/script/Reload.sh new file mode 100644 index 00000000..8b2420d6 --- /dev/null +++ b/package/swayscript/script/Reload.sh @@ -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 +} diff --git a/package/swayscript/script/Scratchpad.nix b/package/swayscript/script/Scratchpad.nix deleted file mode 100644 index d45d432c..00000000 --- a/package/swayscript/script/Scratchpad.nix +++ /dev/null @@ -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 - } - ''; -} diff --git a/package/swayscript/script/Scratchpad.sh b/package/swayscript/script/Scratchpad.sh new file mode 100644 index 00000000..34489b68 --- /dev/null +++ b/package/swayscript/script/Scratchpad.sh @@ -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 +} diff --git a/package/swayscript/script/Sound.nix b/package/swayscript/script/Sound.nix deleted file mode 100644 index b83da2c0..00000000 --- a/package/swayscript/script/Sound.nix +++ /dev/null @@ -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}]} - } - ''; -} diff --git a/package/swayscript/script/Sound.sh b/package/swayscript/script/Sound.sh new file mode 100644 index 00000000..bbbf3d04 --- /dev/null +++ b/package/swayscript/script/Sound.sh @@ -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}]} +} diff --git a/package/swayscript/script/Util.nix b/package/swayscript/script/Util.nix deleted file mode 100644 index cfefa7e0..00000000 --- a/package/swayscript/script/Util.nix +++ /dev/null @@ -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 - } - ''; -} diff --git a/package/swayscript/script/Util.sh b/package/swayscript/script/Util.sh new file mode 100644 index 00000000..6b3bd8dc --- /dev/null +++ b/package/swayscript/script/Util.sh @@ -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 +} diff --git a/package/swayscript/script/Vpn.nix b/package/swayscript/script/Vpn.nix deleted file mode 100644 index 9b3ba004..00000000 --- a/package/swayscript/script/Vpn.nix +++ /dev/null @@ -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 - } - ''; -} diff --git a/package/swayscript/script/Vpn.sh b/package/swayscript/script/Vpn.sh new file mode 100644 index 00000000..512e3790 --- /dev/null +++ b/package/swayscript/script/Vpn.sh @@ -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 +}