Skip to content

Commit

Permalink
print matching results on the go
Browse files Browse the repository at this point in the history
Fixes #20

Signed-off-by: Iskander Sharipov <[email protected]>
  • Loading branch information
quasilyte committed Aug 14, 2019
1 parent 17f68a7 commit 947adbb
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 24 deletions.
1 change: 0 additions & 1 deletion cmd/phpgrep/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ func main() {
{"compile filters", p.compileFilters},
{"compile pattern", p.compilePattern},
{"execute pattern", p.executePattern},
{"print results", p.printResults},
}

for _, step := range steps {
Expand Down
45 changes: 25 additions & 20 deletions cmd/phpgrep/program.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"path/filepath"
"strings"
"sync"
"sync/atomic"

"github.com/quasilyte/phpgrep"
)
Expand All @@ -22,7 +23,7 @@ type program struct {

workers []*worker
filters []phpgrep.Filter
matches int
matches int64
}

func (p *program) validateFlags() error {
Expand Down Expand Up @@ -104,9 +105,18 @@ func (p *program) executePattern() error {
if p.args.verbose {
log.Printf("debug: worker#%d greps %q file", w.id, filename)
}
if err := w.grepFile(filename); err != nil {

matches, err := w.grepFile(filename)
if err != nil {
log.Printf("error: execute pattern: %s: %v", filename, err)
continue
}
if len(matches) == 0 {
continue
}

atomic.AddInt64(&p.matches, int64(len(matches)))
printMatches(&p.args, matches)
}
}(w)
}
Expand All @@ -130,26 +140,21 @@ func (p *program) executePattern() error {
return err
}

func (p *program) printResults() error {
// TODO(quasilyte): add JSON output format?
for _, w := range p.workers {
for _, m := range w.matches {
p.matches++

text := m.text
if !p.args.multiline {
text = strings.Replace(text, "\n", `\n`, -1)
}
filename := m.filename
if p.args.abs {
abs, err := filepath.Abs(filename)
if err != nil {
return fmt.Errorf("abs(%q): %v", m.filename, err)
}
filename = abs
func printMatches(args *arguments, matches []match) error {
for _, m := range matches {
text := m.text
if !args.multiline {
text = strings.Replace(text, "\n", `\n`, -1)
}
filename := m.filename
if args.abs {
abs, err := filepath.Abs(filename)
if err != nil {
return fmt.Errorf("abs(%q): %v", m.filename, err)
}
fmt.Printf("%s:%d: %s\n", filename, m.line, text)
filename = abs
}
fmt.Printf("%s:%d: %s\n", filename, m.line, text)
}

return nil
Expand Down
7 changes: 4 additions & 3 deletions cmd/phpgrep/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,13 @@ type worker struct {
matches []match
}

func (w *worker) grepFile(filename string) error {
func (w *worker) grepFile(filename string) ([]match, error) {
data, err := ioutil.ReadFile(filename)
if err != nil {
return fmt.Errorf("read target: %v", err)
return nil, fmt.Errorf("read target: %v", err)
}

w.matches = w.matches[:0]
w.m.Find(data, func(m *phpgrep.MatchData) bool {
w.matches = append(w.matches, match{
text: string(data[m.PosFrom:m.PosTo]),
Expand All @@ -28,5 +29,5 @@ func (w *worker) grepFile(filename string) error {
return true
})

return nil
return w.matches, nil
}

0 comments on commit 947adbb

Please sign in to comment.