fix(battery): render a detected battery at 0% instead of disabling the segment - #7784
Merged
Conversation
…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
|
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. |
📦 Release binary size reportCompares this PR's release-equivalent build against the latest published release, per OS (amd64).
🎉 Binary size shrank on at least one platform. |
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Prerequisites
Description
Fixes #7782.
Battery.Enabled()disabled the segment whenevererr == 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 timeerr == nil, a real battery was found, andPercentage == 0is legitimate data — e.g. a battery reportingcapacity: 0,status: Not charging— that should render as0%, 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: truepath,BatteryState()returns(nil, err)for any error other thanNoBatteryError, butEnabled()unconditionally didb.Info = *info, dereferencing that nil pointer. Guarded it with aninfo != nilcheck.Added
TestBatteryEnabledcovering: 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) whendisplay_erroris enabled.Generated by Claude Code