modbot/lib/ui/formatter.go

45 lines
1.3 KiB
Go
Raw Normal View History

2024-07-30 04:02:05 -04:00
// sysinfo queries information about your system
// Copyright (C) 2024 frosty <inthishouseofcards@gmail.com>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
2024-07-30 04:04:03 -04:00
package ui
2024-07-30 04:02:05 -04:00
import "fmt"
const (
kibibyte = 1
mebibyte = 1024 * kibibyte
gibibyte = 1024 * mebibyte
2024-07-30 04:02:05 -04:00
)
func PrettifySize(sizeKibibytes uint64, decimalPlaces uint8) string {
2024-07-30 04:02:05 -04:00
var size float64
var unit string
if sizeKibibytes < mebibyte {
size = float64(sizeKibibytes)
2024-07-30 04:02:05 -04:00
unit = "KiB"
} else if sizeKibibytes < gibibyte {
size = float64(sizeKibibytes) / float64(mebibyte)
2024-07-30 04:02:05 -04:00
unit = "MiB"
} else {
size = float64(sizeKibibytes) / float64(gibibyte)
2024-07-30 04:02:05 -04:00
unit = "GiB"
}
format := fmt.Sprintf("%%.%df %s", decimalPlaces, unit)
return fmt.Sprintf(format, size)
}