Skip to content

Commit

Permalink
Respect NO_COLOR environmental variable
Browse files Browse the repository at this point in the history
  • Loading branch information
ayoisaiah committed May 4, 2021
1 parent a59b18b commit 1a8ea86
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 21 deletions.
4 changes: 2 additions & 2 deletions src/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}
}

Expand All @@ -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{
Expand Down
20 changes: 10 additions & 10 deletions src/operation.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
)
)

Expand Down Expand Up @@ -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),
)
}
}
Expand All @@ -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
Expand All @@ -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

Expand Down Expand Up @@ -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
}

Expand All @@ -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
}
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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
Expand Down
17 changes: 17 additions & 0 deletions src/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
22 changes: 13 additions & 9 deletions src/validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -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)
}
Expand All @@ -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)
}
Expand All @@ -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)
Expand All @@ -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)
Expand Down

0 comments on commit 1a8ea86

Please sign in to comment.