refactoring

This commit is contained in:
Timon Ringwald 2022-05-22 22:39:08 +02:00
parent a0487aa1fa
commit 0f6a7ce9ec
2 changed files with 75 additions and 22 deletions

View File

@ -1,36 +1,85 @@
package main package main
import "github.com/fatih/color" import (
"fmt"
"strings"
"github.com/fatih/color"
)
var colorCache = map[string]*color.Color{} var colorCache = map[string]*color.Color{}
func makeColor(name string) (c *color.Color) { func makeColor(colorSpec string) (c *color.Color) {
// caching // caching
if c, ok := colorCache[name]; ok { if c, ok := colorCache[colorSpec]; ok {
return c 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 { switch name {
case "black": case "black":
return color.New(color.FgBlack) c.Add(color.FgBlack)
case "red": case "red":
return color.New(color.FgRed) c.Add(color.FgRed)
case "green": case "green":
return color.New(color.FgGreen) c.Add(color.FgGreen)
case "yellow": case "yellow":
return color.New(color.FgYellow) c.Add(color.FgYellow)
case "blue": case "blue":
return color.New(color.FgBlue) c.Add(color.FgBlue)
case "magenta": case "magenta":
return color.New(color.FgMagenta) c.Add(color.FgMagenta)
case "cyan": case "cyan":
return color.New(color.FgCyan) c.Add(color.FgCyan)
case "white": case "white":
return color.New(color.FgWhite) c.Add(color.FgWhite)
case "": case "":
return color.New()
default: default:
panic("unknown color name. valid color names: black, red, green, yellow, blue, magenta, cyan, white") 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
} }

22
main.go
View File

@ -14,6 +14,10 @@ import (
"github.com/fatih/color" "github.com/fatih/color"
) )
const (
DefaultOutput = "{0}"
)
var ( var (
// regex with sub groups // regex with sub groups
input = flag.String("i", `^(.|\n)*?$`, "input pattern") input = flag.String("i", `^(.|\n)*?$`, "input pattern")
@ -36,7 +40,7 @@ var (
// //
// The following number mutators (integers and floats) are allowed: // 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. // don't ignore lines which do not match against input.
// they will be copied without any changes // they will be copied without any changes
@ -60,19 +64,19 @@ func main() {
panic(err) panic(err)
} }
matchesWholeLine := strings.HasPrefix(*input, "^") && strings.HasSuffix(*input, "$") reformatting := strings.HasPrefix(*input, "^") && strings.HasSuffix(*input, "$") && *output != DefaultOutput
if matchesWholeLine { if reformatting {
escapedOutput := EscSeqReplacer.Replace(*output) escapedOutput := EscSeqReplacer.Replace(*output)
handleWholeLineRegex(pattern, escapedOutput) reformatLine(pattern, escapedOutput)
} else { } else {
handleInlineRegex(pattern) colorCodeMatches(pattern)
} }
} }
// handleWholeLineRegex is using input pattern ot replace // reformatLine is using input pattern to replace
// placeholders in output pattern with the given subgroups // placeholders in output pattern with the given subgroups
func handleWholeLineRegex(pattern *regexp.Regexp, output string) { func reformatLine(pattern *regexp.Regexp, output string) {
for line := range readLines(os.Stdin) { for line := range readLines(os.Stdin) {
matches := pattern.FindStringSubmatch(line) matches := pattern.FindStringSubmatch(line)
if len(matches) == 0 { if len(matches) == 0 {
@ -85,9 +89,9 @@ func handleWholeLineRegex(pattern *regexp.Regexp, output string) {
} }
} }
// handleInlineRegex is using input pattern // colorCodeMatches is using input pattern
// and color-codes all matches within input // and color-codes all matches within input
func handleInlineRegex(pattern *regexp.Regexp) { func colorCodeMatches(pattern *regexp.Regexp) {
c := color.New(color.FgRed, color.Bold) c := color.New(color.FgRed, color.Bold)
for line := range readLines(os.Stdin) { for line := range readLines(os.Stdin) {
matches := pattern.FindAllStringIndex(line, -1) matches := pattern.FindAllStringIndex(line, -1)