feat: add dynamic RAM unit detection and fix file descriptor leaks

This commit is contained in:
Juan Perdomo [Jakepy] 2026-06-12 18:25:12 -05:00 committed by nekohepott
parent 22da87e2b0
commit 93ec94b40e
No known key found for this signature in database

View File

@ -45,13 +45,32 @@ func GetDist() (string, string) {
return id, prettyName return id, prettyName
} }
func getSizeRam(total, avaible, used *int) string {
if *total >= 1024*1024 && *avaible >= 1024*1024 {
*used = *total - *avaible
usedGiB := *used / (1024 * 1024)
totalGiB := *total / (1024 * 1024)
ram := fmt.Sprintf("%d / %d Gi", usedGiB, totalGiB)
ram = strings.TrimSpace(ram)
return ram
}
totalMB := *total / 1024
avaibleMB := *avaible / 1024
*used = totalMB - avaibleMB
return fmt.Sprintf("%d / %d MiB", *used, totalMB)
}
func GetRam() string { func GetRam() string {
f, err := os.Open("/proc/meminfo") f, err := os.Open("/proc/meminfo")
if err != nil { if err != nil {
return "error" return "error"
} }
var total, available int defer f.Close()
var total, available, used int
s := bufio.NewScanner(f) s := bufio.NewScanner(f)
for s.Scan() { for s.Scan() {
line := s.Text() line := s.Text()
@ -68,11 +87,8 @@ func GetRam() string {
} }
} }
} }
totalMB := total / 1024
availableMB := available / 1024
usedMB := totalMB - availableMB
ram := fmt.Sprintf("%d / %d MiB", usedMB, totalMB) ram := getSizeRam(&total, &available, &used)
ram = strings.TrimSpace(ram) ram = strings.TrimSpace(ram)
return ram return ram
@ -84,6 +100,8 @@ func GetCpu() string {
return "unknown" return "unknown"
} }
defer f.Close()
var cpu string var cpu string
s := bufio.NewScanner(f) s := bufio.NewScanner(f)
for s.Scan() { for s.Scan() {