Hacker News new | past | comments | ask | show | jobs | submit login

This project started as a rewrite of a Bash script (for learning purposes), so I can easily answer this question. It's indeed small. Here's the full Bash script I used to use (including comments):

    #!/usr/bin/env bash

    # Print the remaining amount of energy (or charge)
    # stored in the battery identified by `BAT0`.  This
    # is what works on my ThinkPad X220 and may not be
    # portable to other laptops without changes.

    cd /sys/class/power_supply/BAT0 || exit $?

    # Sometimes there are `energy_now` and `energy_full`
    # pseudofiles, and sometimes there are `charge_now`
    # and `charge_full` pseudofiles instead.
    if [[ -e energy_now ]]; then
       now=$(<energy_now)
       full=$(<energy_full)
       unit=Wh
    elif [[ -e charge_now ]]; then
       now=$(<charge_now)
       full=$(<charge_full)
       unit=Ah
    fi
    percent=$((100 * now / full))
    # Convert from microwatt-hours (or microampere-hours)
    # to watt-hours (or ampere-hours).
    now=$(bc <<< "scale=1; $now / 1000000")
    full=$(bc <<< "scale=1; $full / 1000000")
    echo "$now $unit / $full $unit (${percent}%)"



Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: