Skip to content

Commit

Permalink
show file count [BC break]
Browse files Browse the repository at this point in the history
closes dundee#55
  • Loading branch information
dundee committed May 22, 2021
1 parent 0f6922d commit 29e298c
Show file tree
Hide file tree
Showing 26 changed files with 169 additions and 61 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
NAME := gdu
MAJOR_VER := v4
MAJOR_VER := v5
PACKAGE := github.com/dundee/$(NAME)/$(MAJOR_VER)
CMD_GDU := cmd/gdu
VERSION := $(shell git describe --tags 2>/dev/null)
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ Using curl:

[Go](https://pkg.go.dev/github.com/dundee/gdu):

go get -u github.com/dundee/gdu/v4/cmd/gdu
go get -u github.com/dundee/gdu/v5/cmd/gdu


## Usage
Expand Down
12 changes: 6 additions & 6 deletions cmd/gdu/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ import (
"os"
"runtime"

"github.com/dundee/gdu/v4/build"
"github.com/dundee/gdu/v4/internal/common"
"github.com/dundee/gdu/v4/pkg/analyze"
"github.com/dundee/gdu/v4/pkg/device"
"github.com/dundee/gdu/v4/stdout"
"github.com/dundee/gdu/v4/tui"
"github.com/dundee/gdu/v5/build"
"github.com/dundee/gdu/v5/internal/common"
"github.com/dundee/gdu/v5/pkg/analyze"
"github.com/dundee/gdu/v5/pkg/device"
"github.com/dundee/gdu/v5/stdout"
"github.com/dundee/gdu/v5/tui"
"github.com/gdamore/tcell/v2"
"github.com/rivo/tview"
)
Expand Down
8 changes: 4 additions & 4 deletions cmd/gdu/app/app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ import (
"strings"
"testing"

"github.com/dundee/gdu/v4/internal/testapp"
"github.com/dundee/gdu/v4/internal/testdev"
"github.com/dundee/gdu/v4/internal/testdir"
"github.com/dundee/gdu/v4/pkg/device"
"github.com/dundee/gdu/v5/internal/testapp"
"github.com/dundee/gdu/v5/internal/testdev"
"github.com/dundee/gdu/v5/internal/testdir"
"github.com/dundee/gdu/v5/pkg/device"
"github.com/stretchr/testify/assert"
)

Expand Down
4 changes: 2 additions & 2 deletions cmd/gdu/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import (
"os"
"runtime"

"github.com/dundee/gdu/v4/cmd/gdu/app"
"github.com/dundee/gdu/v4/pkg/device"
"github.com/dundee/gdu/v5/cmd/gdu/app"
"github.com/dundee/gdu/v5/pkg/device"
"github.com/gdamore/tcell/v2"
"github.com/mattn/go-isatty"
"github.com/rivo/tview"
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module github.com/dundee/gdu/v4
module github.com/dundee/gdu/v5

go 1.16

Expand Down
2 changes: 1 addition & 1 deletion internal/common/ignore.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"regexp"
"strings"

"github.com/dundee/gdu/v4/pkg/analyze"
"github.com/dundee/gdu/v5/pkg/analyze"
)

// CreateIgnorePattern creates one pattern from all path patterns
Expand Down
2 changes: 1 addition & 1 deletion internal/common/ignore_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package common_test
import (
"testing"

"github.com/dundee/gdu/v4/internal/common"
"github.com/dundee/gdu/v5/internal/common"
"github.com/stretchr/testify/assert"
)

Expand Down
2 changes: 1 addition & 1 deletion internal/common/ui.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"io/fs"
"regexp"

"github.com/dundee/gdu/v4/pkg/analyze"
"github.com/dundee/gdu/v5/pkg/analyze"
)

// UI struct
Expand Down
2 changes: 1 addition & 1 deletion internal/testanalyze/analyze.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package testanalyze
import (
"errors"

"github.com/dundee/gdu/v4/pkg/analyze"
"github.com/dundee/gdu/v5/pkg/analyze"
)

// MockedAnalyzer returns dir with files with diferent size exponents
Expand Down
2 changes: 1 addition & 1 deletion internal/testapp/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"errors"
"sync"

"github.com/dundee/gdu/v4/internal/common"
"github.com/dundee/gdu/v5/internal/common"
"github.com/gdamore/tcell/v2"
"github.com/rivo/tview"
)
Expand Down
2 changes: 1 addition & 1 deletion internal/testdev/dev.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package testdev

import "github.com/dundee/gdu/v4/pkg/device"
import "github.com/dundee/gdu/v5/pkg/device"

// DevicesInfoGetterMock is mock of DevicesInfoGetter
type DevicesInfoGetterMock struct {
Expand Down
2 changes: 1 addition & 1 deletion pkg/analyze/dir_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"sort"
"testing"

"github.com/dundee/gdu/v4/internal/testdir"
"github.com/dundee/gdu/v5/internal/testdir"
"github.com/stretchr/testify/assert"
)

