Skip to content

Commit

Permalink
Workaround for issue#3: text containing tag like content is not color…
Browse files Browse the repository at this point in the history
…ized properly.
  • Loading branch information
Thomas von Dein committed Oct 24, 2022
1 parent 5c42f7a commit 001021d
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
18 changes: 17 additions & 1 deletion lib/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,10 +152,26 @@ func trimRow(row []string) []string {
return fixedrow
}

func maskParens(in string) string {
/*
we need to escape brackets, because the color module treats
text enclosed within < and > as a color tag and therefore the
color tags don't work anymore.
See https://github.com/gookit/color/issues/52 for details.
*/
return strings.ReplaceAll(strings.ReplaceAll(in, ">", "⦘"), "<", "⦗")
}

func unmaskParens(in string) string {
// does the reverse from above during actual output
return strings.ReplaceAll(strings.ReplaceAll(in, "⦘", ">"), "⦗", "<")
}

func colorizeData(c cfg.Config, output string) string {
if len(c.Pattern) > 0 && !c.NoColor && color.IsConsole(os.Stdout) {
r := regexp.MustCompile("(" + c.Pattern + ")")
return r.ReplaceAllString(output, "<bg="+c.MatchBG+";fg="+c.MatchFG+">$1</>")
return r.ReplaceAllString(maskParens(output), "<bg="+c.MatchBG+";fg="+c.MatchFG+">$1</>")
} else {
return output
}
Expand Down
2 changes: 1 addition & 1 deletion lib/printer.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func printData(w io.Writer, c cfg.Config, data *Tabdata) {
}

func output(w io.Writer, str string) {
fmt.Fprint(w, str)
fmt.Fprint(w, unmaskParens(str))
}

/*
Expand Down

0 comments on commit 001021d

Please # to comment.