From 0ed27ba9bf8f98b8d798757c8ced61d988a9e286 Mon Sep 17 00:00:00 2001 From: Daniel Milde Date: Fri, 23 Jul 2021 21:40:18 +0200 Subject: [PATCH] added thousand separator to item count number closes #77 --- internal/common/ui.go | 19 +++++++++++++++++++ internal/common/ui_test.go | 12 ++++++++++++ report/export.go | 2 +- stdout/stdout.go | 2 +- tui/tui.go | 2 +- 5 files changed, 34 insertions(+), 3 deletions(-) create mode 100644 internal/common/ui_test.go diff --git a/internal/common/ui.go b/internal/common/ui.go index 6039fb1bc..beebf9466 100644 --- a/internal/common/ui.go +++ b/internal/common/ui.go @@ -3,6 +3,7 @@ package common import ( "io/fs" "regexp" + "strconv" "github.com/dundee/gdu/v5/pkg/analyze" ) @@ -36,3 +37,21 @@ const ( M int = 1e6 G int = 1e9 ) + +// FormatNumber returns number as a string with thousands separator +func FormatNumber(n int64) string { + in := []byte(strconv.FormatInt(n, 10)) + + var out []byte + if i := len(in) % 3; i != 0 { + if out, in = append(out, in[:i]...), in[i:]; len(in) > 0 { + out = append(out, ',') + } + } + for len(in) > 0 { + if out, in = append(out, in[:3]...), in[3:]; len(in) > 0 { + out = append(out, ',') + } + } + return string(out) +} diff --git a/internal/common/ui_test.go b/internal/common/ui_test.go new file mode 100644 index 000000000..3a6c28a6b --- /dev/null +++ b/internal/common/ui_test.go @@ -0,0 +1,12 @@ +package common + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestFormatNumber(t *testing.T) { + res := FormatNumber(1234567890) + assert.Equal(t, "1,234,567,890", res) +} diff --git a/report/export.go b/report/export.go index fc187fba9..4823c95d9 100644 --- a/report/export.go +++ b/report/export.go @@ -164,7 +164,7 @@ func (ui *UI) updateProgress() { fmt.Fprint(ui.output, "Writing output file...") } else { fmt.Fprint(ui.output, "Scanning... Total items: "+ - ui.red.Sprint(progress.ItemCount)+ + ui.red.Sprint(common.FormatNumber(int64(progress.ItemCount)))+ " size: "+ ui.formatSize(progress.TotalSize)) } diff --git a/stdout/stdout.go b/stdout/stdout.go index b572e9a58..21acfff30 100644 --- a/stdout/stdout.go +++ b/stdout/stdout.go @@ -285,7 +285,7 @@ func (ui *UI) updateProgress() { fmt.Fprintf(ui.output, "\r %s ", string(progressRunes[i])) fmt.Fprint(ui.output, "Scanning... Total items: "+ - ui.red.Sprint(progress.ItemCount)+ + ui.red.Sprint(common.FormatNumber(int64(progress.ItemCount)))+ " size: "+ ui.formatSize(progress.TotalSize)) diff --git a/tui/tui.go b/tui/tui.go index 434279112..c3244f919 100644 --- a/tui/tui.go +++ b/tui/tui.go @@ -355,7 +355,7 @@ func (ui *UI) updateProgress() { ui.app.QueueUpdateDraw(func() { ui.progress.SetText("Total items: " + color + - fmt.Sprint(itemCount) + + common.FormatNumber(int64(itemCount)) + "[white:black:-] size: " + color + ui.formatSize(totalSize, false, false) +