Skip to content

Commit

Permalink
chore: switch to for range instead of select with ticker
Browse files Browse the repository at this point in the history
  • Loading branch information
BradLewis committed Sep 3, 2023
1 parent 55ca3d0 commit 7e4e03c
Showing 1 changed file with 42 additions and 50 deletions.
92 changes: 42 additions & 50 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,48 +98,44 @@ func updateLowPowerStateMenu(hardwareUUID string) {
var currentState BatteryState
tick := time.Tick(1 * time.Second)

//lint:ignore S1000 ingore
for {
select {
case <-tick:
cmd := exec.Command("defaults", "read", plistPath)
out, err := cmd.Output()
if err != nil {
log.Println(err)
continue
}
for range tick {
cmd := exec.Command("defaults", "read", plistPath)
out, err := cmd.Output()
if err != nil {
log.Println(err)
continue
}

var config map[string]interface{}
_, err = plist.Unmarshal(out, &config)
if err != nil {
log.Println(err)
continue
}
var config map[string]interface{}
_, err = plist.Unmarshal(out, &config)
if err != nil {
log.Println(err)
continue
}

// extract the LowPowerMode values for Battery and AC
batteryLowPowerModeStr := config["Battery Power"].(map[string]interface{})["LowPowerMode"].(string)
batteryLowPowerMode, err := strconv.ParseBool(batteryLowPowerModeStr)
if err != nil {
log.Println(err)
continue
}
// extract the LowPowerMode values for Battery and AC
batteryLowPowerModeStr := config["Battery Power"].(map[string]interface{})["LowPowerMode"].(string)
batteryLowPowerMode, err := strconv.ParseBool(batteryLowPowerModeStr)
if err != nil {
log.Println(err)
continue
}

acLowPowerModeStr := config["AC Power"].(map[string]interface{})["LowPowerMode"].(string)
acLowPowerMode, err := strconv.ParseBool(acLowPowerModeStr)
if err != nil {
log.Println(err)
continue
}
acLowPowerModeStr := config["AC Power"].(map[string]interface{})["LowPowerMode"].(string)
acLowPowerMode, err := strconv.ParseBool(acLowPowerModeStr)
if err != nil {
log.Println(err)
continue
}

// Get the state for the current condition
state := getStateFromCondition(acLowPowerMode, batteryLowPowerMode)
// Only update if state has changed
if state != currentState {
setMenuStatesFalse()
menuet.Defaults().SetBoolean(state.String(), true)
log.Printf("Updated state from %s to %s\n", currentState, state)
currentState = state
}
// Get the state for the current condition
state := getStateFromCondition(acLowPowerMode, batteryLowPowerMode)
// Only update if state has changed
if state != currentState {
setMenuStatesFalse()
menuet.Defaults().SetBoolean(state.String(), true)
log.Printf("Updated state from %s to %s\n", currentState, state)
currentState = state
}
}
}
Expand Down Expand Up @@ -245,18 +241,14 @@ func menu() {
currentIconState := ""
newIconState := ""
tick := time.Tick(1 * time.Second)
//lint:ignore S1000 ingore
for {
select {
case <-tick:
newIconState = updateCurrentState(currentIconState)
if currentIconState != newIconState {
menuet.App().SetMenuState(&menuet.MenuState{
Image: newIconState,
})
menuet.App().MenuChanged()
currentIconState = newIconState
}
for range tick {
newIconState = updateCurrentState(currentIconState)
if currentIconState != newIconState {
menuet.App().SetMenuState(&menuet.MenuState{
Image: newIconState,
})
menuet.App().MenuChanged()
currentIconState = newIconState
}
}
}
Expand Down

0 comments on commit 7e4e03c

Please sign in to comment.