mirror of
https://codeberg.org/scip/tablizer.git
synced 2025-12-17 12:31:06 +01:00
fixed #5, colorization now always works as expected
This commit is contained in:
6
TODO.md
6
TODO.md
@@ -1,17 +1,11 @@
|
|||||||
## Fixes to be implemented
|
## Fixes to be implemented
|
||||||
|
|
||||||
- highlighting does not repeat, only 1 will be highlighted, see #3
|
|
||||||
|
|
||||||
- rm printYamlData() log.Fatal(), maybe return error on all printers?
|
|
||||||
|
|
||||||
- refactor parser, there's some duplicate code, remove pattern from parser args
|
- refactor parser, there's some duplicate code, remove pattern from parser args
|
||||||
|
|
||||||
## Features to be implemented
|
## Features to be implemented
|
||||||
|
|
||||||
- add comment support (csf.NewReader().Comment = '#')
|
- add comment support (csf.NewReader().Comment = '#')
|
||||||
|
|
||||||
- add output mode csv
|
|
||||||
|
|
||||||
- add --no-headers option
|
- add --no-headers option
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -46,12 +46,10 @@ type Config struct {
|
|||||||
|
|
||||||
/*
|
/*
|
||||||
FIXME: make configurable somehow, config file or ENV
|
FIXME: make configurable somehow, config file or ENV
|
||||||
see https://github.com/gookit/color will be set by
|
see https://github.com/gookit/color.
|
||||||
io.ProcessFiles() according to currently supported
|
|
||||||
color mode.
|
|
||||||
*/
|
*/
|
||||||
MatchFG string
|
ColorStyle color.Style
|
||||||
MatchBG string
|
|
||||||
NoColor bool
|
NoColor bool
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -84,22 +82,43 @@ type Sortmode struct {
|
|||||||
Age bool
|
Age bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func Colors() map[color.Level]map[string]string {
|
// default color schemes
|
||||||
// default color schemes
|
func Colors() map[color.Level]map[string]color.Color {
|
||||||
return map[color.Level]map[string]string{
|
return map[color.Level]map[string]color.Color{
|
||||||
color.Level16: {
|
color.Level16: {
|
||||||
"bg": "green", "fg": "black",
|
"bg": color.BgGreen, "fg": color.FgBlack,
|
||||||
},
|
},
|
||||||
color.Level256: {
|
color.Level256: {
|
||||||
"bg": "lightGreen", "fg": "black",
|
"bg": color.BgLightGreen, "fg": color.FgBlack,
|
||||||
},
|
},
|
||||||
color.LevelRgb: {
|
color.LevelRgb: {
|
||||||
// FIXME: maybe use something nicer
|
// FIXME: maybe use something nicer
|
||||||
"bg": "lightGreen", "fg": "black",
|
"bg": color.BgLightGreen, "fg": color.FgBlack,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// find supported color mode, modifies config based on constants
|
||||||
|
func (c *Config) DetermineColormode() {
|
||||||
|
if !isTerminal(os.Stdout) {
|
||||||
|
color.Disable()
|
||||||
|
} else {
|
||||||
|
level := color.TermColorLevel()
|
||||||
|
colors := Colors()
|
||||||
|
c.ColorStyle = color.New(colors[level]["bg"], colors[level]["fg"])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Return true if current terminal is interactive
|
||||||
|
func isTerminal(f *os.File) bool {
|
||||||
|
o, _ := f.Stat()
|
||||||
|
if (o.Mode() & os.ModeCharDevice) == os.ModeCharDevice {
|
||||||
|
return true
|
||||||
|
} else {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func Getversion() string {
|
func Getversion() string {
|
||||||
// main program version
|
// main program version
|
||||||
|
|
||||||
|
|||||||
@@ -69,10 +69,11 @@ func Execute() {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// prepare flags
|
// Setup
|
||||||
conf.CheckEnv()
|
conf.CheckEnv()
|
||||||
conf.PrepareModeFlags(modeflag)
|
conf.PrepareModeFlags(modeflag)
|
||||||
conf.PrepareSortFlags(sortmode)
|
conf.PrepareSortFlags(sortmode)
|
||||||
|
conf.DetermineColormode()
|
||||||
|
|
||||||
if err := conf.PreparePattern(); err != nil {
|
if err := conf.PreparePattern(); err != nil {
|
||||||
return err
|
return err
|
||||||
|
|||||||
@@ -152,36 +152,13 @@ func trimRow(row []string) []string {
|
|||||||
return fixedrow
|
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 {
|
func colorizeData(c cfg.Config, output string) string {
|
||||||
if len(c.Pattern) > 0 && !c.NoColor && color.IsConsole(os.Stdout) {
|
if len(c.Pattern) > 0 && !c.NoColor && color.IsConsole(os.Stdout) {
|
||||||
r := regexp.MustCompile("(" + c.Pattern + ")")
|
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 {
|
} else {
|
||||||
return output
|
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 (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"github.com/gookit/color"
|
|
||||||
"github.com/tlinden/tablizer/cfg"
|
"github.com/tlinden/tablizer/cfg"
|
||||||
"io"
|
"io"
|
||||||
"os"
|
"os"
|
||||||
@@ -32,8 +31,6 @@ func ProcessFiles(c cfg.Config, args []string) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
determineColormode(&c)
|
|
||||||
|
|
||||||
for _, fd := range fds {
|
for _, fd := range fds {
|
||||||
data, err := parseFile(c, fd, pattern)
|
data, err := parseFile(c, fd, pattern)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -51,18 +48,6 @@ func ProcessFiles(c cfg.Config, args []string) error {
|
|||||||
return nil
|
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) {
|
func determineIO(c *cfg.Config, args []string) ([]io.Reader, string, error) {
|
||||||
var pattern string
|
var pattern string
|
||||||
var fds []io.Reader
|
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) {
|
func output(w io.Writer, str string) {
|
||||||
fmt.Fprint(w, unmaskParens(str))
|
fmt.Fprint(w, str)
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
|||||||
Reference in New Issue
Block a user