2021-08-20 23:37:18 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"flag"
|
|
|
|
"fmt"
|
2022-04-18 13:00:34 +02:00
|
|
|
"io"
|
2021-08-20 23:37:18 +02:00
|
|
|
"os"
|
|
|
|
"regexp"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
2022-05-06 10:51:52 +02:00
|
|
|
"unicode/utf8"
|
2022-08-29 13:45:28 +02:00
|
|
|
|
|
|
|
"git.milar.in/milarin/buildinfo"
|
2021-08-20 23:37:18 +02:00
|
|
|
)
|
|
|
|
|
2023-03-20 11:07:09 +01:00
|
|
|
var ( //flags
|
2021-08-20 23:47:41 +02:00
|
|
|
// regex with sub groups
|
2022-05-06 10:51:52 +02:00
|
|
|
input = flag.String("i", `^(.|\n)*?$`, "input pattern")
|
2021-08-20 23:47:41 +02:00
|
|
|
|
|
|
|
// format string with {0} as placeholders
|
|
|
|
// {0} always matches the whole line
|
|
|
|
// {1} and onwards match their respective sub groups
|
2022-04-18 17:39:41 +02:00
|
|
|
//
|
2021-08-20 23:47:57 +02:00
|
|
|
// You can optionally specify a printf-syntax for formatting like this: {1:%s} or {1:%02d}
|
2022-04-18 17:39:41 +02:00
|
|
|
// printf-syntax is currently supported for: strings, floats, integers.
|
|
|
|
//
|
|
|
|
// Addtionally mutators can be provided
|
|
|
|
// to further manipulate the value using the given syntax: {1:%d:+1}
|
|
|
|
//
|
|
|
|
// The value mutated by can also be a back reference to another group
|
|
|
|
// using round brackets like this: {1:%d:+(2)}}
|
|
|
|
//
|
|
|
|
// Multiple mutators can be used at once: {1:%d:+2*3-(2)}
|
|
|
|
// Be aware that they will be applied strictly from left to right!
|
|
|
|
//
|
|
|
|
// The following number mutators (integers and floats) are allowed:
|
|
|
|
// + - * / ^ %
|
2021-08-20 23:47:41 +02:00
|
|
|
output = flag.String("o", "{0}", "output pattern")
|
|
|
|
|
|
|
|
// don't ignore lines which do not match against input.
|
|
|
|
// they will be copied without any changes
|
2021-08-20 23:37:18 +02:00
|
|
|
keepUnmatched = flag.Bool("k", false, "keep unmatched lines")
|
|
|
|
|
2022-04-18 13:00:34 +02:00
|
|
|
// if the amount of lines in stdin are not divisible by lineParseAmount,
|
|
|
|
// the last lines will be ignored completely.
|
|
|
|
// it may be useful to have a boolean flag for this behavior
|
|
|
|
lineParseAmount = flag.Int("n", 1, "amount of lines to feed into input pattern")
|
|
|
|
|
2022-08-29 13:45:28 +02:00
|
|
|
showVersion = flag.Bool("v", false, "show version and exit")
|
|
|
|
|
2023-03-20 11:07:09 +01:00
|
|
|
OutputNullByte = flag.Bool("0", false, "use nullbyte instead of newline as line separator for printing output")
|
|
|
|
)
|
|
|
|
|
|
|
|
var ( // globals
|
|
|
|
LineSeparator string = "\n"
|
|
|
|
|
2022-07-25 01:50:52 +02:00
|
|
|
replacePattern = regexp.MustCompile(`\{(\d+)(?::(.*?))?(?::(.*?))?(?::(.*?))?\}`)
|
2022-04-18 17:39:41 +02:00
|
|
|
numMutationPattern = regexp.MustCompile(`([+\-*/])(\d+|\((\d+)\))`)
|
2021-08-20 23:37:18 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
flag.Parse()
|
|
|
|
|
2022-08-29 13:45:28 +02:00
|
|
|
if *showVersion {
|
|
|
|
buildinfo.Print(buildinfo.Options{})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-03-20 11:07:09 +01:00
|
|
|
if *OutputNullByte {
|
|
|
|
LineSeparator = string(rune(0))
|
|
|
|
}
|
|
|
|
|
2021-08-20 23:37:18 +02:00
|
|
|
pattern, err := regexp.Compile(*input)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2022-05-06 10:51:52 +02:00
|
|
|
escapedOutput := EscSeqReplacer.Replace(*output)
|
|
|
|
|
2022-04-18 13:00:34 +02:00
|
|
|
for line := range readLines(os.Stdin) {
|
2021-08-20 23:37:18 +02:00
|
|
|
matches := pattern.FindStringSubmatch(line)
|
|
|
|
|
|
|
|
if len(matches) == 0 {
|
|
|
|
if *keepUnmatched {
|
2023-03-20 11:07:09 +01:00
|
|
|
fmt.Print(line, LineSeparator)
|
2021-08-20 23:37:18 +02:00
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2023-03-20 11:07:09 +01:00
|
|
|
fmt.Print(replaceVars(escapedOutput, matches...), LineSeparator)
|
2021-08-20 23:37:18 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-18 13:00:34 +02:00
|
|
|
func readLines(r io.Reader) <-chan string {
|
|
|
|
ch := make(chan string, 10)
|
|
|
|
|
|
|
|
go func(out chan<- string, source io.Reader) {
|
|
|
|
defer close(out)
|
|
|
|
r := bufio.NewReader(source)
|
|
|
|
|
|
|
|
for {
|
2023-03-20 11:07:09 +01:00
|
|
|
line, err := ReadLine(r)
|
2022-05-06 10:51:52 +02:00
|
|
|
|
|
|
|
// use data as line if reading was successfull or EOF has been reached
|
|
|
|
// in the latter case: only use data if something could be read until EOF
|
2023-03-20 11:07:09 +01:00
|
|
|
if err == nil || err == io.EOF && line != "" {
|
|
|
|
out <- line
|
2022-05-06 10:51:52 +02:00
|
|
|
}
|
|
|
|
|
2022-04-18 13:00:34 +02:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}(ch, r)
|
|
|
|
|
|
|
|
return ch
|
|
|
|
}
|
|
|
|
|
2023-03-20 11:07:09 +01:00
|
|
|
func ReadLine(r *bufio.Reader) (string, error) {
|
|
|
|
lines := make([]string, 0, *lineParseAmount)
|
|
|
|
|
|
|
|
var line string
|
|
|
|
var err error
|
|
|
|
for line, err = r.ReadString('\n'); ; line, err = r.ReadString('\n') {
|
|
|
|
if rn, size := utf8.DecodeLastRuneInString(line); rn == '\n' {
|
|
|
|
line = line[:len(line)-size]
|
|
|
|
}
|
|
|
|
|
|
|
|
lines = append(lines, line)
|
|
|
|
|
|
|
|
// stop reading as soon as lineParseAmount is reached or an error occured (most likely EOF)
|
|
|
|
if len(lines) == cap(lines) || err != nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
linesCombined := strings.Join(lines, "\n")
|
|
|
|
return linesCombined, err
|
|
|
|
}
|
|
|
|
|
2021-08-20 23:37:18 +02:00
|
|
|
func replaceVars(format string, vars ...string) string {
|
|
|
|
replacements := replacePattern.FindAllStringSubmatch(format, -1)
|
|
|
|
|
|
|
|
for _, replacement := range replacements {
|
|
|
|
rplStr := replacement[0]
|
|
|
|
varIndex, _ := strconv.Atoi(replacement[1])
|
2022-05-22 21:06:06 +02:00
|
|
|
rplColor := makeColor(replacement[2])
|
|
|
|
rplFmt := replacement[3]
|
2021-08-20 23:37:18 +02:00
|
|
|
|
|
|
|
// default format if not specified by user
|
|
|
|
if rplFmt == "" {
|
|
|
|
rplFmt = "%s"
|
|
|
|
}
|
|
|
|
|
|
|
|
if strings.HasSuffix(rplFmt, "d") { // replace integers
|
|
|
|
value, _ := strconv.ParseInt(vars[varIndex], 10, 64)
|
2022-05-22 21:06:06 +02:00
|
|
|
mutate := numMut2func[int64](replacement[4])
|
|
|
|
format = strings.Replace(format, rplStr, rplColor.Sprintf(rplFmt, mutate(value, vars)), 1)
|
2022-04-18 20:59:52 +02:00
|
|
|
} else if strings.HasSuffix(rplFmt, "f") || strings.HasSuffix(rplFmt, "g") { // replace floats
|
2021-08-20 23:37:18 +02:00
|
|
|
value, _ := strconv.ParseFloat(vars[varIndex], 64)
|
2022-05-22 21:06:06 +02:00
|
|
|
mutate := numMut2func[float64](replacement[4])
|
|
|
|
format = strings.Replace(format, rplStr, rplColor.Sprintf(rplFmt, mutate(value, vars)), 1)
|
2021-08-20 23:37:18 +02:00
|
|
|
} else { // replace strings
|
2022-05-22 21:06:06 +02:00
|
|
|
format = strings.Replace(format, rplStr, rplColor.Sprintf(rplFmt, vars[varIndex]), 1)
|
2021-08-20 23:37:18 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return format
|
|
|
|
}
|