Skip to content

Commit

Permalink
Add render function to avoid Go formatter in text
Browse files Browse the repository at this point in the history
There were multiple issues where Go formatter strings (`%s`) in input text lead
to undesirable output.

Add `render` function to handle all color functions with a simple check whether
the output string needs to be created using the formatting or not.
  • Loading branch information
HeavyWombat committed Jul 6, 2021
1 parent ace250f commit 82058b9
Showing 1 changed file with 15 additions and 7 deletions.
22 changes: 15 additions & 7 deletions pkg/dyff/colors.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,24 +38,32 @@ func color(hex string) colorful.Color {
return color
}

func render(format string, a ...interface{}) string {
if len(a) == 0 {
return format
}

return fmt.Sprintf(format, a...)
}

func green(format string, a ...interface{}) string {
return colored(additionGreen, format, a...)
return colored(additionGreen, render(format, a...))
}

func red(format string, a ...interface{}) string {
return colored(removalRed, format, a...)
return colored(removalRed, render(format, a...))
}

func yellow(format string, a ...interface{}) string {
return colored(modificationYellow, format, a...)
return colored(modificationYellow, render(format, a...))
}

func lightgreen(format string, a ...interface{}) string {
return colored(bunt.LightGreen, format, a...)
return colored(bunt.LightGreen, render(format, a...))
}

func lightred(format string, a ...interface{}) string {
return colored(bunt.LightSalmon, format, a...)
return colored(bunt.LightSalmon, render(format, a...))
}

func bold(format string, a ...interface{}) string {
Expand All @@ -68,15 +76,15 @@ func bold(format string, a ...interface{}) string {

func italic(format string, a ...interface{}) string {
return bunt.Style(
fmt.Sprintf(format, a...),
render(format, a...),
bunt.EachLine(),
bunt.Italic(),
)
}

func colored(color colorful.Color, format string, a ...interface{}) string {
return bunt.Style(
fmt.Sprintf(format, a...),
render(format, a...),
bunt.EachLine(),
bunt.Foreground(color),
)
Expand Down

0 comments on commit 82058b9

Please sign in to comment.