Compare commits
2 Commits
Author | SHA1 | Date | |
---|---|---|---|
|
0f6a7ce9ec | ||
|
a0487aa1fa |
75
color.go
75
color.go
@ -1,36 +1,85 @@
|
||||
package main
|
||||
|
||||
import "github.com/fatih/color"
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/fatih/color"
|
||||
)
|
||||
|
||||
var colorCache = map[string]*color.Color{}
|
||||
|
||||
func makeColor(name string) (c *color.Color) {
|
||||
func makeColor(colorSpec string) (c *color.Color) {
|
||||
// caching
|
||||
if c, ok := colorCache[name]; ok {
|
||||
if c, ok := colorCache[colorSpec]; ok {
|
||||
return c
|
||||
}
|
||||
defer func() { colorCache[name] = c }()
|
||||
defer func() { colorCache[colorSpec] = c }()
|
||||
|
||||
splits := strings.Split(colorSpec, ",")
|
||||
name := strings.TrimSpace(splits[0])
|
||||
|
||||
c = color.New()
|
||||
|
||||
switch name {
|
||||
case "black":
|
||||
return color.New(color.FgBlack)
|
||||
c.Add(color.FgBlack)
|
||||
case "red":
|
||||
return color.New(color.FgRed)
|
||||
c.Add(color.FgRed)
|
||||
case "green":
|
||||
return color.New(color.FgGreen)
|
||||
c.Add(color.FgGreen)
|
||||
case "yellow":
|
||||
return color.New(color.FgYellow)
|
||||
c.Add(color.FgYellow)
|
||||
case "blue":
|
||||
return color.New(color.FgBlue)
|
||||
c.Add(color.FgBlue)
|
||||
case "magenta":
|
||||
return color.New(color.FgMagenta)
|
||||
c.Add(color.FgMagenta)
|
||||
case "cyan":
|
||||
return color.New(color.FgCyan)
|
||||
c.Add(color.FgCyan)
|
||||
case "white":
|
||||
return color.New(color.FgWhite)
|
||||
c.Add(color.FgWhite)
|
||||
case "":
|
||||
return color.New()
|
||||
default:
|
||||
panic("unknown color name. valid color names: black, red, green, yellow, blue, magenta, cyan, white")
|
||||
}
|
||||
|
||||
if len(splits) >= 2 {
|
||||
attr := splits[1]
|
||||
|
||||
switch attr {
|
||||
case "bold":
|
||||
fmt.Println("bold")
|
||||
c.Add(color.Bold)
|
||||
case "faint":
|
||||
fmt.Println("faint")
|
||||
c.Add(color.Faint)
|
||||
case "italic":
|
||||
fmt.Println("italic")
|
||||
c.Add(color.Italic)
|
||||
case "underline":
|
||||
fmt.Println("underline")
|
||||
c.Add(color.Underline)
|
||||
case "blink_slow":
|
||||
fmt.Println("blink_slow")
|
||||
c.Add(color.BlinkSlow)
|
||||
case "blink_rapid":
|
||||
fmt.Println("blink_rapid")
|
||||
c.Add(color.BlinkRapid)
|
||||
case "reverse_video":
|
||||
fmt.Println("reverse_video")
|
||||
c.Add(color.ReverseVideo)
|
||||
case "concealed":
|
||||
fmt.Println("concealed")
|
||||
c.Add(color.Concealed)
|
||||
case "crossed_out":
|
||||
fmt.Println("crossed_out")
|
||||
c.Add(color.CrossedOut)
|
||||
case "":
|
||||
fmt.Println("")
|
||||
default:
|
||||
panic("unknown attribute. valid attributes: bold, faint, italic, underline, blink_slow, blink_rapid, reverse_video, concealed, crossed_out")
|
||||
}
|
||||
}
|
||||
|
||||
return c
|
||||
}
|
||||
|
58
main.go
58
main.go
@ -10,6 +10,12 @@ import (
|
||||
"strconv"
|
||||
"strings"
|
||||
"unicode/utf8"
|
||||
|
||||
"github.com/fatih/color"
|
||||
)
|
||||
|
||||
const (
|
||||
DefaultOutput = "{0}"
|
||||
)
|
||||
|
||||
var (
|
||||
@ -34,7 +40,7 @@ var (
|
||||
//
|
||||
// The following number mutators (integers and floats) are allowed:
|
||||
// + - * / ^ %
|
||||
output = flag.String("o", "{0}", "output pattern")
|
||||
output = flag.String("o", DefaultOutput, "output pattern")
|
||||
|
||||
// don't ignore lines which do not match against input.
|
||||
// they will be copied without any changes
|
||||
@ -58,10 +64,37 @@ func main() {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
escapedOutput := EscSeqReplacer.Replace(*output)
|
||||
reformatting := strings.HasPrefix(*input, "^") && strings.HasSuffix(*input, "$") && *output != DefaultOutput
|
||||
|
||||
if reformatting {
|
||||
escapedOutput := EscSeqReplacer.Replace(*output)
|
||||
reformatLine(pattern, escapedOutput)
|
||||
} else {
|
||||
colorCodeMatches(pattern)
|
||||
}
|
||||
}
|
||||
|
||||
// reformatLine is using input pattern to replace
|
||||
// placeholders in output pattern with the given subgroups
|
||||
func reformatLine(pattern *regexp.Regexp, output string) {
|
||||
for line := range readLines(os.Stdin) {
|
||||
matches := pattern.FindStringSubmatch(line)
|
||||
if len(matches) == 0 {
|
||||
if *keepUnmatched {
|
||||
fmt.Println(line)
|
||||
}
|
||||
continue
|
||||
}
|
||||
fmt.Println(replaceVars(output, matches...))
|
||||
}
|
||||
}
|
||||
|
||||
// colorCodeMatches is using input pattern
|
||||
// and color-codes all matches within input
|
||||
func colorCodeMatches(pattern *regexp.Regexp) {
|
||||
c := color.New(color.FgRed, color.Bold)
|
||||
for line := range readLines(os.Stdin) {
|
||||
matches := pattern.FindAllStringIndex(line, -1)
|
||||
|
||||
if len(matches) == 0 {
|
||||
if *keepUnmatched {
|
||||
@ -70,7 +103,26 @@ func main() {
|
||||
continue
|
||||
}
|
||||
|
||||
fmt.Println(replaceVars(escapedOutput, matches...))
|
||||
runes := []rune(line)
|
||||
b := new(strings.Builder)
|
||||
nextMatch := 0
|
||||
currentIndex := 0
|
||||
for currentRune := 0; currentRune < len(runes); currentRune++ {
|
||||
if nextMatch >= len(matches) || currentIndex < matches[nextMatch][0] {
|
||||
// handle next rune
|
||||
b.WriteRune(runes[currentRune])
|
||||
currentIndex += utf8.RuneLen(runes[currentRune])
|
||||
} else {
|
||||
// handle next match
|
||||
match := line[matches[nextMatch][0]:matches[nextMatch][1]]
|
||||
b.WriteString(c.Sprint(match))
|
||||
currentIndex += len(match)
|
||||
currentRune += utf8.RuneCountInString(match) - 1
|
||||
nextMatch++
|
||||
}
|
||||
}
|
||||
|
||||
fmt.Println(b.String())
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user