Skip to content

Commit

Permalink
internal/phpgrep: add "update" and "none" progress printing modes (#74)
Browse files Browse the repository at this point in the history
  • Loading branch information
quasilyte committed May 23, 2021
1 parent c4a00bf commit 985a218
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
5 changes: 5 additions & 0 deletions internal/phpgrep/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ type arguments struct {
exclude string
format string

progressMode string

filenameColor string
lineColor string
matchColor string
Expand Down Expand Up @@ -156,6 +158,9 @@ Supported command-line flags:
flag.StringVar(&args.exclude, "exclude", "",
`exclude files or directories by regexp pattern`)

flag.StringVar(&args.progressMode, "progress", "update",
"progress printing mode: `update`, `append` or `none`")

flag.StringVar(&args.filenameColor, "color-filename", envVarOrDefault("PHPGREP_COLOR_FILENAME", "dark-magenta"),
`{{.Filename}} text color`)
flag.StringVar(&args.lineColor, "color-line", envVarOrDefault("PHPGREP_COLOR_LINE", "dark-green"),
Expand Down
18 changes: 17 additions & 1 deletion internal/phpgrep/program.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,12 @@ func (p *program) validateFlags() error {
if _, err := colorizeText("", p.args.matchColor); err != nil {
return fmt.Errorf("color-match: %v", err)
}
switch p.args.progressMode {
case "none", "append", "update":
// OK.
default:
return fmt.Errorf("progress: unexpected mode %q", p.args.progressMode)
}
// If there are more than 100k results, something is wrong.
// Most likely, a user pattern is too generic and needs adjustment.
const maxLimit = 100000
Expand Down Expand Up @@ -216,6 +222,9 @@ func (p *program) executePattern() error {
close(filenameQueue)
ticker.Stop()
wg.Wait()
if p.args.progressMode == "update" {
os.Stderr.WriteString("\n")
}
}()

for _, w := range p.workers {
Expand Down Expand Up @@ -306,7 +315,14 @@ func (p *program) walkTarget(target string, filenameQueue chan<- string, ticker
filesProcessed++
return nil
case <-ticker.C:
log.Printf("%d matches so far, processed %d files", numMatches, filesProcessed)
switch p.args.progressMode {
case "append":
fmt.Fprintf(os.Stderr, "%d matches so far, processed %d files\n", numMatches, filesProcessed)
case "update":
fmt.Fprintf(os.Stderr, "\r%d matches so far, processed %d files", numMatches, filesProcessed)
case "none":
// Do nothing.
}
}
}
})
Expand Down

0 comments on commit 985a218

Please sign in to comment.