added pattern highlighting support

This commit is contained in:
2022-10-10 20:14:51 +02:00
parent 34e2b8d855
commit f890596b4c
11 changed files with 74 additions and 8 deletions

View File

@@ -33,12 +33,18 @@ var (
OutflagShell bool
OutputMode string
InvertMatch bool
Pattern string
// FIXME: make configurable somehow, config file or ENV
MatchFG string = "black" // see https://github.com/gookit/color
MatchBG string = "green"
NoColor bool
// used for validation
validOutputmodes = "(orgtbl|markdown|extended|ascii)"
// main program version
Version = "v1.0.6"
Version = "v1.0.7"
// generated version string, used by -v contains lib.Version on
// main branch, and lib.Version-$branch-$lastcommit-$date on

View File

@@ -20,6 +20,8 @@ package lib
import (
"errors"
"fmt"
"github.com/gookit/color"
"os"
"regexp"
"strconv"
"strings"
@@ -130,3 +132,21 @@ func trimRow(row []string) []string {
return fixedrow
}
func colorizeData(output string) string {
if len(Pattern) > 0 && !NoColor && color.IsConsole(os.Stdout) {
r := regexp.MustCompile("(" + Pattern + ")")
return r.ReplaceAllString(output, "<bg="+MatchBG+";fg="+MatchFG+">$1</>")
} else {
return output
}
}
func isTerminal(f *os.File) bool {
o, _ := f.Stat()
if (o.Mode() & os.ModeCharDevice) == os.ModeCharDevice {
return true
} else {
return false
}
}

View File

@@ -19,6 +19,7 @@ package lib
import (
"errors"
"github.com/gookit/color"
"io"
"os"
)
@@ -26,6 +27,10 @@ import (
func ProcessFiles(args []string) error {
fds, pattern, err := determineIO(args)
if !isTerminal(os.Stdout) {
color.Disable()
}
if err != nil {
return err
}
@@ -52,6 +57,7 @@ func determineIO(args []string) ([]io.Reader, string, error) {
// first one is not a file, consider it as regexp and
// shift arg list
pattern = args[0]
Pattern = args[0] // FIXME
args = args[1:]
}

View File

@@ -19,8 +19,8 @@ package lib
import (
"fmt"
"github.com/gookit/color"
"github.com/olekukonko/tablewriter"
"os"
"regexp"
"strings"
)
@@ -76,14 +76,18 @@ func printOrgmodeData(data *Tabdata) {
leftR := regexp.MustCompile("(?m)^\\+")
rightR := regexp.MustCompile("\\+(?m)$")
fmt.Print(rightR.ReplaceAllString(leftR.ReplaceAllString(tableString.String(), "|"), "|"))
color.Print(
colorizeData(
rightR.ReplaceAllString(
leftR.ReplaceAllString(tableString.String(), "|"), "|")))
}
/*
Markdown table
*/
func printMarkdownData(data *Tabdata) {
table := tablewriter.NewWriter(os.Stdout)
tableString := &strings.Builder{}
table := tablewriter.NewWriter(tableString)
table.SetHeader(data.headers)
@@ -95,13 +99,15 @@ func printMarkdownData(data *Tabdata) {
table.SetCenterSeparator("|")
table.Render()
color.Print(colorizeData(tableString.String()))
}
/*
Simple ASCII table without any borders etc, just like the input we expect
*/
func printAsciiData(data *Tabdata) {
table := tablewriter.NewWriter(os.Stdout)
tableString := &strings.Builder{}
table := tablewriter.NewWriter(tableString)
table.SetHeader(data.headers)
table.AppendBulk(data.entries)
@@ -123,6 +129,7 @@ func printAsciiData(data *Tabdata) {
table.SetNoWhiteSpace(true)
table.Render()
color.Print(colorizeData(tableString.String()))
}
/*
@@ -143,7 +150,7 @@ func printExtendedData(data *Tabdata) {
}
}
fmt.Printf(format, data.headers[idx], value)
color.Printf(format, data.headers[idx], value)
idx++
}
fmt.Println()

View File

@@ -19,6 +19,7 @@ package lib
import (
"fmt"
"github.com/gookit/color"
"os"
"strings"
"testing"
@@ -64,6 +65,8 @@ asd igig cxxxncnc
ONE="19191" TWO="EDD 1" THREE="X"`,
}
NoColor = true
r, w, err := os.Pipe()
if err != nil {
t.Fatal(err)
@@ -71,6 +74,9 @@ ONE="19191" TWO="EDD 1" THREE="X"`,
origStdout := os.Stdout
os.Stdout = w
// we need to tell the color mode the io.Writer, even if we don't usw colorization
color.SetOutput(w)
for mode, expect := range expects {
testname := fmt.Sprintf("print-%s", mode)
t.Run(testname, func(t *testing.T) {