format/main.go

115 lines
2.7 KiB
Go
Raw Normal View History

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"
)
var (
2021-08-20 23:47:41 +02:00
// regex with sub groups
input = flag.String("i", "^.*?$", "input pattern")
// format string with {0} as placeholders
// {0} always matches the whole line
// {1} and onwards match their respective sub groups
2021-08-20 23:47:57 +02:00
// You can optionally specify a printf-syntax for formatting like this: {1:%s} or {1:%02d}
2021-08-20 23:47:41 +02:00
// printf-syntax is currently supported for: strings, floats, integers
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")
2021-08-20 23:47:41 +02:00
replacePattern = regexp.MustCompile(`\{(\d+)(?::(.*?))?\}`)
2021-08-20 23:37:18 +02:00
)
func main() {
flag.Parse()
pattern, err := regexp.Compile(*input)
if err != nil {
panic(err)
}
2022-04-18 13:00:34 +02:00
for line := range readLines(os.Stdin) {
2021-08-20 23:37:18 +02:00
line = line[:len(line)-1]
matches := pattern.FindStringSubmatch(line)
if len(matches) == 0 {
if *keepUnmatched {
fmt.Println(line)
}
continue
}
fmt.Println(replaceVars(*output, matches...))
}
}
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 {
var line string
var err error
lines := make([]string, 0, *lineParseAmount)
for line, err = r.ReadString('\n'); err == nil; line, err = r.ReadString('\n') {
lines = append(lines, line)
if len(lines) == cap(lines) {
break
}
}
if err != nil {
return
}
out <- strings.Join(lines, "")
}
}(ch, r)
return ch
}
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])
rplFmt := replacement[2]
// 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)
format = strings.Replace(format, rplStr, fmt.Sprintf(rplFmt, value), 1)
} else if strings.HasSuffix(rplFmt, "f") { // replace floats
value, _ := strconv.ParseFloat(vars[varIndex], 64)
format = strings.Replace(format, rplStr, fmt.Sprintf(rplFmt, value), 1)
} else { // replace strings
format = strings.Replace(format, rplStr, fmt.Sprintf(rplFmt, vars[varIndex]), 1)
}
}
return format
}