Skip to content

Commit 3d7cfed

Browse files
justincampbellmartinbeentjes
authored andcommitted
Add @batt_remain_short variable (#52)
* Remove format string count * Add variable @batt_remain_short Hides output if battery is charging or charged, and shortens the discharging output to ~H:MM.
1 parent 8fa3b62 commit 3d7cfed

File tree

2 files changed

+37
-5
lines changed

2 files changed

+37
-5
lines changed

README.md

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ Battery between 50% and 16% charged:<br/>
6565
Battery between 15% and dead:<br/>
6666
![battery_status_bg_red](/screenshots/battery_status_bg_red.png)
6767

68-
This is done by introducing 4 new format strings that can be added to
68+
This is done by introducing new format strings that can be added to
6969
`status-right` option:
7070
- `#{battery_icon}` - will display a battery status icon
7171
- `#{battery_percentage}` - will show battery percentage
@@ -96,6 +96,15 @@ after you do this.
9696

9797
*Warning*: The battery icon change most likely will not be instant. When you un-plug the power cord, it will take some time (15 - 60 seconds) for the icon to change. This depends on the `status-interval` tmux option. Setting it to 15 seconds should be good enough.
9898

99+
## Shortened remaining output
100+
101+
To shorten the output of `#{battery_remain}`, set the following variable:
102+
103+
set -g @batt_remain_short true
104+
105+
This will hide output if the battery is charging or charged, and shorten the
106+
time remaining (when discharging) to `~H:MM`.
107+
99108
### Tmux Plugins
100109

101110
This plugin is part of the [tmux-plugins](https://github.com/tmux-plugins) organisation. Checkout plugins as [resurrect](https://github.com/tmux-plugins/tmux-resurrect), [logging](https://github.com/tmux-plugins/tmux-logging), [online status](https://github.com/tmux-plugins/tmux-online-status), and many more over at the [tmux-plugins](https://github.com/tmux-plugins) organisation page.

scripts/battery_remain.sh

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@ CURRENT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
44

55
source "$CURRENT_DIR/helpers.sh"
66

7+
short=false
8+
9+
get_remain_settings() {
10+
short=$(get_tmux_option "@batt_remain_short" false)
11+
}
12+
713
battery_discharging() {
814
local status="$(battery_status)"
915
[[ $status =~ (discharging) ]]
@@ -12,13 +18,23 @@ battery_discharging() {
1218
pmset_battery_remaining_time() {
1319
local status="$(pmset -g batt)"
1420
if echo $status | grep 'no estimate' >/dev/null 2>&1; then
15-
echo '- Calculating estimate...'
21+
if $short; then
22+
echo '~?:??'
23+
else
24+
echo '- Calculating estimate...'
25+
fi
1626
else
1727
local remaining_time="$(echo $status | grep -o '[0-9]\{1,2\}:[0-9]\{1,2\}')"
1828
if battery_discharging; then
19-
echo $remaining_time | awk '{printf "- %s left", $1}'
29+
if $short; then
30+
echo $remaining_time | awk '{printf "~%s"}'
31+
else
32+
echo $remaining_time | awk '{printf "- %s left", $1}'
33+
fi
2034
else
21-
echo $remaining_time | awk '{printf "- %s till full", $1}'
35+
if !$short; then
36+
echo $remaining_time | awk '{printf "- %s till full", $1}'
37+
fi
2238
fi
2339
fi
2440
}
@@ -43,6 +59,10 @@ print_battery_remain() {
4359
}
4460

4561
print_battery_full() {
62+
if !$short; then
63+
return
64+
fi
65+
4666
if command_exists "pmset"; then
4767
pmset_battery_remaining_time
4868
elif command_exists "upower"; then
@@ -52,10 +72,13 @@ print_battery_full() {
5272
}
5373

5474
main() {
75+
get_remain_settings
5576
if battery_discharging; then
5677
print_battery_remain
5778
else
58-
print_battery_full
79+
if !$short; then
80+
print_battery_full
81+
fi
5982
fi
6083
}
6184
main

0 commit comments

Comments
 (0)