From 3e06633cec9c43c0b34c81443f642b49d8c1a506 Mon Sep 17 00:00:00 2001 From: Dmitry Voronin Date: Tue, 17 Dec 2024 09:12:00 +0300 Subject: [PATCH] Networkwidget: Add support for bt power levels. --- home/file/waybar/style/Plugin.nix | 1 + package/swayscript/script/DisplayWidget.nix | 16 +++++----- package/swayscript/script/NetworkWidget.nix | 34 ++++++++++++++++----- 3 files changed, 36 insertions(+), 15 deletions(-) diff --git a/home/file/waybar/style/Plugin.nix b/home/file/waybar/style/Plugin.nix index 610ad752..9837bb64 100644 --- a/home/file/waybar/style/Plugin.nix +++ b/home/file/waybar/style/Plugin.nix @@ -69,6 +69,7 @@ in { #custom-display.modified, #custom-network.issue, #custom-network.vpn, + #custom-network.btlow, #memory.critical, #pulseaudio.muted, #pulseaudio.source-muted, diff --git a/package/swayscript/script/DisplayWidget.nix b/package/swayscript/script/DisplayWidget.nix index 3965638e..14deefea 100644 --- a/package/swayscript/script/DisplayWidget.nix +++ b/package/swayscript/script/DisplayWidget.nix @@ -83,29 +83,29 @@ class="modified" fi - printf "{\"text\": \"󰍹\", \"tooltip\": \"DND: ''${_dndstate} / Monitor: ''${_monitorstate} / Gaming: ''${_gamingstate} / Recording: ''${_recordingstate}\", \"class\": \"''${class}\"}\n" + printf "%s" "{\"text\": \"󰍹\", \"tooltip\": \"DND: ''${_dndstate} / Monitor: ''${_monitorstate} / Gaming: ''${_gamingstate} / Recording: ''${_recordingstate}\", \"class\": \"''${class}\"}\n" } function monitorstate() { local outputs=($(swaymsg -t get_outputs | jq -r '.[] | .power')) for state in "''${outputs[@]}"; do - ''${state} || { - printf Y + ''${state} || { + printf "%s" Y return 1 } done - printf n + printf "%s" n return 0 } function recordingstate() { - [[ "$(ps cax | rg wf-recorder)" = "" ]] && printf n || printf Y + [[ "$(ps cax | rg wf-recorder)" = "" ]] && printf "%s" n || printf "%s" Y } function dndstate() { - [[ "$(makoctl mode)" = "dnd" ]] && printf Y || printf n + [[ "$(makoctl mode)" = "dnd" ]] && printf "%s" Y || printf "%s" n } function gamingstate() { @@ -113,12 +113,12 @@ for state in "''${outputs[@]}"; do [[ "''${state}" = "disabled" ]] || { - printf Y + printf "%s" Y return 1 } done - printf n + printf "%s" n return 0 } ''; diff --git a/package/swayscript/script/NetworkWidget.nix b/package/swayscript/script/NetworkWidget.nix index e6e004e3..f1949ea8 100644 --- a/package/swayscript/script/NetworkWidget.nix +++ b/package/swayscript/script/NetworkWidget.nix @@ -7,12 +7,32 @@ 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=($(bluetoothctl devices Connected | cut -d\ -f3)) + 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 20 ]]; then + class="btlow" + fi fi if [[ "''${_vpns}" != "" ]]; then @@ -26,23 +46,23 @@ fi for net in ''${_vpns[@]}; do - networks+=" ''${net}\\\n" + networks+=" ''${net}\\n" done for net in ''${_ethernets[@]}; do - networks+=" ''${net}\\\n" + networks+=" ''${net}\\n" done for net in ''${_wifis[@]}; do - networks+="󰖩 ''${net}\\\n" + networks+="󰖩 ''${net}\\n" done for bt in ''${_bts[@]}; do - networks+="󰂯 ''${bt}\\\n" + networks+="󰂯 ''${bt}\\n" done - networks=''${networks%\\\n} - printf "{\"text\": \"''${icon}\", \"tooltip\": \"''${networks}\", \"class\": \"''${class}\"}\n" + networks=''${networks%\\n} + printf "%s" "{\"text\": \"''${icon}\", \"tooltip\": \"''${networks}\", \"class\": \"''${class}\"}\n" } # Toggle network.