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: introduce more style options #396

Merged
merged 10 commits into from
Dec 29, 2024
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
8 changes: 4 additions & 4 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,18 @@ jobs:
- name: Install Go
uses: actions/setup-go@v5
with:
go-version: 1.18.x
go-version: 1.23.x
- name: Checkout code
uses: actions/checkout@v4
- name: Run linters
uses: golangci/golangci-lint-action@v5
with:
version: v1.57.2
version: v1.62.2

test:
strategy:
matrix:
go-version: [1.18.x, 1.19.x, 1.20.x, 1.21.x, 1.22.x]
go-version: [1.21.x, 1.22.x, 1.23.x]
platform: [ubuntu-latest]
include:
- go-version: 1.22.x
Expand All @@ -49,7 +49,7 @@ jobs:
if: success()
uses: actions/setup-go@v5
with:
go-version: 1.18.x
go-version: 1.23.x
- name: Checkout code
uses: actions/checkout@v4
- name: Calc coverage
Expand Down
2 changes: 1 addition & 1 deletion .tool-versions
Original file line number Diff line number Diff line change
@@ -1 +1 @@
golang 1.22.2
golang 1.23.3
188 changes: 128 additions & 60 deletions cmd/gdu/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,12 @@ type Flags struct {

// Style define style config
type Style struct {
SelectedRow ColorStyle `yaml:"selected-row"`
ProgressModal ProgressModalOpts `yaml:"progress-modal"`
UseOldSizeBar bool `yaml:"use-old-size-bar"`
SelectedRow ColorStyle `yaml:"selected-row"`
ProgressModal ProgressModalOpts `yaml:"progress-modal"`
UseOldSizeBar bool `yaml:"use-old-size-bar"`
Footer FooterColorStyle `yaml:"footer"`
Header HeaderColorStyle `yaml:"header"`
ResultRow ResultRowColorStyle `yaml:"result-row"`
}

// ProgressModalOpts defines options for progress modal
Expand All @@ -101,6 +104,26 @@ type ColorStyle struct {
BackgroundColor string `yaml:"background-color"`
}

// FooterColorStyle defines styling of footer
type FooterColorStyle struct {
TextColor string `yaml:"text-color"`
BackgroundColor string `yaml:"background-color"`
NumberColor string `yaml:"number-color"`
}

// HeaderColorStyle defines styling of header
type HeaderColorStyle struct {
TextColor string `yaml:"text-color"`
BackgroundColor string `yaml:"background-color"`
Hidden bool `yaml:"hidden"`
}

// ResultRowColorStyle defines styling of result row
type ResultRowColorStyle struct {
NumberColor string `yaml:"number-color"`
DirectoryColor string `yaml:"directory-color"`
}

// Sorting defines default sorting of items
type Sorting struct {
By string `yaml:"by"`
Expand Down Expand Up @@ -250,63 +273,7 @@ func (a *App) createUI() (UI, error) {
}
ui = stdoutUI
default:
var opts []tui.Option

if a.Flags.Style.SelectedRow.TextColor != "" {
opts = append(opts, func(ui *tui.UI) {
ui.SetSelectedTextColor(tcell.GetColor(a.Flags.Style.SelectedRow.TextColor))
})
}
if a.Flags.Style.SelectedRow.BackgroundColor != "" {
opts = append(opts, func(ui *tui.UI) {
ui.SetSelectedBackgroundColor(tcell.GetColor(a.Flags.Style.SelectedRow.BackgroundColor))
})
}
if a.Flags.Style.ProgressModal.CurrentItemNameMaxLen > 0 {
opts = append(opts, func(ui *tui.UI) {
ui.SetCurrentItemNameMaxLen(a.Flags.Style.ProgressModal.CurrentItemNameMaxLen)
})
}
if a.Flags.Style.UseOldSizeBar || a.Flags.NoUnicode {
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)
})
}
if a.Flags.ChangeCwd {
opts = append(opts, func(ui *tui.UI) {
ui.SetChangeCwdFn(os.Chdir)
})
}
if a.Flags.ShowItemCount {
opts = append(opts, func(ui *tui.UI) {
ui.SetShowItemCount()
})
}
if a.Flags.ShowMTime {
opts = append(opts, func(ui *tui.UI) {
ui.SetShowMTime()
})
}
if a.Flags.NoDelete {
opts = append(opts, func(ui *tui.UI) {
ui.SetNoDelete()
})
}
if a.Flags.DeleteInBackground {
opts = append(opts, func(ui *tui.UI) {
ui.SetDeleteInBackground()
})
}
if a.Flags.DeleteInParallel {
opts = append(opts, func(ui *tui.UI) {
ui.SetDeleteInParallel()
})
}
opts := a.getOptions()

