From 1a8ea86ecebb7bae5de05156e1e2dfef74f32526 Mon Sep 17 00:00:00 2001 From: Ayooluwa Isaiah Date: Tue, 4 May 2021 23:17:24 +0100 Subject: [PATCH] Respect NO_COLOR environmental variable --- src/app.go | 4 ++-- src/operation.go | 20 ++++++++++---------- src/utils.go | 17 +++++++++++++++++ src/validation.go | 22 +++++++++++++--------- 4 files changed, 42 insertions(+), 21 deletions(-) diff --git a/src/app.go b/src/app.go index bbd81f0..630dd1b 100644 --- a/src/app.go +++ b/src/app.go @@ -69,7 +69,7 @@ func checkForUpdates(app *cli.App) { app.Name, ) } else { - fmt.Printf("%s: %s at %s\n", green.Sprint("Update available"), version, resp.Request.URL.String()) + fmt.Printf("%s: %s at %s\n", printColor("green", "Update available"), version, resp.Request.URL.String()) } } @@ -85,7 +85,7 @@ func GetApp() *cli.App { }, Usage: "F2 is a command-line tool for batch renaming multiple files and directories quickly and safely", UsageText: "FLAGS [OPTIONS] [PATHS...]", - Version: "v1.5.6", + Version: "v1.5.7", EnableBashCompletion: true, Flags: []cli.Flag{ &cli.StringFlag{ diff --git a/src/operation.go b/src/operation.go index 5db8f9d..307a1e0 100644 --- a/src/operation.go +++ b/src/operation.go @@ -28,8 +28,8 @@ var ( ) errConflictDetected = fmt.Errorf( - "Conflict detected! Please resolve before proceeding or append the %s flag to fix conflicts automatically", - yellow.Sprint("-F"), + "Resolve conflicts before proceeding or use the %s flag to auto fix all conflicts", + printColor("yellow", "-F"), ) ) @@ -188,7 +188,7 @@ func (op *Operation) undo(path string) error { if err = os.Remove(path); err != nil { fmt.Printf( "Unable to remove redundant undo file '%s' after successful operation.", - yellow.Sprint(path), + printColor("yellow", path), ) } } @@ -204,9 +204,9 @@ func (op *Operation) printChanges() { source := filepath.Join(v.BaseDir, v.Source) target := filepath.Join(v.BaseDir, v.Target) - status := green.Sprint("ok") + status := printColor("green", "ok") if source == target { - status = yellow.Sprint("unchanged") + status = printColor("yellow", "unchanged") } d := []string{source, target, status} data[i] = d @@ -217,7 +217,7 @@ func (op *Operation) printChanges() { // rename iterates over all the matches and renames them on the filesystem // directories are auto-created if necessary. -// Errors are aggregated instead of being reported one by one +// Errors are aggregated ins""tead of being reported one by one func (op *Operation) rename() { var errs []renameError @@ -269,7 +269,7 @@ func (op *Operation) reportErrors() { for i, v := range op.matches { source := filepath.Join(v.BaseDir, v.Source) target := filepath.Join(v.BaseDir, v.Target) - d := []string{source, target, green.Sprint("success")} + d := []string{source, target, printColor("green", "success")} data[i] = d } @@ -284,7 +284,7 @@ func (op *Operation) reportErrors() { d := []string{ source, target, - red.Sprintf("%s", strings.TrimPrefix(msg, ": ")), + printColor("red", strings.TrimPrefix(msg, ": ")), } data[i+len(op.matches)] = d } @@ -316,7 +316,7 @@ func (op *Operation) handleErrors() error { if err == nil && len(op.matches) > 0 { return fmt.Errorf( "Some files could not be renamed. To revert the changes, run %s", - yellow.Sprint("f2 -u"), + printColor("yellow", "f2 -u"), ) } else if err != nil && len(op.matches) > 0 { return fmt.Errorf("The above files could not be renamed") @@ -392,7 +392,7 @@ func (op *Operation) apply() error { op.printChanges() fmt.Printf( "Append the %s flag to apply the above changes\n", - yellow.Sprint("-x"), + printColor("yellow", "-x"), ) return nil diff --git a/src/utils.go b/src/utils.go index 4c33c88..f4b38c9 100644 --- a/src/utils.go +++ b/src/utils.go @@ -11,6 +11,23 @@ import ( "github.com/olekukonko/tablewriter" ) +func printColor(color, text string) string { + if _, ok := os.LookupEnv("NO_COLOR"); ok { + return text + } + + switch color { + case "yellow": + return yellow.Sprint(text) + case "green": + return green.Sprint(text) + case "red": + return red.Sprint(text) + } + + return text +} + func printError(silent bool, err error) { if !silent { fmt.Fprintln(os.Stderr, err) diff --git a/src/validation.go b/src/validation.go index 93d38f3..3249950 100644 --- a/src/validation.go +++ b/src/validation.go @@ -97,7 +97,7 @@ func (op *Operation) reportConflicts() { slice := []string{ strings.Join(v.source, ""), "", - red.Sprint("❌ [Empty filename]"), + printColor("red", "❌ [Empty filename]"), } data = append(data, slice) } @@ -108,7 +108,7 @@ func (op *Operation) reportConflicts() { slice := []string{ strings.Join(v.source, ""), v.target, - red.Sprint("❌ [Path already exists]"), + printColor("red", "❌ [Path already exists]"), } data = append(data, slice) } @@ -120,7 +120,7 @@ func (op *Operation) reportConflicts() { slice := []string{ s, v.target, - red.Sprint("❌ [Overwriting newly renamed path]"), + printColor("red", "❌ [Overwriting newly renamed path]"), } data = append(data, slice) } @@ -133,9 +133,11 @@ func (op *Operation) reportConflicts() { slice := []string{ s, v.target, - red.Sprintf( - "❌ [Invalid characters present: (%s)]", - v.cause, + printColor("red", + fmt.Sprintf( + "❌ [Invalid characters present: (%s)]", + v.cause, + ), ), } data = append(data, slice) @@ -149,9 +151,11 @@ func (op *Operation) reportConflicts() { slice := []string{ s, v.target, - red.Sprintf( - "❌ [Maximum file name exceeded: (%s)]", - v.cause, + printColor("red", + fmt.Sprintf( + "❌ [Maximum file name exceeded: (%s)]", + v.cause, + ), ), } data = append(data, slice)