From f0933cbe3d13953d5dad5953ec45b89e1cad4e27 Mon Sep 17 00:00:00 2001 From: Alexander Solovyov Date: Tue, 26 Aug 2014 16:38:59 +0300 Subject: [PATCH] no-group option --- goreplace.go | 27 +++++++++++++++++---------- tests/main.t | 1 + utils.go | 31 +++++++++++++++++-------------- 3 files changed, 35 insertions(+), 24 deletions(-) diff --git a/goreplace.go b/goreplace.go index cf84831..3762199 100644 --- a/goreplace.go +++ b/goreplace.go @@ -17,7 +17,7 @@ import ( const ( Author = "Alexander Solovyov" - Version = "1.12" + Version = "2.0" ) var byteNewLine = []byte("\n") @@ -36,6 +36,7 @@ var opts struct { OnlyName bool `short:"n" long:"filename" description:"print only filenames"` Verbose bool `short:"v" long:"verbose" description:"be verbose (show non-fatal errors, like unreadable files)"` NoColors bool `short:"c" long:"no-colors" description:"do not show colors in output"` + NoGroup bool `short:"N" long:"no-group" description:"print file name before each line"` ShowVersion bool `short:"V" long:"version" description:"show version and exit"` ShowHelp bool `short:"h" long:"help" description:"show this help message"` } @@ -114,7 +115,7 @@ func errhandle(err error, exit bool) bool { func searchFiles(pattern *regexp.Regexp, ignoreFileMatcher Matcher, acceptedFileMatcher Matcher) { - printer := &Printer{NoColors: NoColors} + printer := &Printer{NoColors, opts.NoGroup, ""} v := &GRVisitor{printer, pattern, ignoreFileMatcher, acceptedFileMatcher} err := filepath.Walk(".", v.Walk) @@ -249,15 +250,18 @@ func (v *GRVisitor) SearchFile(fn string, content []byte) { seen := NewIntSet() binary := bytes.IndexByte(content, 0) != -1 found := v.FindAllIndex(content) + idxFmt := "%d:" - maxVal := 0 - for _, info := range found { - if info.num > maxVal { - maxVal = info.num + if !opts.NoGroup { + maxVal := 0 + for _, info := range found { + if info.num > maxVal { + maxVal = info.num + } } + idxLength := int(math.Ceil(math.Log10(float64(maxVal)))) + idxFmt = fmt.Sprintf("%%%dd:", idxLength) } - idxLength := int(math.Ceil(math.Log10(float64(maxVal)))) - idxFmt := fmt.Sprintf("%%%dd:", idxLength) for _, info := range found { if !seen.Add(info.num) { @@ -274,12 +278,15 @@ func (v *GRVisitor) SearchFile(fn string, content []byte) { return } - v.printer.FilePrintf(fn, "@!@y" + idxFmt, idxFmt, info.num) colored := v.pattern.ReplaceAllStringFunc(string(info.line), func(wrap string) string { return v.printer.Sprintf("@Y%s", "%s", wrap) }) - fmt.Println(colored) + + v.printer.FilePrintf(fn, + "@!@y" + idxFmt + "@|" + colored + "\n", + idxFmt + colored + "\n", + info.num) } } diff --git a/tests/main.t b/tests/main.t index 246e7ef..3d0ded0 100644 --- a/tests/main.t +++ b/tests/main.t @@ -28,6 +28,7 @@ Usage: -v, --verbose be verbose (show non-fatal errors, like unreadable files) -c, --no-colors do not show colors in output + -N, --no-group print file name before each line -V, --version show version and exit -h, --help show this help message diff --git a/utils.go b/utils.go index 9e2debf..35a9d2c 100644 --- a/utils.go +++ b/utils.go @@ -10,23 +10,10 @@ import ( type Printer struct { NoColors bool + NoGroup bool previous string } -func (p *Printer) FilePrintf(fn, colorfmt, plainfmt string, - args... interface{}) { - - if fn != p.previous { - if p.previous != "" { - fmt.Println("") - } - p.Printf("@g%s\n", "%s\n", fn) - p.previous = fn - } - - p.Printf(colorfmt, plainfmt, args...) -} - func (p *Printer) Printf(colorfmt, plainfmt string, args... interface{}) { @@ -46,3 +33,19 @@ func (p *Printer) Sprintf(colorfmt, plainfmt string, return color.Sprintf(colorfmt, args...) } } + +func (p *Printer) FilePrintf(fn, colorfmt, plainfmt string, + args... interface{}) { + + if p.NoGroup { + p.Printf("@g%s:", "%s:", fn) + } else if fn != p.previous { + if p.previous != "" { + fmt.Println("") + } + p.Printf("@g%s\n", "%s\n", fn) + p.previous = fn + } + + p.Printf(colorfmt, plainfmt, args...) +}