Expand Down
2 changes: 1 addition & 1 deletion pkg/analyze/file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"os"
"testing"

"github.com/dundee/gdu/v4/internal/testdir"
"github.com/dundee/gdu/v5/internal/testdir"
"github.com/stretchr/testify/assert"
)

Expand Down
6 changes: 3 additions & 3 deletions stdout/stdout.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ import (
"sync"
"time"

"github.com/dundee/gdu/v4/internal/common"
"github.com/dundee/gdu/v4/pkg/analyze"
"github.com/dundee/gdu/v4/pkg/device"
"github.com/dundee/gdu/v5/internal/common"
"github.com/dundee/gdu/v5/pkg/analyze"
"github.com/dundee/gdu/v5/pkg/device"
"github.com/fatih/color"
)

Expand Down
8 changes: 4 additions & 4 deletions stdout/stdout_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ import (
"bytes"
"testing"

"github.com/dundee/gdu/v4/internal/testanalyze"
"github.com/dundee/gdu/v4/internal/testdev"
"github.com/dundee/gdu/v4/internal/testdir"
"github.com/dundee/gdu/v4/pkg/device"
"github.com/dundee/gdu/v5/internal/testanalyze"
"github.com/dundee/gdu/v5/internal/testdev"
"github.com/dundee/gdu/v5/internal/testdir"
"github.com/dundee/gdu/v5/pkg/device"
"github.com/stretchr/testify/assert"
)

Expand Down
4 changes: 2 additions & 2 deletions tui/actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import (
"path/filepath"
"runtime"

"github.com/dundee/gdu/v4/pkg/analyze"
"github.com/dundee/gdu/v4/pkg/device"
"github.com/dundee/gdu/v5/pkg/analyze"
"github.com/dundee/gdu/v5/pkg/device"
"github.com/gdamore/tcell/v2"
"github.com/rivo/tview"
)
Expand Down
10 changes: 5 additions & 5 deletions tui/actions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ import (
"runtime"
"testing"

"github.com/dundee/gdu/v4/internal/testanalyze"
"github.com/dundee/gdu/v4/internal/testapp"
"github.com/dundee/gdu/v4/internal/testdir"
"github.com/dundee/gdu/v4/pkg/analyze"
"github.com/dundee/gdu/v4/pkg/device"
"github.com/dundee/gdu/v5/internal/testanalyze"
"github.com/dundee/gdu/v5/internal/testapp"
"github.com/dundee/gdu/v5/internal/testdir"
"github.com/dundee/gdu/v5/pkg/analyze"
"github.com/dundee/gdu/v5/pkg/device"
"github.com/gdamore/tcell/v2"
"github.com/stretchr/testify/assert"
)
Expand Down
44 changes: 43 additions & 1 deletion tui/format.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package tui
import (
"fmt"

"github.com/dundee/gdu/v4/pkg/analyze"
"github.com/dundee/gdu/v5/pkg/analyze"
)

// file size constants
Expand All @@ -17,6 +17,16 @@ const (
EB
)

// file count constants
const (
K int = 1e3
M int = 1e6
G int = 1e9
T int = 1e12
P int = 1e15
E int = 1e18
)

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

Expand All @@ -42,6 +52,15 @@ func (ui *UI) formatFileRow(item analyze.Item) string {

row += getUsageGraph(part)

if ui.showItemCount {
if ui.UseColors {
row += "[#e67100::b]"
} else {
row += "[::b]"
}
row += fmt.Sprintf("%11s ", ui.formatCount(item.GetItemCount()))
}

if item.IsDir() {
if ui.UseColors {
row += "[#3498db::b]/"
Expand Down Expand Up @@ -88,3 +107,26 @@ func (ui *UI) formatSize(size int64, reverseColor bool, transparentBg bool) stri
return fmt.Sprintf("%d%s B", size, color)
}
}

func (ui *UI) formatCount(count int) string {
row := ""
color := "[-::]"

switch {
case count >= E:
row += fmt.Sprintf("%.1f%sE", float64(count)/float64(E), color)
case count >= P:
row += fmt.Sprintf("%.1f%sP", float64(count)/float64(P), color)
case count >= T:
row += fmt.Sprintf("%.1f%sT", float64(count)/float64(T), color)
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)
default:
row += fmt.Sprintf("%d%s", count, color)
}
return row
}
15 changes: 14 additions & 1 deletion tui/format_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package tui
import (
"testing"

"github.com/dundee/gdu/v4/internal/testapp"
"github.com/dundee/gdu/v5/internal/testapp"
"github.com/stretchr/testify/assert"
)

