summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDaniel Milde <daniel@milde.cz>2022-10-22 21:24:03 +0200
committerDaniel Milde <daniel@milde.cz>2022-10-22 21:41:41 +0200
commitbc55f2c79651b17aca26c6997ebb3cae7a159bd0 (patch)
tree43849a24ed990183fe55946e3fa2947003df1c1b
parent49e0cbad72bd0161b1f60044bae2b8f62e2eac2e (diff)
feat: set default sorting using config option
closes #181
-rw-r--r--README.md8
-rw-r--r--cmd/gdu/app/app.go14
-rw-r--r--cmd/gdu/app/app_test.go21
-rw-r--r--tui/sort.go10
-rw-r--r--tui/sort_test.go28
-rw-r--r--tui/tui.go8
6 files changed, 86 insertions, 3 deletions
diff --git a/README.md b/README.md
index 3b0a6a1..5114342 100644
--- a/README.md
+++ b/README.md
@@ -157,6 +157,14 @@ Gdu can read (and write) YAML configuration file.
echo "no-color: true" > ~/.gdu.yaml
```
+* To set default sorting in configuration file:
+
+```
+sorting:
+ by: name // size, name, itemCount, mtime
+ order: desc
+```
+
* To save the current configuration
```
diff --git a/cmd/gdu/app/app.go b/cmd/gdu/app/app.go
index f7c0c17..2a34c51 100644
--- a/cmd/gdu/app/app.go
+++ b/cmd/gdu/app/app.go
@@ -62,8 +62,9 @@ type Flags struct {
Summarize bool `yaml:"summarize"`
UseSIPrefix bool `yaml:"use-si-prefix"`
NoPrefix bool `yaml:"no-prefix"`
- WriteConfig bool `yaml:"write-config"`
+ WriteConfig bool `yaml:"-"`
Style Style `yaml:"style"`
+ Sorting Sorting `yaml:"sorting"`
}
// Style define style config
@@ -83,6 +84,12 @@ type ColorStyle struct {
BackgroundColor string `yaml:"background-color"`
}
+// Sorting defines default sorting of items
+type Sorting struct {
+ By string `yaml:"by"`
+ Order string `yaml:"order"`
+}
+
// App defines the main application
type App struct {
Args []string
@@ -229,6 +236,11 @@ func (a *App) createUI() (UI, error) {
ui.SetCurrentItemNameMaxLen(a.Flags.Style.ProgressModal.CurrentItemNameMaxLen)
})
}
+ 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)
+ })
+ }
ui = tui.CreateUI(
a.TermApp,
diff --git a/cmd/gdu/app/app_test.go b/cmd/gdu/app/app_test.go
index cba3829..746550e 100644
--- a/cmd/gdu/app/app_test.go
+++ b/cmd/gdu/app/app_test.go
@@ -134,6 +134,27 @@ func TestAnalyzePathWithGui(t *testing.T) {
assert.Nil(t, err)
}
+func TestAnalyzePathWithDefaultSorting(t *testing.T) {
+ fin := testdir.CreateTestDir()
+ defer fin()
+
+ out, err := runApp(
+ &Flags{
+ LogFile: "/dev/null",
+ Sorting: Sorting{
+ By: "name",
+ Order: "asc",
+ },
+ },
+ []string{"test_dir"},
+ true,
+ testdev.DevicesInfoGetterMock{},
+ )
+
+ assert.Empty(t, out)
+ assert.Nil(t, err)
+}
+
func TestAnalyzePathWithStyle(t *testing.T) {
fin := testdir.CreateTestDir()
defer fin()
diff --git a/tui/sort.go b/tui/sort.go
index 71eea0c..2e2af32 100644
--- a/tui/sort.go
+++ b/tui/sort.go
@@ -7,6 +7,16 @@ import (
"github.com/dundee/gdu/v5/pkg/fs"
)
+// SetDefaultSorting sets the default sorting
+func (ui *UI) SetDefaultSorting(by, order string) {
+ if by != "" {
+ ui.defaultSortBy = by
+ }
+ if order == "asc" || order == "desc" {
+ ui.defaultSortOrder = order
+ }
+}
+
func (ui *UI) setSorting(newOrder string) {
if newOrder == ui.sortBy {
if ui.sortOrder == "asc" {
diff --git a/tui/sort_test.go b/tui/sort_test.go
index 71b8d74..d0a8528 100644
--- a/tui/sort_test.go
+++ b/tui/sort_test.go
@@ -123,6 +123,34 @@ func TestSetSorting(t *testing.T) {
assert.Equal(t, "asc", ui.sortOrder)
}
+func TestSetDEfaultSorting(t *testing.T) {
+ simScreen := testapp.CreateSimScreen(50, 50)
+ defer simScreen.Fini()
+
+ var opts []Option
+ opts = append(opts, func(ui *UI) {
+ ui.SetDefaultSorting("name", "asc")
+ })
+
+ app := testapp.CreateMockedApp(true)
+ ui := CreateUI(app, simScreen, &bytes.Buffer{}, false, false, false, false, false, opts...)
+ ui.Analyzer = &testanalyze.MockedAnalyzer{}
+ ui.done = make(chan struct{})
+
+ if err := ui.AnalyzePath("test_dir", nil); err != nil {
+ panic(err)
+ }
+
+ <-ui.done // wait for analyzer
+
+ for _, f := range ui.app.(*testapp.MockedApp).GetUpdateDraws() {
+ f()
+ }
+
+ assert.Equal(t, "name", ui.sortBy)
+ assert.Equal(t, "asc", ui.sortOrder)
+}
+
func TestSortDevicesByName(t *testing.T) {
app, simScreen := testapp.CreateTestAppWithSimScreen(50, 50)
defer simScreen.Fini()
diff --git a/tui/tui.go b/tui/tui.go
index 9cab623..79dda14 100644
--- a/tui/tui.go
+++ b/tui/tui.go
@@ -76,6 +76,8 @@ type UI struct {
selectedTextColor tcell.Color
selectedBackgroundColor tcell.Color
currentItemNameMaxLen int
+ defaultSortBy string
+ defaultSortOrder string
}
// Option is optional function customizing the bahaviour of UI
@@ -114,6 +116,8 @@ func CreateUI(
selectedTextColor: tview.Styles.TitleColor,
selectedBackgroundColor: tview.Styles.MoreContrastBackgroundColor,
currentItemNameMaxLen: 70,
+ defaultSortBy: "size",
+ defaultSortOrder: "desc",
}
for _, o := range opts {
o(ui)
@@ -206,8 +210,8 @@ func (ui *UI) StartUILoop() error {
}
func (ui *UI) resetSorting() {
- ui.sortBy = "size"
- ui.sortOrder = "desc"
+ ui.sortBy = ui.defaultSortBy
+ ui.sortOrder = ui.defaultSortOrder
}
func (ui *UI) rescanDir() {