Skip to content

Commit

Permalink
Merge pull request #503 from noborus/add-filter-option
Browse files Browse the repository at this point in the history
Add command line options for filter
  • Loading branch information
noborus authored Mar 28, 2024
2 parents 8454193 + 24b5282 commit 82a71e5
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 59 deletions.
9 changes: 7 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ var (
// pattern is search pattern.
pattern string

// filter is filter pattern.
filter string

// ver is version information.
ver bool
// helpKey is key bind information.
Expand Down Expand Up @@ -156,7 +159,9 @@ func RunOviewer(args []string) error {
if pattern != "" {
ov.Search(pattern)
}

if filter != "" {
ov.Filter(filter)
}
if err := ov.Run(); err != nil {
return err
}
Expand Down Expand Up @@ -331,7 +336,7 @@ func init() {
})

rootCmd.PersistentFlags().StringVarP(&pattern, "pattern", "", "", "search pattern")

rootCmd.PersistentFlags().StringVarP(&filter, "filter", "", "", "filter search pattern")
rootCmd.PersistentFlags().BoolVarP(&oviewer.SkipExtract, "skip-extract", "", false, "skip extracting compressed files")

// Config.General
Expand Down
72 changes: 72 additions & 0 deletions oviewer/filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,78 @@ import (
"io"
"log"
"strings"

"github.com/gdamore/tcell/v2"
)

// eventInputFilter represents the filter input mode.
type eventInputFilter struct {
tcell.EventTime
clist *candidate
value string
}

// setBackSearchMode sets the inputMode to Backsearch.
func (root *Root) setSearchFilterMode() {
input := root.input
input.value = ""
input.cursorX = 0

if root.searcher != nil {
input.SearchCandidate.toLast(root.searcher.String())
}

input.Event = newSearchFilterEvent(input.SearchCandidate)
}

// newSearchFilterEvent returns FilterInput.
func newSearchFilterEvent(clist *candidate) *eventInputFilter {
return &eventInputFilter{
value: "",
clist: clist,
EventTime: tcell.EventTime{},
}
}

// Mode returns InputMode.
func (e *eventInputFilter) Mode() InputMode {
return Filter
}

// Prompt returns the prompt string in the input field.
func (e *eventInputFilter) Prompt() string {
return "&"
}

// Confirm returns the event when the input is confirmed.
func (e *eventInputFilter) Confirm(str string) tcell.Event {
e.value = str
e.clist.toLast(str)
e.SetEventNow()
return e
}

// Up returns strings when the up key is pressed during input.
func (e *eventInputFilter) Up(str string) string {
e.clist.toAddLast(str)
return e.clist.up()
}

// Down returns strings when the down key is pressed during input.
func (e *eventInputFilter) Down(str string) string {
e.clist.toAddTop(str)
return e.clist.down()
}

func (root *Root) Filter(str string) {
root.input.value = str
ev := &eventInputFilter{
value: str,
}
root.postEvent(ev)
}

// filter filters the document by the input value.
func (root *Root) filter(ctx context.Context) {
searcher := root.setSearcher(root.input.value, root.Config.CaseSensitive)
if searcher == nil {
Expand Down Expand Up @@ -47,6 +117,7 @@ func (root *Root) filter(ctx context.Context) {
filterDoc.SkipLines = m.SkipLines

go m.searchWriter(ctx, searcher, filterDoc, m.firstLine())

root.setMessagef("filter:%v", word)
}

Expand All @@ -57,6 +128,7 @@ func (m *Document) searchWriter(ctx context.Context, searcher Searcher, renderDo
for {
lineNum, err := m.searchLine(ctx, searcher, true, nextLN)
if err != nil {
log.Println(err)
break
}
line, err := m.Line(lineNum)
Expand Down
58 changes: 1 addition & 57 deletions oviewer/input_search.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ func newBackSearchEvent(clist *candidate) *eventInputBackSearch {
return &eventInputBackSearch{clist: clist}
}

// Mode returns InputMode.
func (e *eventInputBackSearch) Mode() InputMode {
return Backsearch
}
Expand Down Expand Up @@ -135,60 +136,3 @@ func (input *Input) searchCandidates(n int) []string {
start := max(0, listLen-n)
return input.SearchCandidate.list[start:listLen]
}

type eventInputFilter struct {
tcell.EventTime
clist *candidate
value string
}

// setBackSearchMode sets the inputMode to Backsearch.
func (root *Root) setSearchFilterMode() {
input := root.input
input.value = ""
input.cursorX = 0

if root.searcher != nil {
input.SearchCandidate.toLast(root.searcher.String())
}

input.Event = newSearchFilterEvent(input.SearchCandidate)
}

func newSearchFilterEvent(clist *candidate) *eventInputFilter {
return &eventInputFilter{
value: "",
clist: clist,
EventTime: tcell.EventTime{},
}
}

// Mode returns InputMode.
func (e *eventInputFilter) Mode() InputMode {
return Filter
}

// Prompt returns the prompt string in the input field.
func (e *eventInputFilter) Prompt() string {
return "&"
}

// Confirm returns the event when the input is confirmed.
func (e *eventInputFilter) Confirm(str string) tcell.Event {
e.value = str
e.clist.toLast(str)
e.SetEventNow()
return e
}

// Up returns strings when the up key is pressed during input.
func (e *eventInputFilter) Up(str string) string {
e.clist.toAddLast(str)
return e.clist.up()
}

// Down returns strings when the down key is pressed during input.
func (e *eventInputFilter) Down(str string) string {
e.clist.toAddTop(str)
return e.clist.down()
}

0 comments on commit 82a71e5

Please sign in to comment.