Skip to content

Commit

Permalink
no-group option
Browse files Browse the repository at this point in the history
  • Loading branch information
piranha committed Aug 26, 2014
1 parent 77b05f4 commit f0933cb
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 24 deletions.
27 changes: 17 additions & 10 deletions goreplace.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (

const (
Author = "Alexander Solovyov"
Version = "1.12"
Version = "2.0"
)

var byteNewLine = []byte("\n")
Expand All @@ -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"`
}
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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) {
Expand All @@ -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)
}
}

Expand Down
1 change: 1 addition & 0 deletions tests/main.t
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
31 changes: 17 additions & 14 deletions utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}) {

Expand All @@ -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...)
}

0 comments on commit f0933cb

Please sign in to comment.