Skip to content

Commit

Permalink
added thousand separator to item count number
Browse files Browse the repository at this point in the history
closes #77
  • Loading branch information
dundee committed Jul 23, 2021
1 parent da5f797 commit 0ed27ba
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 3 deletions.
19 changes: 19 additions & 0 deletions internal/common/ui.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package common
import (
"io/fs"
"regexp"
"strconv"

"github.com/dundee/gdu/v5/pkg/analyze"
)
Expand Down Expand Up @@ -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)
}
12 changes: 12 additions & 0 deletions internal/common/ui_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
2 changes: 1 addition & 1 deletion report/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
Expand Down
2 changes: 1 addition & 1 deletion stdout/stdout.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))

Expand Down
2 changes: 1 addition & 1 deletion tui/tui.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) +
Expand Down

0 comments on commit 0ed27ba

Please sign in to comment.