-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.go
More file actions
105 lines (91 loc) · 2.22 KB
/
utils.go
File metadata and controls
105 lines (91 loc) · 2.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package main
import (
"fmt"
"os/exec"
"runtime"
"strconv"
"strings"
)
// checkLowPower checks if system is on low battery
func checkLowPower() bool {
// Platform-specific battery check
switch runtime.GOOS {
case "windows":
return checkWindowsBattery()
case "linux":
return checkLinuxBattery()
case "darwin":
return checkMacBattery()
default:
return false
}
}
// checkWindowsBattery checks battery on Windows
func checkWindowsBattery() bool {
cmd := exec.Command("powershell", "-Command", "Get-WmiObject -Class Win32_Battery | Select-Object -ExpandProperty EstimatedChargeRemaining")
output, err := cmd.Output()
if err != nil {
return false
}
charge := strings.TrimSpace(string(output))
chargeInt, err := strconv.Atoi(charge)
if err != nil {
return false
}
// Consider low power if below 20%
return chargeInt < 20
}
// checkLinuxBattery checks battery on Linux
func checkLinuxBattery() bool {
cmd := exec.Command("cat", "/sys/class/power_supply/BAT0/capacity")
output, err := cmd.Output()
if err != nil {
return false
}
charge := strings.TrimSpace(string(output))
chargeInt, err := strconv.Atoi(charge)
if err != nil {
return false
}
return chargeInt < 20
}
// checkMacBattery checks battery on macOS
func checkMacBattery() bool {
cmd := exec.Command("pmset", "-g", "batt")
output, err := cmd.Output()
if err != nil {
return false
}
// Parse output for percentage
outputStr := string(output)
if strings.Contains(outputStr, "%") {
// Extract percentage
parts := strings.Split(outputStr, "%")
if len(parts) > 0 {
// Find the number before %
numStr := ""
for i := len(parts[0]) - 1; i >= 0; i-- {
if parts[0][i] >= '0' && parts[0][i] <= '9' {
numStr = string(parts[0][i]) + numStr
} else if numStr != "" {
break
}
}
if chargeInt, err := strconv.Atoi(numStr); err == nil {
return chargeInt < 20
}
}
}
return false
}
// formatError formats error messages consistently
func formatError(context string, err error) string {
return fmt.Sprintf("[%s Error] %v", context, err)
}
// truncateString truncates a string to specified length
func truncateString(s string, maxLen int) string {
if len(s) <= maxLen {
return s
}
return s[:maxLen] + "..."
}