From 5de28528a942219d8188c5073cd2872d7acb4e5d Mon Sep 17 00:00:00 2001 From: Daniel Milde Date: Tue, 21 May 2024 13:38:06 +0200 Subject: [PATCH] feat: add option `--no-unicode` to disable unicode symbols --- cmd/gdu/app/app.go | 9 +++++++-- cmd/gdu/app/app_test.go | 18 ++++++++++++++++++ cmd/gdu/main.go | 1 + stdout/stdout.go | 17 +++++++++++++---- 4 files changed, 39 insertions(+), 6 deletions(-) diff --git a/cmd/gdu/app/app.go b/cmd/gdu/app/app.go index 7f9612cf1..5860453f1 100644 --- a/cmd/gdu/app/app.go +++ b/cmd/gdu/app/app.go @@ -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"` @@ -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, @@ -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 @@ -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() }) diff --git a/cmd/gdu/app/app_test.go b/cmd/gdu/app/app_test.go index 61e0255f6..55b428ac1 100644 --- a/cmd/gdu/app/app_test.go +++ b/cmd/gdu/app/app_test.go @@ -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() diff --git a/cmd/gdu/main.go b/cmd/gdu/main.go index a842f1021..e79e0dda1 100644 --- a/cmd/gdu/main.go +++ b/cmd/gdu/main.go @@ -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") diff --git a/stdout/stdout.go b/stdout/stdout.go index 669fc69d4..ced65fd53 100644 --- a/stdout/stdout.go +++ b/stdout/stdout.go @@ -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( @@ -68,6 +72,11 @@ func CreateStdoutUI( return ui } +func (ui *UI) UseOldProgressRunes() { + progressRunes = progressRunesOld + progressRunesCount = len(progressRunes) +} + // StartUILoop stub func (ui *UI) StartUILoop() error { return nil @@ -321,7 +330,7 @@ func (ui *UI) showReadingProgress(doneChan chan struct{}) { time.Sleep(100 * time.Millisecond) i++ - i %= 10 + i %= progressRunesCount } } @@ -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: @@ -370,7 +379,7 @@ func (ui *UI) updateProgress(updateStatsDone <-chan struct{}) { time.Sleep(100 * time.Millisecond) i++ - i %= 10 + i %= progressRunesCount } }