Skip to content

Commit 797f11d

Browse files
authored
Merge pull request #9 from imwally/v2.0.0
v2.0.0
2 parents 1d72ee1 + 54e7896 commit 797f11d

File tree

4 files changed

+44
-56
lines changed

4 files changed

+44
-56
lines changed

README.md

Lines changed: 29 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,36 +2,44 @@
22

33
`battstat` is a shell script that displays formatted information about the status of your battery.
44

5-
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.
6-
7-
![battery charging](https://github.com/imwally/battstat/raw/master/img/charging.png)
8-
![battery discharging](https://github.com/imwally/battstat/raw/master/img/discharging.png)
9-
![battery full charged](https://github.com/imwally/battstat/raw/master/img/charged.png)
5+
![battery discharging](/img/discharging.png)
106

117
## Examples
128

139
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.
1410

1511
```
16-
$ battstat -d "🍕" {t} {i}
17-
10:30 🍕
12+
~ % battstat -d "💀"
13+
11:25 74% 💀
14+
15+
~ % battstat -d "😎" -f "{i} ({t})"
16+
😎 (11:15)
1817
19-
$ battstat {t} {i}
20-
11:47 🔋
18+
~ % battstat -c "AC:" -d "BAT:" -f "{i} {p} {t}"
19+
BAT: 74% 11:35
2120
22-
$ battstat -c "AC:" -d "BAT:" {i} {p} {t}
23-
BAT: 82% 12:11
21+
~ % battstat -d "Battery:" -f "{i} {p}"
22+
Battery: 74%
23+
```
2424

25-
$ battstat {i} {p}
26-
🔋 81%
25+
## Formatting
2726

28-
$ battstat -d "Battery:" {i} {p}
29-
Battery: 81%
27+
`battstat` uses printf style formatting when using the `-f` flag. This means you can print replacement tokens however you want.
28+
29+
```
30+
~ % battstat -f "{i}\t{t}\t({p})"
31+
🔋 11:55 (74%)
32+
33+
~ % battstat -f "{i}\nType whatever you want.\n{t}\n({p})"
34+
🔋
35+
Type whatever you want.
36+
12:05
37+
(73%)
3038
```
3139

3240
## macOS menu bar
3341

34-
![bitbar screenshot](https://github.com/imwally/battstat/blob/master/img/bitbar.png)
42+
![bitbar screenshot](/img/bitbar.png)
3543

3644
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.
3745

@@ -40,7 +48,7 @@ The screenshot above is using the following shell script. Make sure the script i
4048
```
4149
#!/bin/sh
4250
43-
time=$(/usr/local/bin/battstat {t})
51+
time=$(/usr/local/bin/battstat -f {t})
4452
4553
echo "($time) | size=13"
4654
```
@@ -79,12 +87,14 @@ options:
7987
-h, --help display help information
8088
-c, --charging-icon string to display in icon's place when battery is charging
8189
-d, --discharging-icon string to display in icon's place when battery is discharging
90+
-f, --format formatted output of battstat
8291
--percent-when-charged only display percent when charged
8392
84-
format:
93+
format replacement tokens:
8594
{i} display icon
8695
{t} display time remaining
8796
{p} display percent
8897
89-
Note: There must be a space between each format token.
98+
example:
99+
battstat -f "Cool Battery: {t} ({p})"
90100
```

battstat

Lines changed: 15 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
charging_icon="" # U+26A1 - Thunderbolt
44
discharging_icon="🔋" # U+1F50B - Battery
5-
format=false # Track whether formatting was supplied from the user
5+
format="{t} {p} {i}"
66

77
print_help() {
88
echo "usage: battstat [options] format"
@@ -11,14 +11,16 @@ print_help() {
1111
echo " -h, --help display help information"
1212
echo " -c, --charging-icon string to display in icon's place when battery is charging"
1313
echo " -d, --discharging-icon string to display in icon's place when battery is discharging"
14+
echo " -f, --format formatted output of battstat"
1415
echo " --percent-when-charged only display percent when charged"
1516
echo ""
16-
echo "format:"
17+
echo "format replacement tokens:"
1718
echo " {i} display icon"
1819
echo " {t} display time remaining"
1920
echo " {p} display percent"
2021
echo ""
21-
echo " Note: There must be a space between each format token."
22+
echo "example:"
23+
echo " battstat -f \"Cool Battery: {t} ({p})\""
2224
}
2325

2426
exit_no_battery() {
@@ -116,17 +118,15 @@ hide_percent_until_charged() {
116118
fi
117119
}
118120

119-
print_icon() {
121+
set_icon() {
120122
if [ ! -z "$charging" ] || [ ! -z "$charged" ]; then
121123
icon=$charging_icon
122124
elif [ ! -z "$discharging" ]; then
123125
icon=$discharging_icon
124126
fi
125-
126-
printf " %s " $icon
127127
}
128128

129-
print_time() {
129+
set_time() {
130130
# Display "calc..." when calculating time remaining.
131131
if [ -z "$time" ] || [ $time = "0:00" ]; then
132132
time="calc..."
@@ -136,17 +136,6 @@ print_time() {
136136
if [ ! -z "$charged" ]; then
137137
time=""
138138
fi
139-
140-
if [ ! -z "$time" ]; then
141-
printf " %s " $time
142-
fi
143-
144-
}
145-
146-
print_percent() {
147-
if [ ! -z "$percent" ]; then
148-
printf " %s " $percent
149-
fi
150139
}
151140

152141
if [ "$1" = "-h" ] || [ "$1" = "--help" ]; then
@@ -186,33 +175,22 @@ while test $# -gt 0; do
186175
shift
187176
shift
188177
;;
189-
{i})
190-
format=true
191-
print_icon
192-
shift
193-
;;
194-
{t})
195-
format=true
196-
print_time
178+
-f | --format)
179+
format="$2"
197180
shift
198-
;;
199-
{p})
200-
format=true
201-
print_percent
202181
shift
203182
;;
204183
*)
205184
print_help
185+
format=""
206186
break
207187
;;
208188
esac
209189
done
210190

211-
if [ "$format" = false ]; then
212-
# No format was provided by the user, so print the default format
213-
print_time
214-
print_percent
215-
print_icon
216-
fi
191+
set_icon
192+
set_time
217193

218-
printf "\n"
194+
if [ ! -z "$format" ]; then
195+
echo $format | sed "s/{t}/$time/g;s/{p}/$percent/g;s/{i}/$icon/g"
196+
fi

img/bitbar.png

60.6 KB
Loading

img/discharging.png

87.8 KB
Loading

0 commit comments

Comments
 (0)