Cancel key reading when 'execute' triggered via a server request (#4653)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Fix #4524 Close #4648
authored by Junegunn Choi · committed by GitHub · parent 3c7cbc9 · Browse files · 8 files·+174 added−41 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
8 files+174 added−41 removed
- other:+174 added−41 removed
Provenance
- Commit
3f94bcb- Tree
f612372- Author
Junegunn Choi <junegunn.c@gmail.com>- Committer
GitHub <noreply@github.com>- Parent
3c7cbc9
8 files changed+174 added−41 removed
modified:src/tui/dummy.go+5 added-5 removed
32 unmodified lines33 func (r *FullscreenRenderer) ShowCursor() {}34 func (r *FullscreenRenderer) Refresh() {}35 func (r *FullscreenRenderer) Close() {}36 func (r *FullscreenRenderer) Size() TermSize { return TermSize{} }37+func (r *FullscreenRenderer) Top() int { return 0 }38+func (r *FullscreenRenderer) MaxX() int { return 0 }39+func (r *FullscreenRenderer) MaxY() int { return 0 }40+func (r *FullscreenRenderer) GetChar(bool) Event { return Event{} }41+func (r *FullscreenRenderer) CancelGetChar() {}42 38-func (r *FullscreenRenderer) GetChar() Event { return Event{} }39-func (r *FullscreenRenderer) Top() int { return 0 }40-func (r *FullscreenRenderer) MaxX() int { return 0 }41-func (r *FullscreenRenderer) MaxY() int { return 0 }42-43 func (r *FullscreenRenderer) RefreshWindows(windows []Window) {}44 45 func (r *FullscreenRenderer) NewWindow(top int, left int, width int, height int, windowType WindowType, borderStyle BorderStyle, erase bool) Window {46 return nil1 unmodified linemodified:src/tui/light_test.go+18 added-1 removed
13 unmodified lines14 func(h int) int { return h })15 16 light_renderer := renderer.(*LightRenderer)17 18+ go func() {19+ for {20+ light_renderer.mutex.Lock()21+ ready := light_renderer.cancel != nil22+ light_renderer.mutex.Unlock()23+24+ if ready {25+ light_renderer.CancelGetChar()26+ break27+ }28+ }29+ }()30+ event := light_renderer.GetChar(true)31+ if event.Type != Invalid {32+ t.Error("Not cancelled")33+ }34+35 assertCharSequence := func(sequence string, name string) {36 bytes := []byte(sequence)37 light_renderer.buffer = bytes21- event := light_renderer.GetChar()38+ event := light_renderer.GetChar(true)39 if event.KeyName() != name {40 t.Errorf(41 "sequence: %q | %v | '%s' (%s) != %s",42 string(bytes), bytes,310 unmodified linesmodified:src/tui/light_unix.go+54 added-7 removed
97 unmodified lines98 r.flush()99 var err error100 bytes := []byte{}101 for tries := range offsetPollTries {102- bytes, err = r.getBytesInternal(bytes, tries > 0)102+ bytes, _, err = r.getBytesInternal(false, bytes, tries > 0)103 if err != nil {104 return -1, -1105 }106 6 unmodified lines113 }114 return -1, -1115 }116 117-func (r *LightRenderer) getch(nonblock bool) (int, bool) {118- b := make([]byte, 1)117+func (r *LightRenderer) getch(cancellable bool, nonblock bool) (int, getCharResult) {118 fd := r.fd()119+ getter := func() (int, getCharResult) {120+ b := make([]byte, 1)120- util.SetNonblock(r.ttyin, nonblock)121- _, err := util.Read(fd, b)121+ util.SetNonblock(r.ttyin, nonblock)122+ _, err := util.Read(fd, b)123+ if err != nil {124+ return 0, getCharError125+ }126+ return int(b[0]), getCharSuccess127+ }128+ if nonblock || !cancellable {129+ return getter()130+ }131+132+ rpipe, wpipe, err := os.Pipe()133 if err != nil {123- return 0, false134+ // Fallback to blocking read without cancellation135+ return getter()136 }125- return int(b[0]), true137+ r.setCancel(func() {138+ wpipe.Write([]byte{0})139+ })140+ defer func() {141+ r.setCancel(nil)142+ rpipe.Close()143+ wpipe.Close()144+ }()145+146+ cancelFd := int(rpipe.Fd())147+ for range maxSelectTries {148+ var rfds unix.FdSet149+ limit := len(rfds.Bits) * unix.NFDBITS150+ if fd >= limit || cancelFd >= limit {151+ return getter()152+ }153+154+ rfds.Set(fd)155+ rfds.Set(cancelFd)156+ _, err := unix.Select(max(fd, cancelFd)+1, &rfds, nil, nil, nil)157+ if err != nil {158+ if err == syscall.EINTR {159+ continue160+ }161+ return 0, getCharError162+ }163+164+ if rfds.IsSet(cancelFd) {165+ return 0, getCharCancelled166+ }167+168+ if rfds.IsSet(fd) {169+ return getter()170+ }171+ }172+ return 0, getCharError173 }174 175 func (r *LightRenderer) Size() TermSize {176 ws, err := unix.IoctlGetWinsize(int(r.ttyin.Fd()), unix.TIOCGWINSZ)5 unmodified linesmodified:src/tui/light_windows.go+27 added-10 removed
149 unmodified lines150 }151 return int(bufferInfo.CursorPosition.Y), int(bufferInfo.CursorPosition.X)152 }153 154-func (r *LightRenderer) getch(nonblock bool) (int, bool) {155- if nonblock {156- select {157- case bc := <-r.ttyinChannel:158- return int(bc), true159- case <-time.After(timeoutInterval * time.Millisecond):160- return 0, false161- }162- } else {154+func (r *LightRenderer) getch(cancellable bool, nonblock bool) (int, getCharResult) {155+ if !nonblock && !cancellable {156 bc := <-r.ttyinChannel164- return int(bc), true157+ return int(bc), getCharSuccess158 }159+160+ var timeout <-chan time.Time161+ if nonblock {162+ timeout = time.After(timeoutInterval * time.Millisecond)163+ }164+165+ var cancel chan struct{}166+ if cancellable {167+ cancel = make(chan struct{})168+ r.setCancel(func() {169+ close(cancel)170+ })171+ defer r.setCancel(nil)172+ }173+174+ select {175+ case bc := <-r.ttyinChannel:176+ return int(bc), getCharSuccess177+ case <-cancel:178+ return 0, getCharCancelled179+ case <-timeout:180+ // NOTE: not really an error181+ return 0, getCharError182+ }183 }modified:src/tui/light.go+55 added-15 removed
24 unmodified lines25 defaultEscDelay = 10026 escPollInterval = 527 offsetPollTries = 1028 maxInputBuffer = 1024 * 102429+ maxSelectTries = 10030 )31 32 const DefaultTtyDevice string = "/dev/tty"33 15 unmodified lines49 const DIM string = "\x1b[2m"50 const CR string = DIM + "␍"51 const LF string = DIM + "␊"52 53+type getCharResult int54+55+const (56+ getCharSuccess getCharResult = iota57+ getCharError58+ getCharCancelled59+)60+61+func (r getCharResult) ok() bool {62+ return r == getCharSuccess63+}64+65 func (r *LightRenderer) stderrInternal(str string, allowNLCR bool, resetCode string) {66 bytes := []byte(str)67 runes := []rune{}68 for len(bytes) > 0 {47 unmodified lines116 prevDownTime time.Time117 clicks [][2]int118 ttyin *os.File119 ttyout *os.File120+ cancel func()121 buffer []byte122 origState *term.State123 width int124 height int6 unmodified lines131 y int132 x int133 maxHeightFunc func(int) int134 showCursor bool135+ mutex sync.Mutex136 137 // Windows only123- mutex sync.Mutex138 ttyinChannel chan byte139 inHandle uintptr140 outHandle uintptr141 origStateInput uint32133 unmodified lines275 }276 return atoi(env, defaultValue)277 }278 265-func (r *LightRenderer) getBytes() ([]byte, error) {266- bytes, err := r.getBytesInternal(r.buffer, false)267- return bytes, err279+func (r *LightRenderer) getBytes(cancellable bool) ([]byte, getCharResult, error) {280+ return r.getBytesInternal(cancellable, r.buffer, false)281 }282 270-func (r *LightRenderer) getBytesInternal(buffer []byte, nonblock bool) ([]byte, error) {271- c, ok := r.getch(nonblock)272- if !nonblock && !ok {283+func (r *LightRenderer) getBytesInternal(cancellable bool, buffer []byte, nonblock bool) ([]byte, getCharResult, error) {284+ c, result := r.getch(cancellable, nonblock)285+ if result == getCharCancelled {286+ return buffer, getCharCancelled, nil287+ }288+ if !nonblock && !result.ok() {289 r.Close()274- return nil, errors.New("failed to read " + DefaultTtyDevice)290+ return nil, getCharError, errors.New("failed to read " + DefaultTtyDevice)291 }292 293 retries := 0294 if c == Esc.Int() || nonblock {2 unmodified lines297 buffer = append(buffer, byte(c))298 299 pc := c300 for {285- c, ok = r.getch(true)286- if !ok {301+ c, result = r.getch(false, true)302+ if !result.ok() {303 if retries > 0 {304 retries--305 time.Sleep(escPollInterval * time.Millisecond)306 continue10 unmodified lines317 // This should never happen under normal conditions,318 // so terminate fzf immediately.319 if len(buffer) > maxInputBuffer {320 r.Close()305- return nil, fmt.Errorf("input buffer overflow (%d): %v", len(buffer), buffer)321+ return nil, getCharError, fmt.Errorf("input buffer overflow (%d): %v", len(buffer), buffer)322 }323 }324 309- return buffer, nil325+ return buffer, getCharSuccess, nil326 }327 312-func (r *LightRenderer) GetChar() Event {328+func (r *LightRenderer) GetChar(cancellable bool) Event {329 var err error330+ var result getCharResult331 if len(r.buffer) == 0 {315- r.buffer, err = r.getBytes()332+ r.buffer, result, err = r.getBytes(cancellable)333 if err != nil {334 return Event{Fatal, 0, nil}335 }336+ if result == getCharCancelled {337+ return Event{Invalid, 0, nil}338+ }339 }340 if len(r.buffer) == 0 {341 return Event{Fatal, 0, nil}342 }27 unmodified lines370 case Esc.Byte():371 ev := r.escSequence(&sz)372 // Second chance373 if ev.Type == Invalid {354- if r.buffer, err = r.getBytes(); err != nil {374+ r.buffer, result, err = r.getBytes(true)375+ if err != nil {376 return Event{Fatal, 0, nil}377 }378+ if result == getCharCancelled {379+ return Event{Invalid, 0, nil}380+ }381+382 ev = r.escSequence(&sz)383 }384 return ev385 }9 unmodified lines395 sz = rsz396 return Event{Rune, char, nil}397 }398 399+func (r *LightRenderer) CancelGetChar() {400+ r.mutex.Lock()401+ if r.cancel != nil {402+ r.cancel()403+ r.cancel = nil404+ }405+ r.mutex.Unlock()406+}407+408+func (r *LightRenderer) setCancel(f func()) {409+ r.mutex.Lock()410+ r.cancel = f411+ r.mutex.Unlock()412+}413+414 func (r *LightRenderer) escSequence(sz *int) Event {415 if len(r.buffer) < 2 {416 return Event{Esc, 0, nil}417 }1128 unmodified linesmodified:src/tui/tcell.go+5 added-1 removed
244 unmodified lines245 cols, lines := _screen.Size()246 return TermSize{lines, cols, 0, 0}247 }248 249-func (r *FullscreenRenderer) GetChar() Event {249+func (r *FullscreenRenderer) GetChar(cancellable bool) Event {250 ev := _screen.PollEvent()251 switch ev := ev.(type) {252 case *tcell.EventPaste:253 if ev.Start() {448 unmodified lines702 // section 8: Invalid703 return Event{Invalid, 0, nil}704 }705 706+func (r *FullscreenRenderer) CancelGetChar() {707+ // TODO708+}709+710 func (r *FullscreenRenderer) Pause(clear bool) {711 if clear {712 _screen.Suspend()713 }357 unmodified linesmodified:src/tui/tui.go+2 added-1 removed
747 unmodified lines748 Bell()749 HideCursor()750 ShowCursor()751 752- GetChar() Event752+ GetChar(cancellable bool) Event753+ CancelGetChar()754 755 Top() int756 MaxX() int757 MaxY() int572 unmodified linesmodified:src/terminal.go+8 added-1 removed
5613 unmodified lines5614 }5615 select {5616 case <-ctx.Done():5617 return5618- case t.keyChan <- t.tui.GetChar():5618+ case t.keyChan <- t.tui.GetChar(t.listenAddr != nil):5619 }5620 }5621 }()5622 previewDraggingPos := -178 unmodified lines5701 actions = append(actions, action)5702 }5703 }5704 }5705+ for _, action := range actions {5706+ if action.t == actExecute {5707+ t.tui.CancelGetChar()5708+ break5709+ }5710+ }5711+5712 case callback := <-t.callbackChan:5713 event = tui.Invalid.AsEvent()5714 actions = append(actions, &action{t: actAsync})5715 callbacks = append(callbacks, callback)1677 unmodified lines