From 2be407b7c9efbd257a1b6207e31000e94fcfc9bb Mon Sep 17 00:00:00 2001 From: Viyurz Date: Tue, 19 Nov 2024 09:37:19 +0100 Subject: [PATCH] feat(eww): Add simple bluetooth widget --- .config/eww/eww.yuck | 20 +++++++++++++ .config/eww/scripts/get-bluetooth.sh | 30 +++++++++++++++++++ .config/eww/scripts/toggle-bluetooth-state.sh | 12 ++++++++ 3 files changed, 62 insertions(+) create mode 100755 .config/eww/scripts/get-bluetooth.sh create mode 100755 .config/eww/scripts/toggle-bluetooth-state.sh diff --git a/.config/eww/eww.yuck b/.config/eww/eww.yuck index 6eb249d..5aa3917 100644 --- a/.config/eww/eww.yuck +++ b/.config/eww/eww.yuck @@ -6,6 +6,9 @@ ; Battery (defvar battery-icons '{"Full": {"10": "󰂄"}, "Charging": ["󰢟", "󰢜", "󰂆", "󰂇", "󰂈", "󰢝", "󰂉", "󰢞", "󰂊", "󰂋", "󰂅"], "Discharging": ["󰂎", "󰁺", "󰁻", "󰁼", "󰁽", "󰁾", "󰁿", "󰂀", "󰂁", "󰂂", "󰁹"]}') +; Bluetooth +(deflisten bluetooth :initial "{}" "~/.config/eww/scripts/get-bluetooth.sh") + ; DateTime (defpoll datetime :interval "1s" "date +'%a %-e %b %X'") (defpoll month :interval "1m" :initial "1" "date +'%m'") @@ -46,6 +49,7 @@ (packages-updates) (volume) (network) + (bluetooth) ) ) @@ -118,8 +122,24 @@ ) ) +(defwidget bluetooth [] + (eventbox :onclick "~/.config/eww/scripts/toggle-bluetooth-state.sh" + (label :text "${bluetooth.icon} ${bluetooth.device}" + :class "${bluetooth.state}" + ) + ) +) + (defwidget bar-middle [] + (box :halign "center" + :spacing 12 + :space-evenly false + (title) + ) +) + +(defwidget title [] (label :limit-width 42 :show-truncated false :text "${window-title}" diff --git a/.config/eww/scripts/get-bluetooth.sh b/.config/eww/scripts/get-bluetooth.sh new file mode 100755 index 0000000..98cf42c --- /dev/null +++ b/.config/eww/scripts/get-bluetooth.sh @@ -0,0 +1,30 @@ +#!/bin/bash + +interval=2 + +function get { + unset icon state device + + if bluetoothctl show | grep -q 'Powered: no'; then + icon="󰂲" + state="disabled" + device="Disabled" + else + icon="󰂯" + state="powered" + device="Not connected" + fi + + cat << EOF +{"icon": "$icon", "state": "$state", "device": "$device"} +EOF +} + +if [[ "$1" == "once" ]]; then + get +else + while :; do + get + sleep $interval + done +fi diff --git a/.config/eww/scripts/toggle-bluetooth-state.sh b/.config/eww/scripts/toggle-bluetooth-state.sh new file mode 100755 index 0000000..808dd18 --- /dev/null +++ b/.config/eww/scripts/toggle-bluetooth-state.sh @@ -0,0 +1,12 @@ +#!/bin/bash + +if [[ $(bluetoothctl show | grep Powered: | cut -f 2 -d ' ') == "no" ]]; then + if [[ ! $(bluetoothctl power on) ]]; then + rfkill block bluetooth + rfkill unblock bluetooth + fi +else + bluetoothctl power off +fi + +eww update bluetooth="$(~/.config/eww/scripts/get-bluetooth.sh once)"