nickclyde / rofi-bluetooth

🔷 A script that generates a rofi menu that uses bluetoothctl to connect to bluetooth devices and display status info.
GNU General Public License v3.0
479 stars 51 forks source link

feature request: add battery percentage to polybar status #16

Open ehula opened 2 years ago

ehula commented 2 years ago

I am really liking this application and am using it in my polybar. I would love to be able to display the remaining battery percentage of my connected device in the polybar status line. Is this something that could be added?

K4R7IK commented 1 year ago

@xiaoshihou514 i think @ehula is referring to the battery percentage of connected bluetooth device not the laptop battery.

DylanHalstead commented 10 months ago

Here was my solution if any else is interested: image

Updated device_menu including battery percentage:

device_menu() {
    device=$1

    # Get device name, mac address, and battery percentage
    device_name=$(echo "$device" | cut -d ' ' -f 3-)
    mac=$(echo "$device" | cut -d ' ' -f 2)
    # BATTERY % VARS
    battery_info=$(bluetoothctl info "$mac" | grep "Battery" | cut -d ' ' -f 2-)
    battery_percentage=$(echo "$battery_info" | grep -oP '\(\K[^)]+')

    # Build options
    if device_connected "$mac"; then
        connected="Connected: yes"
    else
        connected="Connected: no"
    fi
    paired=$(device_paired "$mac")
    trusted=$(device_trusted "$mac")
    # UPDATED OPTIONS
    options="$connected\n$paired\n$trusted\nBattery: $battery_percentage%\n$divider\n$goback\nExit"

    # Open rofi menu, read chosen option
    chosen="$(echo -e "$options" | $rofi_command "$device_name")"

    # Match chosen option to command
    case "$chosen" in
        "" | "$divider")
            echo "No option chosen."
            ;;
        "$connected")
            toggle_connection "$mac"
            ;;
        "$paired")
            toggle_paired "$mac"
            ;;
        "$trusted")
            toggle_trust "$mac"
            ;;
        "$goback")
            show_menu
            ;;
    esac
}

Updated print_status for polybar:

print_status() {
    if power_on; then
        printf ''

        paired_devices_cmd="devices Paired"
        # Check if an outdated version of bluetoothctl is used to preserve backwards compatibility
        if (( $(echo "$(bluetoothctl version | cut -d ' ' -f 2) < 5.65" | bc -l) )); then
            paired_devices_cmd="paired-devices"
        fi

        mapfile -t paired_devices < <(bluetoothctl $paired_devices_cmd | grep Device | cut -d ' ' -f 2)
        counter=0

        for device in "${paired_devices[@]}"; do
            if device_connected "$device"; then
                device_alias=$(bluetoothctl info "$device" | grep "Alias" | cut -d ' ' -f 2-)
                # BATTERY % VARS
                battery_info=$(bluetoothctl info "$device" | grep "Battery" | cut -d ' ' -f 2-)
                battery_percentage=$(echo "$battery_info" | grep -oP '\(\K[^)]+')

                # UPDATED PRINT
                if [ $counter -gt 0 ]; then
                    printf ", %s" "$device_alias ($battery_percentage%)"
                else
                    printf " %s" "$device_alias ($battery_percentage%)"
                fi

                ((counter++))
            fi
        done
        printf "\n"
    else
        echo ""
    fi
}
mashtonian commented 9 months ago

Thanks for this, works a treat.

Perhaps this could be merged?