Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature request: add battery percentage to polybar status #16

Open
ehula opened this issue Mar 15, 2022 · 3 comments
Open

feature request: add battery percentage to polybar status #16

ehula opened this issue Mar 15, 2022 · 3 comments

Comments

@ehula
Copy link

ehula commented Mar 15, 2022

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
Copy link
Contributor

K4R7IK commented Jan 10, 2023

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

@DylanHalstead
Copy link

DylanHalstead commented Jan 11, 2024

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
Copy link

Thanks for this, works a treat.

Perhaps this could be merged?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants