Skip to content

Commit

Permalink
Merge pull request #3 from BradLewis/main
Browse files Browse the repository at this point in the history
fix: switch to using ticker so we don't fall into an infinite loop
  • Loading branch information
TheDen authored Sep 3, 2023
2 parents 1030032 + 5cc1ebd commit 6756041
Showing 1 changed file with 61 additions and 45 deletions.
106 changes: 61 additions & 45 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
)

const (
appVersion = "0.1.2"
appVersion = "0.1.3"
boltIconOutline = "bolt.png"
boltIconFilled = "bolt-filled.png"
)
Expand Down Expand Up @@ -77,56 +77,69 @@ func getHardwareUUID() (string, error) {
}

func setLowPowerMode(str string) error {
cmd := exec.Command("/usr/bin/osascript", "-e", fmt.Sprintf("do shell script \"%s\" with prompt \"Galvani is trying to update battery prefrences\" with administrator privileges", str))
cmd := exec.Command(
"/usr/bin/osascript",
"-e",
fmt.Sprintf(
"do shell script \"%s\" with prompt \"Galvani is trying to update battery prefrences\" with administrator privileges",
str,
),
)
err := cmd.Run()
return err
}

func updateLowPowerStateMenu(hardwareUUID string) {
log.Printf("Hardware UUID is %s\n", hardwareUUID)
plistPath := fmt.Sprintf("/Library/Preferences/com.apple.PowerManagement.%s.plist", hardwareUUID)
plistPath := fmt.Sprintf(
"/Library/Preferences/com.apple.PowerManagement.%s.plist",
hardwareUUID,
)
var currentState BatteryState
tick := time.Tick(1 * time.Second)

for {

Check failure on line 101 in main.go

View workflow job for this annotation

GitHub Actions / runner / Go package

should use for range instead of for { select {} } (S1000)
cmd := exec.Command("defaults", "read", plistPath)
out, err := cmd.Output()
if err != nil {
log.Println(err)
continue
}
select {
case <-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
}
}
time.Sleep(time.Second)
}
}

Expand Down Expand Up @@ -230,16 +243,19 @@ func menuItems() []menuet.MenuItem {
func menu() {
currentIconState := ""
newIconState := ""
tick := time.Tick(1 * time.Second)
for {

Check failure on line 247 in main.go

View workflow job for this annotation

GitHub Actions / runner / Go package

should use for range instead of for { select {} } (S1000)
newIconState = updateCurrentState(currentIconState)
if currentIconState != newIconState {
menuet.App().SetMenuState(&menuet.MenuState{
Image: newIconState,
})
menuet.App().MenuChanged()
currentIconState = newIconState
select {
case <-tick:
newIconState = updateCurrentState(currentIconState)
if currentIconState != newIconState {
menuet.App().SetMenuState(&menuet.MenuState{
Image: newIconState,
})
menuet.App().MenuChanged()
currentIconState = newIconState
}
}
time.Sleep(time.Second)
}
}

Expand Down

0 comments on commit 6756041

Please sign in to comment.