mirror of
https://codeberg.org/scip/tablizer.git
synced 2025-12-17 04:30:56 +01:00
fixed #5, colorization now always works as expected
This commit is contained in:
@@ -152,36 +152,13 @@ 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(maskParens(output), "<bg="+c.MatchBG+";fg="+c.MatchFG+">$1</>")
|
||||
return r.ReplaceAllStringFunc(output, func(in string) string {
|
||||
return c.ColorStyle.Sprint(in)
|
||||
})
|
||||
} else {
|
||||
return output
|
||||
}
|
||||
}
|
||||
|
||||
func isTerminal(f *os.File) bool {
|
||||
o, _ := f.Stat()
|
||||
if (o.Mode() & os.ModeCharDevice) == os.ModeCharDevice {
|
||||
return true
|
||||
} else {
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
15
lib/io.go
15
lib/io.go
@@ -19,7 +19,6 @@ package lib
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"github.com/gookit/color"
|
||||
"github.com/tlinden/tablizer/cfg"
|
||||
"io"
|
||||
"os"
|
||||
@@ -32,8 +31,6 @@ func ProcessFiles(c cfg.Config, args []string) error {
|
||||
return err
|
||||
}
|
||||
|
||||
determineColormode(&c)
|
||||
|
||||
for _, fd := range fds {
|
||||
data, err := parseFile(c, fd, pattern)
|
||||
if err != nil {
|
||||
@@ -51,18 +48,6 @@ func ProcessFiles(c cfg.Config, args []string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// find supported color mode, modifies config based on constants
|
||||
func determineColormode(c *cfg.Config) {
|
||||
if !isTerminal(os.Stdout) {
|
||||
color.Disable()
|
||||
} else {
|
||||
level := color.TermColorLevel()
|
||||
colors := cfg.Colors()
|
||||
c.MatchFG = colors[level]["fg"]
|
||||
c.MatchBG = colors[level]["bg"]
|
||||
}
|
||||
}
|
||||
|
||||
func determineIO(c *cfg.Config, args []string) ([]io.Reader, string, error) {
|
||||
var pattern string
|
||||
var fds []io.Reader
|
||||
|
||||
@@ -65,7 +65,7 @@ func printData(w io.Writer, c cfg.Config, data *Tabdata) {
|
||||
}
|
||||
|
||||
func output(w io.Writer, str string) {
|
||||
fmt.Fprint(w, unmaskParens(str))
|
||||
fmt.Fprint(w, str)
|
||||
}
|
||||
|
||||
/*
|
||||
|
||||
Reference in New Issue
Block a user