move decimal place counts in main to consts, update const names in formatter, and remove byte format

This commit is contained in:
frosty 2024-07-30 04:14:13 -04:00
parent 336111f854
commit 1e204a2ab7
2 changed files with 16 additions and 14 deletions

View file

@ -19,26 +19,23 @@ package ui
import "fmt"
const (
Kibibyte = 1
Mebibyte = 1024 * Kibibyte
Gibibyte = 1024 * Mebibyte
kibibyte = 1
mebibyte = 1024 * kibibyte
gibibyte = 1024 * mebibyte
)
func PrettifySize(sizeKiB uint64, decimalPlaces uint8) string {
func PrettifySize(sizeKibibytes uint64, decimalPlaces uint8) string {
var size float64
var unit string
if sizeKiB < Kibibyte {
size = float64(sizeKiB)
unit = "B"
} else if sizeKiB < Mebibyte {
size = float64(sizeKiB)
if sizeKibibytes < mebibyte {
size = float64(sizeKibibytes)
unit = "KiB"
} else if sizeKiB < Gibibyte {
size = float64(sizeKiB) / float64(Mebibyte)
} else if sizeKibibytes < gibibyte {
size = float64(sizeKibibytes) / float64(mebibyte)
unit = "MiB"
} else {
size = float64(sizeKiB) / float64(Gibibyte)
size = float64(sizeKibibytes) / float64(gibibyte)
unit = "GiB"
}

View file

@ -24,6 +24,11 @@ import (
"codeberg.org/frosty/sysinfo/lib/ui"
)
const (
memoryDecimalPlaces = 2
cpuUsageDecimalPlaces = 2
)
func main() {
osInfo, err := readers.ReadOs()
if err != nil {
@ -35,11 +40,11 @@ func main() {
if err != nil {
log.Fatalf("%v\n", err)
}
fmt.Printf("RAM: %v / %v\n", ui.PrettifySize(memoryInfo.Used, 2), ui.PrettifySize(memoryInfo.Total, 2))
fmt.Printf("RAM: %v / %v\n", ui.PrettifySize(memoryInfo.Used, memoryDecimalPlaces), ui.PrettifySize(memoryInfo.Total, memoryDecimalPlaces))
cpuUsageInfo, err := readers.ReadCpuUsage()
if err != nil {
log.Fatalf("%v\n", err)
}
fmt.Printf("CPU: %.*f%%\n", 2, cpuUsageInfo.UsagePercent)
fmt.Printf("CPU: %.*f%%\n", cpuUsageDecimalPlaces, cpuUsageInfo.UsagePercent)
}