Skip to content

Commit e3f927b

Browse files
multi battery display
1 parent 89d16fd commit e3f927b

File tree

2 files changed

+42
-14
lines changed

2 files changed

+42
-14
lines changed

docs/CONFIG.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,18 @@ alternatively, if you have no battery and would like a nerdfont icon to indicate
209209
set -g @dracula-no-battery-label ""
210210
```
211211

212+
in case you have multiple batteries:
213+
214+
the default battery label is only displayed in the very front.
215+
you can specify multiple battery labels by splitting them with `\n` like so:
216+
```bash
217+
set -g @dracula-battery-label "1:\n2:"
218+
```
219+
additionally you can specify the separator between each battery like so:
220+
```bash
221+
set -g @dracula-battery-separator "; "
222+
```
223+
212224
### compact-alt - [up](#table-of-contents)
213225
This widget allows the user to switch to an alternate list of widgets when the terminal becomes narrow.
214226
Switching only works if the widget is added to `set -g @dracula-plugins "your-plugins-here"`.

scripts/battery.sh

Lines changed: 30 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ battery_percent()
6161
esac
6262
}
6363

64-
battery_status()
64+
get_battery_status()
6565
{
6666
# Check OS
6767
case $(uname -s) in
@@ -84,9 +84,15 @@ battery_status()
8484
*)
8585
;;
8686
esac
87+
echo "$status"
88+
}
8789

88-
tmp_bat_perc=$(battery_percent)
89-
bat_perc="${tmp_bat_perc%\%}"
90+
parse_battery_status()
91+
{
92+
# $1 is battery_percent
93+
bat_perc="$1"
94+
# $2 is get_battery_status
95+
status="$2"
9096

9197
case $status in
9298
discharging|Discharging)
@@ -153,31 +159,41 @@ battery_status()
153159

154160
main()
155161
{
162+
# get left most custom label
156163
bat_label=$(get_tmux_option "@dracula-battery-label" "")
157164
if [ "$bat_label" == false ]; then
158165
bat_label=""
159166
fi
160167

168+
# get label for when there is no battery
161169
no_bat_label=$(get_tmux_option "@dracula-no-battery-label" "AC")
162170
if [ "$no_bat_label" == false ]; then
163171
no_bat_label=""
164172
fi
165173

166-
show_bat_label=$(get_tmux_option "@dracula-show-battery-status" false)
167-
if $show_bat_label; then
168-
bat_stat=$(battery_status)
169-
else
170-
bat_stat=""
171-
fi
172-
173174
bat_perc=$(battery_percent)
175+
bat_perc="${bat_perc%\%}"
174176

175-
if [ -z "$bat_stat" ]; then # Test if status is empty or not
176-
echo "$bat_label $bat_perc"
177-
elif [ -z "$bat_perc" ]; then # In case it is a desktop with no battery percent, only AC power
177+
# display widget
178+
if [ -z "$bat_perc" ]; then # In case it is a desktop with no battery percent, only AC power
178179
echo "$no_bat_label"
179180
else
180-
echo "$bat_label$bat_stat $bat_perc"
181+
num_bats=$(wc -l <<< "$bat_perc")
182+
183+
IFS=$'\n' read -rd '' -a percs <<<"$bat_perc"
184+
IFS=$'\n' read -rd '' -a stats <<<"$(get_battery_status)"
185+
IFS=$'\n' read -rd '' -a lbls <<<"$bat_label"
186+
show_bat_label=$(get_tmux_option "@dracula-show-battery-status" false)
187+
for ((i=0; i<num_bats; i++)); do
188+
if [[ i -gt 0 ]]; then
189+
echo -n "$(get_tmux_option "@dracula-battery-separator" "; ")"
190+
fi
191+
echo -n "${lbls[$i]}"
192+
if $show_bat_label; then
193+
echo -n "$(parse_battery_status "${percs[$i]}" "${stats[$i]}") "
194+
fi
195+
echo -n "${percs[$i]}%"
196+
done
181197
fi
182198
}
183199

0 commit comments

Comments
 (0)