orama/pkg/cli/shared/format.go

45 lines
1.0 KiB
Go

package shared
import "fmt"
// FormatBytes formats a byte count into a human-readable string (KB, MB, GB, etc.)
func FormatBytes(bytes int64) string {
const unit = 1024
if bytes < unit {
return fmt.Sprintf("%d B", bytes)
}
div, exp := int64(unit), 0
for n := bytes / unit; n >= unit; n /= unit {
div *= unit
exp++
}
return fmt.Sprintf("%.1f %cB", float64(bytes)/float64(div), "KMGTPE"[exp])
}
// FormatUptime formats seconds into a human-readable uptime string.
func FormatUptime(seconds float64) string {
s := int(seconds)
days := s / 86400
hours := (s % 86400) / 3600
mins := (s % 3600) / 60
if days > 0 {
return fmt.Sprintf("%dd %dh %dm", days, hours, mins)
}
if hours > 0 {
return fmt.Sprintf("%dh %dm", hours, mins)
}
return fmt.Sprintf("%dm", mins)
}
// FormatSize formats a megabyte value into a human-readable string.
func FormatSize(mb float64) string {
if mb < 0.1 {
return fmt.Sprintf("%.1f KB", mb*1024)
}
if mb >= 1024 {
return fmt.Sprintf("%.1f GB", mb/1024)
}
return fmt.Sprintf("%.1f MB", mb)
}