Skip to content

Commit

Permalink
feat: show sizes as raw numbers without prefixes
Browse files Browse the repository at this point in the history
closes #147
  • Loading branch information
dundee committed Aug 2, 2022
1 parent 386ada6 commit 5d7cd6a
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 18 deletions.
6 changes: 6 additions & 0 deletions cmd/gdu/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ type Flags struct {
ConstGC bool
Summarize bool
UseSIPrefix bool
NoPrefix bool
}

// App defines the main application
Expand Down Expand Up @@ -106,6 +107,10 @@ func (a *App) Run() (err error) {

log.Printf("Runtime flags: %+v", *a.Flags)

if a.Flags.NoPrefix && a.Flags.UseSIPrefix {
return fmt.Errorf("--no-prefix and --si cannot be used at once")
}

path := a.getPath()
path, _ = filepath.Abs(path)

Expand Down Expand Up @@ -199,6 +204,7 @@ func (a *App) createUI() (UI, error) {
a.Flags.Summarize,
a.Flags.ConstGC,
a.Flags.UseSIPrefix,
a.Flags.NoPrefix,
)
} else {
ui = tui.CreateUI(
Expand Down
1 change: 1 addition & 0 deletions cmd/gdu/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ func init() {
flags.BoolVarP(&af.NoProgress, "no-progress", "p", false, "Do not show progress in non-interactive mode")
flags.BoolVarP(&af.Summarize, "summarize", "s", false, "Show only a total in non-interactive mode")
flags.BoolVar(&af.UseSIPrefix, "si", false, "Show sizes with decimal SI prefixes (kB, MB, GB) instead of binary prefixes (KiB, MiB, GiB)")
flags.BoolVar(&af.NoPrefix, "no-prefix", false, "Show sizes as raw numbers without any prefixes (SI or binary) in non-interactive mode")
}

func runE(command *cobra.Command, args []string) error {
Expand Down
21 changes: 19 additions & 2 deletions gdu.1
Original file line number Diff line number Diff line change
@@ -1,6 +1,20 @@
.\" Automatically generated by Pandoc 2.14.2
.\" Automatically generated by Pandoc 2.18
.\"
.TH "gdu" "1" "2022-01-18" "" ""
.\" Define V font for inline verbatim, using C font in formats
.\" that render this, and otherwise B font.
.ie "\f[CB]x\f[]"x" \{\
. ftr V B
. ftr VI BI
. ftr VB B
. ftr VBI BI
.\}
.el \{\
. ftr V CR
. ftr VI CI
. ftr VB CB
. ftr VBI CBI
.\}
.TH "gdu" "1" "2022-08-03" "" ""
.hy
.SH NAME
.PP
Expand Down Expand Up @@ -56,6 +70,9 @@ non-interactive mode
\f[B]--si\f[R][=false] Show sizes with decimal SI prefixes (kB, MB, GB)
instead of binary prefixes (KiB, MiB, GiB)
.PP
\f[B]--no-prefix\f[R][=false] Show sizes as raw numbers without any
prefixes (SI or binary) in non-interactive mode
.PP
\f[B]-f\f[R], \f[B]-\[em]input-file\f[R] Import analysis from JSON file.
If the file is \[dq]-\[dq], read from standard input.
.PP
Expand Down
2 changes: 2 additions & 0 deletions gdu.1.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ non-interactive mode

**\--si**\[=false\] Show sizes with decimal SI prefixes (kB, MB, GB) instead of binary prefixes (KiB, MiB, GiB)

**\--no-prefix**\[=false\] Show sizes as raw numbers without any prefixes (SI or binary) in non-interactive mode

**-f**, **\----input-file** Import analysis from JSON file. If the file is \"-\", read from standard input.

**-o**, **\----output-file** Export all info into file as JSON. If the file is \"-\", write to standard output.
Expand Down
6 changes: 6 additions & 0 deletions stdout/stdout.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ type UI struct {
orange *color.Color
blue *color.Color
summarize bool
noPrefix bool
}

var progressRunes = []rune(`⠇⠏⠋⠙⠹⠸⠼⠴⠦⠧`)
Expand All @@ -39,6 +40,7 @@ func CreateStdoutUI(
summarize bool,
constGC bool,
useSIPrefix bool,
noPrefix bool,
) *UI {
ui := &UI{
UI: &common.UI{
Expand All @@ -52,6 +54,7 @@ func CreateStdoutUI(
},
output: output,
summarize: summarize,
noPrefix: noPrefix,
}

ui.red = color.New(color.FgRed).Add(color.Bold)
Expand Down Expand Up @@ -336,6 +339,9 @@ func (ui *UI) updateProgress() {
}

func (ui *UI) formatSize(size int64) string {
if ui.noPrefix {
return ui.orange.Sprintf("%d", size)
}
if ui.UseSIPrefix {
return ui.formatWithDecPrefix(size)
}
Expand Down
2 changes: 1 addition & 1 deletion stdout/stdout_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func TestShowDevicesWithErr(t *testing.T) {
output := bytes.NewBuffer(make([]byte, 10))

getter := device.LinuxDevicesInfoGetter{MountsPath: "/xyzxyz"}
ui := CreateStdoutUI(output, false, true, false, false, false, false, false)
ui := CreateStdoutUI(output, false, true, false, false, false, false, false, false)
err := ui.ListDevices(getter)

assert.Contains(t, err.Error(), "no such file")
Expand Down
44 changes: 29 additions & 15 deletions stdout/stdout_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func TestAnalyzePath(t *testing.T) {
buff := make([]byte, 10)
output := bytes.NewBuffer(buff)

ui := CreateStdoutUI(output, false, false, false, false, false, true, false)
ui := CreateStdoutUI(output, false, false, false, false, false, true, false, false)
ui.SetIgnoreDirPaths([]string{"/xxx"})
err := ui.AnalyzePath("test_dir", nil)
assert.Nil(t, err)
Expand All @@ -42,7 +42,7 @@ func TestShowSummary(t *testing.T) {
buff := make([]byte, 10)
output := bytes.NewBuffer(buff)

ui := CreateStdoutUI(output, true, false, true, false, true, false, false)
ui := CreateStdoutUI(output, true, false, true, false, true, false, false, false)
ui.SetIgnoreDirPaths([]string{"/xxx"})
err := ui.AnalyzePath("test_dir", nil)
assert.Nil(t, err)
Expand All @@ -59,7 +59,7 @@ func TestShowSummaryBw(t *testing.T) {
buff := make([]byte, 10)
output := bytes.NewBuffer(buff)

ui := CreateStdoutUI(output, false, false, false, false, true, false, false)
ui := CreateStdoutUI(output, false, false, false, false, true, false, false, false)
ui.SetIgnoreDirPaths([]string{"/xxx"})
err := ui.AnalyzePath("test_dir", nil)
assert.Nil(t, err)
Expand All @@ -76,7 +76,7 @@ func TestAnalyzeSubdir(t *testing.T) {
buff := make([]byte, 10)
output := bytes.NewBuffer(buff)

ui := CreateStdoutUI(output, false, false, false, false, false, false, false)
ui := CreateStdoutUI(output, false, false, false, false, false, false, false, false)
ui.SetIgnoreDirPaths([]string{"/xxx"})
err := ui.AnalyzePath("test_dir/nested", nil)
assert.Nil(t, err)
Expand All @@ -93,7 +93,7 @@ func TestAnalyzePathWithColors(t *testing.T) {
buff := make([]byte, 10)
output := bytes.NewBuffer(buff)

ui := CreateStdoutUI(output, true, false, true, false, false, false, false)
ui := CreateStdoutUI(output, true, false, true, false, false, false, false, false)
ui.SetIgnoreDirPaths([]string{"/xxx"})
err := ui.AnalyzePath("test_dir/nested", nil)

Expand All @@ -104,7 +104,7 @@ func TestAnalyzePathWithColors(t *testing.T) {
func TestItemRows(t *testing.T) {
output := bytes.NewBuffer(make([]byte, 10))

ui := CreateStdoutUI(output, false, true, false, false, false, false, false)
ui := CreateStdoutUI(output, false, true, false, false, false, false, false, false)
ui.Analyzer = &testanalyze.MockedAnalyzer{}
err := ui.AnalyzePath("test_dir", nil)

Expand All @@ -118,7 +118,7 @@ func TestAnalyzePathWithProgress(t *testing.T) {

output := bytes.NewBuffer(make([]byte, 10))

ui := CreateStdoutUI(output, false, true, true, false, false, false, false)
ui := CreateStdoutUI(output, false, true, true, false, false, false, false, false)
ui.SetIgnoreDirPaths([]string{"/xxx"})
err := ui.AnalyzePath("test_dir", nil)

Expand All @@ -129,7 +129,7 @@ func TestAnalyzePathWithProgress(t *testing.T) {
func TestShowDevices(t *testing.T) {
output := bytes.NewBuffer(make([]byte, 10))

ui := CreateStdoutUI(output, false, true, false, false, false, false, false)
ui := CreateStdoutUI(output, false, true, false, false, false, false, false, false)
err := ui.ListDevices(getDevicesInfoMock())

assert.Nil(t, err)
Expand All @@ -140,7 +140,7 @@ func TestShowDevices(t *testing.T) {
func TestShowDevicesWithColor(t *testing.T) {
output := bytes.NewBuffer(make([]byte, 10))

ui := CreateStdoutUI(output, true, true, true, false, false, false, false)
ui := CreateStdoutUI(output, true, true, true, false, false, false, false, false)
err := ui.ListDevices(getDevicesInfoMock())

assert.Nil(t, err)
Expand All @@ -154,7 +154,7 @@ func TestReadAnalysisWithColor(t *testing.T) {

output := bytes.NewBuffer(make([]byte, 10))

ui := CreateStdoutUI(output, true, true, true, false, false, false, false)
ui := CreateStdoutUI(output, true, true, true, false, false, false, false, false)
err = ui.ReadAnalysis(input)

assert.Nil(t, err)
Expand All @@ -167,7 +167,7 @@ func TestReadAnalysisBw(t *testing.T) {

output := bytes.NewBuffer(make([]byte, 10))

ui := CreateStdoutUI(output, false, false, false, false, false, false, false)
ui := CreateStdoutUI(output, false, false, false, false, false, false, false, false)
err = ui.ReadAnalysis(input)

assert.Nil(t, err)
Expand All @@ -180,7 +180,7 @@ func TestReadAnalysisWithWrongFile(t *testing.T) {

output := bytes.NewBuffer(make([]byte, 10))

ui := CreateStdoutUI(output, true, true, true, false, false, false, false)
ui := CreateStdoutUI(output, true, true, true, false, false, false, false, false)
err = ui.ReadAnalysis(input)

assert.NotNil(t, err)
Expand All @@ -192,7 +192,7 @@ func TestReadAnalysisWithSummarize(t *testing.T) {

output := bytes.NewBuffer(make([]byte, 10))

ui := CreateStdoutUI(output, false, false, false, false, true, false, false)
ui := CreateStdoutUI(output, false, false, false, false, true, false, false, false)
err = ui.ReadAnalysis(input)

assert.Nil(t, err)
Expand All @@ -207,7 +207,7 @@ func TestMaxInt(t *testing.T) {
func TestFormatSize(t *testing.T) {
output := bytes.NewBuffer(make([]byte, 10))

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

assert.Contains(t, ui.formatSize(1), "B")
assert.Contains(t, ui.formatSize(1<<10+1), "KiB")
Expand All @@ -221,7 +221,7 @@ func TestFormatSize(t *testing.T) {
func TestFormatSizeDec(t *testing.T) {
output := bytes.NewBuffer(make([]byte, 10))

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

assert.Contains(t, ui.formatSize(1), "B")
assert.Contains(t, ui.formatSize(1<<10+1), "kB")
Expand All @@ -232,6 +232,20 @@ func TestFormatSizeDec(t *testing.T) {
assert.Contains(t, ui.formatSize(1<<60+1), "EB")
}

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

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

assert.Equal(t, ui.formatSize(1), "1")
assert.Equal(t, ui.formatSize(1<<10+1), "1025")
assert.Equal(t, ui.formatSize(1<<20+1), "1048577")
assert.Equal(t, ui.formatSize(1<<30+1), "1073741825")
assert.Equal(t, ui.formatSize(1<<40+1), "1099511627777")
assert.Equal(t, ui.formatSize(1<<50+1), "1125899906842625")
assert.Equal(t, ui.formatSize(1<<60+1), "1152921504606846977")
}

// func printBuffer(buff *bytes.Buffer) {
// for i, x := range buff.String() {
// println(i, string(x))
Expand Down

0 comments on commit 5d7cd6a

Please sign in to comment.