Skip to content

Commit

Permalink
highlight matched line number
Browse files Browse the repository at this point in the history
  • Loading branch information
bayashi committed Mar 4, 2024
1 parent b2ceea6 commit 29b2f32
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion xfg.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ type path struct {
type xfg struct {
options *options

pathHighlitColor *color.Color
pathHighlighter string
grepHighlitColor *color.Color
grepHighlighter string

result []path
Expand All @@ -37,9 +39,11 @@ func NewX(o *options, pathHighlightColor *color.Color, grepHighlightColor *color
options: o,
}
if pathHighlightColor != nil {
x.pathHighlitColor = pathHighlightColor
x.pathHighlighter = pathHighlightColor.Sprintf(x.options.searchPath)
}
if grepHighlightColor != nil {
x.grepHighlitColor = grepHighlightColor
x.grepHighlighter = grepHighlightColor.Sprintf(x.options.searchGrep)
}

Expand All @@ -52,7 +56,11 @@ func (x *xfg) Show(w io.Writer) error {
return err
}
for _, line := range p.content {
if _, err := fmt.Fprintf(w, " %d: %s\n", line.lc, line.content); err != nil {
lc := fmt.Sprintf("%d", line.lc)
if !x.options.noColor && line.matched {
lc = x.grepHighlitColor.Sprint(lc)
}
if _, err := fmt.Fprintf(w, " %s: %s\n", lc, line.content); err != nil {
return err
}
}
Expand Down

0 comments on commit 29b2f32

Please sign in to comment.