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" import "fmt"
const ( const (
Kibibyte = 1 kibibyte = 1
Mebibyte = 1024 * Kibibyte mebibyte = 1024 * kibibyte
Gibibyte = 1024 * Mebibyte gibibyte = 1024 * mebibyte
) )
func PrettifySize(sizeKiB uint64, decimalPlaces uint8) string { func PrettifySize(sizeKibibytes uint64, decimalPlaces uint8) string {
var size float64 var size float64
var unit string var unit string
if sizeKiB < Kibibyte { if sizeKibibytes < mebibyte {
size = float64(sizeKiB) size = float64(sizeKibibytes)
unit = "B"
} else if sizeKiB < Mebibyte {
size = float64(sizeKiB)
unit = "KiB" unit = "KiB"
} else if sizeKiB < Gibibyte { } else if sizeKibibytes < gibibyte {
size = float64(sizeKiB) / float64(Mebibyte) size = float64(sizeKibibytes) / float64(mebibyte)
unit = "MiB" unit = "MiB"
} else { } else {
size = float64(sizeKiB) / float64(Gibibyte) size = float64(sizeKibibytes) / float64(gibibyte)
unit = "GiB" unit = "GiB"
} }

View file

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