2024-08-14 12:28:29 -04:00
|
|
|
// modbot is a system information agregator
|
2024-07-30 04:02:05 -04:00
|
|
|
// 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 (
|
2024-08-14 12:28:29 -04:00
|
|
|
Kibibyte = 1
|
|
|
|
Mebibyte = 1024 * Kibibyte
|
|
|
|
Gibibyte = 1024 * Mebibyte
|
|
|
|
Tebibyte = 1024 * Gibibyte
|
2024-07-30 04:02:05 -04:00
|
|
|
)
|
|
|
|
|
2024-08-14 12:28:29 -04:00
|
|
|
func PrettifyKib(sizeKibibytes uint64, decimalPlaces uint8) string {
|
2024-07-30 04:02:05 -04:00
|
|
|
var size float64
|
|
|
|
var unit string
|
|
|
|
|
2024-08-14 12:28:29 -04:00
|
|
|
if sizeKibibytes < Mebibyte {
|
2024-07-30 04:14:13 -04:00
|
|
|
size = float64(sizeKibibytes)
|
2024-07-30 04:02:05 -04:00
|
|
|
unit = "KiB"
|
2024-08-14 12:28:29 -04:00
|
|
|
} else if sizeKibibytes < Gibibyte {
|
|
|
|
size = float64(sizeKibibytes) / float64(Mebibyte)
|
2024-07-30 04:02:05 -04:00
|
|
|
unit = "MiB"
|
2024-08-14 12:28:29 -04:00
|
|
|
} else if sizeKibibytes < Tebibyte {
|
|
|
|
size = float64(sizeKibibytes) / float64(Gibibyte)
|
2024-07-30 04:02:05 -04:00
|
|
|
unit = "GiB"
|
2024-08-14 12:28:29 -04:00
|
|
|
} else {
|
|
|
|
size = float64(sizeKibibytes) / float64(Tebibyte)
|
|
|
|
unit = "TiB"
|
2024-07-30 04:02:05 -04:00
|
|
|
}
|
|
|
|
|
2024-08-14 12:28:29 -04:00
|
|
|
return fmt.Sprintf(fmt.Sprintf("%%.%df %s", decimalPlaces, unit), size)
|
2024-07-30 04:02:05 -04:00
|
|
|
}
|