Skip to content
Myr

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

3 files+39 added−32 removed

modified:src/core.go+34 added-26 removed
93 unmodified lines
94 opts.Mode, opts.Case, opts.Nth, opts.Delimiter, runes)95 }96 matcher := NewMatcher(patternBuilder, opts.Sort > 0, eventBox)9798 // 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))101102 eventBox.Unwatch(EvtReadNew)103 eventBox.WaitFor(EvtReadFin)104105 snapshot, _ := chunkList.Snapshot()117 merger, cancelled := matcher.scan(MatchRequest{106 merger, _ := matcher.scan(MatchRequest{107 chunks: snapshot,119 pattern: pattern}, limit)108 pattern: pattern})109121 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 }118119 // Synchronous search120 if opts.Sync {
5 unmodified lines
126 go matcher.Loop()127128 // Terminal I/O129 terminal := NewTerminal(opts, eventBox)130 deferred := opts.Select1 || opts.Exit0131 go terminal.Loop()132 if !deferred {133 terminal.startChan <- true134 }135136 // Event coordination137 reading := true138 ticks := 0
25 unmodified lines
164165 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 lines
modified:src/matcher.go+2 added-6 removed
84 unmodified lines
85 m.mergerCache = make(map[string]*Merger)86 }8788 if !foundCache {89 merger, cancelled = m.scan(request, 0)89 merger, cancelled = m.scan(request)90 }9192 if !cancelled {93 m.mergerCache[patternString] = merger
26 unmodified lines
120 index int121 matches []*Item122}123124func (m *Matcher) scan(request MatchRequest, limit int) (*Merger, bool) {124func (m *Matcher) scan(request MatchRequest) (*Merger, bool) {125 startedAt := time.Now()126127 numChunks := len(request.chunks)128 if numChunks == 0 {
45 unmodified lines
174 for matchesInChunk := range countChan {175 count++176 matchCount += matchesInChunk177178 if limit > 0 && matchCount > limit {179 return nil, wait() // For --select-1 and --exit-0180 }181178 if count == numChunks {179 break180 }181
29 unmodified lines
modified:src/terminal.go+3 added
38 unmodified lines
39 eventBox *util.EventBox40 mutex sync.Mutex41 initFunc func()42 suppress bool43 startChan chan bool44}4546type selectedItem struct {47 at time.Time
51 unmodified lines
99 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 lines
447}448449// Loop is called to start Terminal I/O450func (t *Terminal) Loop() {451 <-t.startChan452 { // Late initialization453 t.mutex.Lock()454 t.initFunc()455 t.printPrompt()
264 unmodified lines