Skip to content
Closed
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
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,16 @@ set -g status-right '#{battery_status_bg} Batt: #{battery_icon} #{battery_percen

- `@batt_remain_short`: 'true' / 'false' - This will shorten the time remaining (when charging or discharging) to `~H:MM`.

`#{battery_percentage}`

- `@batt_percentage_hide_if_above`: integer (`[0-100]`) - Hides the battery percentage indicator if the battery level is above this threshold.

### Defaults

#### Options

- `@batt_remain_short`: 'false'
- `@batt_percentage_hide_if_above`: '' (always display battery percentage)

#### Icons/Colors

Expand Down
21 changes: 20 additions & 1 deletion scripts/battery_percentage.sh
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,25 @@ print_battery_percentage() {
}

main() {
print_battery_percentage
local bat_pct
local bat_pct_raw

bat_pct_raw="$(print_battery_percentage)"
bat_pct="${bat_pct_raw//%/}"

if ! [[ "$bat_pct" =~ ^[0-9]+$ ]]
then
# Display bat percentage string if not a number
echo -n "$bat_pct_raw"
return
fi

local hide_if_above
hide_if_above="$(get_tmux_option "@batt_percentage_hide_if_above")"

if [[ "$bat_pct" -lt "${hide_if_above}" ]]
Copy link

Copilot AI Dec 30, 2025

Choose a reason for hiding this comment

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

The comparison operator creates an off-by-one boundary issue. The PR description states the feature should hide the percentage if battery level is ">" (greater than) the threshold, using the example "> 90". However, using -lt here means the percentage is displayed when battery is strictly less than the threshold, which means it's hidden when battery is greater than OR EQUAL to the threshold.

For a threshold of 90:

  • Current behavior: displays at 89%, hides at 90% and above
  • Expected behavior (per description): displays at 90%, hides at 91% and above

To match the description, change the comparison to use -le (less than or equal to) instead of -lt.

Suggested change
if [[ "$bat_pct" -lt "${hide_if_above}" ]]
if [[ "$bat_pct" -le "${hide_if_above}" ]]

Copilot uses AI. Check for mistakes.
Copy link

Copilot AI Dec 30, 2025

Choose a reason for hiding this comment

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

The comparison will fail when the hide_if_above option is not set or is empty. When get_tmux_option is called without a default value and the option is not configured, it returns an empty string. This causes the comparison to become [[ "$bat_pct" -lt "" ]] which results in a bash error.

To fix this, either:

  1. Provide a default value when calling get_tmux_option (e.g., an empty string or a value like "101" that effectively disables the feature)
  2. Add a check to ensure hide_if_above is not empty before performing the comparison
Suggested change
if [[ "$bat_pct" -lt "${hide_if_above}" ]]
if [[ -n "$hide_if_above" && "$hide_if_above" =~ ^[0-9]+$ && "$bat_pct" -lt "$hide_if_above" ]]

Copilot uses AI. Check for mistakes.
then
echo -n "$bat_pct_raw"
fi
}
main