Make transform*, --info-command, and execute-silent cancellable
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Users can press CTRL-C after 1 second to terminate the command. Close #3883
authored by Junegunn Choi · committed by Junegunn Choi · parent db01e7d · Browse files · 4 files·+92 added−29 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
4 files+92 added−29 removed
- other:+92 added−29 removed
Provenance
- Commit
7c2ffd3- Tree
0cf1df1- Author
Junegunn Choi <junegunn.c@gmail.com>- Committer
Junegunn Choi <junegunn.c@gmail.com>- Parent
db01e7d
4 files changed+92 added−29 removed
modified:man/man1/fzf.1+2 added
455 unmodified lines456 .BI "--info-command=" "COMMAND"457 Command to generate the finder info line. The command runs synchronously and458 blocks the UI until completion, so make sure that it's fast. ANSI color codes459 are supported. \fB$FZF_INFO\fR variable is set to the original info text.460+For additional environment variables available to the command, see the section461+ENVIRONMENT VARIABLES EXPORTED TO CHILD PROCESSES.462 463 e.g.464 \fB# Prepend the current cursor position in yellow465 fzf --info-command='echo -e "\\x1b[33;1m$FZF_POS\\x1b[m/$FZF_INFO 💛"'\fR1214 unmodified linesmodified:src/util/util_test.go+8 added-2 removed
135 unmodified lines136 o := Once(false)137 if o() {138 t.Error("Expected: false")139 }140- if o() {141- t.Error("Expected: false")140+ if !o() {141+ t.Error("Expected: true")142 }143+ if !o() {144+ t.Error("Expected: true")145+ }146 147 o = Once(true)148 if !o() {149 t.Error("Expected: true")150 }151 if o() {152 t.Error("Expected: false")153 }154+ if o() {155+ t.Error("Expected: false")156+ }157 }158 159 func TestRunesWidth(t *testing.T) {160 for _, args := range [][]int{82 unmodified linesmodified:src/util/util.go+11 added-1 removed
142 unmodified lines143 fd := file.Fd()144 return isatty.IsTerminal(fd) || isatty.IsCygwinTerminal(fd)145 }146 147+// RunOnce runs the given function only once148+func RunOnce(f func()) func() {149+ once := Once(true)150+ return func() {151+ if once() {152+ f()153+ }154+ }155+}156+157 // Once returns a function that returns the specified boolean value only once158 func Once(nextResponse bool) func() bool {159 state := nextResponse160 return func() bool {161 prevState := state152- state = false162+ state = !nextResponse163 return prevState164 }165 }166 62 unmodified linesmodified:src/terminal.go+71 added-26 removed
13 unmodified lines14 "sort"15 "strconv"16 "strings"17 "sync"18+ "sync/atomic"19 "syscall"20 "time"21 22 "github.com/rivo/uniseg"35 unmodified lines58 59 // Number of maximum focus events to process synchronously60 const maxFocusEvents = 1000061 62+// execute-silent and transform* actions will block user input for this duration.63+// After this duration, users can press CTRL-C to terminate the command.64+const blockDuration = 1 * time.Second65+66 func init() {67 placeholder = regexp.MustCompile(`\\?(?:{[+sf]*[0-9,-.]*}|{q}|{fzf:(?:query|action|prompt)}|{\+?f?nf?})`)68 whiteSuffix = regexp.MustCompile(`\s*$`)69 offsetComponentRegex = regexp.MustCompile(`([+-][0-9]+)|(-?/[1-9][0-9]*)`)1726 unmodified lines1796 } else if pad {1797 t.window.Print(strings.Repeat(" ", fillLength+1))1798 }1799 }1795- switch t.infoStyle {1796- case infoDefault:1797- move(line+1, 0, t.separatorLen == 0)1798- printSpinner()1799- t.window.Print(" ") // Margin1800- pos = 21801- case infoRight:1802- move(line+1, 0, false)1803- case infoInlineRight:1804- pos = t.promptLen + t.queryLen[0] + t.queryLen[1] + 11805- case infoInline:1806- pos = t.promptLen + t.queryLen[0] + t.queryLen[1] + 11807- printInfoPrefix()1808- case infoHidden:1800+1801+ if t.infoStyle == infoHidden {1802 if t.separatorLen > 0 {1803 move(line+1, 0, false)1804 printSeparator(t.window.Width()-1, false)1805 }35 unmodified lines1841 output = t.executeCommand(t.infoCommand, false, true, true, true, output)1842 outputPrinter, outputLen = t.ansiLabelPrinter(output, &tui.ColInfo, false)1843 }1844 1845+ switch t.infoStyle {1846+ case infoDefault:1847+ move(line+1, 0, t.separatorLen == 0)1848+ printSpinner()1849+ t.window.Print(" ") // Margin1850+ pos = 21851+ case infoRight:1852+ move(line+1, 0, false)1853+ case infoInlineRight:1854+ pos = t.promptLen + t.queryLen[0] + t.queryLen[1] + 11855+ case infoInline:1856+ pos = t.promptLen + t.queryLen[0] + t.queryLen[1] + 11857+ printInfoPrefix()1858+ }1859+1860 if t.infoStyle == infoRight {1861 maxWidth := t.window.Width()1862 if t.reading {1863 // Need space for spinner and a margin column1198 unmodified lines3062 t.tui.Pause(true)3063 cmd.Run()3064 t.tui.Resume(true, false)3065 t.mutex.Lock()3066+ // NOTE: Using t.reqBox.Set(reqFullRedraw...) instead can cause a deadlock3067 t.fullRedraw()3068 t.flush()3069 } else {3070 t.mutex.Unlock()3071 if len(info) == 0 {3072 t.uiMutex.Lock()3073 }3074+ paused := atomic.Int32{}3075+ ctx, cancel := context.WithCancel(context.Background())3076+ go func() {3077+ select {3078+ case <-ctx.Done():3079+ return3080+ case <-time.After(blockDuration):3081+ if paused.CompareAndSwap(0, 1) {3082+ t.tui.Pause(false)3083+ }3084+ }3085+ }()3086 if capture {3087 out, _ := cmd.StdoutPipe()3088 reader := bufio.NewReader(out)3089 cmd.Start()7 unmodified lines3097 cmd.Wait()3098 } else {3099 cmd.Run()3100 }3101+ cancel()3102+ if paused.CompareAndSwap(1, 2) {3103+ t.tui.Resume(false, false)3104+ }3105 t.mutex.Lock()3106+3107+ // Redraw prompt in case the user has typed something after blockDuration3108+ if paused.Load() > 0 {3109+ // NOTE: Using t.reqBox.Set(reqXXX...) instead can cause a deadlock3110+ t.printPrompt()3111+ if t.infoStyle == infoInline || t.infoStyle == infoInlineRight {3112+ t.printInfo()3113+ }3114+ }3115 }3116 if len(info) == 0 {3117 t.uiMutex.Unlock()3118 }214 unmodified lines3333 }3334 t.termSize = t.tui.Size()3335 t.resizeWindows(false)3336 t.window.Erase()3303- t.printPrompt()3304- t.printInfo()3305- t.printHeader()3306- t.flush()3337 t.mutex.Unlock()3338+3339+ t.reqBox.Set(reqPrompt, nil)3340+ t.reqBox.Set(reqInfo, nil)3341+ t.reqBox.Set(reqHeader, nil)3342 if t.initDelay > 0 {3343 go func() {3344 timer := time.NewTimer(t.initDelay)3345 <-timer.C217 unmodified lines3563 for running {3564 t.reqBox.Wait(func(events *util.Events) {3565 defer events.Clear()3566 3567+ // Sort events.3568+ // e.g. Make sure that reqPrompt is processed before reqInfo3569+ keys := make([]int, 0, len(*events))3570+ for key := range *events {3571+ keys = append(keys, int(key))3572+ }3573+ sort.Ints(keys)3574+3575 // t.uiMutex must be locked first to avoid deadlock. Execute actions3576 // will 1. unlock t.mutex to allow GET endpoint and 2. lock t.uiMutex3577 // to block rendering during the execution.3578 //9 unmodified lines3588 // L t.mutex | U t.uiMutex3589 // U t.uiMutex |3590 t.uiMutex.Lock()3591 t.mutex.Lock()3550- for req, value := range *events {3592+ printInfo := util.RunOnce(t.printInfo)3593+ for _, key := range keys {3594+ req := util.EventType(key)3595+ value := (*events)[req]3596 switch req {3597 case reqPrompt:3598 t.printPrompt()3599 if t.infoStyle == infoInline || t.infoStyle == infoInlineRight {3555- t.printInfo()3600+ printInfo()3601 }3602 case reqInfo:3558- t.printInfo()3603+ printInfo()3604 case reqList:3605 t.printList()3606 currentIndex := t.currentIndex()3607 focusChanged := focusedIndex != currentIndex3563- printInfo := false3608+ info := false3609 if focusChanged && t.track == trackCurrent {3610 t.track = trackDisabled3566- printInfo = true3611+ info = true3612 }3613 if (t.hasFocusActions || t.infoCommand != "") && focusChanged && currentIndex != t.lastFocus {3614 t.lastFocus = currentIndex3615 t.eventChan <- tui.Focus.AsEvent()3616 if t.infoCommand != "" {3572- printInfo = true3617+ info = true3618 }3619 }3575- if printInfo {3576- t.printInfo()3620+ if info {3621+ printInfo()3622 }3623 if focusChanged || version != t.version {3624 version = t.version3625 focusedIndex = currentIndex1299 unmodified lines