Skip to content

Commit 6000e2a

Browse files
Theoreticallyhugowebdavis
authored andcommitted
Merge pull request dracula#348 from dracula/fix/battery-status
feat/fix/multi battery display
2 parents e177157 + f35a171 commit 6000e2a

File tree

2 files changed

+37
-24
lines changed

2 files changed

+37
-24
lines changed

docs/CONFIG.md

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -232,11 +232,16 @@ alternatively, if you have no battery and would like a nerdfont icon to indicate
232232
set -g @dracula-no-battery-label ""
233233
```
234234

235-
if you only want to hide the battery widget on a desktop pc, set the following:
235+
in case you have multiple batteries:
236236

237+
the default battery label is only displayed in the very front.
238+
you can specify multiple battery labels by splitting them with `\n` like so:
237239
```bash
238-
# default is false
239-
set -g @dracula-battery-hide-on-desktop true
240+
set -g @dracula-battery-label "1:\n2:"
241+
```
242+
additionally you can specify the separator between each battery like so:
243+
```bash
244+
set -g @dracula-battery-separator "; "
240245
```
241246

242247
### compact-alt - [up](#table-of-contents)

scripts/battery.sh

Lines changed: 29 additions & 21 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,38 +159,40 @@ 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-
hide_on_desktop=$(get_tmux_option "@dracula-battery-hide-on-desktop" false)
176-
# If no battery percent and the feature flag is enabled, hide the widget completely
177-
if $hide_on_desktop && [ -z "$bat_perc" ]; then
178-
echo ""
179-
return
180-
fi
181-
182-
if [ -z "$bat_stat" ]; then # Test if status is empty or not
183-
echo "$bat_label $bat_perc"
184-
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
185179
echo "$no_bat_label"
186180
else
187-
echo "$bat_label$bat_stat $bat_perc"
181+
IFS=$'\n' read -rd '' -a percs <<<"$bat_perc"
182+
IFS=$'\n' read -rd '' -a stats <<<"$(get_battery_status)"
183+
IFS=$'\n' read -rd '' -a lbls <<<"$bat_label"
184+
num_bats=${#percs[@]}
185+
show_bat_label=$(get_tmux_option "@dracula-show-battery-status" false)
186+
for ((i=0; i<num_bats; i++)); do
187+
if [[ i -gt 0 ]]; then
188+
echo -n "$(get_tmux_option "@dracula-battery-separator" "; ")"
189+
fi
190+
echo -n "${lbls[$i]}"
191+
if $show_bat_label; then
192+
echo -n "$(parse_battery_status "${percs[$i]}" "${stats[$i]}") "
193+
fi
194+
echo -n "${percs[$i]}%"
195+
done
188196
fi
189197
}
190198

0 commit comments

Comments
 (0)