Compare commits
No commits in common. "main" and "v1.0.2" have entirely different histories.
@ -313,7 +313,7 @@ Content:
|
|||||||
#!/usr/bin/env sh
|
#!/usr/bin/env sh
|
||||||
|
|
||||||
if [ "$3" = "exec" ]; then
|
if [ "$3" = "exec" ]; then
|
||||||
command ls | format -0 -i "$1" -o "mv \"{0}\" \"$2\"" | xargs -0 -P 4 -I {} sh -c "{}"
|
command ls | format -i "$1" -o "mv \"{0}\" \"$2\"" | xargs -0 -P 4 sh -c
|
||||||
else
|
else
|
||||||
command ls | format -i "$1" -o "mv \"{0}\" \"$2\""
|
command ls | format -i "$1" -o "mv \"{0}\" \"$2\""
|
||||||
echo
|
echo
|
||||||
|
55
main.go
55
main.go
@ -14,7 +14,7 @@ import (
|
|||||||
"git.milar.in/milarin/buildinfo"
|
"git.milar.in/milarin/buildinfo"
|
||||||
)
|
)
|
||||||
|
|
||||||
var ( //flags
|
var (
|
||||||
// regex with sub groups
|
// regex with sub groups
|
||||||
input = flag.String("i", `^(.|\n)*?$`, "input pattern")
|
input = flag.String("i", `^(.|\n)*?$`, "input pattern")
|
||||||
|
|
||||||
@ -49,12 +49,6 @@ var ( //flags
|
|||||||
|
|
||||||
showVersion = flag.Bool("v", false, "show version and exit")
|
showVersion = flag.Bool("v", false, "show version and exit")
|
||||||
|
|
||||||
OutputNullByte = flag.Bool("0", false, "use nullbyte instead of newline as line separator for printing output")
|
|
||||||
)
|
|
||||||
|
|
||||||
var ( // globals
|
|
||||||
LineSeparator string = "\n"
|
|
||||||
|
|
||||||
replacePattern = regexp.MustCompile(`\{(\d+)(?::(.*?))?(?::(.*?))?(?::(.*?))?\}`)
|
replacePattern = regexp.MustCompile(`\{(\d+)(?::(.*?))?(?::(.*?))?(?::(.*?))?\}`)
|
||||||
numMutationPattern = regexp.MustCompile(`([+\-*/])(\d+|\((\d+)\))`)
|
numMutationPattern = regexp.MustCompile(`([+\-*/])(\d+|\((\d+)\))`)
|
||||||
)
|
)
|
||||||
@ -67,10 +61,6 @@ func main() {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if *OutputNullByte {
|
|
||||||
LineSeparator = string(rune(0))
|
|
||||||
}
|
|
||||||
|
|
||||||
pattern, err := regexp.Compile(*input)
|
pattern, err := regexp.Compile(*input)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
@ -83,12 +73,12 @@ func main() {
|
|||||||
|
|
||||||
if len(matches) == 0 {
|
if len(matches) == 0 {
|
||||||
if *keepUnmatched {
|
if *keepUnmatched {
|
||||||
fmt.Print(line, LineSeparator)
|
fmt.Println(line)
|
||||||
}
|
}
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Print(replaceVars(escapedOutput, matches...), LineSeparator)
|
fmt.Println(replaceVars(escapedOutput, matches...))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -97,31 +87,13 @@ func readLines(r io.Reader) <-chan string {
|
|||||||
|
|
||||||
go func(out chan<- string, source io.Reader) {
|
go func(out chan<- string, source io.Reader) {
|
||||||
defer close(out)
|
defer close(out)
|
||||||
|
|
||||||
r := bufio.NewReader(source)
|
r := bufio.NewReader(source)
|
||||||
|
|
||||||
for {
|
for {
|
||||||
line, err := ReadLine(r)
|
|
||||||
|
|
||||||
// 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
|
|
||||||
if err == nil || err == io.EOF && line != "" {
|
|
||||||
out <- line
|
|
||||||
}
|
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}(ch, r)
|
|
||||||
|
|
||||||
return ch
|
|
||||||
}
|
|
||||||
|
|
||||||
func ReadLine(r *bufio.Reader) (string, error) {
|
|
||||||
lines := make([]string, 0, *lineParseAmount)
|
|
||||||
|
|
||||||
var line string
|
var line string
|
||||||
var err error
|
var err error
|
||||||
|
lines := make([]string, 0, *lineParseAmount)
|
||||||
for line, err = r.ReadString('\n'); ; line, err = r.ReadString('\n') {
|
for line, err = r.ReadString('\n'); ; line, err = r.ReadString('\n') {
|
||||||
if rn, size := utf8.DecodeLastRuneInString(line); rn == '\n' {
|
if rn, size := utf8.DecodeLastRuneInString(line); rn == '\n' {
|
||||||
line = line[:len(line)-size]
|
line = line[:len(line)-size]
|
||||||
@ -136,11 +108,24 @@ func ReadLine(r *bufio.Reader) (string, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
linesCombined := strings.Join(lines, "\n")
|
linesCombined := strings.Join(lines, "\n")
|
||||||
return linesCombined, err
|
|
||||||
|
// 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
|
||||||
|
if err == nil || err == io.EOF && linesCombined != "" {
|
||||||
|
out <- linesCombined
|
||||||
|
}
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}(ch, r)
|
||||||
|
|
||||||
|
return ch
|
||||||
}
|
}
|
||||||
|
|
||||||
func replaceVars(format string, vars ...string) string {
|
func replaceVars(format string, vars ...string) string {
|
||||||
replacements := replacePattern.FindAllStringSubmatch(format, -1) // TODO arguments do not change in outer loop (can be moved to main method)
|
replacements := replacePattern.FindAllStringSubmatch(format, -1)
|
||||||
|
|
||||||
for _, replacement := range replacements {
|
for _, replacement := range replacements {
|
||||||
rplStr := replacement[0]
|
rplStr := replacement[0]
|
||||||
|
Loading…
Reference in New Issue
Block a user