ui = tui.CreateUI(
a.TermApp,
Expand All @@ -331,6 +298,107 @@ func (a *App) createUI() (UI, error) {
return ui, nil
}

func (a *App) getOptions() []tui.Option {
var opts []tui.Option

if a.Flags.Style.SelectedRow.TextColor != "" {
opts = append(opts, func(ui *tui.UI) {
ui.SetSelectedTextColor(tcell.GetColor(a.Flags.Style.SelectedRow.TextColor))
})
}
if a.Flags.Style.SelectedRow.BackgroundColor != "" {
opts = append(opts, func(ui *tui.UI) {
ui.SetSelectedBackgroundColor(tcell.GetColor(a.Flags.Style.SelectedRow.BackgroundColor))
})
}
if a.Flags.Style.Footer.TextColor != "" {
opts = append(opts, func(ui *tui.UI) {
ui.SetFooterTextColor(a.Flags.Style.Footer.TextColor)
})
}
if a.Flags.Style.Footer.BackgroundColor != "" {
opts = append(opts, func(ui *tui.UI) {
ui.SetFooterBackgroundColor(a.Flags.Style.Footer.BackgroundColor)
})
}
if a.Flags.Style.Footer.NumberColor != "" {
opts = append(opts, func(ui *tui.UI) {
ui.SetFooterNumberColor(a.Flags.Style.Footer.NumberColor)
})
}
if a.Flags.Style.Header.TextColor != "" {
opts = append(opts, func(ui *tui.UI) {
ui.SetHeaderTextColor(a.Flags.Style.Header.TextColor)
})
}
if a.Flags.Style.Header.BackgroundColor != "" {
opts = append(opts, func(ui *tui.UI) {
ui.SetHeaderBackgroundColor(a.Flags.Style.Header.BackgroundColor)
})
}
if a.Flags.Style.Header.Hidden {
opts = append(opts, func(ui *tui.UI) {
ui.SetHeaderHidden()
})
}
if a.Flags.Style.ResultRow.NumberColor != "" {
opts = append(opts, func(ui *tui.UI) {
ui.SetResultRowNumberColor(a.Flags.Style.ResultRow.NumberColor)
})
}
if a.Flags.Style.ResultRow.DirectoryColor != "" {
opts = append(opts, func(ui *tui.UI) {
ui.SetResultRowDirectoryColor(a.Flags.Style.ResultRow.DirectoryColor)
})
}
if a.Flags.Style.ProgressModal.CurrentItemNameMaxLen > 0 {
opts = append(opts, func(ui *tui.UI) {
ui.SetCurrentItemNameMaxLen(a.Flags.Style.ProgressModal.CurrentItemNameMaxLen)
})
}
if a.Flags.Style.UseOldSizeBar || a.Flags.NoUnicode {
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)
})
}
if a.Flags.ChangeCwd {
opts = append(opts, func(ui *tui.UI) {
ui.SetChangeCwdFn(os.Chdir)
})
}
if a.Flags.ShowItemCount {
opts = append(opts, func(ui *tui.UI) {
ui.SetShowItemCount()
})
}
if a.Flags.ShowMTime {
opts = append(opts, func(ui *tui.UI) {
ui.SetShowMTime()
})
}
if a.Flags.NoDelete {
opts = append(opts, func(ui *tui.UI) {
ui.SetNoDelete()
})
}
if a.Flags.DeleteInBackground {
opts = append(opts, func(ui *tui.UI) {
ui.SetDeleteInBackground()
})
}
if a.Flags.DeleteInParallel {
opts = append(opts, func(ui *tui.UI) {
ui.SetDeleteInParallel()
})
}
return opts
}

func (a *App) setNoCross(path string) error {
if a.Flags.NoCross {
mounts, err := a.Getter.GetMounts()
Expand Down
14 changes: 14 additions & 0 deletions cmd/gdu/app/app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,20 @@ func TestAnalyzePathWithStyle(t *testing.T) {
ProgressModal: ProgressModalOpts{
CurrentItemNameMaxLen: 10,
},
Footer: FooterColorStyle{
TextColor: "black",
BackgroundColor: "red",
NumberColor: "white",
},
Header: HeaderColorStyle{
TextColor: "black",
BackgroundColor: "red",
Hidden: true,
},
ResultRow: ResultRowColorStyle{
NumberColor: "orange",
DirectoryColor: "blue",
},
UseOldSizeBar: true,
},
},
Expand Down
22 changes: 22 additions & 0 deletions cmd/gdu/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,28 @@
}

