Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

internal/phpgrep: add "update" and "none" progress printing modes #74

Merged
merged 1 commit into from
May 23, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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