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: add option --no-unicode to disable unicode symbols #362

Merged
merged 1 commit into from
May 21, 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
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)
}

// 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
15 changes: 15 additions & 0 deletions stdout/stdout_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,21 @@ func TestAnalyzePathWithColors(t *testing.T) {
assert.Contains(t, output.String(), "subnested")
}

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

buff := make([]byte, 10)
output := bytes.NewBuffer(buff)

ui := CreateStdoutUI(output, false, true, true, false, false, false, false, false)
ui.UseOldProgressRunes()
err := ui.AnalyzePath("test_dir/nested", nil)

assert.Nil(t, err)
assert.Contains(t, output.String(), "subnested")
}

func TestItemRows(t *testing.T) {
output := bytes.NewBuffer(make([]byte, 10))

Expand Down
Loading