Skip to content

Commit

Permalink
Merge pull request #352 from dundee/dundee/refactor/lint
Browse files Browse the repository at this point in the history
refactor: lints
  • Loading branch information
dundee authored Apr 24, 2024
2 parents 2558cf7 + 693a37c commit 4cdc89d
Show file tree
Hide file tree
Showing 11 changed files with 110 additions and 91 deletions.
46 changes: 25 additions & 21 deletions cmd/gdu/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,14 +122,14 @@ func init() {
}

// Run starts gdu main logic
func (a *App) Run() (err error) {
func (a *App) Run() error {
var ui UI

if a.Flags.ShowVersion {
fmt.Fprintln(a.Writer, "Version:\t", build.Version)
fmt.Fprintln(a.Writer, "Built time:\t", build.Time)
fmt.Fprintln(a.Writer, "Built user:\t", build.User)
return
return nil
}

log.Printf("Runtime flags: %+v", *a.Flags)
Expand All @@ -139,11 +139,14 @@ func (a *App) Run() (err error) {
}

path := a.getPath()
path, _ = filepath.Abs(path)
path, err := filepath.Abs(path)
if err != nil {
return err
}

ui, err = a.createUI()
if err != nil {
return
return err
}

if a.Flags.UseStorage {
Expand All @@ -155,21 +158,21 @@ func (a *App) Run() (err error) {
if a.Flags.FollowSymlinks {
ui.SetFollowSymlinks(true)
}
if err = a.setNoCross(path); err != nil {
return
if err := a.setNoCross(path); err != nil {
return err
}

ui.SetIgnoreDirPaths(a.Flags.IgnoreDirs)

if len(a.Flags.IgnoreDirPatterns) > 0 {
if err = ui.SetIgnoreDirPatterns(a.Flags.IgnoreDirPatterns); err != nil {
return
if err := ui.SetIgnoreDirPatterns(a.Flags.IgnoreDirPatterns); err != nil {
return err
}
}

if a.Flags.IgnoreFromFile != "" {
if err = ui.SetIgnoreFromFile(a.Flags.IgnoreFromFile); err != nil {
return
if err := ui.SetIgnoreFromFile(a.Flags.IgnoreFromFile); err != nil {
return err
}
}

Expand All @@ -179,12 +182,11 @@ func (a *App) Run() (err error) {

a.setMaxProcs()

if err = a.runAction(ui, path); err != nil {
return
if err := a.runAction(ui, path); err != nil {
return err
}

err = ui.StartUILoop()
return
return ui.StartUILoop()
}

func (a *App) getPath() string {
Expand All @@ -208,7 +210,8 @@ func (a *App) setMaxProcs() {
func (a *App) createUI() (UI, error) {
var ui UI

if a.Flags.OutputFile != "" {
switch {
case a.Flags.OutputFile != "":
var output io.Writer
var err error
if a.Flags.OutputFile == "-" {
Expand All @@ -227,7 +230,7 @@ func (a *App) createUI() (UI, error) {
a.Flags.ConstGC,
a.Flags.UseSIPrefix,
)
} else if a.Flags.NonInteractive || !a.Istty {
case a.Flags.NonInteractive || !a.Istty:
ui = stdout.CreateStdoutUI(
a.Writer,
!a.Flags.NoColor && a.Istty,
Expand All @@ -239,7 +242,7 @@ func (a *App) createUI() (UI, error) {
a.Flags.UseSIPrefix,
a.Flags.NoPrefix,
)
} else {
default:
var opts []tui.Option

if a.Flags.Style.SelectedRow.TextColor != "" {
Expand Down Expand Up @@ -346,11 +349,12 @@ func (a *App) runAction(ui UI, path string) error {
}()
}

if a.Flags.ShowDisks {
switch {
case a.Flags.ShowDisks:
if err := ui.ListDevices(a.Getter); err != nil {
return fmt.Errorf("loading mount points: %w", err)
}
} else if a.Flags.InputFile != "" {
case a.Flags.InputFile != "":
var input io.Reader
var err error
if a.Flags.InputFile == "-" {
Expand All @@ -365,12 +369,12 @@ func (a *App) runAction(ui UI, path string) error {
if err := ui.ReadAnalysis(input); err != nil {
return fmt.Errorf("reading analysis: %w", err)
}
} else if a.Flags.ReadFromStorage {
case a.Flags.ReadFromStorage:
ui.SetAnalyzer(analyze.CreateStoredAnalyzer(a.Flags.StoragePath))
if err := ui.ReadFromStorage(a.Flags.StoragePath, path); err != nil {
return fmt.Errorf("reading from storage (%s): %w", a.Flags.StoragePath, err)
}
} else {
default:
if build.RootPathPrefix != "" {
path = build.RootPathPrefix + path
}
Expand Down
8 changes: 4 additions & 4 deletions internal/common/ignore_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func TestCreateIgnorePattern(t *testing.T) {
re, err := common.CreateIgnorePattern([]string{"[abc]+"})

assert.Nil(t, err)
assert.True(t, re.Match([]byte("aa")))
assert.True(t, re.MatchString("aa"))
}

func TestCreateIgnorePatternWithErr(t *testing.T) {
Expand Down Expand Up @@ -63,13 +63,13 @@ func TestIgnoreFromFile(t *testing.T) {
}
defer file.Close()

if _, err := file.Write([]byte("/aaa\n")); err != nil {
if _, err := file.WriteString("/aaa\n"); err != nil {
panic(err)
}
if _, err := file.Write([]byte("/aaabc\n")); err != nil {
if _, err := file.WriteString("/aaabc\n"); err != nil {
panic(err)
}
if _, err := file.Write([]byte("/[abd]+\n")); err != nil {
if _, err := file.WriteString("/[abd]+\n"); err != nil {
panic(err)
}

Expand Down
14 changes: 4 additions & 10 deletions tui/actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -300,14 +300,8 @@ func (ui *UI) showFile() *tview.TextView {
return event
}

switch {
case event.Rune() == 'j':
fallthrough
case event.Rune() == 'G':
fallthrough
case event.Key() == tcell.KeyDown:
fallthrough
case event.Key() == tcell.KeyPgDn:
if event.Rune() == 'j' || event.Rune() == 'G' ||
event.Key() == tcell.KeyDown || event.Key() == tcell.KeyPgDn {
_, _, _, height := file.GetInnerRect()
row, _ := file.GetScrollOffset()
if height+row > totalLines-linesTreshold {
Expand Down Expand Up @@ -339,9 +333,9 @@ func (ui *UI) showInfo() {
selectedFile := ui.table.GetCell(row, column).GetReference().(fs.Item)

if ui.UseColors {
numberColor = "[#e67100::b]"
numberColor = orangeBold
} else {
numberColor = "[::b]"
numberColor = defaultColorBold
}

linesCount := 12
Expand Down
51 changes: 32 additions & 19 deletions tui/format.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,36 @@ import (
"github.com/rivo/tview"
)

func (ui *UI) formatFileRow(item fs.Item, maxUsage int64, maxSize int64, marked, ignored bool) string {
const (
blackOnWhite = "[black:white:-]"
blackOnBlue = "[#000000:#2479d0:-]"
whiteOnBlack = "[white:black:-]"

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

defaultColor = "[-::]"
defaultColorBold = "[::b]"
)

func (ui *UI) formatFileRow(item fs.Item, maxUsage, maxSize int64, marked, ignored bool) string {
var part int

if ignored {
switch {
case ignored:
part = 0
} else if ui.ShowApparentSize {
case ui.ShowApparentSize:
part = int(float64(item.GetSize()) / float64(maxSize) * 100.0)
} else {
default:
part = int(float64(item.GetUsage()) / float64(maxUsage) * 100.0)
}

row := string(item.GetFlag())

if ui.UseColors && !marked && !ignored {
row += "[#e67100::b]"
row += orangeBold
} else {
row += "[::b]"
row += defaultColorBold
}

if ui.ShowApparentSize {
Expand All @@ -42,21 +55,21 @@ func (ui *UI) formatFileRow(item fs.Item, maxUsage int64, maxSize int64, marked,

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

if ui.showMtime {
if ui.UseColors && !marked && !ignored {
row += "[#e67100::b]"
row += orangeBold
} else {
row += "[::b]"
row += defaultColorBold
}
row += fmt.Sprintf(
"%s [-::]",
"%s "+defaultColor,
item.GetMtime().Format("2006-01-02 15:04:05"),
)
}
Expand All @@ -72,28 +85,28 @@ func (ui *UI) formatFileRow(item fs.Item, maxUsage int64, maxSize int64, marked,

if item.IsDir() {
if ui.UseColors && !marked && !ignored {
row += "[#3498db::b]/"
row += blueBold + "/"
} else {
row += "[::b]/"
row += defaultColorBold + "/"
}
}
row += tview.Escape(item.GetName())
return row
}

func (ui *UI) formatSize(size int64, reverseColor bool, transparentBg bool) string {
func (ui *UI) formatSize(size int64, reverseColor, transparentBg bool) string {
var color string
if reverseColor {
if ui.UseColors {
color = "[#000000:#2479d0:-]"
color = blackOnBlue
} else {
color = "[black:white:-]"
color = blackOnWhite
}
} else {
if transparentBg {
color = "[-::]"
color = defaultColor
} else {
color = "[white:black:-]"
color = whiteOnBlack
}
}

Expand All @@ -105,7 +118,7 @@ func (ui *UI) formatSize(size int64, reverseColor bool, transparentBg bool) stri

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

switch {
Expand Down
1 change: 0 additions & 1 deletion tui/marked.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ func (ui *UI) deleteMarked(shouldEmpty bool) {

go func() {
for _, one := range markedItems {

ui.app.QueueUpdateDraw(func() {
modal.SetText(
// nolint: staticcheck // Why: fixed string
Expand Down
4 changes: 1 addition & 3 deletions tui/mouse.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,7 @@ func (ui *UI) onMouse(event *tcell.EventMouse, action tview.MouseAction) (*tcell
}
}
return nil, action
case tview.MouseScrollUp:
fallthrough
case tview.MouseScrollDown:
case tview.MouseScrollUp, tview.MouseScrollDown:
row, column := ui.table.GetSelection()
if action == tview.MouseScrollUp && row > 0 {
row--
Expand Down
19 changes: 10 additions & 9 deletions tui/show.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,12 +131,13 @@ func (ui *UI) showDir() {
cell := tview.NewTableCell(ui.formatFileRow(item, maxUsage, maxSize, marked, ignored))
cell.SetReference(ui.currentDir.GetFiles()[i])

if ignored {
switch {
case ignored:
cell.SetStyle(tcell.Style{}.Foreground(tview.Styles.SecondaryTextColor))
} else if marked {
case marked:
cell.SetStyle(tcell.Style{}.Foreground(tview.Styles.PrimaryTextColor))
cell.SetBackgroundColor(tview.Styles.ContrastBackgroundColor)
} else {
default:
cell.SetStyle(tcell.Style{}.Foreground(tcell.ColorDefault))
}

Expand All @@ -147,10 +148,10 @@ func (ui *UI) showDir() {
var footerNumberColor, footerTextColor string
if ui.UseColors {
footerNumberColor = "[#ffffff:#2479d0:b]"
footerTextColor = "[#000000:#2479d0:-]"
footerTextColor = blackOnBlue
} else {
footerNumberColor = "[black:white:b]"
footerTextColor = "[black:white:-]"
footerTextColor = blackOnWhite
}

selected := ""
Expand Down Expand Up @@ -214,10 +215,10 @@ func (ui *UI) showDevices() {
var footerNumberColor, footerTextColor string
if ui.UseColors {
footerNumberColor = "[#ffffff:#2479d0:b]"
footerTextColor = "[#000000:#2479d0:-]"
footerTextColor = blackOnBlue
} else {
footerNumberColor = "[black:white:b]"
footerTextColor = "[black:white:-]"
footerTextColor = blackOnWhite
}

ui.footerLabel.SetText(
Expand Down Expand Up @@ -297,8 +298,8 @@ func (ui *UI) formatHelpTextFor() string {
for i, line := range lines {
if ui.UseColors {
lines[i] = strings.ReplaceAll(
strings.ReplaceAll(line, "[::b]", "[red]"),
"[white:black:-]",
strings.ReplaceAll(line, defaultColorBold, "[red]"),
whiteOnBlack,
"[white]",
)
}
Expand Down
Loading

0 comments on commit 4cdc89d

Please sign in to comment.