Compare commits
1 Commits
Author | SHA1 | Date | |
---|---|---|---|
50d1ac83b6 |
58
main.go
58
main.go
@ -10,12 +10,6 @@ import (
|
|||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"unicode/utf8"
|
"unicode/utf8"
|
||||||
|
|
||||||
"github.com/fatih/color"
|
|
||||||
)
|
|
||||||
|
|
||||||
const (
|
|
||||||
DefaultOutput = "{0}"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
@ -40,7 +34,7 @@ var (
|
|||||||
//
|
//
|
||||||
// The following number mutators (integers and floats) are allowed:
|
// The following number mutators (integers and floats) are allowed:
|
||||||
// + - * / ^ %
|
// + - * / ^ %
|
||||||
output = flag.String("o", DefaultOutput, "output pattern")
|
output = flag.String("o", "{0}", "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
|
||||||
@ -64,37 +58,10 @@ func main() {
|
|||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
reformatting := strings.HasPrefix(*input, "^") && strings.HasSuffix(*input, "$") && *output != DefaultOutput
|
escapedOutput := EscSeqReplacer.Replace(*output)
|
||||||
|
|
||||||
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) {
|
for line := range readLines(os.Stdin) {
|
||||||
matches := pattern.FindStringSubmatch(line)
|
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 len(matches) == 0 {
|
||||||
if *keepUnmatched {
|
if *keepUnmatched {
|
||||||
@ -103,26 +70,7 @@ func colorCodeMatches(pattern *regexp.Regexp) {
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
runes := []rune(line)
|
fmt.Println(replaceVars(escapedOutput, matches...))
|
||||||
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())
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user