ark/users/modules/eww/scripts/bluetooth

52 lines
1.4 KiB
Plaintext
Raw Normal View History

2022-09-14 15:35:52 +03:00
#!/usr/bin/env bash
2022-10-22 23:54:44 +03:00
declare -A baticon=([10]="󰤾" [20]="󰤿" [30]="󰥀" [40]="󰥁" [50]="󰥂" [60]="󰥃" [70]="󰥄" [80]="󰥅" [90]="󰥆" [100]="󰥈")
toggle() {
status=$(rfkill -J | jq -r '.rfkilldevices[] | select(.type == "bluetooth") | .soft' | head -1)
if [ "$status" = "unblocked" ]; then
rfkill block bluetooth
else
rfkill unblock bluetooth
fi
}
if [ "$1" = "toggle" ]; then
toggle
else
while true; do
powered=$(bluetoothctl show | rg Powered | cut -f 2- -d ' ')
status=$(bluetoothctl info)
name=$(echo "$status" | rg Name | cut -f 2- -d ' ')
mac=$(echo "$status" | head -1 | awk '{print $2}' | tr ':' '_')
if [[ "$(echo "$status" | rg Percentage)" != "" ]]; then
battery=$(upower -i /org/freedesktop/UPower/devices/headset_dev_"$mac" | rg percentage | awk '{print $2}' | cut -f 1 -d '%')
batt_icon=${baticon[$battery]}
else
batt_icon=""
fi
if [ "$powered" = "yes" ]; then
if [ "$status" != "Missing device address argument" ]; then
2022-09-14 15:35:52 +03:00
text="$name"
2022-10-22 23:54:44 +03:00
icon=""
color="#b4befe"
else
icon=""
text="Disconnected"
color="#45475a"
2022-09-14 15:35:52 +03:00
fi
else
2022-10-22 23:54:44 +03:00
icon=""
text="Bluetooth off"
2022-09-14 15:35:52 +03:00
color="#45475a"
fi
2022-10-22 23:54:44 +03:00
echo '{ "icon": "'"$icon"'", "batt_icon": "'"$batt_icon"'", "text": "'"$text"'", "color": "'"$color"'" }'
2022-09-14 15:35:52 +03:00
2022-10-22 23:54:44 +03:00
sleep 3
done
fi