From da5f79732baa511a7bf5e47544eba8c10e0335bb Mon Sep 17 00:00:00 2001 From: Daniel Milde Date: Fri, 23 Jul 2021 12:38:10 +0200 Subject: [PATCH] fixed not showing colors for export fixes #76 --- cmd/gdu/app/app.go | 1 + report/export.go | 6 +++++- report/export_test.go | 12 ++++++------ 3 files changed, 12 insertions(+), 7 deletions(-) diff --git a/cmd/gdu/app/app.go b/cmd/gdu/app/app.go index 4517ee3c4..9807596e7 100644 --- a/cmd/gdu/app/app.go +++ b/cmd/gdu/app/app.go @@ -140,6 +140,7 @@ func (a *App) createUI() (UI, error) { ui = report.CreateExportUI( a.Writer, output, + !a.Flags.NoColor && a.Istty, !a.Flags.NoProgress && a.Istty, ) return ui, nil diff --git a/report/export.go b/report/export.go index 48b4b5a25..fc187fba9 100644 --- a/report/export.go +++ b/report/export.go @@ -29,7 +29,7 @@ type UI struct { } // CreateExportUI creates UI for stdout -func CreateExportUI(output io.Writer, exportOutput io.Writer, showProgress bool) *UI { +func CreateExportUI(output io.Writer, exportOutput io.Writer, useColors bool, showProgress bool) *UI { ui := &UI{ UI: &common.UI{ ShowProgress: showProgress, @@ -43,6 +43,10 @@ func CreateExportUI(output io.Writer, exportOutput io.Writer, showProgress bool) ui.red = color.New(color.FgRed).Add(color.Bold) ui.orange = color.New(color.FgYellow).Add(color.Bold) + if !useColors { + color.NoColor = true + } + return ui } diff --git a/report/export_test.go b/report/export_test.go index 7548c161e..dd21debbc 100644 --- a/report/export_test.go +++ b/report/export_test.go @@ -23,7 +23,7 @@ func TestAnalyzePath(t *testing.T) { output := bytes.NewBuffer(make([]byte, 10)) reportOutput := bytes.NewBuffer(make([]byte, 10)) - ui := CreateExportUI(output, reportOutput, false) + ui := CreateExportUI(output, reportOutput, false, false) ui.SetIgnoreDirPaths([]string{"/xxx"}) err := ui.AnalyzePath("test_dir", nil) assert.Nil(t, err) @@ -40,7 +40,7 @@ func TestAnalyzePathWithProgress(t *testing.T) { output := bytes.NewBuffer(make([]byte, 10)) reportOutput := bytes.NewBuffer(make([]byte, 10)) - ui := CreateExportUI(output, reportOutput, true) + ui := CreateExportUI(output, reportOutput, true, true) ui.SetIgnoreDirPaths([]string{"/xxx"}) err := ui.AnalyzePath("test_dir", nil) assert.Nil(t, err) @@ -54,7 +54,7 @@ func TestShowDevices(t *testing.T) { output := bytes.NewBuffer(make([]byte, 10)) reportOutput := bytes.NewBuffer(make([]byte, 10)) - ui := CreateExportUI(output, reportOutput, true) + ui := CreateExportUI(output, reportOutput, false, true) err := ui.ListDevices(device.Getter) assert.Contains(t, err.Error(), "not supported") @@ -64,7 +64,7 @@ func TestReadAnalysisWhileExporting(t *testing.T) { output := bytes.NewBuffer(make([]byte, 10)) reportOutput := bytes.NewBuffer(make([]byte, 10)) - ui := CreateExportUI(output, reportOutput, true) + ui := CreateExportUI(output, reportOutput, false, true) err := ui.ReadAnalysis(output) assert.Contains(t, err.Error(), "not possible while exporting") @@ -82,7 +82,7 @@ func TestExportToFile(t *testing.T) { output := bytes.NewBuffer(make([]byte, 10)) - ui := CreateExportUI(output, reportOutput, true) + ui := CreateExportUI(output, reportOutput, false, true) ui.SetIgnoreDirPaths([]string{"/xxx"}) err = ui.AnalyzePath("test_dir", nil) assert.Nil(t, err) @@ -104,7 +104,7 @@ func TestFormatSize(t *testing.T) { output := bytes.NewBuffer(make([]byte, 10)) reportOutput := bytes.NewBuffer(make([]byte, 10)) - ui := CreateExportUI(output, reportOutput, true) + ui := CreateExportUI(output, reportOutput, false, true) assert.Contains(t, ui.formatSize(1), "B") assert.Contains(t, ui.formatSize(1<<10+1), "KiB")