Skip to content

fix(battery): render a detected battery at 0% instead of disabling the segment - #7784

Merged
JanDeDobbeleer merged 2 commits into
mainfrom
claude/loving-shannon-70yllw
Aug 8, 2026
Merged

fix(battery): render a detected battery at 0% instead of disabling the segment#7784
JanDeDobbeleer merged 2 commits into
mainfrom
claude/loving-shannon-70yllw

Conversation

@JanDeDobbeleer

Copy link
Copy Markdown
Owner

Prerequisites

  • I have read and understood the contributing guide.
  • The commit message follows the conventional commits guidelines.
  • Tests for the changes have been added (for bug fixes / features).
  • Docs have been added/updated (for bug fixes / features). — not applicable, this restores existing documented behavior; no options/docs changed.

Description

Fixes #7782.

Battery.Enabled() disabled the segment whenever err == nil && b.Percentage == 0, treating a computed 0% reading as a "no battery" sentinel. That's wrong: every platform backend (battery_linux.go, battery_windows.go, battery_darwin.go) already signals "no battery" through a dedicated error (NoBatteryError / ErrNotFound) before this check is reached. By the time err == nil, a real battery was found, and Percentage == 0 is legitimate data — e.g. a battery reporting capacity: 0, status: Not charging — that should render as 0%, not be hidden.

While adding regression tests I also found and fixed a related nil-pointer panic in the same function: on the display_error: true path, BatteryState() returns (nil, err) for any error other than NoBatteryError, but Enabled() unconditionally did b.Info = *info, dereferencing that nil pointer. Guarded it with an info != nil check.

Added TestBatteryEnabled covering: a detected 0% battery renders, discharging/full render normally, no battery present disables the segment, a retrieval error is hidden by default, and a retrieval error renders (without crashing) when display_error is enabled.


Generated by Claude Code

…e segment

Enabled() treated err == nil && Percentage == 0 as a "no battery"
sentinel, but every platform backend already signals "no battery"
through a dedicated error (NoBatteryError/ErrNotFound) before that
point is reached. A present battery genuinely reporting 0% charge
was therefore hidden instead of rendered.

Also guard against a nil Info dereference on the display_error path,
where BatteryState() returns (nil, err) for any non-NoBatteryError.

Fixes #7782
@cursor

cursor Bot commented Aug 8, 2026

Copy link
Copy Markdown

Bugbot is not enabled for your account, so this pull request was not reviewed.

Enable Bugbot in the Cursor dashboard to get automatic reviews on future PRs.

@github-actions

github-actions Bot commented Aug 8, 2026

Copy link
Copy Markdown

📦 Release binary size report

Compares this PR's release-equivalent build against the latest published release, per OS (amd64).

OS Baseline This PR Delta
darwin 13.78 MB 13.78 MB +0.0 KB (+0%)
linux 13.45 MB 13.45 MB -4.0 KB (-0.03%)
windows 14.01 MB 13.97 MB -42.3 KB (-0.29%)

🎉 Binary size shrank on at least one platform.

@JanDeDobbeleer
JanDeDobbeleer enabled auto-merge (squash) August 8, 2026 15:17
CI's fieldalignment check flagged the new test's table-driven struct
for avoidable padding: bool fields interspersed between the pointer
and interface fields moved the last pointer field to the very end,
inflating the GC-scanned pointer prefix from 48 to 56 bytes.
@JanDeDobbeleer
JanDeDobbeleer merged commit bfa4f3b into main Aug 8, 2026
13 checks passed
@JanDeDobbeleer
JanDeDobbeleer deleted the claude/loving-shannon-70yllw branch August 8, 2026 15:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Battery segment is disabled for a detected battery at 0%

2 participants