Skip to content

Commit

Permalink
Chore(ostor): remove duplicate formatter
Browse files Browse the repository at this point in the history
  • Loading branch information
till committed Oct 5, 2024
1 parent d7c066c commit f826ca1
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 30 deletions.
22 changes: 1 addition & 21 deletions internal/cmd/users.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func Users(cCtx *cli.Context) error {

for _, u := range users.Users {
if cCtx.Bool("usage") {
tbl.AddRow(u.Email, u.ID, u.State, formatBytes(u.Space.Current))
tbl.AddRow(u.Email, u.ID, u.State, utils.PrettyByteSize(int(u.Space.Current)))
} else {
tbl.AddRow(u.Email, u.ID, u.State)
}
Expand Down Expand Up @@ -165,23 +165,3 @@ func CreateKey(cCtx *cli.Context) error {

return nil
}

func formatBytes(bytes int64) string {
const (
KB = 1024
MB = KB * 1024
GB = MB * 1024
TB = GB * 1024
)

switch {
case bytes >= TB:
return fmt.Sprintf("%.2f TB", float64(bytes)/float64(TB))
case bytes >= GB:
return fmt.Sprintf("%.2f GB", float64(bytes)/float64(GB))
case bytes >= MB:
return fmt.Sprintf("%.2f MB", float64(bytes)/float64(MB))
default:
return fmt.Sprintf("%d Bytes", bytes)
}
}
28 changes: 19 additions & 9 deletions internal/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,28 @@ package utils

import (
"fmt"
"math"
)

// credit:
// https://gist.github.com/anikitenko/b41206a49727b83a530142c76b1cb82d?permalink_comment_id=4467913#gistcomment-4467913
func PrettyByteSize(b int) string {
bf := float64(b)
for _, unit := range []string{"", "Ki", "Mi", "Gi", "Ti", "Pi", "Ei", "Zi"} {
if math.Abs(bf) < 1024.0 {
return fmt.Sprintf("%3.1f%sB", bf, unit)
}
bf /= 1024.0
func PrettyByteSize(bytes int) string {
const (
KB = 1024
MB = KB * 1024
GB = MB * 1024
TB = GB * 1024
)

switch {
case bytes >= TB:
return fmt.Sprintf("%.2f TB", float64(bytes)/float64(TB))
case bytes >= GB:
return fmt.Sprintf("%.2f GB", float64(bytes)/float64(GB))
case bytes >= MB:
return fmt.Sprintf("%.2f MB", float64(bytes)/float64(MB))
case bytes >= KB:
return fmt.Sprintf("%.2f KB", float64(bytes)/float64(KB))
default:
return fmt.Sprintf("%d B", bytes)
}
return fmt.Sprintf("%.1fYiB", bf)
}

0 comments on commit f826ca1

Please sign in to comment.