Make GET endpoint available from 'execute' and 'transform' actions
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 9dc3ed6 · Browse files · 4 files·+50 added−27 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+50 added−27 removed
- other:+50 added−27 removed
Provenance
- Commit
b2ecb63- Tree
ce71d24- Author
Junegunn Choi <junegunn.c@gmail.com>- Committer
Junegunn Choi <junegunn.c@gmail.com>- Parent
9dc3ed6
4 files changed+50 added−27 removed
modified:src/actiontype_string.go+4 added-5 removed
115 unmodified lines116 _ = x[actDeselect-105]117 _ = x[actUnbind-106]118 _ = x[actRebind-107]119 _ = x[actBecome-108]120- _ = x[actResponse-109]121- _ = x[actShowHeader-110]122- _ = x[actHideHeader-111]120+ _ = x[actShowHeader-109]121+ _ = x[actHideHeader-110]122 }123 125-const _actionType_name = "actIgnoreactStartactClickactInvalidactCharactMouseactBeginningOfLineactAbortactAcceptactAcceptNonEmptyactAcceptOrPrintQueryactBackwardCharactBackwardDeleteCharactBackwardDeleteCharEofactBackwardWordactCancelactChangeBorderLabelactChangeHeaderactChangeMultiactChangePreviewLabelactChangePromptactChangeQueryactClearScreenactClearQueryactClearSelectionactCloseactDeleteCharactDeleteCharEofactEndOfLineactFatalactForwardCharactForwardWordactKillLineactKillWordactUnixLineDiscardactUnixWordRuboutactYankactBackwardKillWordactSelectAllactDeselectAllactToggleactToggleSearchactToggleAllactToggleDownactToggleUpactToggleInactToggleOutactToggleTrackactToggleTrackCurrentactToggleHeaderactTrackCurrentactUntrackCurrentactDownactUpactPageUpactPageDownactPositionactHalfPageUpactHalfPageDownactOffsetUpactOffsetDownactJumpactJumpAcceptactPrintQueryactRefreshPreviewactReplaceQueryactToggleSortactShowPreviewactHidePreviewactTogglePreviewactTogglePreviewWrapactTransformactTransformBorderLabelactTransformHeaderactTransformPreviewLabelactTransformPromptactTransformQueryactPreviewactChangePreviewactChangePreviewWindowactPreviewTopactPreviewBottomactPreviewUpactPreviewDownactPreviewPageUpactPreviewPageDownactPreviewHalfPageUpactPreviewHalfPageDownactPrevHistoryactPrevSelectedactPrintactPutactNextHistoryactNextSelectedactExecuteactExecuteSilentactExecuteMultiactSigStopactFirstactLastactReloadactReloadSyncactDisableSearchactEnableSearchactSelectactDeselectactUnbindactRebindactBecomeactResponseactShowHeaderactHideHeader"124+const _actionType_name = "actIgnoreactStartactClickactInvalidactCharactMouseactBeginningOfLineactAbortactAcceptactAcceptNonEmptyactAcceptOrPrintQueryactBackwardCharactBackwardDeleteCharactBackwardDeleteCharEofactBackwardWordactCancelactChangeBorderLabelactChangeHeaderactChangeMultiactChangePreviewLabelactChangePromptactChangeQueryactClearScreenactClearQueryactClearSelectionactCloseactDeleteCharactDeleteCharEofactEndOfLineactFatalactForwardCharactForwardWordactKillLineactKillWordactUnixLineDiscardactUnixWordRuboutactYankactBackwardKillWordactSelectAllactDeselectAllactToggleactToggleSearchactToggleAllactToggleDownactToggleUpactToggleInactToggleOutactToggleTrackactToggleTrackCurrentactToggleHeaderactTrackCurrentactUntrackCurrentactDownactUpactPageUpactPageDownactPositionactHalfPageUpactHalfPageDownactOffsetUpactOffsetDownactJumpactJumpAcceptactPrintQueryactRefreshPreviewactReplaceQueryactToggleSortactShowPreviewactHidePreviewactTogglePreviewactTogglePreviewWrapactTransformactTransformBorderLabelactTransformHeaderactTransformPreviewLabelactTransformPromptactTransformQueryactPreviewactChangePreviewactChangePreviewWindowactPreviewTopactPreviewBottomactPreviewUpactPreviewDownactPreviewPageUpactPreviewPageDownactPreviewHalfPageUpactPreviewHalfPageDownactPrevHistoryactPrevSelectedactPrintactPutactNextHistoryactNextSelectedactExecuteactExecuteSilentactExecuteMultiactSigStopactFirstactLastactReloadactReloadSyncactDisableSearchactEnableSearchactSelectactDeselectactUnbindactRebindactBecomeactShowHeaderactHideHeader"125 127-var _actionType_index = [...]uint16{0, 9, 17, 25, 35, 42, 50, 68, 76, 85, 102, 123, 138, 159, 183, 198, 207, 227, 242, 256, 277, 292, 306, 320, 333, 350, 358, 371, 387, 399, 407, 421, 435, 446, 457, 475, 492, 499, 518, 530, 544, 553, 568, 580, 593, 604, 615, 627, 641, 662, 677, 692, 709, 716, 721, 730, 741, 752, 765, 780, 791, 804, 811, 824, 837, 854, 869, 882, 896, 910, 926, 946, 958, 981, 999, 1023, 1041, 1058, 1068, 1084, 1106, 1119, 1135, 1147, 1161, 1177, 1195, 1215, 1237, 1251, 1266, 1274, 1280, 1294, 1309, 1319, 1335, 1350, 1360, 1368, 1375, 1384, 1397, 1413, 1428, 1437, 1448, 1457, 1466, 1475, 1486, 1499, 1512}126+var _actionType_index = [...]uint16{0, 9, 17, 25, 35, 42, 50, 68, 76, 85, 102, 123, 138, 159, 183, 198, 207, 227, 242, 256, 277, 292, 306, 320, 333, 350, 358, 371, 387, 399, 407, 421, 435, 446, 457, 475, 492, 499, 518, 530, 544, 553, 568, 580, 593, 604, 615, 627, 641, 662, 677, 692, 709, 716, 721, 730, 741, 752, 765, 780, 791, 804, 811, 824, 837, 854, 869, 882, 896, 910, 926, 946, 958, 981, 999, 1023, 1041, 1058, 1068, 1084, 1106, 1119, 1135, 1147, 1161, 1177, 1195, 1215, 1237, 1251, 1266, 1274, 1280, 1294, 1309, 1319, 1335, 1350, 1360, 1368, 1375, 1384, 1397, 1413, 1428, 1437, 1448, 1457, 1466, 1475, 1488, 1501}127 128 func (i actionType) String() string {129 if i < 0 || i >= actionType(len(_actionType_index)-1) {130 return "actionType(" + strconv.FormatInt(int64(i), 10) + ")"3 unmodified linesmodified:src/server.go+10 added-16 removed
36 unmodified lines37 maxContentLength = 1024 * 102438 )39 40 type httpServer struct {41- apiKey []byte42- actionChannel chan []*action43- responseChannel chan string41+ apiKey []byte42+ actionChannel chan []*action43+ getHandler func(getParams) string44 }45 46 type listenAddress struct {47 host string24 unmodified lines72 }73 return listenAddress{parts[0], port}, nil74 }75 76-func startHttpServer(address listenAddress, actionChannel chan []*action, responseChannel chan string) (net.Listener, int, error) {76+func startHttpServer(address listenAddress, actionChannel chan []*action, getHandler func(getParams) string) (net.Listener, int, error) {77 host := address.host78 port := address.port79 apiKey := os.Getenv("FZF_API_KEY")80 if !address.IsLocal() && len(apiKey) == 0 {17 unmodified lines98 }99 }100 101 server := httpServer{102- apiKey: []byte(apiKey),103- actionChannel: actionChannel,104- responseChannel: responseChannel,102+ apiKey: []byte(apiKey),103+ actionChannel: actionChannel,104+ getHandler: getHandler,105 }106 107 go func() {108 for {55 unmodified lines164 switch section {165 case 0:166 getMatch := getRegex.FindStringSubmatch(text)167 if len(getMatch) > 0 {168- server.actionChannel <- []*action{{t: actResponse, a: getMatch[1]}}169- select {170- case response := <-server.responseChannel:168+ response := server.getHandler(parseGetParams(getMatch[1]))169+ if len(response) > 0 {170 return good(response)172- case <-time.After(channelTimeout):173- go func() {174- // Drain the channel175- <-server.responseChannel176- }()177- return answer(httpUnavailable+jsonContentType, `{"error":"timeout"}`)171 }172+ return answer(httpUnavailable+jsonContentType, `{"error":"timeout"}`)173 } else if !strings.HasPrefix(text, "POST / HTTP") {174 return bad("invalid request method")175 }176 section++70 unmodified linesmodified:src/terminal.go+27 added-6 removed
316 unmodified lines317 sigstop bool318 startChan chan fitpad319 killChan chan bool320 serverInputChan chan []*action321- serverOutputChan chan string321 eventChan chan tui.Event322 slab *util.Slab323 theme *tui.ColorTheme324 tui tui.Renderer170 unmodified lines495 actDeselect496 actUnbind497 actRebind498 actBecome500- actResponse499 actShowHeader500 actHideHeader501 )502 333 unmodified lines836 theme: opts.Theme,837 startChan: make(chan fitpad, 1),838 killChan: make(chan bool),839 serverInputChan: make(chan []*action, 100),842- serverOutputChan: make(chan string),840 eventChan: make(chan tui.Event, 6), // (load + result + zero|one) | (focus) | (resize) | (GetChar)841 tui: renderer,842 ttyin: ttyin,843 initFunc: func() error { return renderer.Init() },45 unmodified lines889 _, t.hasFocusActions = t.keymap[tui.Focus.AsEvent()]890 _, t.hasLoadActions = t.keymap[tui.Load.AsEvent()]891 892 if t.listenAddr != nil {896- listener, port, err := startHttpServer(*t.listenAddr, t.serverInputChan, t.serverOutputChan)893+ listener, port, err := startHttpServer(*t.listenAddr, t.serverInputChan, t.dumpStatus)894 if err != nil {895 return nil, err896 }897 t.listener = listener2094 unmodified lines2992 }2993 }2994 2995 t.tui.Pause(true)2996+ t.mutex.Unlock()2997 cmd.Run()2998+ t.mutex.Lock()2999 t.tui.Resume(true, false)3000 t.redraw()3001 t.refresh()3002 } else {3003+ t.mutex.Unlock()3004 if capture {3005 out, _ := cmd.StdoutPipe()3006 reader := bufio.NewReader(out)3007 cmd.Start()7 unmodified lines3015 cmd.Wait()3016 } else {3017 cmd.Run()3018 }3019+ t.mutex.Lock()3020 }3021 t.executing.Set(false)3022 removeFiles(tempFiles)3023 return line695 unmodified lines3719 }3720 doAction = func(a *action) bool {3721 switch a.t {3722 case actIgnore, actStart, actClick:3722- case actResponse:3723- t.serverOutputChan <- t.dumpStatus(parseGetParams(a.a))3723 case actBecome:3724 valid, list := t.buildPlusList(a.a, false)3725 if valid {3726 // We do not remove temp files in this case979 unmodified lines4706 Text: i.AsString(t.ansi),4707 }4708 }4709 4710+func (t *Terminal) tryLock(timeout time.Duration) bool {4711+ sleepDuration := 10 * time.Millisecond4712+4713+ for {4714+ if t.mutex.TryLock() {4715+ return true4716+ }4717+4718+ timeout -= sleepDuration4719+ if timeout <= 0 {4720+ break4721+ }4722+ time.Sleep(sleepDuration)4723+ }4724+ return false4725+}4726+4727 func (t *Terminal) dumpStatus(params getParams) string {4728+ if !t.tryLock(channelTimeout) {4729+ return ""4730+ }4731+ defer t.mutex.Unlock()4732+4733 selectedItems := t.sortSelected()4734 selected := make([]StatusItem, util.Max(0, util.Min(params.limit, len(selectedItems)-params.offset)))4735 for i := range selected {4736 selected[i] = t.dumpItem(selectedItems[i+params.offset].item)29 unmodified linesmodified:CHANGELOG.md+9 added
2 unmodified lines3 4 0.53.15 ------6 - Bug fixes and minor improvements7+ - Better cache management and improved rendering for `--tail`8+ - Fixed crash when using `--tiebreak=end` with very long items9+ - Fixed mouse support on Windows10+ - zsh 5.0 compatibility (thanks to @LangLangBart)11+ - Fixed `--walker-skip` to also skip symlinks to directories12+ - GET endpoint is now available from `execute` and `transform` actions (it used to timeout due to lock conflict)13+ ```sh14+ fzf --listen --bind 'focus:transform-header:curl -s localhost:$FZF_PORT?limit=0 | jq .'15+ ```16 17 0.53.018 ------19 - Multi-line display2145 unmodified lines