Make --select-1 and --exit-0 asynchronous
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Junegunn Choi · committed by Junegunn Choi · parent d760b79 · Browse files · 3 files·+39 added−32 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+39 added−32 removed
- other:+39 added−32 removed
Provenance
- Commit
e808151- Tree
3f43b59- Author
Junegunn Choi <junegunn.c@gmail.com>- Committer
Junegunn Choi <junegunn.c@gmail.com>- Parent
d760b79
3 files changed+39 added−32 removed
modified:src/core.go+34 added-26 removed
93 unmodified lines94 opts.Mode, opts.Case, opts.Nth, opts.Delimiter, runes)95 }96 matcher := NewMatcher(patternBuilder, opts.Sort > 0, eventBox)97 98- // Defered-interactive / Non-interactive99- // --select-1 | --exit-0 | --filter100- if filtering := opts.Filter != nil; filtering || opts.Select1 || opts.Exit0 {101- limit := 0102- var patternString string103- if filtering {104- patternString = *opts.Filter105- } else {106- if opts.Select1 || opts.Exit0 {107- limit = 1108- }109- patternString = opts.Query110- }111- pattern := patternBuilder([]rune(patternString))98+ // Filtering mode99+ if opts.Filter != nil {100+ pattern := patternBuilder([]rune(*opts.Filter))101 102 eventBox.Unwatch(EvtReadNew)103 eventBox.WaitFor(EvtReadFin)104 105 snapshot, _ := chunkList.Snapshot()117- merger, cancelled := matcher.scan(MatchRequest{106+ merger, _ := matcher.scan(MatchRequest{107 chunks: snapshot,119- pattern: pattern}, limit)108+ pattern: pattern})109 121- if !cancelled && (filtering ||122- opts.Exit0 && merger.Length() == 0 ||123- opts.Select1 && merger.Length() == 1) {124- if opts.PrintQuery {125- fmt.Println(patternString)110+ if opts.PrintQuery {111+ fmt.Println(*opts.Filter)126- }127- for i := 0; i < merger.Length(); i++ {128- fmt.Println(merger.Get(i).AsString())129- }130- os.Exit(0)112 }113+ for i := 0; i < merger.Length(); i++ {114+ fmt.Println(merger.Get(i).AsString())115+ }116+ os.Exit(0)117 }118 119 // Synchronous search120 if opts.Sync {5 unmodified lines126 go matcher.Loop()127 128 // Terminal I/O129 terminal := NewTerminal(opts, eventBox)130+ deferred := opts.Select1 || opts.Exit0131 go terminal.Loop()132+ if !deferred {133+ terminal.startChan <- true134+ }135 136 // Event coordination137 reading := true138 ticks := 025 unmodified lines164 165 case EvtSearchFin:166 switch val := value.(type) {167 case *Merger:168+ if deferred {169+ count := val.Length()170+ if opts.Select1 && count > 1 || opts.Exit0 && !opts.Select1 && count > 0 {171+ deferred = false172+ terminal.startChan <- true173+ } else if !reading {174+ if opts.Exit0 && count == 0 || opts.Select1 && count == 1 {175+ if opts.PrintQuery {176+ fmt.Println(opts.Query)177+ }178+ for i := 0; i < count; i++ {179+ fmt.Println(val.Get(i).AsString())180+ }181+ os.Exit(0)182+ }183+ deferred = false184+ terminal.startChan <- true185+ }186+ }187 terminal.UpdateList(val)188 }189 }190 }9 unmodified linesmodified:src/matcher.go+2 added-6 removed
84 unmodified lines85 m.mergerCache = make(map[string]*Merger)86 }87 88 if !foundCache {89- merger, cancelled = m.scan(request, 0)89+ merger, cancelled = m.scan(request)90 }91 92 if !cancelled {93 m.mergerCache[patternString] = merger26 unmodified lines120 index int121 matches []*Item122 }123 124-func (m *Matcher) scan(request MatchRequest, limit int) (*Merger, bool) {124+func (m *Matcher) scan(request MatchRequest) (*Merger, bool) {125 startedAt := time.Now()126 127 numChunks := len(request.chunks)128 if numChunks == 0 {45 unmodified lines174 for matchesInChunk := range countChan {175 count++176 matchCount += matchesInChunk177 178- if limit > 0 && matchCount > limit {179- return nil, wait() // For --select-1 and --exit-0180- }181-178 if count == numChunks {179 break180 }181 29 unmodified linesmodified:src/terminal.go+3 added
38 unmodified lines39 eventBox *util.EventBox40 mutex sync.Mutex41 initFunc func()42 suppress bool43+ startChan chan bool44 }45 46 type selectedItem struct {47 at time.Time51 unmodified lines99 reqBox: util.NewEventBox(),100 eventBox: eventBox,101 mutex: sync.Mutex{},102 suppress: true,103+ startChan: make(chan bool, 1),104 initFunc: func() {105 C.Init(opts.Color, opts.Color256, opts.Black, opts.Mouse)106 }}107 }339 unmodified lines447 }448 449 // Loop is called to start Terminal I/O450 func (t *Terminal) Loop() {451+ <-t.startChan452 { // Late initialization453 t.mutex.Lock()454 t.initFunc()455 t.printPrompt()264 unmodified lines