Skip to content
Myr

change-preview-window to take multiple option sets separated by '|'

Before adoption: this commit came in with the history the project adopted, so no landing records it.

So you can "rotate" through the different options with a single binding. fzf --preview 'cat {}' \ --bind 'ctrl-/:change-preview-window(70%|down,40%,border-horizontal|hidden|)' Close #2376

authored by Junegunn Choi · committed by Junegunn Choi · parent 20b4e69 · Browse files · 5 files+87 added−37 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

5 files+87 added−37 removed

  • tests:+17 added−0 removed
  • other:+70 added−37 removed

5 files+87 added−37 removed

modified:man/man1/fzf.1+17 added-2 removed
819 unmodified lines
820 \fBbackward-word\fR \fIalt-b shift-left\fR821 \fBbeginning-of-line\fR \fIctrl-a home\fR822 \fBcancel\fR (clear query string if not empty, abort fzf otherwise)823 \fBchange-preview(...)\fR (change \fB--preview\fR option)824 \fBchange-preview-window(...)\fR (change \fB--preview-window\fR option)824 \fBchange-preview-window(...)\fR (change \fB--preview-window\fR option; rotate through the multiple option sets separated by '|')825 \fBchange-prompt(...)\fR (change prompt to the given string)826 \fBclear-screen\fR \fIctrl-l\fR827 \fBclear-selection\fR (clear multi-selection)828 \fBclose\fR (close preview window if open, abort fzf otherwise)
142 unmodified lines
971commands in addition to the default preview command given by \fB--preview\fR972option.973974e.g.975975 # Default preview command with an extra preview binding976 fzf --preview 'file {}' --bind '?:preview:cat {}'977978 # A preview binding with no default preview command
2 unmodified lines
981982 # Preview window hidden by default, it appears when you first hit '?'983 fzf --bind '?:preview:cat {}' --preview-window hidden984985.SS CHANGE PREVIEW WINDOW ATTRIBUTES986987\fBchange-preview-window\fR action can be used to change the properties of the988preview window. Unlike the \fB--preview-window\fR option, you can specify989multiple sets of options separated by '|' characters.990991e.g.992 # Rotate through the options using CTRL-/993 fzf --preview 'cat {}' --bind 'ctrl-/:change-preview-window(right,70%|down,40%,border-horizontal|hidden|right)'994995 # The default properties given by `--preview-window` are inherited, so an empty string in the list is interpreted as the default996 fzf --preview 'cat {}' --preview-window 'right,40%,border-left' --bind 'ctrl-/:change-preview-window(70%|down,border-top|hidden|)'997998 # This is equivalent to toggle-preview action999 fzf --preview 'cat {}' --bind 'ctrl-/:change-preview-window(hidden|)'10001001.SH AUTHOR1002Junegunn Choi (\fIjunegunn.c@gmail.com\fR)10031004.SH SEE ALSO
14 unmodified lines
modified:src/options.go+13 added-11 removed
222 unmodified lines
223 Exit0 bool224 Filter *string225 ToggleSort bool226 Expect map[tui.Event]string227 Keymap map[tui.Event][]action227 Keymap map[tui.Event][]*action228 Preview previewOpts229 PrintQuery bool230 ReadZero bool231 Printer func(string)
54 unmodified lines
286 Exit0: false,287 Filter: nil,288 ToggleSort: false,289 Expect: make(map[tui.Event]string),290 Keymap: make(map[tui.Event][]action),290 Keymap: make(map[tui.Event][]*action),291 Preview: defaultPreviewOpts(""),292 PrintQuery: false,293 ReadZero: false,294 Printer: func(str string) { fmt.Println(str) },
502 unmodified lines
797 executeRegexp = regexp.MustCompile(798 `(?si)[:+](execute(?:-multi|-silent)?|reload|preview|change-prompt|change-preview-window|change-preview|unbind):.+|[:+](execute(?:-multi|-silent)?|reload|preview|change-prompt|change-preview-window|change-preview|unbind)(\([^)]*\)|\[[^\]]*\]|~[^~]*~|![^!]*!|@[^@]*@|\#[^\#]*\#|\$[^\$]*\$|%[^%]*%|\^[^\^]*\^|&[^&]*&|\*[^\*]*\*|;[^;]*;|/[^/]*/|\|[^\|]*\|)`)799}800801func parseKeymap(keymap map[tui.Event][]action, str string) {801func parseKeymap(keymap map[tui.Event][]*action, str string) {802 masked := executeRegexp.ReplaceAllStringFunc(str, func(src string) string {803 symbol := ":"804 if strings.HasPrefix(src, "+") {805 symbol = "+"
47 unmodified lines
853 }854855 idx2 := len(pair[0]) + 1856 specs := strings.Split(pair[1], "+")857 actions := make([]action, 0, len(specs))857 actions := make([]*action, 0, len(specs))858 appendAction := func(types ...actionType) {859 actions = append(actions, toActions(types...)...)860 }861 prevSpec := ""
170 unmodified lines
1032 var actionArg string1033 if spec[offset] == ':' {1034 if specIndex == len(specs)-1 {1035 actionArg = spec[offset+1:]1036 actions = append(actions, action{t: t, a: actionArg})1036 actions = append(actions, &action{t: t, a: actionArg})1037 } else {1038 prevSpec = spec + "+"1039 continue1040 }1041 } else {1042 actionArg = spec[offset+1 : len(spec)-1]1043 actions = append(actions, action{t: t, a: actionArg})1043 actions = append(actions, &action{t: t, a: actionArg})1044 }1045 if t == actUnbind {1046 parseKeyChords(actionArg, "unbind target required")1047 } else if t == actChangePreviewWindow {1048 opts := previewOpts{}1049 for _, arg := range strings.Split(actionArg, "|") {1049 parsePreviewWindow(&opts, actionArg)1050 parsePreviewWindow(&opts, arg)1051 }1052 }1053 }1054 }1055 prevSpec = ""
33 unmodified lines
1089 }1090 return actIgnore1091}10921091func parseToggleSort(keymap map[tui.Event][]action, str string) {1093func parseToggleSort(keymap map[tui.Event][]*action, str string) {1094 keys := parseKeyChords(str, "key name required")1095 if len(keys) != 1 {1096 errorExit("multiple keys specified")1097 }
559 unmodified lines
16571658 // Extend the default key map1659 keymap := defaultKeymap()1660 for key, actions := range opts.Keymap {1659 lastChangePreviewWindow := action{t: actIgnore}1661 var lastChangePreviewWindow *action1662 for _, act := range actions {1663 switch act.t {1664 case actToggleSort:1665 // To display "+S"/"-S" on info line
5 unmodified lines
1671 // Re-organize actions so that we only keep the last change-preview-window1672 // and it comes first in the list.1673 // * change-preview-window(up,+10)+preview(sleep 3; cat {})+change-preview-window(up,+20)1674 // -> change-preview-window(up,+20)+preview(sleep 3; cat {})1673 if lastChangePreviewWindow.t == actChangePreviewWindow {1674 reordered := []action{lastChangePreviewWindow}1675 if lastChangePreviewWindow != nil {1676 reordered := []*action{lastChangePreviewWindow}1677 for _, act := range actions {1678 if act.t != actChangePreviewWindow {1679 reordered = append(reordered, act)1680 }
54 unmodified lines
modified:src/terminal.go+36 added-24 removed
133 unmodified lines
134 sort bool135 toggleSort bool136 delimiter Delimiter137 expect map[tui.Event]string138 keymap map[tui.Event][]action138 keymap map[tui.Event][]*action139 pressed string140 printQuery bool141 history *History142 cycle bool
73 unmodified lines
216 reqJump217 reqRefresh218 reqReinit219 reqRedraw220 reqFullRedraw221 reqClose222 reqPrintQuery223 reqPreviewEnqueue224 reqPreviewDisplay
4 unmodified lines
229230type action struct {231 t actionType232 a string233 c int234}235236type actionType int237
103 unmodified lines
341 offset int342 spinner string343}344343func toActions(types ...actionType) []action {344 actions := make([]action, len(types))345func toActions(types ...actionType) []*action {346 actions := make([]*action, len(types))347 for idx, t := range types {346 actions[idx] = action{t: t, a: ""}348 actions[idx] = &action{t: t, a: ""}349 }350 return actions351}352351func defaultKeymap() map[tui.Event][]action {352 keymap := make(map[tui.Event][]action)353func defaultKeymap() map[tui.Event][]*action {354 keymap := make(map[tui.Event][]*action)355 add := func(e tui.EventType, a actionType) {356 keymap[e.AsEvent()] = toActions(a)357 }358 addEvent := func(e tui.Event, a actionType) {
1420 unmodified lines
1779 return strings.Join(replacements, " ")1780 })1781}17821781func (t *Terminal) redraw() {1782 t.tui.Clear()1783func (t *Terminal) redraw(clear bool) {1784 if clear {1785 t.tui.Clear()1786 }1787 t.tui.Refresh()1788 t.printAll()1789}1790
11 unmodified lines
1802 cmd.Stderr = os.Stderr1803 t.tui.Pause(true)1804 cmd.Run()1805 t.tui.Resume(true, false)1802 t.redraw()1806 t.redraw(true)1807 t.refresh()1808 } else {1809 t.tui.Pause(false)1810 cmd.Run()
136 unmodified lines
1947 notifyOnResize(resizeChan) // Non-portable1948 go func() {1949 for {1950 <-resizeChan1947 t.reqBox.Set(reqRedraw, nil)1951 t.reqBox.Set(reqFullRedraw, nil)1952 }1953 }()19541955 t.mutex.Lock()
241 unmodified lines
2197 case reqRefresh:2198 t.suppress = false2199 case reqReinit:2200 t.tui.Resume(t.fullscreen, t.sigstop)2197 t.redraw()2201 t.redraw(true)2202 case reqRedraw:2199 t.redraw()2203 t.redraw(false)2204 case reqFullRedraw:2205 t.redraw(true)2206 case reqClose:2207 exit(func() int {2208 if t.output() {2209 return exitOk
63 unmodified lines
2273 togglePreview := func(enabled bool) bool {2274 if t.previewer.enabled != enabled {2275 t.previewer.enabled = enabled2276 // We need to immediately update t.pwindow so we don't use reqRedraw2271 t.tui.Clear()2277 t.resizeWindows()2278 req(reqPrompt, reqList, reqInfo, reqHeader)2279 return true2280 }
33 unmodified lines
2314 return2315 }2316 }23172313 actionsFor := func(eventType tui.EventType) []action {2318 actionsFor := func(eventType tui.EventType) []*action {2319 return t.keymap[eventType.AsEvent()]2320 }23212317 var doAction func(action) bool2318 doActions := func(actions []action) bool {2322 var doAction func(*action) bool2323 doActions := func(actions []*action) bool {2324 for _, action := range actions {2325 if !doAction(action) {2326 return false2327 }2328 }2329 return true2330 }2326 doAction = func(a action) bool {2331 doAction = func(a *action) bool {2332 switch a.t {2333 case actIgnore:2334 case actExecute, actExecuteSilent:2335 t.executeCommand(a.a, false, a.t == actExecuteSilent)
171 unmodified lines
2507 req(reqList, reqInfo)2508 }2509 case actToggleIn:2510 if t.layout != layoutDefault {2506 return doAction(action{t: actToggleUp})2511 return doAction(&action{t: actToggleUp})2512 }2508 return doAction(action{t: actToggleDown})2513 return doAction(&action{t: actToggleDown})2514 case actToggleOut:2515 if t.layout != layoutDefault {2511 return doAction(action{t: actToggleDown})2516 return doAction(&action{t: actToggleDown})2517 }2513 return doAction(action{t: actToggleUp})2518 return doAction(&action{t: actToggleUp})2519 case actToggleDown:2520 if t.multi > 0 && t.merger.Length() > 0 && toggle() {2521 t.vmove(-1, true)2522 req(reqList)
15 unmodified lines
2538 if len(t.selected) > 0 || t.merger.Length() > 0 || !t.reading && t.count == 0 {2539 req(reqClose)2540 }2541 case actClearScreen:2537 req(reqRedraw)2542 req(reqFullRedraw)2543 case actClearQuery:2544 t.input = []rune{}2545 t.cx = 02546 case actClearSelection:
189 unmodified lines
2736 currentPreviewOpts := t.previewOpts27372738 // Reset preview options and apply the additional options2739 t.previewOpts = t.initialPreviewOpts2735 parsePreviewWindow(&t.previewOpts, a.a)27402741 // Split window options2742 tokens := strings.Split(a.a, "|")2743 parsePreviewWindow(&t.previewOpts, tokens[0])2744 if len(tokens) > 1 {2745 a.a = strings.Join(append(tokens[1:], tokens[0]), "|")2746 a.c++2747 }27482749 if t.previewOpts.hidden {2750 togglePreview(false)2751 } else {2752 // Full redraw
19 unmodified lines
27722773 if t.jumping == jumpDisabled {2774 actions := t.keymap[event.Comparable()]2775 if len(actions) == 0 && event.Type == tui.Rune {2764 doAction(action{t: actRune})2776 doAction(&action{t: actRune})2777 } else if !doActions(actions) {2778 continue2779 }2780 t.truncateQuery()
111 unmodified lines
modified:test/test_go.rb+17 added
2189 unmodified lines
2190 assert_equal '::', lines[0]2191 assert_equal ' 3', lines[1]2192 end2193 end21942195 def test_change_preview_window_rotate2196 tmux.send_keys "seq 100 | #{FZF} --preview-window left,border-none --preview 'echo hello' --bind '" \2197 "a:change-preview-window(right|down|up|hidden|)'", :Enter2198 3.times do2199 tmux.until { |lines| lines[0].start_with?('hello') }2200 tmux.send_keys 'a'2201 tmux.until { |lines| lines[0].end_with?('hello') }2202 tmux.send_keys 'a'2203 tmux.until { |lines| lines[-1].start_with?('hello') }2204 tmux.send_keys 'a'2205 tmux.until { |lines| assert_equal 'hello', lines[0] }2206 tmux.send_keys 'a'2207 tmux.until { |lines| refute_includes lines[0], 'hello' }2208 tmux.send_keys 'a'2209 end2210 end2211end22122213module TestShell2214 def setup
478 unmodified lines
modified:CHANGELOG.md+4 added
5 unmodified lines
6- Added `change-preview(...)` action to change the `--preview` command7 - cf. `preview(...)` is a one-off action that doesn't change the default8 preview command9- Added `change-preview-window(...)` action10 - You can rotate through the different options separated by `|`11 ```sh12 fzf --preview 'cat {}' --bind 'ctrl-/:change-preview-window(right,70%|down,40%,border-horizontal|hidden|right)'13 ```14150.28.016------17- Added `--header-first` option to print header before the prompt line
1187 unmodified lines