Skip to content

Commit

Permalink
feat: add option --no-unicode to disable unicode symbols
Browse files Browse the repository at this point in the history
  • Loading branch information
dundee committed May 21, 2024
1 parent 55b21a1 commit 5de2852
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 6 deletions.
9 changes: 7 additions & 2 deletions cmd/gdu/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ type Flags struct {
NoMouse bool `yaml:"no-mouse"`
NonInteractive bool `yaml:"non-interactive"`
NoProgress bool `yaml:"no-progress"`
NoUnicode bool `yaml:"no-unicode"`
NoCross bool `yaml:"no-cross"`
NoHidden bool `yaml:"no-hidden"`
NoDelete bool `yaml:"no-delete"`
Expand Down Expand Up @@ -231,7 +232,7 @@ func (a *App) createUI() (UI, error) {
a.Flags.UseSIPrefix,
)
case a.Flags.NonInteractive || !a.Istty:
ui = stdout.CreateStdoutUI(
stdoutUI := stdout.CreateStdoutUI(
a.Writer,
!a.Flags.NoColor && a.Istty,
!a.Flags.NoProgress && a.Istty,
Expand All @@ -242,6 +243,10 @@ func (a *App) createUI() (UI, error) {
a.Flags.UseSIPrefix,
a.Flags.NoPrefix,
)
if a.Flags.NoUnicode {
stdoutUI.UseOldProgressRunes()
}
ui = stdoutUI
default:
var opts []tui.Option

Expand All @@ -260,7 +265,7 @@ func (a *App) createUI() (UI, error) {
ui.SetCurrentItemNameMaxLen(a.Flags.Style.ProgressModal.CurrentItemNameMaxLen)
})
}
if a.Flags.Style.UseOldSizeBar {
if a.Flags.Style.UseOldSizeBar || a.Flags.NoUnicode {
opts = append(opts, func(ui *tui.UI) {
ui.UseOldSizeBar()
})
Expand Down
18 changes: 18 additions & 0 deletions cmd/gdu/app/app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,24 @@ func TestAnalyzePathWithStyle(t *testing.T) {
assert.Nil(t, err)
}

func TestAnalyzePathNoUnicode(t *testing.T) {
fin := testdir.CreateTestDir()
defer fin()

out, err := runApp(
&Flags{
LogFile: "/dev/null",
NoUnicode: true,
},
[]string{"test_dir"},
false,
testdev.DevicesInfoGetterMock{},
)

assert.Contains(t, out, "nested")
assert.Nil(t, err)
}

func TestAnalyzePathWithExport(t *testing.T) {
fin := testdir.CreateTestDir()
defer fin()
Expand Down
1 change: 1 addition & 0 deletions cmd/gdu/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ func init() {
flags.BoolVarP(&af.ShowMTime, "show-mtime", "M", false, "Show latest mtime of items in directory")
flags.BoolVarP(&af.NonInteractive, "non-interactive", "n", false, "Do not run in interactive mode")
flags.BoolVarP(&af.NoProgress, "no-progress", "p", false, "Do not show progress in non-interactive mode")
flags.BoolVarP(&af.NoUnicode, "no-unicode", "u", false, "Do not use Unicode symbols (for size bar)")
flags.BoolVarP(&af.Summarize, "summarize", "s", false, "Show only a total in non-interactive mode")
flags.BoolVar(&af.UseSIPrefix, "si", false, "Show sizes with decimal SI prefixes (kB, MB, GB) instead of binary prefixes (KiB, MiB, GiB)")
flags.BoolVar(&af.NoPrefix, "no-prefix", false, "Show sizes as raw numbers without any prefixes (SI or binary) in non-interactive mode")
Expand Down
17 changes: 13 additions & 4 deletions stdout/stdout.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,11 @@ type UI struct {
noPrefix bool
}

var progressRunes = []rune(`⠇⠏⠋⠙⠹⠸⠼⠴⠦⠧`)
var (
progressRunes = []rune(`⠇⠏⠋⠙⠹⠸⠼⠴⠦⠧`)
progressRunesOld = []rune(`-\\|/`)
progressRunesCount = len(progressRunes)
)

// CreateStdoutUI creates UI for stdout
func CreateStdoutUI(
Expand Down Expand Up @@ -68,6 +72,11 @@ func CreateStdoutUI(
return ui
}

func (ui *UI) UseOldProgressRunes() {
progressRunes = progressRunesOld
progressRunesCount = len(progressRunes)

Check warning on line 77 in stdout/stdout.go

View check run for this annotation

Codecov / codecov/patch

stdout/stdout.go#L75-L77

Added lines #L75 - L77 were not covered by tests
}

// StartUILoop stub
func (ui *UI) StartUILoop() error {
return nil
Expand Down Expand Up @@ -321,7 +330,7 @@ func (ui *UI) showReadingProgress(doneChan chan struct{}) {

time.Sleep(100 * time.Millisecond)
i++
i %= 10
i %= progressRunesCount
}
}

Expand Down Expand Up @@ -349,7 +358,7 @@ func (ui *UI) updateProgress(updateStatsDone <-chan struct{}) {
fmt.Fprint(ui.output, "Calculating disk usage...")
time.Sleep(100 * time.Millisecond)
i++
i %= 10
i %= progressRunesCount

select {
case <-updateStatsDone:
Expand All @@ -370,7 +379,7 @@ func (ui *UI) updateProgress(updateStatsDone <-chan struct{}) {

time.Sleep(100 * time.Millisecond)
i++
i %= 10
i %= progressRunesCount
}
}

Expand Down

0 comments on commit 5de2852

Please sign in to comment.