Compare commits
4 Commits
Author | SHA1 | Date | |
---|---|---|---|
b7a50df5e7 | |||
![]() |
10de418b6e | ||
![]() |
929784da83 | ||
![]() |
e7a2a65eb6 |
@ -313,7 +313,7 @@ Content:
|
||||
#!/usr/bin/env sh
|
||||
|
||||
if [ "$3" = "exec" ]; then
|
||||
command ls | format -i "$1" -o "mv \"{0}\" \"$2\"" | xargs -0 -P 4 sh -c
|
||||
command ls | format -0 -i "$1" -o "mv \"{0}\" \"$2\"" | xargs -0 -P 4 -I {} sh -c "{}"
|
||||
else
|
||||
command ls | format -i "$1" -o "mv \"{0}\" \"$2\""
|
||||
echo
|
||||
|
8
go.mod
8
go.mod
@ -1,9 +1,13 @@
|
||||
module git.milar.in/milarin/format
|
||||
module git.tordarus.net/tordarus/format
|
||||
|
||||
go 1.18
|
||||
|
||||
require (
|
||||
github.com/fatih/color v1.13.0 // indirect
|
||||
git.milar.in/milarin/buildinfo v1.0.0
|
||||
github.com/fatih/color v1.13.0
|
||||
)
|
||||
|
||||
require (
|
||||
github.com/mattn/go-colorable v0.1.12 // indirect
|
||||
github.com/mattn/go-isatty v0.0.14 // indirect
|
||||
golang.org/x/sys v0.0.0-20220517195934-5e4e11fc645e // indirect
|
||||
|
2
go.sum
2
go.sum
@ -1,3 +1,5 @@
|
||||
git.milar.in/milarin/buildinfo v1.0.0 h1:tw98GupUYl/0a/3aPGuezhE4wseycOSsbcLp70hy60U=
|
||||
git.milar.in/milarin/buildinfo v1.0.0/go.mod h1:arI9ZoENOgcZcanv25k9y4dKDUhPp0buJrlVerGruas=
|
||||
github.com/fatih/color v1.13.0 h1:8LOYc1KYPPmyKMuN8QV2DNRWNbLo6LZ0iLs8+mlH53w=
|
||||
github.com/fatih/color v1.13.0/go.mod h1:kLAiJbzzSOZDVNGyDpeOxJ47H46qBXwg5ILebYFFOfk=
|
||||
github.com/mattn/go-colorable v0.1.9/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope9wVRipJSqc=
|
||||
|
72
main.go
72
main.go
@ -10,9 +10,11 @@ import (
|
||||
"strconv"
|
||||
"strings"
|
||||
"unicode/utf8"
|
||||
|
||||
"git.milar.in/milarin/buildinfo"
|
||||
)
|
||||
|
||||
var (
|
||||
var ( //flags
|
||||
// regex with sub groups
|
||||
input = flag.String("i", `^(.|\n)*?$`, "input pattern")
|
||||
|
||||
@ -45,6 +47,14 @@ var (
|
||||
// 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")
|
||||
|
||||
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+)(?::(.*?))?(?::(.*?))?(?::(.*?))?\}`)
|
||||
numMutationPattern = regexp.MustCompile(`([+\-*/])(\d+|\((\d+)\))`)
|
||||
)
|
||||
@ -52,6 +62,15 @@ var (
|
||||
func main() {
|
||||
flag.Parse()
|
||||
|
||||
if *showVersion {
|
||||
buildinfo.Print(buildinfo.Options{})
|
||||
return
|
||||
}
|
||||
|
||||
if *OutputNullByte {
|
||||
LineSeparator = string(rune(0))
|
||||
}
|
||||
|
||||
pattern, err := regexp.Compile(*input)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
@ -64,12 +83,12 @@ func main() {
|
||||
|
||||
if len(matches) == 0 {
|
||||
if *keepUnmatched {
|
||||
fmt.Println(line)
|
||||
fmt.Print(line, LineSeparator)
|
||||
}
|
||||
continue
|
||||
}
|
||||
|
||||
fmt.Println(replaceVars(escapedOutput, matches...))
|
||||
fmt.Print(replaceVars(escapedOutput, matches...), LineSeparator)
|
||||
}
|
||||
}
|
||||
|
||||
@ -78,32 +97,15 @@ func readLines(r io.Reader) <-chan string {
|
||||
|
||||
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'); ; 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")
|
||||
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 && linesCombined != "" {
|
||||
out <- linesCombined
|
||||
if err == nil || err == io.EOF && line != "" {
|
||||
out <- line
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
@ -115,8 +117,30 @@ func readLines(r io.Reader) <-chan string {
|
||||
return ch
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
func replaceVars(format string, vars ...string) string {
|
||||
replacements := replacePattern.FindAllStringSubmatch(format, -1)
|
||||
replacements := replacePattern.FindAllStringSubmatch(format, -1) // TODO arguments do not change in outer loop (can be moved to main method)
|
||||
|
||||
for _, replacement := range replacements {
|
||||
rplStr := replacement[0]
|
||||
|
Loading…
x
Reference in New Issue
Block a user