UI improvements
This commit is contained in:
@@ -1,9 +1,11 @@
|
||||
package sysinfo
|
||||
|
||||
type Info struct {
|
||||
CPU string `json:"cpu,omitempty"`
|
||||
GPU string `json:"gpu,omitempty"`
|
||||
RAM string `json:"ram,omitempty"`
|
||||
CPU string `json:"cpu,omitempty"`
|
||||
GPU string `json:"gpu,omitempty"`
|
||||
RAM string `json:"ram,omitempty"`
|
||||
BatteryPercent *int `json:"batteryPercent,omitempty"`
|
||||
BatteryCharging bool `json:"batteryCharging,omitempty"`
|
||||
}
|
||||
|
||||
func Collect() Info {
|
||||
|
||||
@@ -4,6 +4,7 @@ package sysinfo
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os/exec"
|
||||
"runtime"
|
||||
"strings"
|
||||
"unsafe"
|
||||
@@ -12,13 +13,54 @@ import (
|
||||
)
|
||||
|
||||
func collectPlatform() Info {
|
||||
percent, charging := batteryStatus()
|
||||
return Info{
|
||||
CPU: cpuName(),
|
||||
GPU: gpuName(),
|
||||
RAM: totalRAM(),
|
||||
CPU: cpuName(),
|
||||
GPU: gpuName(),
|
||||
RAM: totalRAM(),
|
||||
BatteryPercent: percent,
|
||||
BatteryCharging: charging,
|
||||
}
|
||||
}
|
||||
|
||||
func OSName() string {
|
||||
out, err := exec.Command("sw_vers", "-productVersion").Output()
|
||||
if err != nil {
|
||||
return "macOS"
|
||||
}
|
||||
version := strings.TrimSpace(string(out))
|
||||
if version == "" {
|
||||
return "macOS"
|
||||
}
|
||||
return "macOS " + version
|
||||
}
|
||||
|
||||
func batteryStatus() (*int, bool) {
|
||||
out, err := exec.Command("pmset", "-g", "batt").Output()
|
||||
if err != nil {
|
||||
return nil, false
|
||||
}
|
||||
text := string(out)
|
||||
idx := strings.Index(text, "%")
|
||||
if idx <= 0 {
|
||||
return nil, false
|
||||
}
|
||||
start := idx - 1
|
||||
for start >= 0 && text[start] >= '0' && text[start] <= '9' {
|
||||
start--
|
||||
}
|
||||
var percent int
|
||||
if _, err := fmt.Sscanf(text[start+1:idx], "%d", &percent); err != nil {
|
||||
return nil, false
|
||||
}
|
||||
if percent < 0 || percent > 100 {
|
||||
return nil, false
|
||||
}
|
||||
lower := strings.ToLower(text)
|
||||
charging := strings.Contains(lower, "charging") || strings.Contains(lower, "charged")
|
||||
return &percent, charging
|
||||
}
|
||||
|
||||
func cpuName() string {
|
||||
name, err := unix.Sysctl("machdep.cpu.brand_string")
|
||||
if err != nil || name == "" {
|
||||
|
||||
@@ -11,13 +11,74 @@ import (
|
||||
)
|
||||
|
||||
func collectPlatform() Info {
|
||||
percent, charging := batteryStatus()
|
||||
return Info{
|
||||
CPU: cpuName(),
|
||||
GPU: gpuName(),
|
||||
RAM: totalRAM(),
|
||||
CPU: cpuName(),
|
||||
GPU: gpuName(),
|
||||
RAM: totalRAM(),
|
||||
BatteryPercent: percent,
|
||||
BatteryCharging: charging,
|
||||
}
|
||||
}
|
||||
|
||||
func OSName() string {
|
||||
data, err := os.ReadFile("/etc/os-release")
|
||||
if err != nil {
|
||||
return "Linux"
|
||||
}
|
||||
values := map[string]string{}
|
||||
for _, line := range strings.Split(string(data), "\n") {
|
||||
line = strings.TrimSpace(line)
|
||||
if line == "" || strings.HasPrefix(line, "#") {
|
||||
continue
|
||||
}
|
||||
parts := strings.SplitN(line, "=", 2)
|
||||
if len(parts) != 2 {
|
||||
continue
|
||||
}
|
||||
values[parts[0]] = strings.Trim(strings.TrimSpace(parts[1]), `"`)
|
||||
}
|
||||
if pretty := strings.TrimSpace(values["PRETTY_NAME"]); pretty != "" {
|
||||
return pretty
|
||||
}
|
||||
if name := strings.TrimSpace(values["NAME"]); name != "" {
|
||||
if version := strings.TrimSpace(values["VERSION_ID"]); version != "" {
|
||||
return name + " " + version
|
||||
}
|
||||
return name
|
||||
}
|
||||
return "Linux"
|
||||
}
|
||||
|
||||
func batteryStatus() (*int, bool) {
|
||||
entries, err := os.ReadDir("/sys/class/power_supply")
|
||||
if err != nil {
|
||||
return nil, false
|
||||
}
|
||||
for _, entry := range entries {
|
||||
if !strings.HasPrefix(entry.Name(), "BAT") {
|
||||
continue
|
||||
}
|
||||
base := "/sys/class/power_supply/" + entry.Name()
|
||||
capacityRaw, err := os.ReadFile(base + "/capacity")
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
var percent int
|
||||
if _, err := fmt.Sscanf(strings.TrimSpace(string(capacityRaw)), "%d", &percent); err != nil {
|
||||
continue
|
||||
}
|
||||
if percent < 0 || percent > 100 {
|
||||
continue
|
||||
}
|
||||
statusRaw, _ := os.ReadFile(base + "/status")
|
||||
status := strings.ToLower(strings.TrimSpace(string(statusRaw)))
|
||||
charging := status == "charging" || status == "full"
|
||||
return &percent, charging
|
||||
}
|
||||
return nil, false
|
||||
}
|
||||
|
||||
func cpuName() string {
|
||||
data, err := os.ReadFile("/proc/cpuinfo")
|
||||
if err != nil {
|
||||
|
||||
@@ -14,13 +14,70 @@ import (
|
||||
)
|
||||
|
||||
func collectPlatform() Info {
|
||||
percent, charging := batteryStatus()
|
||||
return Info{
|
||||
CPU: cpuName(),
|
||||
GPU: gpuName(),
|
||||
RAM: totalRAM(),
|
||||
CPU: cpuName(),
|
||||
GPU: gpuName(),
|
||||
RAM: totalRAM(),
|
||||
BatteryPercent: percent,
|
||||
BatteryCharging: charging,
|
||||
}
|
||||
}
|
||||
|
||||
func OSName() string {
|
||||
k, err := registry.OpenKey(registry.LOCAL_MACHINE,
|
||||
`SOFTWARE\Microsoft\Windows NT\CurrentVersion`, registry.QUERY_VALUE)
|
||||
if err != nil {
|
||||
return "Windows"
|
||||
}
|
||||
defer k.Close()
|
||||
|
||||
product, _, _ := k.GetStringValue("ProductName")
|
||||
display, _, _ := k.GetStringValue("DisplayVersion")
|
||||
build, _, _ := k.GetStringValue("CurrentBuildNumber")
|
||||
|
||||
name := strings.TrimSpace(product)
|
||||
if name == "" {
|
||||
name = "Windows"
|
||||
}
|
||||
var buildNumber int
|
||||
_, _ = fmt.Sscanf(strings.TrimSpace(build), "%d", &buildNumber)
|
||||
if buildNumber >= 22000 && !strings.Contains(name, "11") {
|
||||
name = strings.Replace(name, "Windows 10", "Windows 11", 1)
|
||||
if !strings.Contains(name, "11") {
|
||||
name = "Windows 11"
|
||||
}
|
||||
}
|
||||
if display = strings.TrimSpace(display); display != "" {
|
||||
name += " " + display
|
||||
}
|
||||
return name
|
||||
}
|
||||
|
||||
func batteryStatus() (*int, bool) {
|
||||
type systemPowerStatus struct {
|
||||
ACLineStatus byte
|
||||
BatteryFlag byte
|
||||
BatteryLifePercent byte
|
||||
SystemStatusFlag byte
|
||||
BatteryLifeTime uint32
|
||||
BatteryFullLifeTime uint32
|
||||
}
|
||||
|
||||
kernel32 := windows.NewLazySystemDLL("kernel32.dll")
|
||||
getSystemPowerStatus := kernel32.NewProc("GetSystemPowerStatus")
|
||||
var status systemPowerStatus
|
||||
ret, _, _ := getSystemPowerStatus.Call(uintptr(unsafe.Pointer(&status)))
|
||||
if ret == 0 || status.BatteryLifePercent == 255 || status.BatteryFlag == 128 {
|
||||
return nil, false
|
||||
}
|
||||
percent := int(status.BatteryLifePercent)
|
||||
if percent < 0 || percent > 100 {
|
||||
return nil, false
|
||||
}
|
||||
return &percent, status.ACLineStatus == 1
|
||||
}
|
||||
|
||||
func cpuName() string {
|
||||
k, err := registry.OpenKey(registry.LOCAL_MACHINE,
|
||||
`HARDWARE\DESCRIPTION\System\CentralProcessor\0`, registry.QUERY_VALUE)
|
||||
|
||||
Reference in New Issue
Block a user