Skip to content

Commit

Permalink
Merge pull request #5 from bayashi/improve-option-struct-xfg-struct
Browse files Browse the repository at this point in the history
Sort struct
  • Loading branch information
bayashi authored Mar 3, 2024
2 parents 05ce46e + 3358724 commit 4356655
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 47 deletions.
18 changes: 9 additions & 9 deletions arg.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ var (
)

type options struct {
path string
grep string
start string
searchPath string
searchGrep string
searchStart string

relax bool
noColor bool
Expand All @@ -33,9 +33,9 @@ func (cli *runner) parseArgs() *options {

var flagHelp bool
var flagVersion bool
flag.StringVarP(&o.path, "path", "p", "", "A path string of a root to find")
flag.StringVarP(&o.grep, "grep", "g", "", "A string to search for contents")
flag.StringVarP(&o.start, "start", "s", ".", "A location to start searching")
flag.StringVarP(&o.searchPath, "path", "p", "", "A path string of a root to find")
flag.StringVarP(&o.searchGrep, "grep", "g", "", "A string to search for contents")
flag.StringVarP(&o.searchStart, "start", "s", ".", "A location to start searching")
flag.BoolVarP(&o.noColor, "no-color", "", false, "disable colors for matched words")
flag.BoolVarP(&o.relax, "relax", "", false, "Insert blank space between contents for relaxing view")
flag.BoolVarP(&o.abs, "abs", "", false, "Show absolute paths")
Expand All @@ -60,7 +60,7 @@ func (cli *runner) parseArgs() *options {
}

func (o *options) targetPathFromArgs(cli *runner) {
if o.path != "" {
if o.searchPath != "" {
return
}

Expand All @@ -70,10 +70,10 @@ func (o *options) targetPathFromArgs(cli *runner) {

arg := flag.Args()[0]
if arg != "-" && arg != "--" {
o.path = arg
o.searchPath = arg
}

if o.path == "" {
if o.searchPath == "" {
cli.putHelp(errNeedToSetPath)
}
}
Expand Down
10 changes: 1 addition & 9 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,7 @@ func (cli *runner) xfg(o *options) error {
grepColor = color.New(color.FgHiRed)
)

x := NewX(pathColor, grepColor)

x.SearchPath = o.path
x.SearchGrep = o.grep
x.SearchStart = o.start

x.Relax = o.relax
x.NoColor = o.noColor
x.Abs = o.abs
x := NewX(o, pathColor, grepColor)

if err := x.Search(); err != nil {
return fmt.Errorf("error during Search %w", err)
Expand Down
14 changes: 7 additions & 7 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func TestRunner_OK(t *testing.T) {
}{
"service-b": {
opt: &options{
path: "service-b",
searchPath: "service-b",
},
expect: here.Doc(`
testdata/service-b
Expand All @@ -26,8 +26,8 @@ func TestRunner_OK(t *testing.T) {
},
"service-b grep": {
opt: &options{
path: "service-b",
grep: "func",
searchPath: "service-b",
searchGrep: "func",
},
expect: here.Doc(`
testdata/service-b
Expand All @@ -37,9 +37,9 @@ func TestRunner_OK(t *testing.T) {
},
"service grep relax": {
opt: &options{
path: "main",
grep: "package b",
relax: true,
searchPath: "main",
searchGrep: "package b",
relax: true,
},
expect: here.Doc(`
testdata/service-a/main.go
Expand All @@ -56,7 +56,7 @@ func TestRunner_OK(t *testing.T) {
out: &o,
}

tt.opt.start = "./testdata"
tt.opt.searchStart = "./testdata"

cli.xfg(tt.opt)

Expand Down
40 changes: 18 additions & 22 deletions xfg.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,22 +23,18 @@ type path struct {
}

type xfg struct {
options *options

pathHighlightColor *color.Color
grepHighlightColor *color.Color
NoColor bool

Relax bool
Abs bool

SearchPath string
SearchGrep string
SearchStart string

result []path
}

func NewX(pathHighlightColor *color.Color, grepHighlightColor *color.Color) *xfg {
x := &xfg{}
func NewX(o *options, pathHighlightColor *color.Color, grepHighlightColor *color.Color) *xfg {
x := &xfg{
options: o,
}
if pathHighlightColor != nil {
x.pathHighlightColor = pathHighlightColor
}
Expand All @@ -59,7 +55,7 @@ func (x *xfg) Show(w io.Writer) error {
return err
}
}
if x.Relax && len(p.content) > 0 {
if x.options.relax && len(p.content) > 0 {
if _, err := fmt.Fprint(w, "\n"); err != nil {
return err
}
Expand All @@ -70,13 +66,13 @@ func (x *xfg) Show(w io.Writer) error {
}

func (x *xfg) Search() error {
sPath, err := validateStartPath(x.SearchStart)
sPath, err := validateStartPath(x.options.searchStart)
if err != nil {
return err
}

hPath := x.pathHighlightColor.Sprintf(x.SearchPath)
hGrep := x.grepHighlightColor.Sprintf(x.SearchGrep)
hPath := x.pathHighlightColor.Sprintf(x.options.searchPath)
hGrep := x.grepHighlightColor.Sprintf(x.options.searchGrep)

var paths []path
walkErr := filepath.Walk(sPath, func(fPath string, fInfo os.FileInfo, err error) error {
Expand All @@ -88,38 +84,38 @@ func (x *xfg) Search() error {
return filepath.SkipDir
}

if !strings.Contains(fPath, x.SearchPath) {
if !strings.Contains(fPath, x.options.searchPath) {
return nil
}

matchedPath := path{
info: fInfo,
}

if x.Abs {
if x.options.abs {
absPath, err := filepath.Abs(fPath)
if err != nil {
return err
}
fPath = absPath
}

if x.NoColor {
if x.options.noColor {
matchedPath.path = fPath
} else {
matchedPath.path = strings.ReplaceAll(fPath, x.SearchPath, hPath)
matchedPath.path = strings.ReplaceAll(fPath, x.options.searchPath, hPath)
}

if !fInfo.IsDir() && x.SearchGrep != "" {
matchedContents, err := matchedContents(fPath, x.SearchGrep)
if !fInfo.IsDir() && x.options.searchGrep != "" {
matchedContents, err := matchedContents(fPath, x.options.searchGrep)
if err != nil {
return err
}

if x.NoColor {
if x.options.noColor {
matchedPath.content = matchedContents
} else {
matchedPath.content = colorMatchedContents(matchedContents, x.SearchGrep, hGrep)
matchedPath.content = colorMatchedContents(matchedContents, x.options.searchGrep, hGrep)
}
}

Expand Down

0 comments on commit 4356655

Please sign in to comment.