GithubHelp home page GithubHelp logo

Comments (3)

K4R7IK avatar K4R7IK commented on June 7, 2024

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

from rofi-bluetooth.

DylanHalstead avatar DylanHalstead commented on June 7, 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
}

from rofi-bluetooth.

mashtonian avatar mashtonian commented on June 7, 2024

Thanks for this, works a treat.

Perhaps this could be merged?

from rofi-bluetooth.

Related Issues (14)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.