configErr = yaml.Unmarshal(data, &af)

if af.Style.Footer.BackgroundColor == "" {
af.Style.Footer.BackgroundColor = "#2479D0"
}
if af.Style.Footer.TextColor == "" {
af.Style.Footer.TextColor = "#000000"
}
if af.Style.Footer.NumberColor == "" {
af.Style.Footer.NumberColor = "#FFFFFF"
}
if af.Style.Header.BackgroundColor == "" {
af.Style.Header.BackgroundColor = "#2479D0"
}
if af.Style.Header.TextColor == "" {
af.Style.Header.TextColor = "#000000"
}
if af.Style.ResultRow.NumberColor == "" {
af.Style.ResultRow.NumberColor = "#e67100"
}
if af.Style.ResultRow.DirectoryColor == "" {
af.Style.ResultRow.DirectoryColor = "#3498db"
}

Check warning on line 117 in cmd/gdu/main.go

View check run for this annotation

Codecov / codecov/patch

cmd/gdu/main.go#L96-L117

Added lines #L96 - L117 were not covered by tests
}

func setConfigFilePath() {
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/dundee/gdu/v5

go 1.18
go 1.21

require (
github.com/dgraph-io/badger/v3 v3.2103.2
Expand Down
2 changes: 1 addition & 1 deletion stdout/stdout.go
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ func (ui *UI) printItem(file fs.Item) {
lineFormat,
string(file.GetFlag()),
ui.formatSize(size),
ui.blue.Sprintf("/"+file.GetName()))
ui.blue.Sprint("/"+file.GetName()))
} else {
fmt.Fprintf(ui.output,
lineFormat,
Expand Down
5 changes: 4 additions & 1 deletion tui/actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,10 @@ func (ui *UI) showInfo() {
selectedFile := ui.table.GetCell(row, column).GetReference().(fs.Item)

if ui.UseColors {
numberColor = orangeBold
numberColor = fmt.Sprintf(
"[%s::b]",
ui.resultRow.NumberColor,
)
} else {
numberColor = defaultColorBold
}
Expand Down
23 changes: 14 additions & 9 deletions tui/format.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,8 @@ import (

const (
blackOnWhite = "[black:white:-]"
blackOnBlue = "[#000000:#2479d0:-]"
whiteOnBlack = "[white:black:-]"

orangeBold = "[#e67100::b]"
blueBold = "[#3498db::b]"

defaultColor = "[-::]"
defaultColorBold = "[::b]"
)
Expand All @@ -37,8 +33,13 @@ func (ui *UI) formatFileRow(item fs.Item, maxUsage, maxSize int64, marked, ignor

row := string(item.GetFlag())

numberColor := fmt.Sprintf(
"[%s::b]",
ui.resultRow.NumberColor,
)

if ui.UseColors && !marked && !ignored {
row += orangeBold
row += numberColor
} else {
row += defaultColorBold
}
Expand All @@ -57,7 +58,7 @@ func (ui *UI) formatFileRow(item fs.Item, maxUsage, maxSize int64, marked, ignor

if ui.showItemCount {
if ui.UseColors && !marked && !ignored {
row += orangeBold
row += numberColor
} else {
row += defaultColorBold
}
Expand All @@ -66,7 +67,7 @@ func (ui *UI) formatFileRow(item fs.Item, maxUsage, maxSize int64, marked, ignor

if ui.showMtime {
if ui.UseColors && !marked && !ignored {
row += orangeBold
row += numberColor
} else {
row += defaultColorBold
}
Expand All @@ -87,7 +88,7 @@ func (ui *UI) formatFileRow(item fs.Item, maxUsage, maxSize int64, marked, ignor

if item.IsDir() {
if ui.UseColors && !marked && !ignored {
row += blueBold + "/"
row += fmt.Sprintf("[%s::b]/", ui.resultRow.DirectoryColor)
} else {
row += defaultColorBold + "/"
}
Expand All @@ -100,7 +101,11 @@ func (ui *UI) formatSize(size int64, reverseColor, transparentBg bool) string {
var color string
if reverseColor {
if ui.UseColors {
color = blackOnBlue
color = fmt.Sprintf(
"[%s:%s:-]",
ui.footerTextColor,
ui.footerBackgroundColor,
)
} else {
color = blackOnWhite
}
Expand Down
Loading
Loading