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

Simplify grepper interface #139

Merged
merged 2 commits into from
Jun 4, 2016
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
10 changes: 2 additions & 8 deletions extended_grep.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,19 @@ import (
"io"
"log"
"os"
"sync"
)

type extendedGrep struct {
lineGrep
pattern pattern
}

func (g extendedGrep) grep(path string, sem chan struct{}, wg *sync.WaitGroup) {
func (g extendedGrep) grep(path string) {
f, err := getFileHandler(path)
if err != nil {
log.Fatalf("open: %s\n", err)
}

defer func() {
f.Close()
<-sem
wg.Done()
}()
defer f.Close()

if f == os.Stdin {
// TODO: File type is fixed in ASCII because it can not determine the character code.
Expand Down
10 changes: 2 additions & 8 deletions fixed_grep.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,19 @@ import (
"io/ioutil"
"log"
"os"
"sync"
)

type fixedGrep struct {
lineGrep
pattern pattern
}

func (g fixedGrep) grep(path string, sem chan struct{}, wg *sync.WaitGroup) {
func (g fixedGrep) grep(path string) {
f, err := getFileHandler(path)
if err != nil {
log.Fatalf("open: %s\n", err)
}

defer func() {
f.Close()
<-sem
wg.Done()
}()
defer f.Close()

if f == os.Stdin {
// TODO: File type is fixed in ASCII because it can not determine the character code.
Expand Down
8 changes: 6 additions & 2 deletions grep.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,18 @@ func (g grep) start() {
for path := range g.in {
sem <- struct{}{}
wg.Add(1)
go g.grepper.grep(path, sem, wg)
go func(path string) {
defer wg.Done()
defer func() { <-sem }()
g.grepper.grep(path)
}(path)
}
wg.Wait()
g.done <- struct{}{}
}

type grepper interface {
grep(path string, sem chan struct{}, wg *sync.WaitGroup)
grep(path string)
}

func newGrepper(pattern pattern, printer printer, opts Option) grepper {
Expand Down
8 changes: 1 addition & 7 deletions passthrough_grep.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,10 @@
package the_platinum_searcher

import "sync"

type passthroughGrep struct {
printer printer
}

func (g passthroughGrep) grep(path string, sem chan struct{}, wg *sync.WaitGroup) {
defer func() {
<-sem
wg.Done()
}()
func (g passthroughGrep) grep(path string) {
match := match{path: path, lines: []line{line{}}}
g.printer.print(match)
}