Skip to content

Commit

Permalink
formatting constants moved to common
Browse files Browse the repository at this point in the history
  • Loading branch information
dundee committed Jul 10, 2021
1 parent f9a1ac3 commit 1ea6de5
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 45 deletions.
18 changes: 18 additions & 0 deletions internal/common/ui.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,21 @@ type UI struct {
ShowApparentSize bool
PathChecker func(string) (fs.FileInfo, error)
}

// file size constants
const (
_ = iota
KB float64 = 1 << (10 * iota)
MB
GB
TB
PB
EB
)

// file count constants
const (
K int = 1e3
M int = 1e6
G int = 1e9
)
22 changes: 14 additions & 8 deletions stdout/stdout.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,15 +215,21 @@ func (ui *UI) updateProgress() {
}

func (ui *UI) formatSize(size int64) string {
fsize := float64(size)

switch {
case size > 1e12:
return ui.orange.Sprintf("%.1f", float64(size)/math.Pow(2, 40)) + " TiB"
case size > 1e9:
return ui.orange.Sprintf("%.1f", float64(size)/math.Pow(2, 30)) + " GiB"
case size > 1e6:
return ui.orange.Sprintf("%.1f", float64(size)/math.Pow(2, 20)) + " MiB"
case size > 1e3:
return ui.orange.Sprintf("%.1f", float64(size)/math.Pow(2, 10)) + " KiB"
case fsize >= common.EB:
return ui.orange.Sprintf("%.1f", fsize/common.EB) + " EiB"
case fsize >= common.PB:
return ui.orange.Sprintf("%.1f", fsize/common.PB) + " PiB"
case fsize >= common.TB:
return ui.orange.Sprintf("%.1f", fsize/common.TB) + " TiB"
case fsize >= common.GB:
return ui.orange.Sprintf("%.1f", fsize/common.GB) + " GiB"
case fsize >= common.MB:
return ui.orange.Sprintf("%.1f", fsize/common.MB) + " MiB"
case fsize >= common.KB:
return ui.orange.Sprintf("%.1f", fsize/common.KB) + " KiB"
default:
return ui.orange.Sprintf("%d", size) + " B"
}
Expand Down
15 changes: 14 additions & 1 deletion stdout/stdout_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,6 @@ func TestItemRows(t *testing.T) {
err := ui.AnalyzePath("test_dir", nil)

assert.Nil(t, err)
assert.Contains(t, output.String(), "TiB")
assert.Contains(t, output.String(), "GiB")
assert.Contains(t, output.String(), "MiB")
assert.Contains(t, output.String(), "KiB")
Expand Down Expand Up @@ -133,6 +132,20 @@ func TestMaxInt(t *testing.T) {
assert.Equal(t, 4, maxInt(4, 2))
}

func TestFormatSize(t *testing.T) {
output := bytes.NewBuffer(make([]byte, 10))

ui := CreateStdoutUI(output, true, true, true)

assert.Contains(t, ui.formatSize(1), "B")
assert.Contains(t, ui.formatSize(1<<10+1), "KiB")
assert.Contains(t, ui.formatSize(1<<20+1), "MiB")
assert.Contains(t, ui.formatSize(1<<30+1), "GiB")
assert.Contains(t, ui.formatSize(1<<40+1), "TiB")
assert.Contains(t, ui.formatSize(1<<50+1), "PiB")
assert.Contains(t, ui.formatSize(1<<60+1), "EiB")
}

// func printBuffer(buff *bytes.Buffer) {
// for i, x := range buff.String() {
// println(i, string(x))
Expand Down
55 changes: 19 additions & 36 deletions tui/format.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,10 @@ package tui
import (
"fmt"

"github.com/dundee/gdu/v5/internal/common"
"github.com/dundee/gdu/v5/pkg/analyze"
)

// file size constants
const (
_ = iota
KB float64 = 1 << (10 * iota)
MB
GB
TB
PB
EB
)

// file count constants
const (
K int = 1e3
M int = 1e6
G int = 1e9
)

func (ui *UI) formatFileRow(item analyze.Item) string {
var part int

Expand Down Expand Up @@ -88,18 +71,18 @@ func (ui *UI) formatSize(size int64, reverseColor bool, transparentBg bool) stri
fsize := float64(size)

switch {
case fsize >= EB:
return fmt.Sprintf("%.1f%s EiB", fsize/EB, color)
case fsize >= PB:
return fmt.Sprintf("%.1f%s PiB", fsize/PB, color)
case fsize >= TB:
return fmt.Sprintf("%.1f%s TiB", fsize/TB, color)
case fsize >= GB:
return fmt.Sprintf("%.1f%s GiB", fsize/GB, color)
case fsize >= MB:
return fmt.Sprintf("%.1f%s MiB", fsize/MB, color)
case fsize >= KB:
return fmt.Sprintf("%.1f%s KiB", fsize/KB, color)
case fsize >= common.EB:
return fmt.Sprintf("%.1f%s EiB", fsize/common.EB, color)
case fsize >= common.PB:
return fmt.Sprintf("%.1f%s PiB", fsize/common.PB, color)
case fsize >= common.TB:
return fmt.Sprintf("%.1f%s TiB", fsize/common.TB, color)
case fsize >= common.GB:
return fmt.Sprintf("%.1f%s GiB", fsize/common.GB, color)
case fsize >= common.MB:
return fmt.Sprintf("%.1f%s MiB", fsize/common.MB, color)
case fsize >= common.KB:
return fmt.Sprintf("%.1f%s KiB", fsize/common.KB, color)
default:
return fmt.Sprintf("%d%s B", size, color)
}
Expand All @@ -110,12 +93,12 @@ func (ui *UI) formatCount(count int) string {
color := "[-::]"

switch {
case count >= G:
row += fmt.Sprintf("%.1f%sG", float64(count)/float64(G), color)
case count >= M:
row += fmt.Sprintf("%.1f%sM", float64(count)/float64(M), color)
case count >= K:
row += fmt.Sprintf("%.1f%sk", float64(count)/float64(K), color)
case count >= common.G:
row += fmt.Sprintf("%.1f%sG", float64(count)/float64(common.G), color)
case count >= common.M:
row += fmt.Sprintf("%.1f%sM", float64(count)/float64(common.M), color)
case count >= common.K:
row += fmt.Sprintf("%.1f%sk", float64(count)/float64(common.K), color)
default:
row += fmt.Sprintf("%d%s", count, color)
}
Expand Down

0 comments on commit 1ea6de5

Please sign in to comment.