-
Notifications
You must be signed in to change notification settings - Fork 346
feat/fix/multi battery display #348
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -61,7 +61,7 @@ battery_percent() | |
| esac | ||
| } | ||
|
|
||
| battery_status() | ||
| get_battery_status() | ||
| { | ||
| # Check OS | ||
| case $(uname -s) in | ||
|
|
@@ -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) | ||
|
|
@@ -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%\%}" | ||
|
|
||
coderabbitai[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| 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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Avoid per-iteration tmux calls & off-by-one risk
- 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"
fibat_sep=$(get_tmux_option "@dracula-battery-separator" "; ")🤖 Prompt for AI Agents |
||
| fi | ||
| } | ||
|
|
||
|
|
||
There was a problem hiding this comment.
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:
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:
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