diff --git a/main.go b/main.go index 4624e607..2c43b61f 100644 --- a/main.go +++ b/main.go @@ -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. @@ -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 } @@ -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 diff --git a/oviewer/filter.go b/oviewer/filter.go index 3f6d4cf1..a6ac4756 100644 --- a/oviewer/filter.go +++ b/oviewer/filter.go @@ -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 { @@ -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) } @@ -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) diff --git a/oviewer/input_search.go b/oviewer/input_search.go index 09e65b39..076f6502 100644 --- a/oviewer/input_search.go +++ b/oviewer/input_search.go @@ -97,6 +97,7 @@ func newBackSearchEvent(clist *candidate) *eventInputBackSearch { return &eventInputBackSearch{clist: clist} } +// Mode returns InputMode. func (e *eventInputBackSearch) Mode() InputMode { return Backsearch } @@ -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() -}