Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: use unicode block elements in size bar #255

Merged
merged 2 commits into from
Jun 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions cmd/gdu/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ type Flags struct {
type Style struct {
SelectedRow ColorStyle `yaml:"selected-row"`
ProgressModal ProgressModalOpts `yaml:"progress-modal"`
UseOldSizeBar bool `yaml:"use-old-size-bar"`
}

// ProgressModalOpts defines options for progress modal
Expand Down Expand Up @@ -236,6 +237,11 @@ func (a *App) createUI() (UI, error) {
ui.SetCurrentItemNameMaxLen(a.Flags.Style.ProgressModal.CurrentItemNameMaxLen)
})
}
if a.Flags.Style.UseOldSizeBar {
opts = append(opts, func(ui *tui.UI) {
ui.UseOldSizeBar()
})
}
if a.Flags.Sorting.Order != "" || a.Flags.Sorting.By != "" {
opts = append(opts, func(ui *tui.UI) {
ui.SetDefaultSorting(a.Flags.Sorting.By, a.Flags.Sorting.Order)
Expand Down
1 change: 1 addition & 0 deletions cmd/gdu/app/app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@ func TestAnalyzePathWithStyle(t *testing.T) {
ProgressModal: ProgressModalOpts{
CurrentItemNameMaxLen: 10,
},
UseOldSizeBar: true,
},
},
[]string{"test_dir"},
Expand Down
1 change: 1 addition & 0 deletions tui/actions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ func TestDeviceSelected(t *testing.T) {
ui := CreateUI(app, simScreen, &bytes.Buffer{}, true, true, true, false, false)
ui.Analyzer = &testanalyze.MockedAnalyzer{}
ui.done = make(chan struct{})
ui.UseOldSizeBar()
ui.SetIgnoreDirPaths([]string{"/xxx"})
err := ui.ListDevices(getDevicesInfoMock())

Expand Down
10 changes: 7 additions & 3 deletions tui/format.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ func (ui *UI) formatFileRow(item fs.Item, maxUsage int64, maxSize int64, marked
var part int

if ui.ShowApparentSize {
part = int(float64(item.GetSize()) / float64(maxSize) * 10.0)
part = int(float64(item.GetSize()) / float64(maxSize) * 100.0)
} else {
part = int(float64(item.GetUsage()) / float64(maxUsage) * 10.0)
part = int(float64(item.GetUsage()) / float64(maxUsage) * 100.0)
}

row := string(item.GetFlag())
Expand All @@ -32,7 +32,11 @@ func (ui *UI) formatFileRow(item fs.Item, maxUsage int64, maxSize int64, marked
row += fmt.Sprintf("%15s", ui.formatSize(item.GetUsage(), false, true))
}

row += getUsageGraph(part)
if ui.useOldSizeBar {
row += " " + getUsageGraphOld(part) + " "
} else {
row += getUsageGraph(part)
}

if ui.showItemCount {
if ui.UseColors && !marked {
Expand Down
47 changes: 47 additions & 0 deletions tui/format_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ func TestMarked(t *testing.T) {
app := testapp.CreateMockedApp(true)
ui := CreateUI(app, simScreen, &bytes.Buffer{}, false, false, false, false, false)
ui.markedRows[0] = struct{}{}
ui.useOldSizeBar = true

dir := &analyze.Dir{
File: &analyze.File{
Expand All @@ -101,3 +102,49 @@ func TestMarked(t *testing.T) {
assert.Contains(t, ui.formatFileRow(file, file.GetUsage(), file.GetSize(), true), "✓ Aaa")
assert.Contains(t, ui.formatFileRow(file, file.GetUsage(), file.GetSize(), false), "[##########] Aaa")
}

func TestSizeBar(t *testing.T) {
simScreen := testapp.CreateSimScreen()
defer simScreen.Fini()

app := testapp.CreateMockedApp(true)
ui := CreateUI(app, simScreen, &bytes.Buffer{}, false, false, false, false, false)

dir := &analyze.Dir{
File: &analyze.File{
Usage: 10,
},
}

file := &analyze.File{
Name: "Aaa",
Parent: dir,
Usage: 10,
}

assert.Contains(t, ui.formatFileRow(file, file.GetUsage(), file.GetSize(), false), "██████████▏Aaa")
}

func TestOldSizeBar(t *testing.T) {
simScreen := testapp.CreateSimScreen()
defer simScreen.Fini()

app := testapp.CreateMockedApp(true)
ui := CreateUI(app, simScreen, &bytes.Buffer{}, false, false, false, false, false)
ui.markedRows[0] = struct{}{}
ui.useOldSizeBar = true

dir := &analyze.Dir{
File: &analyze.File{
Usage: 20,
},
}

file := &analyze.File{
Name: "Aaa",
Parent: dir,
Usage: 10,
}

assert.Contains(t, ui.formatFileRow(file, dir.GetUsage(), dir.GetSize(), false), "[##### ] Aaa")
}
2 changes: 1 addition & 1 deletion tui/show.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ func (ui *UI) showDevices() {
ui.table.SetCell(i+1, 0, tview.NewTableCell(textColor+device.Name).SetReference(ui.devices[i]))
ui.table.SetCell(i+1, 1, tview.NewTableCell(ui.formatSize(device.Size, false, true)))
ui.table.SetCell(i+1, 2, tview.NewTableCell(sizeColor+ui.formatSize(device.Size-device.Free, false, true)))
ui.table.SetCell(i+1, 3, tview.NewTableCell(getDeviceUsagePart(device)))
ui.table.SetCell(i+1, 3, tview.NewTableCell(getDeviceUsagePart(device, ui.useOldSizeBar)))
ui.table.SetCell(i+1, 4, tview.NewTableCell(ui.formatSize(device.Free, false, true)))
ui.table.SetCell(i+1, 5, tview.NewTableCell(textColor+device.MountPoint))
}
Expand Down
6 changes: 6 additions & 0 deletions tui/tui.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ type UI struct {
selectedTextColor tcell.Color
selectedBackgroundColor tcell.Color
currentItemNameMaxLen int
useOldSizeBar bool
defaultSortBy string
defaultSortOrder string
markedRows map[int]struct{}
Expand Down Expand Up @@ -180,6 +181,11 @@ func (ui *UI) SetCurrentItemNameMaxLen(len int) {
ui.currentItemNameMaxLen = len
}

// UseOldSizeBar uses the old size bar (# chars) instead of the new one (unicode block elements)
func (ui *UI) UseOldSizeBar() {
ui.useOldSizeBar = true
}

// SetChangeCwdFn sets function that can be used to change current working dir
// during dir browsing
func (ui *UI) SetChangeCwdFn(fn func(string) error) {
Expand Down
12 changes: 12 additions & 0 deletions tui/tui_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -438,6 +438,18 @@ func TestSetCurrentItemNameMaxLen(t *testing.T) {
assert.Equal(t, ui.currentItemNameMaxLen, 5)
}

func TestUseOldSizeBar(t *testing.T) {
simScreen := testapp.CreateSimScreen()
defer simScreen.Fini()

app := testapp.CreateMockedApp(true)
ui := CreateUI(app, simScreen, &bytes.Buffer{}, false, true, false, false, false)

ui.UseOldSizeBar()

assert.Equal(t, ui.useOldSizeBar, true)
}

// nolint: deadcode,unused // Why: for debugging
func printScreen(simScreen tcell.SimulationScreen) {
b, _, _ := simScreen.GetContents()
Expand Down
54 changes: 41 additions & 13 deletions tui/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,58 @@ import (
"github.com/dundee/gdu/v5/pkg/device"
)

func getDeviceUsagePart(item *device.Device) string {
part := int(float64(item.Size-item.Free) / float64(item.Size) * 10.0)
row := "["
for i := 0; i < 10; i++ {
if part > i {
row += "#"
} else {
row += " "
}
var (
barFullRune = "\u2588"
barPartRunes = map[int]string{
0: " ",
1: "\u258F",
2: "\u258E",
3: "\u258D",
4: "\u258C",
5: "\u258B",
6: "\u258A",
7: "\u2589",
}
row += "]"
return row
)

func getDeviceUsagePart(item *device.Device, useOld bool) string {
part := int(float64(item.Size-item.Free) / float64(item.Size) * 100.0)
if useOld {
return getUsageGraphOld(part)
}
return getUsageGraph(part)
}

func getUsageGraph(part int) string {
graph := " ["
graph := " "
whole := part / 10
for i := 0; i < whole; i++ {
graph += barFullRune
}
partWidth := (part % 10) * 8 / 10
if part < 100 {
graph += barPartRunes[partWidth]
}

for i := 0; i < 10-whole-1; i++ {
graph += " "
}

graph += "\u258F"
return graph
}

func getUsageGraphOld(part int) string {
part = part / 10
graph := "["
for i := 0; i < 10; i++ {
if part > i {
graph += "#"
} else {
graph += " "
}
}
graph += "] "
graph += "]"
return graph
}

Expand Down
31 changes: 31 additions & 0 deletions tui/utils_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package tui

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestGetUsageGraph(t *testing.T) {
assert.Equal(t, " \u258F", getUsageGraph(0))
assert.Equal(t, " █ \u258F", getUsageGraph(10))
assert.Equal(t, " ██ \u258F", getUsageGraph(20))
assert.Equal(t, " ███ \u258F", getUsageGraph(30))
assert.Equal(t, " ████ \u258F", getUsageGraph(40))
assert.Equal(t, " █████ \u258F", getUsageGraph(50))
assert.Equal(t, " ██████ \u258F", getUsageGraph(60))
assert.Equal(t, " ███████ \u258F", getUsageGraph(70))
assert.Equal(t, " ████████ \u258F", getUsageGraph(80))
assert.Equal(t, " █████████ \u258F", getUsageGraph(90))
assert.Equal(t, " ██████████\u258F", getUsageGraph(100))

assert.Equal(t, " █ \u258F", getUsageGraph(11))
assert.Equal(t, " █▏ \u258F", getUsageGraph(12))
assert.Equal(t, " █▎ \u258F", getUsageGraph(13))
assert.Equal(t, " █▍ \u258F", getUsageGraph(14))
assert.Equal(t, " █▌ \u258F", getUsageGraph(15))
assert.Equal(t, " █▌ \u258F", getUsageGraph(16))
assert.Equal(t, " █▋ \u258F", getUsageGraph(17))
assert.Equal(t, " █▊ \u258F", getUsageGraph(18))
assert.Equal(t, " █▉ \u258F", getUsageGraph(19))
}