Skip to content
Myr

Add change-header and transform-header

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

Close #3237

authored by Junegunn Choi · committed by Junegunn Choi · parent b6e3f44 · Browse files · 5 files+135 added−33 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+135 added−33 removed

  • tests:+61 added−0 removed
  • other:+74 added−33 removed

5 files+135 added−33 removed

modified:man/man1/fzf.1+2 added
1029 unmodified lines
1030 \fBbecome(...)\fR (replace fzf process with the specified command; see below for the details)1031 \fBbeginning-of-line\fR \fIctrl-a home\fR1032 \fBcancel\fR (clear query string if not empty, abort fzf otherwise)1033 \fBchange-border-label(...)\fR (change \fB--border-label\fR to the given string)1034 \fBchange-header(...)\fR (change header to the given string; doesn't affect \fB--header-lines\fR)1035 \fBchange-preview(...)\fR (change \fB--preview\fR option)1036 \fBchange-preview-label(...)\fR (change \fB--preview-label\fR to the given string)1037 \fBchange-preview-window(...)\fR (change \fB--preview-window\fR option; rotate through the multiple option sets separated by '|')1038 \fBchange-prompt(...)\fR (change prompt to the given string)
61 unmodified lines
1100 \fBtoggle-search\fR (toggle search functionality)1101 \fBtoggle-sort\fR1102 \fBtoggle+up\fR \fIbtab (shift-tab)\fR1103 \fBtransform-border-label(...)\fR (transform border label using an external command)1104 \fBtransform-header(...)\fR (transform header using an external command)1105 \fBtransform-preview-label(...)\fR (transform preview label using an external command)1106 \fBtransform-prompt(...)\fR (transform prompt string using an external command)1107 \fBtransform-query(...)\fR (transform query string using an external command)1108 \fBunbind(...)\fR (unbind bindings)
157 unmodified lines
modified:src/options.go+5 added-1 removed
925 unmodified lines
926)927928func init() {929 executeRegexp = regexp.MustCompile(930 `(?si)[:+](become|execute(?:-multi|-silent)?|reload(?:-sync)?|preview|(?:change|transform)-(?:query|prompt|border-label|preview-label)|change-preview-window|change-preview|(?:re|un)bind|pos|put)`)930 `(?si)[:+](become|execute(?:-multi|-silent)?|reload(?:-sync)?|preview|(?:change|transform)-(?:header|query|prompt|border-label|preview-label)|change-preview-window|change-preview|(?:re|un)bind|pos|put)`)931 splitRegexp = regexp.MustCompile("[,:]+")932 actionNameRegexp = regexp.MustCompile("(?i)^[a-z-]+")933}934
313 unmodified lines
1248 case "preview":1249 return actPreview1250 case "change-border-label":1251 return actChangeBorderLabel1252 case "change-header":1253 return actChangeHeader1254 case "change-preview-label":1255 return actChangePreviewLabel1256 case "change-preview-window":1257 return actChangePreviewWindow
16 unmodified lines
1274 case "transform-border-label":1275 return actTransformBorderLabel1276 case "transform-preview-label":1277 return actTransformPreviewLabel1278 case "transform-header":1279 return actTransformHeader1280 case "transform-prompt":1281 return actTransformPrompt1282 case "transform-query":1283 return actTransformQuery
786 unmodified lines
modified:src/terminal.go+52 added-20 removed
1 unmodified line
23import (4 "bufio"5 "fmt"6 "io"7 "io/ioutil"8 "math"9 "os"10 "os/exec"
299 unmodified lines
310 actBackwardDeleteCharEOF311 actBackwardWord312 actCancel313 actChangeBorderLabel314 actChangeHeader315 actChangePreviewLabel316 actChangePrompt317 actChangeQuery318 actClearScreen
38 unmodified lines
357 actHidePreview358 actTogglePreview359 actTogglePreviewWrap360 actTransformBorderLabel361 actTransformHeader362 actTransformPreviewLabel363 actTransformPrompt364 actTransformQuery365 actPreview
260 unmodified lines
626 paused: opts.Phony,627 cycle: opts.Cycle,628 headerFirst: opts.HeaderFirst,629 headerLines: opts.HeaderLines,627 header: header,630 header: []string{},631 header0: header,632 ellipsis: opts.Ellipsis,633 ansi: opts.Ansi,634 tabstop: opts.Tabstop,
250 unmodified lines
885 }886 return reversed887}888889func (t *Terminal) changeHeader(header string) bool {890 lines := strings.Split(strings.TrimSuffix(header, "\n"), "\n")891 switch t.layout {892 case layoutDefault, layoutReverseList:893 lines = reverseStringArray(lines)894 }895 needFullRedraw := len(t.header0) != len(lines)896 t.header0 = lines897 return needFullRedraw898}899900// UpdateHeader updates the header901func (t *Terminal) UpdateHeader(header []string) {902 t.mutex.Lock()889 t.header = append(append([]string{}, t.header0...), header...)903 t.header = header904 t.mutex.Unlock()905 t.reqBox.Set(reqHeader, nil)906}907
450 unmodified lines
1358 switch t.layout {1359 case layoutDefault:1360 y = h - y - 11361 case layoutReverseList:1348 n := 2 + len(t.header)1362 n := 2 + len(t.header0) + len(t.header)1363 if t.noInfoLine() {1364 n--1365 }1366 if y < n {
139 unmodified lines
1506 }1507}15081509func (t *Terminal) printHeader() {1496 if len(t.header) == 0 {1510 if len(t.header0)+len(t.header) == 0 {1511 return1512 }1513 max := t.window.Height()1514 if t.headerFirst {
2 unmodified lines
1517 max--1518 }1519 }1520 var state *ansiState1507 for idx, lineStr := range t.header {1521 for idx, lineStr := range append(append([]string{}, t.header0...), t.header...) {1522 line := idx1523 if !t.headerFirst {1524 line++1525 if !t.noInfoLine() {
25 unmodified lines
1551 i := j1552 if t.layout == layoutDefault {1553 i = maxy - 1 - j1554 }1541 line := i + 2 + len(t.header)1555 line := i + 2 + len(t.header0) + len(t.header)1556 if t.noInfoLine() {1557 line--1558 }1559 if i < count {
729 unmodified lines
2289 t.tui.Refresh()2290 t.printAll()2291}22922279func (t *Terminal) executeCommand(template string, forcePlus bool, background bool, captureFirstLine bool) string {2293func (t *Terminal) executeCommand(template string, forcePlus bool, background bool, capture bool, firstLineOnly bool) string {2294 line := ""2295 valid, list := t.buildPlusList(template, forcePlus)2282 // captureFirstLine is used for transform-{prompt,query} and we don't want to2296 // 'capture' is used for transform-* and we don't want to2297 // return an empty string in those cases2284 if !valid && !captureFirstLine {2298 if !valid && !capture {2299 return line2300 }2301 command := t.replacePlaceholder(template, forcePlus, string(t.input), list)2302 cmd := util.ExecCommand(command, false)
8 unmodified lines
2311 t.tui.Resume(true, false)2312 t.redraw()2313 t.refresh()2314 } else {2301 if captureFirstLine {2315 if capture {2316 out, _ := cmd.StdoutPipe()2317 reader := bufio.NewReader(out)2318 cmd.Start()2319 if firstLineOnly {2305 line, _ = reader.ReadString('\n')2306 line = strings.TrimRight(line, "\r\n")2320 line, _ = reader.ReadString('\n')2321 line = strings.TrimRight(line, "\r\n")2322 } else {2323 bytes, _ := io.ReadAll(reader)2324 line = string(bytes)2325 }2326 cmd.Wait()2327 } else {2328 cmd.Run()2329 }
609 unmodified lines
2939 syscall.Exec(shellPath, []string{shell, "-c", command}, os.Environ())2940 }2941 }2942 case actExecute, actExecuteSilent:2924 t.executeCommand(a.a, false, a.t == actExecuteSilent, false)2943 t.executeCommand(a.a, false, a.t == actExecuteSilent, false, false)2944 case actExecuteMulti:2926 t.executeCommand(a.a, true, false, false)2945 t.executeCommand(a.a, true, false, false, false)2946 case actInvalid:2947 t.mutex.Unlock()2948 return false2949 case actTogglePreview, actShowPreview, actHidePreview:
25 unmodified lines
2975 t.previewed.version = 02976 req(reqPreviewRefresh)2977 }2978 case actTransformPrompt:2960 prompt := t.executeCommand(a.a, false, true, true)2979 prompt := t.executeCommand(a.a, false, true, true, true)2980 t.prompt, t.promptLen = t.parsePrompt(prompt)2981 req(reqPrompt)2982 case actTransformQuery:2964 query := t.executeCommand(a.a, false, true, true)2983 query := t.executeCommand(a.a, false, true, true, true)2984 t.input = []rune(query)2985 t.cx = len(t.input)2986 case actToggleSort:2987 t.sort = !t.sort
40 unmodified lines
3028 req(reqPrintQuery)3029 case actChangeQuery:3030 t.input = []rune(a.a)3031 t.cx = len(t.input)3032 case actTransformHeader:3033 header := t.executeCommand(a.a, false, true, true, false)3034 if t.changeHeader(header) {3035 req(reqFullRedraw)3036 } else {3037 req(reqHeader)3038 }3039 case actChangeHeader:3040 if t.changeHeader(a.a) {3041 req(reqFullRedraw)3042 } else {3043 req(reqHeader)3044 }3045 case actChangeBorderLabel:3046 if t.border != nil {3047 t.borderLabel, t.borderLabelLen = t.ansiLabelPrinter(a.a, &tui.ColBorderLabel, false)3048 req(reqRedrawBorderLabel)
4 unmodified lines
3053 req(reqRedrawPreviewLabel)3054 }3055 case actTransformBorderLabel:3056 if t.border != nil {3025 label := t.executeCommand(a.a, false, true, true)3057 label := t.executeCommand(a.a, false, true, true, true)3058 t.borderLabel, t.borderLabelLen = t.ansiLabelPrinter(label, &tui.ColBorderLabel, false)3059 req(reqRedrawBorderLabel)3060 }3061 case actTransformPreviewLabel:3062 if t.pborder != nil {3031 label := t.executeCommand(a.a, false, true, true)3063 label := t.executeCommand(a.a, false, true, true, true)3064 t.previewLabel, t.previewLabelLen = t.ansiLabelPrinter(label, &tui.ColPreviewLabel, false)3065 req(reqRedrawPreviewLabel)3066 }3067 case actChangePrompt:
321 unmodified lines
33893390 // Translate coordinates3391 mx -= t.window.Left()3392 my -= t.window.Top()3361 min := 2 + len(t.header)3393 min := 2 + len(t.header0) + len(t.header)3394 if t.noInfoLine() {3395 min--3396 }3397 h := t.window.Height()
260 unmodified lines
3658 return t.cy == o3659}36603661func (t *Terminal) maxItems() int {3630 max := t.window.Height() - 2 - len(t.header)3662 max := t.window.Height() - 2 - len(t.header0) - len(t.header)3663 if t.noInfoLine() {3664 max++3665 }3666 return util.Max(max, 0)
1 unmodified line
modified:test/test_go.rb+61 added
1863 unmodified lines
1864 tmux.send_keys :Space1865 tmux.until { |lines| assert_equal '>', lines.last }1866 end18671868 def test_change_and_transform_header1869 [1870 'space:change-header:$(seq 4)',1871 'space:transform-header:seq 4'1872 ].each_with_index do |binding, i|1873 tmux.send_keys %(seq 3 | #{FZF} --header-lines 2 --header bar --bind "#{binding}"), :Enter1874 expected = <<~OUTPUT1875 > 31876 21877 11878 bar1879 1/11880 >1881 OUTPUT1882 tmux.until { assert_block(expected, _1) }1883 tmux.send_keys :Space1884 expected = <<~OUTPUT1885 > 31886 21887 11888 11889 21890 31891 41892 1/11893 >1894 OUTPUT1895 tmux.until { assert_block(expected, _1) }1896 next unless i.zero?18971898 teardown1899 setup1900 end1901 end19021903 def test_change_header1904 tmux.send_keys %(seq 3 | #{FZF} --header-lines 2 --header bar --bind "space:change-header:$(seq 4)"), :Enter1905 expected = <<~OUTPUT1906 > 31907 21908 11909 bar1910 1/11911 >1912 OUTPUT1913 tmux.until { assert_block(expected, _1) }1914 tmux.send_keys :Space1915 expected = <<~OUTPUT1916 > 31917 21918 11919 11920 21921 31922 41923 1/11924 >1925 OUTPUT1926 tmux.until { assert_block(expected, _1) }1927 end19281929 def test_change_query1930 tmux.send_keys %(: | #{FZF} --query foo --bind space:change-query:foobar), :Enter1931 tmux.until { |lines| assert_equal 0, lines.item_count }1932 tmux.until { |lines| assert_equal '> foo', lines.last }
1368 unmodified lines
modified:CHANGELOG.md+15 added-12 removed
1CHANGELOG2=========340.39.140.40.05------6- New actions7 - Added `change-header(...)`8 - Added `transform-header(...)`6- Added `toggle-track` action. Temporarily enabling tracking is useful when7 you want to see the surrounding items by deleting the query string.8 ```sh9 export FZF_CTRL_R_OPTS="10 --preview 'echo {}' --preview-window up:3:hidden:wrap11 --bind 'ctrl-/:toggle-preview'12 --bind 'ctrl-t:toggle-track'13 --bind 'ctrl-y:execute-silent(echo -n {2..} | pbcopy)+abort'14 --color header:italic15 --header 'Press CTRL-Y to copy command into clipboard'"16 ```9 - Added `toggle-track` action. Temporarily enabling tracking is useful when10 you want to see the surrounding items by deleting the query string.11 ```sh12 export FZF_CTRL_R_OPTS="13 --preview 'echo {}' --preview-window up:3:hidden:wrap14 --bind 'ctrl-/:toggle-preview'15 --bind 'ctrl-t:toggle-track'16 --bind 'ctrl-y:execute-silent(echo -n {2..} | pbcopy)+abort'17 --color header:italic18 --header 'Press CTRL-Y to copy command into clipboard'"19 ```20- Fixed `--track` behavior when used with `--tac`21 - However, using `--track` with `--tac` is not recommended. The resulting22 behavior can be very confusing.23- Bug fixes and improvements
1598 unmodified lines