Expand All @@ -19,3 +19,16 @@ func TestFormatSize(t *testing.T) {
assert.Equal(t, "1.0[white:black:-] PiB", ui.formatSize(1<<50, false, false))
assert.Equal(t, "1.0[white:black:-] EiB", ui.formatSize(1<<60, false, false))
}

func TestFormatCount(t *testing.T) {
app := testapp.CreateMockedApp(true)
ui := CreateUI(app, false, false)

assert.Equal(t, "1[-::]", ui.formatCount(1))
assert.Equal(t, "1.0[-::]k", ui.formatCount(1<<10))
assert.Equal(t, "1.0[-::]M", ui.formatCount(1<<20))
assert.Equal(t, "1.1[-::]G", ui.formatCount(1<<30))
assert.Equal(t, "1.1[-::]T", ui.formatCount(1<<40))
assert.Equal(t, "1.1[-::]P", ui.formatCount(1<<50))
assert.Equal(t, "1.2[-::]E", ui.formatCount(1<<60))
}
11 changes: 9 additions & 2 deletions tui/keys.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package tui

import (
"github.com/dundee/gdu/v4/pkg/analyze"
"github.com/dundee/gdu/v5/pkg/analyze"
"github.com/gdamore/tcell/v2"
)

Expand Down Expand Up @@ -53,13 +53,20 @@ func (ui *UI) keyPressed(key *tcell.EventKey) *tcell.EventKey {
ui.showDir()
ui.table.Select(row, column)
}
case 'c':
ui.showItemCount = !ui.showItemCount
if ui.currentDir != nil {
row, column := ui.table.GetSelection()
ui.showDir()
ui.table.Select(row, column)
}
case 'r':
if ui.currentDir != nil {
ui.rescanDir()
}
case 's':
ui.setSorting("size")
case 'c':
case 'C':
ui.setSorting("itemCount")
case 'n':
ui.setSorting("name")
Expand Down
52 changes: 47 additions & 5 deletions tui/keys_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ package tui
import (
"testing"

"github.com/dundee/gdu/v4/internal/testanalyze"
"github.com/dundee/gdu/v4/internal/testapp"
"github.com/dundee/gdu/v4/internal/testdir"
"github.com/dundee/gdu/v4/pkg/analyze"
"github.com/dundee/gdu/v5/internal/testanalyze"
"github.com/dundee/gdu/v5/internal/testapp"
"github.com/dundee/gdu/v5/internal/testdir"
"github.com/dundee/gdu/v5/pkg/analyze"
"github.com/gdamore/tcell/v2"
"github.com/rivo/tview"
"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -242,6 +242,48 @@ func TestSortByApparentSize(t *testing.T) {
assert.True(t, ui.ShowApparentSize)
}

func TestShowFileCount(t *testing.T) {
app := testapp.CreateMockedApp(true)
ui := CreateUI(app, true, false)
ui.Analyzer = &testanalyze.MockedAnalyzer{}
ui.PathChecker = testdir.MockedPathChecker
ui.done = make(chan struct{})
ui.AnalyzePath("test_dir", nil)

<-ui.done // wait for analyzer

assert.Equal(t, "test_dir", ui.currentDir.Name)

for _, f := range ui.app.(*testapp.MockedApp).UpdateDraws {
f()
}

ui.keyPressed(tcell.NewEventKey(tcell.KeyRune, 'c', 0))

assert.True(t, ui.showItemCount)
}

func TestShowFileCountBW(t *testing.T) {
app := testapp.CreateMockedApp(true)
ui := CreateUI(app, false, false)
ui.Analyzer = &testanalyze.MockedAnalyzer{}
ui.PathChecker = testdir.MockedPathChecker
ui.done = make(chan struct{})
ui.AnalyzePath("test_dir", nil)

<-ui.done // wait for analyzer

assert.Equal(t, "test_dir", ui.currentDir.Name)

for _, f := range ui.app.(*testapp.MockedApp).UpdateDraws {
f()
}

ui.keyPressed(tcell.NewEventKey(tcell.KeyRune, 'c', 0))

assert.True(t, ui.showItemCount)
}

func TestRescan(t *testing.T) {
parentDir := &analyze.Dir{
File: &analyze.File{
Expand Down Expand Up @@ -297,7 +339,7 @@ func TestSorting(t *testing.T) {

ui.keyPressed(tcell.NewEventKey(tcell.KeyRune, 's', 0))
assert.Equal(t, "size", ui.sortBy)
ui.keyPressed(tcell.NewEventKey(tcell.KeyRune, 'c', 0))
ui.keyPressed(tcell.NewEventKey(tcell.KeyRune, 'C', 0))
assert.Equal(t, "itemCount", ui.sortBy)
ui.keyPressed(tcell.NewEventKey(tcell.KeyRune, 'n', 0))
assert.Equal(t, "name", ui.sortBy)
Expand Down
Loading

0 comments on commit 29e298c

Please sign in to comment.