Skip to content
Myr

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

8 files+174 added−41 removed

modified:src/tui/dummy.go+5 added-5 removed
32 unmodified lines
33func (r *FullscreenRenderer) ShowCursor() {}34func (r *FullscreenRenderer) Refresh() {}35func (r *FullscreenRenderer) Close() {}36func (r *FullscreenRenderer) Size() TermSize { return TermSize{} }37func (r *FullscreenRenderer) Top() int { return 0 }38func (r *FullscreenRenderer) MaxX() int { return 0 }39func (r *FullscreenRenderer) MaxY() int { return 0 }40func (r *FullscreenRenderer) GetChar(bool) Event { return Event{} }41func (r *FullscreenRenderer) CancelGetChar() {}4238func (r *FullscreenRenderer) GetChar() Event { return Event{} }39func (r *FullscreenRenderer) Top() int { return 0 }40func (r *FullscreenRenderer) MaxX() int { return 0 }41func (r *FullscreenRenderer) MaxY() int { return 0 }4243func (r *FullscreenRenderer) RefreshWindows(windows []Window) {}4445func (r *FullscreenRenderer) NewWindow(top int, left int, width int, height int, windowType WindowType, borderStyle BorderStyle, erase bool) Window {46 return nil
1 unmodified line
modified:src/tui/light_test.go+18 added-1 removed
13 unmodified lines
14 func(h int) int { return h })1516 light_renderer := renderer.(*LightRenderer)1718 go func() {19 for {20 light_renderer.mutex.Lock()21 ready := light_renderer.cancel != nil22 light_renderer.mutex.Unlock()2324 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 }3435 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 lines
modified:src/tui/light_unix.go+54 added-7 removed
97 unmodified lines
98 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 lines
113 }114 return -1, -1115}116117func (r *LightRenderer) getch(nonblock bool) (int, bool) {118 b := make([]byte, 1)117func (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 }131132 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 }()145146 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 }153154 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 }163164 if rfds.IsSet(cancelFd) {165 return 0, getCharCancelled166 }167168 if rfds.IsSet(fd) {169 return getter()170 }171 }172 return 0, getCharError173}174175func (r *LightRenderer) Size() TermSize {176 ws, err := unix.IoctlGetWinsize(int(r.ttyin.Fd()), unix.TIOCGWINSZ)
5 unmodified lines
modified:src/tui/light_windows.go+27 added-10 removed
149 unmodified lines
150 }151 return int(bufferInfo.CursorPosition.Y), int(bufferInfo.CursorPosition.X)152}153154func (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 {154func (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 }159160 var timeout <-chan time.Time161 if nonblock {162 timeout = time.After(timeoutInterval * time.Millisecond)163 }164165 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 }173174 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 lines
25 defaultEscDelay = 10026 escPollInterval = 527 offsetPollTries = 1028 maxInputBuffer = 1024 * 102429 maxSelectTries = 10030)3132const DefaultTtyDevice string = "/dev/tty"33
15 unmodified lines
49const DIM string = "\x1b[2m"50const CR string = DIM + "␍"51const LF string = DIM + "␊"5253type getCharResult int5455const (56 getCharSuccess getCharResult = iota57 getCharError58 getCharCancelled59)6061func (r getCharResult) ok() bool {62 return r == getCharSuccess63}6465func (r *LightRenderer) stderrInternal(str string, allowNLCR bool, resetCode string) {66 bytes := []byte(str)67 runes := []rune{}68 for len(bytes) > 0 {
47 unmodified lines
116 prevDownTime time.Time117 clicks [][2]int118 ttyin *os.File119 ttyout *os.File120 cancel func()121 buffer []byte122 origState *term.State123 width int124 height int
6 unmodified lines
131 y int132 x int133 maxHeightFunc func(int) int134 showCursor bool135 mutex sync.Mutex136137 // Windows only123 mutex sync.Mutex138 ttyinChannel chan byte139 inHandle uintptr140 outHandle uintptr141 origStateInput uint32
133 unmodified lines
275 }276 return atoi(env, defaultValue)277}278265func (r *LightRenderer) getBytes() ([]byte, error) {266 bytes, err := r.getBytesInternal(r.buffer, false)267 return bytes, err279func (r *LightRenderer) getBytes(cancellable bool) ([]byte, getCharResult, error) {280 return r.getBytesInternal(cancellable, r.buffer, false)281}282270func (r *LightRenderer) getBytesInternal(buffer []byte, nonblock bool) ([]byte, error) {271 c, ok := r.getch(nonblock)272 if !nonblock && !ok {283func (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 }292293 retries := 0294 if c == Esc.Int() || nonblock {
2 unmodified lines
297 buffer = append(buffer, byte(c))298299 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 continue
10 unmodified lines
317 // 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 }324309 return buffer, nil325 return buffer, getCharSuccess, nil326}327312func (r *LightRenderer) GetChar() Event {328func (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 lines
370 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 }381382 ev = r.escSequence(&sz)383 }384 return ev385 }
9 unmodified lines
395 sz = rsz396 return Event{Rune, char, nil}397}398399func (r *LightRenderer) CancelGetChar() {400 r.mutex.Lock()401 if r.cancel != nil {402 r.cancel()403 r.cancel = nil404 }405 r.mutex.Unlock()406}407408func (r *LightRenderer) setCancel(f func()) {409 r.mutex.Lock()410 r.cancel = f411 r.mutex.Unlock()412}413414func (r *LightRenderer) escSequence(sz *int) Event {415 if len(r.buffer) < 2 {416 return Event{Esc, 0, nil}417 }
1128 unmodified lines
modified:src/tui/tcell.go+5 added-1 removed
244 unmodified lines
245 cols, lines := _screen.Size()246 return TermSize{lines, cols, 0, 0}247}248249func (r *FullscreenRenderer) GetChar() Event {249func (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 lines
702 // section 8: Invalid703 return Event{Invalid, 0, nil}704}705706func (r *FullscreenRenderer) CancelGetChar() {707 // TODO708}709710func (r *FullscreenRenderer) Pause(clear bool) {711 if clear {712 _screen.Suspend()713 }
357 unmodified lines
modified:src/tui/tui.go+2 added-1 removed
747 unmodified lines
748 Bell()749 HideCursor()750 ShowCursor()751752 GetChar() Event752 GetChar(cancellable bool) Event753 CancelGetChar()754755 Top() int756 MaxX() int757 MaxY() int
572 unmodified lines
modified:src/terminal.go+8 added-1 removed
5613 unmodified lines
5614 }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 := -1
78 unmodified lines
5701 actions = append(actions, action)5702 }5703 }5704 }5705 for _, action := range actions {5706 if action.t == actExecute {5707 t.tui.CancelGetChar()5708 break5709 }5710 }57115712 case callback := <-t.callbackChan:5713 event = tui.Invalid.AsEvent()5714 actions = append(actions, &action{t: actAsync})5715 callbacks = append(callbacks, callback)
1677 unmodified lines