Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions docs/CONFIG.md
Original file line number Diff line number Diff line change
Expand Up @@ -232,11 +232,16 @@ alternatively, if you have no battery and would like a nerdfont icon to indicate
set -g @dracula-no-battery-label " "
```

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

the default battery label is only displayed in the very front.
you can specify multiple battery labels by splitting them with `\n` like so:
```bash
# default is false
set -g @dracula-battery-hide-on-desktop true
set -g @dracula-battery-label "1:\n2:"
```
Comment on lines +238 to +241
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Verification agent

🧩 Analysis chain

Clarify number-of-labels vs. number-of-batteries behavior.

Please document what happens when the number of configured labels doesn’t match the number of detected batteries (e.g., do extra labels get ignored? do missing labels default to empty or reuse the first/last?). This helps users set expectations.

Run this script to locate the relevant logic in scripts/battery.sh and confirm the behavior so we can add a precise sentence to the docs:


🏁 Script executed:

#!/bin/bash
# Explore how labels and separators are read and applied.

echo "1) How is @dracula-battery-label read?"
rg -n -A3 -B3 '@dracula-battery-label' scripts/battery.sh

echo -e "\n2) Look for splitting on the literal \\n:"
rg -n -A2 -B2 '\\\\n' scripts/battery.sh

echo -e "\n3) How is @dracula-battery-separator read and applied?"
rg -n -A3 -B3 '@dracula-battery-separator' scripts/battery.sh

echo -e "\n4) Check logic around mapping labels to multiple batteries:"
rg -n -A4 -B4 -e 'label' -e 'labels' -e 'separator' -e 'map' -e 'join' scripts/battery.sh

Length of output: 3356


Document battery-label count behavior and newline semantics

Verified in scripts/battery.sh: labels are split on actual newline chars into an array and mapped one-to-one to detected batteries. Extra labels are ignored; missing labels produce empty (no) label; the separator is printed between battery entries. Default label is "♥" (setting the option to false yields an empty label).

Files/locations to reference:

  • scripts/battery.sh
    • bat_label read & default/false handling: line ~163 (bat_label=$(get_tmux_option "@dracula-battery-label" "♥"))
    • splitting into array: line ~183 (IFS=$'\n' read -rd '' -a lbls <<<"$bat_label")
    • mapping loop & separator: lines ~184–195 (num_bats, loop, separator at ~188, echo -n "${lbls[$i]}" at ~190)
  • docs/CONFIG.md — update near lines 238–241 (the multi-label example)

Suggested one-line addition for docs/CONFIG.md (insert after the example):
Note: labels are split on actual newline characters and matched one-to-one to detected batteries — extra labels are ignored, and missing labels are left empty (no label). The @dracula-battery-separator is inserted between battery entries. The default label is "♥" (or empty if you set the option to false).

🤖 Prompt for AI Agents
In docs/CONFIG.md around lines 238–241, update the multi-label battery example
by adding a single-line note after the example stating that labels are split on
actual newline characters and matched one-to-one to detected batteries (extra
labels are ignored, missing labels produce an empty label), that
@dracula-battery-separator is inserted between battery entries, and that the
default label is "♥" (or empty if the option is set to false); base the wording
on scripts/battery.sh behavior (bat_label default/false handling ~line 163, IFS
split ~line 183, mapping loop & separator ~lines 184–195).

additionally you can specify the separator between each battery like so:
```bash
set -g @dracula-battery-separator "; "
```

### compact-alt - [up](#table-of-contents)
Expand Down
50 changes: 29 additions & 21 deletions scripts/battery.sh
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ battery_percent()
esac
}

battery_status()
get_battery_status()
{
# Check OS
case $(uname -s) in
Expand All @@ -84,9 +84,15 @@ battery_status()
*)
;;
esac
echo "$status"
}

tmp_bat_perc=$(battery_percent)
bat_perc="${tmp_bat_perc%\%}"
parse_battery_status()
{
# $1 is battery_percent
bat_perc="$1"
# $2 is get_battery_status
status="$2"

case $status in
discharging|Discharging)
Expand Down Expand Up @@ -153,38 +159,40 @@ battery_status()

main()
{
# get left most custom label
bat_label=$(get_tmux_option "@dracula-battery-label" "♥")
if [ "$bat_label" == false ]; then
bat_label=""
fi

# get label for when there is no battery
no_bat_label=$(get_tmux_option "@dracula-no-battery-label" "AC")
if [ "$no_bat_label" == false ]; then
no_bat_label=""
fi

show_bat_label=$(get_tmux_option "@dracula-show-battery-status" false)
if $show_bat_label; then
bat_stat=$(battery_status)
else
bat_stat=""
fi

bat_perc=$(battery_percent)
bat_perc="${bat_perc%\%}"

hide_on_desktop=$(get_tmux_option "@dracula-battery-hide-on-desktop" false)
# If no battery percent and the feature flag is enabled, hide the widget completely
if $hide_on_desktop && [ -z "$bat_perc" ]; then
echo ""
return
fi

if [ -z "$bat_stat" ]; then # Test if status is empty or not
echo "$bat_label $bat_perc"
elif [ -z "$bat_perc" ]; then # In case it is a desktop with no battery percent, only AC power
# display widget
if [ -z "$bat_perc" ]; then # In case it is a desktop with no battery percent, only AC power
echo "$no_bat_label"
else
echo "$bat_label$bat_stat $bat_perc"
IFS=$'\n' read -rd '' -a percs <<<"$bat_perc"
IFS=$'\n' read -rd '' -a stats <<<"$(get_battery_status)"
IFS=$'\n' read -rd '' -a lbls <<<"$bat_label"
num_bats=${#percs[@]}
show_bat_label=$(get_tmux_option "@dracula-show-battery-status" false)
for ((i=0; i<num_bats; i++)); do
if [[ i -gt 0 ]]; then
echo -n "$(get_tmux_option "@dracula-battery-separator" "; ")"
fi
echo -n "${lbls[$i]}"
if $show_bat_label; then
echo -n "$(parse_battery_status "${percs[$i]}" "${stats[$i]}") "
fi
echo -n "${percs[$i]}%"
done
Comment on lines +186 to +195
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Avoid per-iteration tmux calls & off-by-one risk

  1. get_tmux_option "@dracula-battery-separator" "; " is executed for every
    battery. Cache once before the loop to halve tmux round-trips.

  2. num_bats=$(wc -l <<<"$bat_perc") undercounts when the last line lacks a
    trailing newline and overcounts if bat_perc was empty.
    You already have percs; just use its length.

- num_bats=$(wc -l <<< "$bat_perc")
+num_bats=${#percs[@]}- if [[ i -gt 0 ]]; then
-   echo -n "$(get_tmux_option "@dracula-battery-separator" "; ")"
+if (( i > 0 )); then
+  echo -n "$bat_sep"
 fi
bat_sep=$(get_tmux_option "@dracula-battery-separator" "; ")
🤖 Prompt for AI Agents
In scripts/battery.sh around lines 187 to 196, avoid calling get_tmux_option
inside the loop by caching its result in a variable before the loop starts.
Also, replace the calculation of num_bats using wc -l on bat_perc with using the
length of the percs array to prevent off-by-one errors and incorrect counts when
bat_perc is empty or lacks a trailing newline. Update the loop to use the cached
separator variable and the corrected num_bats value.

fi
}

Expand Down