diff --git a/README.md b/README.md index d5a58bb..44dd036 100644 --- a/README.md +++ b/README.md @@ -2,36 +2,44 @@ `battstat` is a shell script that displays formatted information about the status of your battery. -Information is displayed in the order the format tokens are written. For example, the screenshots below show my __tmux__ status line running the command `battstat --percent-when-charged {i} {t} {p}`. This will display an icon, the time remaining when charging and discharging, and finally the percentage but only when the battery is fully charged. Format tokens can be written in any order and as many times as you like. - -![battery charging](https://github.com/imwally/battstat/raw/master/img/charging.png) -![battery discharging](https://github.com/imwally/battstat/raw/master/img/discharging.png) -![battery full charged](https://github.com/imwally/battstat/raw/master/img/charged.png) +![battery discharging](/img/discharging.png) ## Examples There are a few ways to customize the output of `battstat`. Charging and discharging icons can be replaced with single character or multi-character strings. The `-c` flag sets the charging string and the `-d` flag sets the discharging string. ``` -$ battstat -d "🍕" {t} {i} - 10:30 🍕 +~ % battstat -d "💀" +11:25 74% 💀 + +~ % battstat -d "😎" -f "{i} ({t})" +😎 (11:15) -$ battstat {t} {i} - 11:47 🔋 +~ % battstat -c "AC:" -d "BAT:" -f "{i} {p} {t}" +BAT: 74% 11:35 -$ battstat -c "AC:" -d "BAT:" {i} {p} {t} - BAT: 82% 12:11 +~ % battstat -d "Battery:" -f "{i} {p}" +Battery: 74% +``` -$ battstat {i} {p} - 🔋 81% +## Formatting -$ battstat -d "Battery:" {i} {p} - Battery: 81% +`battstat` uses printf style formatting when using the `-f` flag. This means you can print replacement tokens however you want. + +``` +~ % battstat -f "{i}\t{t}\t({p})" +🔋 11:55 (74%) + +~ % battstat -f "{i}\nType whatever you want.\n{t}\n({p})" +🔋 +Type whatever you want. +12:05 +(73%) ``` ## macOS menu bar -![bitbar screenshot](https://github.com/imwally/battstat/blob/master/img/bitbar.png) +![bitbar screenshot](/img/bitbar.png) Using [bitbar](https://github.com/matryer/bitbar) you can add the output of `battstat` to the menu bar on macOS. There are many ways to customize the output so I suggest reading over the [writing plugins](https://github.com/matryer/bitbar#writing-plugins) section to understand what's possible. @@ -40,7 +48,7 @@ The screenshot above is using the following shell script. Make sure the script i ``` #!/bin/sh -time=$(/usr/local/bin/battstat {t}) +time=$(/usr/local/bin/battstat -f {t}) echo "($time) | size=13" ``` @@ -79,12 +87,14 @@ options: -h, --help display help information -c, --charging-icon string to display in icon's place when battery is charging -d, --discharging-icon string to display in icon's place when battery is discharging + -f, --format formatted output of battstat --percent-when-charged only display percent when charged -format: +format replacement tokens: {i} display icon {t} display time remaining {p} display percent - Note: There must be a space between each format token. +example: + battstat -f "Cool Battery: {t} ({p})" ``` diff --git a/battstat b/battstat index 48f3b0b..91096e7 100755 --- a/battstat +++ b/battstat @@ -2,7 +2,7 @@ charging_icon="⚡" # U+26A1 - Thunderbolt discharging_icon="🔋" # U+1F50B - Battery -format=false # Track whether formatting was supplied from the user +format="{t} {p} {i}" print_help() { echo "usage: battstat [options] format" @@ -11,14 +11,16 @@ print_help() { echo " -h, --help display help information" echo " -c, --charging-icon string to display in icon's place when battery is charging" echo " -d, --discharging-icon string to display in icon's place when battery is discharging" + echo " -f, --format formatted output of battstat" echo " --percent-when-charged only display percent when charged" echo "" - echo "format:" + echo "format replacement tokens:" echo " {i} display icon" echo " {t} display time remaining" echo " {p} display percent" echo "" - echo " Note: There must be a space between each format token." + echo "example:" + echo " battstat -f \"Cool Battery: {t} ({p})\"" } exit_no_battery() { @@ -116,17 +118,15 @@ hide_percent_until_charged() { fi } -print_icon() { +set_icon() { if [ ! -z "$charging" ] || [ ! -z "$charged" ]; then icon=$charging_icon elif [ ! -z "$discharging" ]; then icon=$discharging_icon fi - - printf " %s " $icon } -print_time() { +set_time() { # Display "calc..." when calculating time remaining. if [ -z "$time" ] || [ $time = "0:00" ]; then time="calc..." @@ -136,17 +136,6 @@ print_time() { if [ ! -z "$charged" ]; then time="" fi - - if [ ! -z "$time" ]; then - printf " %s " $time - fi - -} - -print_percent() { - if [ ! -z "$percent" ]; then - printf " %s " $percent - fi } if [ "$1" = "-h" ] || [ "$1" = "--help" ]; then @@ -186,33 +175,22 @@ while test $# -gt 0; do shift shift ;; - {i}) - format=true - print_icon - shift - ;; - {t}) - format=true - print_time + -f | --format) + format="$2" shift - ;; - {p}) - format=true - print_percent shift ;; *) print_help + format="" break ;; esac done -if [ "$format" = false ]; then - # No format was provided by the user, so print the default format - print_time - print_percent - print_icon -fi +set_icon +set_time -printf "\n" +if [ ! -z "$format" ]; then + echo $format | sed "s/{t}/$time/g;s/{p}/$percent/g;s/{i}/$icon/g" +fi diff --git a/img/bitbar.png b/img/bitbar.png index bac18d2..71dbd75 100644 Binary files a/img/bitbar.png and b/img/bitbar.png differ diff --git a/img/discharging.png b/img/discharging.png index e8ac155..946432d 100644 Binary files a/img/discharging.png and b/img/discharging.png differ