Skip to content

Add percentage range support to maintain command#398

Merged
actuallymentor merged 2 commits intoactuallymentor:mainfrom
seefood:feature/maintain-range
Oct 21, 2025
Merged

Add percentage range support to maintain command#398
actuallymentor merged 2 commits intoactuallymentor:mainfrom
seefood:feature/maintain-range

Conversation

@seefood
Copy link
Copy Markdown

@seefood seefood commented Oct 21, 2025

Summary

Adds support for maintaining battery within a percentage range (e.g., battery maintain 70-80) while preserving full backward compatibility with single-value syntax (e.g., battery maintain 80).

Motivation

When using battery maintain 80, the battery constantly cycles between ~79% and ~81%, which still causes some wear. This PR allows users to set a wider range like battery maintain 70-80, letting the battery rest anywhere in that range, reducing charge/discharge cycles.

Changes

Core Functionality

  • New validation function valid_percentage_range():

    • Validates NUMBER-NUMBER format
    • Ensures lower < upper
    • Enforces lower ≥ 10 and upper ≤ 100
    • No maximum range spread limit (user choice)
  • Updated maintain_synchronous logic:

    • Parses setting into lower_bound and upper_bound
    • Enable charging when battery < lower_bound
    • Disable charging when battery ≥ upper_bound
    • Battery rests freely within the range
    • Fully backward compatible (single values work as before)
  • Enhanced status display:

    • Shows "70% - 80%" for ranges
    • Shows "80%" for single values

Documentation

  • Updated help message with range examples
  • Added README examples and usage notes
  • Clarified range syntax in CLI help

Persistence

  • Daemon correctly persists ranges across reboots
  • Stores range format (e.g., 70-80) in ~/.battery/maintain.percentage
  • Recovery logic auto-detects format

Testing

Tested on macOS with the following scenarios:

  • ✅ Single value battery maintain 80 works as before
  • ✅ Valid ranges work: battery maintain 70-80
  • ✅ Invalid ranges rejected:
    • 80-70 (upper < lower)
    • 5-15 (lower < 10)
    • 95-105 (upper > 100)
    • 70-70 (equal values)
  • ✅ Status correctly displays ranges: "70% - 80%"
  • ✅ Settings persist in daemon across reboots
  • ✅ Backward compatibility maintained

CLI-Only Feature

This implementation is CLI-only. GUI support for ranges is deferred to future work.

Example Usage

# Traditional single-value (backward compatible)
battery maintain 80

# New range syntax - lets battery rest between 70-80%
battery maintain 70-80

# Check status
battery status
# Output: Your battery is currently being maintained at 70% - 80%

Ira Abramov and others added 2 commits October 21, 2025 11:25
The .* pattern already covers .claude/ directory, but explicitly adding
CLAUDE.md to ensure Claude Code configuration files are not committed.
Also re-allowing .github and .vscode directories that should be tracked.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
- Add valid_percentage_range() validation function
  - Validates NUMBER-NUMBER format
  - Ensures lower < upper
  - Enforces lower >= 10 and upper <= 100
  - No maximum range spread limit

- Update maintain_synchronous to handle ranges
  - Parse range into lower_bound and upper_bound
  - Enable charging when battery < lower_bound
  - Disable charging when battery >= upper_bound
  - Fully backward compatible with single values

- Update help message and README
  - Add examples: 'battery maintain 70-80'
  - Document range syntax alongside single values

- Maintain daemon persists ranges across reboots
  - Stores range format (e.g., '70-80') in maintain.percentage
  - Recovery logic auto-detects and restores ranges

Tested:
- Single value (80) works as before
- Valid ranges (70-80) work correctly
- Invalid ranges rejected (80-70, 5-15, 95-105, 70-70)
- Status display shows ranges properly (70% - 80%)

CLI-only feature. GUI support deferred to future work.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
@actuallymentor actuallymentor merged commit f8cd2ce into actuallymentor:main Oct 21, 2025
@actuallymentor
Copy link
Copy Markdown
Owner

Thanks for this addition and well formatted PR @seefood! I welcome good AI x human collaborative pull requests.

@seefood seefood deleted the feature/maintain-range branch October 22, 2025 08:29
@seefood
Copy link
Copy Markdown
Author

seefood commented Oct 22, 2025

It was really the second time I submitted it, only this time I implemented nothing and let Claude do all the work, and it was embarrassingly a bit better than my first organic attempt...

@hrist0stoichev
Copy link
Copy Markdown

Is it true that battery maintain 80 will cycle the battery between 79-81? My understanding is that the battery won't be used at all when 80% is reached and the power will come from the power adapter only.

@seefood
Copy link
Copy Markdown
Author

seefood commented Dec 2, 2025

true, I discovered that only after using this feature for a while. unlike a phone, when the upper limit is reached, the laptop does not continue working on the battery, it works directly with the mains.

@actuallymentor
Copy link
Copy Markdown
Owner

true, I discovered that only after using this feature for a while. unlike a phone, when the upper limit is reached, the laptop does not continue working on the battery, it works directly with the mains.

Modern phones work the same actually.

I thought the range support was a feature people wanted because they unplug their laptop a bunch during the day or something.

panoskava pushed a commit to panoskava/battery that referenced this pull request Mar 17, 2026
…ange

Add percentage range support to maintain command
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.

3 participants