Skip to content
Myr

Replace --header-file with --header (#346)

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

and allow using --header and --header-lines at the same time. Close #346.

authored by Junegunn Choi · committed by Junegunn Choi · parent 65d9d41 · Browse files · 7 files+70 added−44 removed
This commit

State

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

Diff stats

7 files+70 added−44 removed

  • tests:+24 added−4 removed
  • other:+46 added−40 removed

7 files+70 added−44 removed

modified:man/man1/fzf.1+5 added-5 removed
283 unmodified lines
284.BI "--history-size=" "N"285Maximum number of entries in the history file (default: 1000). The file is286automatically truncated when the number of the lines exceeds the value.287.TP288.BI "--header-file=" "FILE"289The content of the file will be printed as the sticky header. The lines in the290file are displayed in order from top to bottom regardless of \fB--reverse\fR,291and are not affected by \fB--with-nth\fR. ANSI color codes are processed even292when \fB--ansi\fR is not set.288.BI "--header=" "STR"289The given string will be printed as the sticky header. The lines are displayed290in the given order from top to bottom regardless of \fB--reverse\fR option, and291are not affected by \fB--with-nth\fR. ANSI color codes are processed even when292\fB--ansi\fR is not set.293.TP294.BI "--header-lines=" "N"295The first N lines of the input are treated as the sticky header. When296\fB--with-nth\fR is set, the lines are transformed just like the other
117 unmodified lines
modified:shell/completion.bash+2 added-2 removed
47 unmodified lines
48 --sync49 --cycle50 --history51 --history-size52 --header-file52 --header53 --header-lines54 --margin"5556 case "${prev}" in
4 unmodified lines
61 --color)62 COMPREPLY=( $(compgen -W "dark light 16 bw" -- ${cur}) )63 return 064 ;;65 --history|--header-file)65 --history)66 COMPREPLY=()67 return 068 ;;69 esac
206 unmodified lines
modified:src/core.go+1 added-1 removed
236 unmodified lines
237 terminal.UpdateProgress(val)238 }239240 case EvtHeader:241 terminal.UpdateHeader(value.([]string), opts.HeaderLines)241 terminal.UpdateHeader(value.([]string))242243 case EvtSearchFin:244 switch val := value.(type) {245 case *Merger:
38 unmodified lines
modified:src/options.go+8 added-18 removed
1package fzf23import (4 "io/ioutil"4 "os"5 "regexp"6 "strconv"7 "strings"
35 unmodified lines
43 --prompt=STR Input prompt (default: '> ')44 --bind=KEYBINDS Custom key bindings. Refer to the man page.45 --history=FILE History file46 --history-size=N Maximum number of history entries (default: 1000)48 --header-file=FILE The file whose content to be printed as header47 --header=STR String to print as header48 --header-lines=N The first N lines of the input are treated as header4950 Scripting51 -q, --query=STR Start the finder with the given query
550 unmodified lines
602 keymap[firstKey(keys)] = actToggleSort603 return keymap604}605607func readHeaderFile(filename string) []string {608 content, err := ioutil.ReadFile(filename)609 if err != nil {610 errorExit("failed to read header file: " + filename)611 }612 return strings.Split(strings.TrimSuffix(string(content), "\n"), "\n")606func strLines(str string) []string {607 return strings.Split(strings.TrimSuffix(str, "\n"), "\n")608}609610func parseMargin(margin string) [4]string {611 margins := strings.Split(margin, ",")
175 unmodified lines
787 case "--history":788 setHistory(nextString(allArgs, &i, "history file path required"))789 case "--history-size":790 setHistoryMax(nextInt(allArgs, &i, "history max size required"))796 case "--no-header-file":791 case "--no-header":792 opts.Header = []string{}793 case "--no-header-lines":794 opts.HeaderLines = 0800 case "--header-file":801 opts.Header = readHeaderFile(802 nextString(allArgs, &i, "header file name required"))803 opts.HeaderLines = 0795 case "--header":796 opts.Header = strLines(nextString(allArgs, &i, "header string required"))797 case "--header-lines":805 opts.Header = []string{}798 opts.HeaderLines = atoi(799 nextString(allArgs, &i, "number of header lines required"))800 case "--no-margin":801 opts.Margin = defaultMargin()
32 unmodified lines
834 } else if match, value := optString(arg, "--history="); match {835 setHistory(value)836 } else if match, value := optString(arg, "--history-size="); match {837 setHistoryMax(atoi(value))846 } else if match, value := optString(arg, "--header-file="); match {847 opts.Header = readHeaderFile(value)848 opts.HeaderLines = 0838 } else if match, value := optString(arg, "--header="); match {839 opts.Header = strLines(value)840 } else if match, value := optString(arg, "--header-lines="); match {850 opts.Header = []string{}841 opts.HeaderLines = atoi(value)842 } else if match, value := optString(arg, "--margin="); match {843 opts.Margin = parseMargin(value)844 } else {
48 unmodified lines
modified:src/terminal.go+20 added-14 removed
40 unmodified lines
41 printQuery bool42 history *History43 cycle bool44 header []string45 header0 []string46 ansi bool47 margin [4]string48 marginInt [4]int49 count int
135 unmodified lines
185186// NewTerminal returns new Terminal object187func NewTerminal(opts *Options, eventBox *util.EventBox) *Terminal {188 input := []rune(opts.Query)189 var header []string190 if opts.Reverse {191 header = opts.Header192 } else {193 header = reverseStringArray(opts.Header)194 }195 return &Terminal{196 inlineInfo: opts.InlineInfo,197 prompt: opts.Prompt,198 reverse: opts.Reverse,
14 unmodified lines
213 history: opts.History,214 margin: opts.Margin,215 marginInt: [4]int{0, 0, 0, 0},216 cycle: opts.Cycle,210 header: opts.Header,217 header: header,218 header0: header,219 ansi: opts.Ansi,220 reading: true,221 merger: EmptyMerger,222 selected: make(map[uint32]selectedItem),
25 unmodified lines
248 t.reqBox.Set(reqRefresh, nil)249 }250}251252func reverseStringArray(input []string) []string {253 size := len(input)254 reversed := make([]string, size)255 for idx, str := range input {256 reversed[size-idx-1] = str257 }258 return reversed259}260261// UpdateHeader updates the header245func (t *Terminal) UpdateHeader(header []string, lines int) {262func (t *Terminal) UpdateHeader(header []string) {263 t.mutex.Lock()247 t.header = make([]string, lines)248 copy(t.header, header)249 if !t.reverse {250 reversed := make([]string, lines)251 for idx, str := range t.header {252 reversed[lines-idx-1] = str253 }254 t.header = reversed255 }264 t.header = append(append([]string{}, t.header0...), header...)265 t.mutex.Unlock()266 t.reqBox.Set(reqHeader, nil)267}268
175 unmodified lines
444 }445 max := t.maxHeight()446 var state *ansiState447 for idx, lineStr := range t.header {439 if !t.reverse {440 idx = len(t.header) - idx - 1441 }448 line := idx + 2449 if t.inlineInfo {450 line--451 }
668 unmodified lines
modified:test/test_go.rb+24 added-4 removed
735 unmodified lines
736 tmux.send_keys :Enter737 assert_equal '6', readonce.chomp738 end739740 def test_header_file741 tmux.send_keys "seq 100 | #{fzf "--header-file <(head -5 #{__FILE__})"}", :Enter740 def test_header741 tmux.send_keys "seq 100 | #{fzf "--header \\\"\\$(head -5 #{__FILE__})\\\""}", :Enter742 header = File.readlines(__FILE__).take(5).map(&:strip)743 tmux.until do |lines|744 lines[-2].include?('100/100') &&745 lines[-7..-3].map(&:strip) == header746 end747 end748749 def test_header_file_reverse750 tmux.send_keys "seq 100 | #{fzf "--header-file=<(head -5 #{__FILE__}) --reverse"}", :Enter749 def test_header_reverse750 tmux.send_keys "seq 100 | #{fzf "--header=\\\"\\$(head -5 #{__FILE__})\\\" --reverse"}", :Enter751 header = File.readlines(__FILE__).take(5).map(&:strip)752 tmux.until do |lines|753 lines[1].include?('100/100') &&754 lines[2..6].map(&:strip) == header755 end756 end757758 def test_header_and_header_lines759 tmux.send_keys "seq 100 | #{fzf "--header-lines 10 --header \\\"\\$(head -5 #{__FILE__})\\\""}", :Enter760 header = File.readlines(__FILE__).take(5).map(&:strip)761 tmux.until do |lines|762 lines[-2].include?('90/90') &&763 lines[-7...-2].map(&:strip) == header &&764 lines[-17...-7].map(&:strip) == (1..10).map(&:to_s).reverse765 end766 end767768 def test_header_and_header_lines_reverse769 tmux.send_keys "seq 100 | #{fzf "--reverse --header-lines 10 --header \\\"\\$(head -5 #{__FILE__})\\\""}", :Enter770 header = File.readlines(__FILE__).take(5).map(&:strip)771 tmux.until do |lines|772 lines[1].include?('90/90') &&773 lines[2...7].map(&:strip) == header &&774 lines[7...17].map(&:strip) == (1..10).map(&:to_s)775 end776 end777778 def test_canel779 tmux.send_keys "seq 10 | #{fzf "--bind 2:cancel"}", :Enter780 tmux.until { |lines| lines[-2].include?('10/10') }781 tmux.send_keys '123'
319 unmodified lines
modified:CHANGELOG.md+10 added
1CHANGELOG2=========340.10.65------67- Replaced `--header-file` with `--header` option8- `--header` and `--header-lines` can be used together9- Changed exit status10 - 0: Okay11 - 1: No match12 - 2: Error/Interrupted13140.10.515------1617- `'`-prefix to unquote the term in `--extended-exact` mode
285 unmodified lines