summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorJunegunn Choi <junegunn.c@gmail.com>2015-09-15 19:04:53 +0900
committerJunegunn Choi <junegunn.c@gmail.com>2015-09-15 19:04:53 +0900
commit2022a3ad96f027e056e4fcce11fee0976db657d1 (patch)
treeea0d8c2f34c38577b994971c7a186760223210c9 /src
parent65d9d416b4300e85304fd158d9df2f6272590849 (diff)
downloadfzf-2022a3ad96f027e056e4fcce11fee0976db657d1.tar.gz
Replace --header-file with --header (#346)
and allow using --header and --header-lines at the same time. Close #346.
Diffstat (limited to 'src')
-rw-r--r--src/core.go2
-rw-r--r--src/options.go26
-rw-r--r--src/terminal.go34
3 files changed, 29 insertions, 33 deletions
diff --git a/src/core.go b/src/core.go
index 04b6eab7..35d7cedb 100644
--- a/src/core.go
+++ b/src/core.go
@@ -238,7 +238,7 @@ func Run(opts *Options) {
}
case EvtHeader:
- terminal.UpdateHeader(value.([]string), opts.HeaderLines)
+ terminal.UpdateHeader(value.([]string))
case EvtSearchFin:
switch val := value.(type) {
diff --git a/src/options.go b/src/options.go
index 47d8bb11..9d8aaa15 100644
--- a/src/options.go
+++ b/src/options.go
@@ -1,7 +1,6 @@
package fzf
import (
- "io/ioutil"
"os"
"regexp"
"strconv"
@@ -45,7 +44,7 @@ const usage = `usage: fzf [options]
--bind=KEYBINDS Custom key bindings. Refer to the man page.
--history=FILE History file
--history-size=N Maximum number of history entries (default: 1000)
- --header-file=FILE The file whose content to be printed as header
+ --header=STR String to print as header
--header-lines=N The first N lines of the input are treated as header
Scripting
@@ -604,12 +603,8 @@ func checkToggleSort(keymap map[int]actionType, str string) map[int]actionType {
return keymap
}
-func readHeaderFile(filename string) []string {
- content, err := ioutil.ReadFile(filename)
- if err != nil {
- errorExit("failed to read header file: " + filename)
- }
- return strings.Split(strings.TrimSuffix(string(content), "\n"), "\n")
+func strLines(str string) []string {
+ return strings.Split(strings.TrimSuffix(str, "\n"), "\n")
}
func parseMargin(margin string) [4]string {
@@ -793,16 +788,13 @@ func parseOptions(opts *Options, allArgs []string) {
setHistory(nextString(allArgs, &i, "history file path required"))
case "--history-size":
setHistoryMax(nextInt(allArgs, &i, "history max size required"))
- case "--no-header-file":
+ case "--no-header":
opts.Header = []string{}
case "--no-header-lines":
opts.HeaderLines = 0
- case "--header-file":
- opts.Header = readHeaderFile(
- nextString(allArgs, &i, "header file name required"))
- opts.HeaderLines = 0
+ case "--header":
+ opts.Header = strLines(nextString(allArgs, &i, "header string required"))
case "--header-lines":
- opts.Header = []string{}
opts.HeaderLines = atoi(
nextString(allArgs, &i, "number of header lines required"))
case "--no-margin":
@@ -843,11 +835,9 @@ func parseOptions(opts *Options, allArgs []string) {
setHistory(value)
} else if match, value := optString(arg, "--history-size="); match {
setHistoryMax(atoi(value))
- } else if match, value := optString(arg, "--header-file="); match {
- opts.Header = readHeaderFile(value)
- opts.HeaderLines = 0
+ } else if match, value := optString(arg, "--header="); match {
+ opts.Header = strLines(value)
} else if match, value := optString(arg, "--header-lines="); match {
- opts.Header = []string{}
opts.HeaderLines = atoi(value)
} else if match, value := optString(arg, "--margin="); match {
opts.Margin = parseMargin(value)
diff --git a/src/terminal.go b/src/terminal.go
index c3fb966b..5e8300f9 100644
--- a/src/terminal.go
+++ b/src/terminal.go
@@ -42,6 +42,7 @@ type Terminal struct {
history *History
cycle bool
header []string
+ header0 []string
ansi bool
margin [4]string
marginInt [4]int
@@ -185,6 +186,12 @@ func defaultKeymap() map[int]actionType {
// NewTerminal returns new Terminal object
func NewTerminal(opts *Options, eventBox *util.EventBox) *Terminal {
input := []rune(opts.Query)
+ var header []string
+ if opts.Reverse {
+ header = opts.Header
+ } else {
+ header = reverseStringArray(opts.Header)
+ }
return &Terminal{
inlineInfo: opts.InlineInfo,
prompt: opts.Prompt,
@@ -207,7 +214,8 @@ func NewTerminal(opts *Options, eventBox *util.EventBox) *Terminal {
margin: opts.Margin,
marginInt: [4]int{0, 0, 0, 0},
cycle: opts.Cycle,
- header: opts.Header,
+ header: header,
+ header0: header,
ansi: opts.Ansi,
reading: true,
merger: EmptyMerger,
@@ -241,18 +249,19 @@ func (t *Terminal) UpdateCount(cnt int, final bool) {
}
}
+func reverseStringArray(input []string) []string {
+ size := len(input)
+ reversed := make([]string, size)
+ for idx, str := range input {
+ reversed[size-idx-1] = str
+ }
+ return reversed
+}
+
// UpdateHeader updates the header
-func (t *Terminal) UpdateHeader(header []string, lines int) {
+func (t *Terminal) UpdateHeader(header []string) {
t.mutex.Lock()
- t.header = make([]string, lines)
- copy(t.header, header)
- if !t.reverse {
- reversed := make([]string, lines)
- for idx, str := range t.header {
- reversed[lines-idx-1] = str
- }
- t.header = reversed
- }
+ t.header = append(append([]string{}, t.header0...), header...)
t.mutex.Unlock()
t.reqBox.Set(reqHeader, nil)
}
@@ -436,9 +445,6 @@ func (t *Terminal) printHeader() {
max := t.maxHeight()
var state *ansiState
for idx, lineStr := range t.header {
- if !t.reverse {
- idx = len(t.header) - idx - 1
- }
line := idx + 2
if t.inlineInfo {
line--