From 474c78d231143ee452414bda998a02adad3be218 Mon Sep 17 00:00:00 2001 From: Ikor Jefocur Date: Thu, 19 Nov 2026 13:44:53 +0300 Subject: [PATCH] system-bluetooth-bluetoothctl: power off icon; more configurable battery icons --- .../system-bluetooth-bluetoothctl.sh | 109 +++++++++++------- 1 file changed, 65 insertions(+), 44 deletions(-) mode change 100644 => 100755 polybar-scripts/system-bluetooth-bluetoothctl/system-bluetooth-bluetoothctl.sh diff --git a/polybar-scripts/system-bluetooth-bluetoothctl/system-bluetooth-bluetoothctl.sh b/polybar-scripts/system-bluetooth-bluetoothctl/system-bluetooth-bluetoothctl.sh old mode 100644 new mode 100755 index de5d97d6..e49c723d --- a/polybar-scripts/system-bluetooth-bluetoothctl/system-bluetooth-bluetoothctl.sh +++ b/polybar-scripts/system-bluetooth-bluetoothctl/system-bluetooth-bluetoothctl.sh @@ -1,51 +1,72 @@ #!/bin/sh -bluetooth_print() { - bluetoothctl | grep --line-buffered 'Device\|#' | while read -r REPLY; do - if [ "$(systemctl is-active "bluetooth.service")" = "active" ]; then - printf '#1' - - devices_paired=$(bluetoothctl devices Paired | grep Device | cut -d ' ' -f 2) - counter=0 - - for device in $devices_paired; do - device_info=$(bluetoothctl info "$device") - - if echo "$device_info" | grep -q "Connected: yes"; then - device_output=$(echo "$device_info" | grep "Alias" | cut -d ' ' -f 2-) - device_battery_percent=$(echo "$device_info" | grep "Battery Percentage" | awk -F'[()]' '{print $2}') - - if [ -n "$device_battery_percent" ]; then - if [ "$device_battery_percent" -gt 90 ]; then - device_battery_icon="#25" - elif [ "$device_battery_percent" -gt 60 ]; then - device_battery_icon="#24" - elif [ "$device_battery_percent" -gt 35 ]; then - device_battery_icon="#23" - elif [ "$device_battery_percent" -gt 10 ]; then - device_battery_icon="#22" - else - device_battery_icon="#21" - fi - - device_output="$device_output $device_battery_icon $device_battery_percent%" - fi - - if [ $counter -gt 0 ]; then - printf ", %s" "$device_output" - else - printf " %s" "$device_output" - fi - - counter=$((counter + 1)) - fi - done +active_icon='#1' +inactive_icon='#2' +power_off_icon='#1' +# : +battery_icons='90:#21 60:#22 35:#23 10:#24 0:#25' + +update_line_pattern='Device\|Controller\|#' + +bluetooth_tail() { + bluetooth_print + bluetoothctl | grep --line-buffered $update_line_pattern | while read -r _; do + bluetooth_print + done +} - printf '\n' +bluetooth_print() { + if [ "$(systemctl is-active "bluetooth.service")" = "active" ]; then + if bluetoothctl show | grep -q "Powered: yes"; then + printf '%s' $active_icon else - echo "#2" + printf '%s' $power_off_icon fi - done + + devices_paired=$(bluetoothctl devices Paired | grep Device | cut -d ' ' -f 2) + counter=0 + + for device in $devices_paired; do + device_info=$(bluetoothctl info "$device") + + if echo "$device_info" | grep -q "Connected: yes"; then + if [ $counter -gt 0 ]; then + printf ", " + else + printf " " + fi + device_print "$device_info" + counter=$((counter + 1)) + fi + done + + printf '\n' + else + echo $inactive_icon + fi +} + +device_print() { + device_info=$1 + + device_output=$(echo "$device_info" | grep "Alias" | cut -d ' ' -f 2-) + device_battery_percent=$(echo "$device_info" | grep "Battery Percentage" | awk -F'[()]' '{print $2}') + + if [ -n "$device_battery_percent" ]; then + max_level_found=0 + for icon_config in $battery_icons; do + min_level=$( echo "$icon_config" | awk -F ':' '{print $1}' ) + if [ "$device_battery_percent" -gt "$min_level" ] \ + && [ "$min_level" -gt "$max_level_found" ]; then + device_battery_icon=" $( echo "$icon_config" | awk -F ':' '{print $2}' )" + max_level_found=$min_level + fi + done + + device_output="$device_output$device_battery_icon $device_battery_percent%" + fi + + printf "%s" "$device_output" } bluetooth_toggle() { @@ -72,6 +93,6 @@ case "$1" in bluetooth_toggle ;; *) - bluetooth_print + bluetooth_tail ;; esac