Skip to content

Commit

Permalink
fix: resolve an rare issue when charge may be above the predefined value
Browse files Browse the repository at this point in the history
Signed-off-by: Charlie Chiang <[email protected]>
  • Loading branch information
charlie0129 committed Nov 7, 2023
1 parent f2e130d commit eda96be
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 15 deletions.
15 changes: 6 additions & 9 deletions cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -495,7 +495,7 @@ func NewStatusCommand() *cobra.Command {
// Charging status.
cmd.Println(bold("Charging status:"))

additionalMsg := " (status updates can take up to 5 minutes)"
additionalMsg := " (refreshes can take up to 5 minutes)"
if charging {
cmd.Println(" Allow charging: " + bool2Text(true) + additionalMsg)
cmd.Print(" Your Mac will charge")
Expand Down Expand Up @@ -541,9 +541,9 @@ func NewStatusCommand() *cobra.Command {
state := "not charging"
switch bat.State {
case battery.Charging:
state = "charging"
state = color.GreenString("charging")
case battery.Discharging:
state = "discharging"
state = color.RedString("discharging")
case battery.Full:
state = "full"
}
Expand All @@ -560,7 +560,7 @@ func NewStatusCommand() *cobra.Command {
cmd.Printf(" Upper limit: %s\n", bold("%d%%", conf.Limit))
cmd.Printf(" Lower limit: %s (%d-%d)\n", bold("%d%%", conf.Limit-conf.LowerLimitDelta), conf.Limit, conf.LowerLimitDelta)
} else {
cmd.Printf(" Charge limit: %s\n", bold("100%% (disabled)"))
cmd.Printf(" Charge limit: %s\n", bold("100%% (batt disabled)"))
}
cmd.Printf(" Prevent idle-sleep when charging: %s\n", bool2Text(conf.PreventIdleSleep))
cmd.Printf(" Disable charging before sleep if charge limit is enabled: %s\n", bool2Text(conf.DisableChargingPreSleep))
Expand Down Expand Up @@ -668,13 +668,10 @@ One thing to note: this option is purely cosmetic. batt will still function even
}

func bool2Text(b bool) string {
var c string
if b {
c = "✔"
} else {
c = "✘"
return color.New(color.Bold, color.FgGreen).Sprint("✔")
}
return bold(c)
return color.New(color.Bold, color.FgRed).Sprint("✘")
}

func bold(format string, a ...interface{}) string {
Expand Down
15 changes: 9 additions & 6 deletions sleepcallback.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,16 @@ func canSystemSleepCallback() {
IOAllowPowerChange or IOCancelPowerChange, the system will wait 30
seconds then go to sleep.
*/
logrus.Traceln("received kIOMessageCanSystemSleep notification")
logrus.Debugln("received kIOMessageCanSystemSleep notification")

if !config.PreventIdleSleep {
logrus.Debugln("system is going to sleep, but PreventIdleSleep is disabled, nothing to do")
C.AllowPowerChange()
return
}

maintainLoop()
// Run a loop immediately to update `maintainedChargingInProgress` variable.
maintainLoopForced()

if maintainedChargingInProgress {
logrus.Debugln("idle sleep is about to kick in, but maintained charging is in progress, deny idle sleep")
Expand All @@ -58,7 +59,7 @@ func systemWillSleepCallback() {
NOTE: If you call IOCancelPowerChange to deny sleep it returns
kIOReturnSuccess, however the system WILL still go to sleep.
*/
logrus.Traceln("received kIOMessageSystemWillSleep notification")
logrus.Debugln("received kIOMessageSystemWillSleep notification")

if !config.DisableChargingPreSleep {
logrus.Debugln("system is going to sleep, but DisableChargingPreSleep is disabled, nothing to do")
Expand All @@ -76,7 +77,9 @@ func systemWillSleepCallback() {
if config.Limit < 100 {
logrus.Infof("system is going to sleep but charge limit is enabled, disabling charging just before sleep, and delaying next loop by %d seconds", preSleepLoopDelaySeconds)
// Delay next loop to prevent charging to be re-enabled after we disabled it.
// macOS will wait 30s before going to sleep, so we delay more than that, just to be sure.
// macOS will wait 30s before going to sleep, there is a chance that a maintain loop is
// executed during that time and it enables charging.
// So we delay more than that, just to be sure.
// No need to prevent duplicated runs.
wg.Add(1)
go func() {
Expand All @@ -100,13 +103,13 @@ func systemWillSleepCallback() {
//export systemWillPowerOnCallback
func systemWillPowerOnCallback() {
// System has started the wake-up process...
logrus.Traceln("received kIOMessageSystemWillPowerOn notification")
logrus.Debugln("received kIOMessageSystemWillPowerOn notification")
}

//export systemHasPoweredOnCallback
func systemHasPoweredOnCallback() {
// System has finished waking up...
logrus.Traceln("received kIOMessageSystemHasPoweredOn notification")
logrus.Debugln("received kIOMessageSystemHasPoweredOn notification")

if config.Limit < 100 {
logrus.Debugf("system has finished waking up, delaying next loop by %d seconds", postSleepLoopDelaySeconds)
Expand Down

0 comments on commit eda96be

Please sign in to comment.