Skip to content

Commit

Permalink
Merge pull request #9 from imwally/v2.0.0
Browse files Browse the repository at this point in the history
v2.0.0
  • Loading branch information
imwally authored Mar 29, 2024
2 parents 1d72ee1 + 54e7896 commit 797f11d
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 56 deletions.
48 changes: 29 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand All @@ -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"
```
Expand Down Expand Up @@ -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})"
```
52 changes: 15 additions & 37 deletions battstat
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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() {
Expand Down Expand Up @@ -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..."
Expand All @@ -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
Expand Down Expand Up @@ -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
Binary file modified img/bitbar.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified img/discharging.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 797f11d

Please sign in to comment.