summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorAkinori MUSHA <knu@idaemons.org>2018-06-10 01:41:50 +0900
committerJunegunn Choi <junegunn.c@gmail.com>2018-06-10 01:41:50 +0900
commitdaa1958f8602f91f6df6dac7a87c93da53aed5e4 (patch)
tree7bdf481a8f0424f7a86f7572fcb0a68f95285529 /src
parent2c26f02f5ce263820a339595517152b70ed63b80 (diff)
downloadfzf-daa1958f8602f91f6df6dac7a87c93da53aed5e4.tar.gz
Provide an option to reverse items only (#1267)
Diffstat (limited to 'src')
-rw-r--r--src/options.go40
-rw-r--r--src/terminal.go50
2 files changed, 70 insertions, 20 deletions
diff --git a/src/options.go b/src/options.go
index 45eca5ef..fc32344c 100644
--- a/src/options.go
+++ b/src/options.go
@@ -53,7 +53,7 @@ const usage = `usage: fzf [options]
height instead of using fullscreen
--min-height=HEIGHT Minimum height when --height is given in percent
(default: 10)
- --reverse Reverse orientation
+ --layout=LAYOUT Choose layout: [default|reverse|reverse-list]
--border Draw border above and below the finder
--margin=MARGIN Screen margin (TRBL / TB,RL / T,RL,B / T,R,B,L)
--inline-info Display finder info inline with the query
@@ -90,7 +90,8 @@ const usage = `usage: fzf [options]
Environment variables
FZF_DEFAULT_COMMAND Default command to use when input is tty
- FZF_DEFAULT_OPTS Default options (e.g. '--reverse --inline-info')
+ FZF_DEFAULT_OPTS Default options
+ (e.g. '--layout=reverse --inline-info')
`
@@ -132,6 +133,14 @@ const (
posRight
)
+type layoutType int
+
+const (
+ layoutDefault layoutType = iota
+ layoutReverse
+ layoutReverseList
+)
+
type previewOpts struct {
command string
position windowPosition
@@ -161,7 +170,7 @@ type Options struct {
Bold bool
Height sizeSpec
MinHeight int
- Reverse bool
+ Layout layoutType
Cycle bool
Hscroll bool
HscrollOff int
@@ -211,7 +220,7 @@ func defaultOptions() *Options {
Black: false,
Bold: true,
MinHeight: 10,
- Reverse: false,
+ Layout: layoutDefault,
Cycle: false,
Hscroll: true,
HscrollOff: 10,
@@ -857,6 +866,20 @@ func parseHeight(str string) sizeSpec {
return size
}
+func parseLayout(str string) layoutType {
+ switch str {
+ case "default":
+ return layoutDefault
+ case "reverse":
+ return layoutReverse
+ case "reverse-list":
+ return layoutReverseList
+ default:
+ errorExit("invalid layout (expected: default / reverse / reverse-list)")
+ }
+ return layoutDefault
+}
+
func parsePreviewWindow(opts *previewOpts, input string) {
// Default
opts.position = posRight
@@ -1037,10 +1060,13 @@ func parseOptions(opts *Options, allArgs []string) {
opts.Bold = true
case "--no-bold":
opts.Bold = false
+ case "--layout":
+ opts.Layout = parseLayout(
+ nextString(allArgs, &i, "layout required (default / reverse / reverse-list)"))
case "--reverse":
- opts.Reverse = true
+ opts.Layout = layoutReverse
case "--no-reverse":
- opts.Reverse = false
+ opts.Layout = layoutDefault
case "--cycle":
opts.Cycle = true
case "--no-cycle":
@@ -1156,6 +1182,8 @@ func parseOptions(opts *Options, allArgs []string) {
opts.Height = parseHeight(value)
} else if match, value := optString(arg, "--min-height="); match {
opts.MinHeight = atoi(value)
+ } else if match, value := optString(arg, "--layout="); match {
+ opts.Layout = parseLayout(value)
} else if match, value := optString(arg, "--toggle-sort="); match {
parseToggleSort(opts.Keymap, value)
} else if match, value := optString(arg, "--expect="); match {
diff --git a/src/terminal.go b/src/terminal.go
index 664b7d47..d4685042 100644
--- a/src/terminal.go
+++ b/src/terminal.go
@@ -59,7 +59,7 @@ type Terminal struct {
inlineInfo bool
prompt string
promptLen int
- reverse bool
+ layout layoutType
fullscreen bool
hscroll bool
hscrollOff int
@@ -302,10 +302,11 @@ func trimQuery(query string) []rune {
func NewTerminal(opts *Options, eventBox *util.EventBox) *Terminal {
input := trimQuery(opts.Query)
var header []string
- if opts.Reverse {
- header = opts.Header
- } else {
+ switch opts.Layout {
+ case layoutDefault, layoutReverseList:
header = reverseStringArray(opts.Header)
+ default:
+ header = opts.Header
}
var delay time.Duration
if opts.Tac {
@@ -363,7 +364,7 @@ func NewTerminal(opts *Options, eventBox *util.EventBox) *Terminal {
t := Terminal{
initDelay: delay,
inlineInfo: opts.InlineInfo,
- reverse: opts.Reverse,
+ layout: opts.Layout,
fullscreen: fullscreen,
hscroll: opts.Hscroll,
hscrollOff: opts.HscrollOff,
@@ -643,8 +644,21 @@ func (t *Terminal) resizeWindows() {
}
func (t *Terminal) move(y int, x int, clear bool) {
- if !t.reverse {
- y = t.window.Height() - y - 1
+ h := t.window.Height()
+
+ switch t.layout {
+ case layoutDefault:
+ y = h - y - 1
+ case layoutReverseList:
+ n := 2 + len(t.header)
+ if t.inlineInfo {
+ n--
+ }
+ if y < n {
+ y = h - y - 1
+ } else {
+ y -= n
+ }
}
if clear {
@@ -748,7 +762,7 @@ func (t *Terminal) printList() {
count := t.merger.Length() - t.offset
for j := 0; j < maxy; j++ {
i := j
- if !t.reverse {
+ if t.layout == layoutDefault {
i = maxy - 1 - j
}
line := i + 2 + len(t.header)
@@ -1680,12 +1694,12 @@ func (t *Terminal) Loop() {
req(reqList, reqInfo)
}
case actToggleIn:
- if t.reverse {
+ if t.layout != layoutDefault {
return doAction(action{t: actToggleUp}, mapkey)
}
return doAction(action{t: actToggleDown}, mapkey)
case actToggleOut:
- if t.reverse {
+ if t.layout != layoutDefault {
return doAction(action{t: actToggleDown}, mapkey)
}
return doAction(action{t: actToggleUp}, mapkey)
@@ -1813,13 +1827,21 @@ func (t *Terminal) Loop() {
mx -= t.window.Left()
my -= t.window.Top()
mx = util.Constrain(mx-t.promptLen, 0, len(t.input))
- if !t.reverse {
- my = t.window.Height() - my - 1
- }
min := 2 + len(t.header)
if t.inlineInfo {
min--
}
+ h := t.window.Height()
+ switch t.layout {
+ case layoutDefault:
+ my = h - my - 1
+ case layoutReverseList:
+ if my < h-min {
+ my += min
+ } else {
+ my = h - my - 1
+ }
+ }
if me.Double {
// Double-click
if my >= min {
@@ -1912,7 +1934,7 @@ func (t *Terminal) constrain() {
}
func (t *Terminal) vmove(o int, allowCycle bool) {
- if t.reverse {
+ if t.layout != layoutDefault {
o *= -1
}
dest := t.cy + o