Add --keep-right option to keep the right end of the line visible

Close #1652
This commit is contained in:
Junegunn Choi 2020-03-11 22:35:24 +09:00
parent b8fc828955
commit 373c6d8d55
No known key found for this signature in database
GPG Key ID: 254BC280FEF9C627
5 changed files with 31 additions and 6 deletions

View File

@ -11,6 +11,8 @@ CHANGELOG
- Added `--pointer` and `--marker` options - Added `--pointer` and `--marker` options
- `--height` option is now available on Windows binary (@kelleyma49) - `--height` option is now available on Windows binary (@kelleyma49)
- More keys and actions for `--bind` - More keys and actions for `--bind`
- Added `--keep-right` option that keeps the right end of the line visible
when it's too long
- Bug fixes and improvements - Bug fixes and improvements
- Vim plugin: Floating windows support - Vim plugin: Floating windows support
- bash: Various improvements in key bindings (CTRL-T, CTRL-R, ALT-C) - bash: Various improvements in key bindings (CTRL-T, CTRL-R, ALT-C)

View File

@ -130,6 +130,10 @@ the details.
.B "--cycle" .B "--cycle"
Enable cyclic scroll Enable cyclic scroll
.TP .TP
.B "--keep-right"
Keep the right end of the line visible when it's too long. Effective only when
the query string is empty.
.TP
.B "--no-hscroll" .B "--no-hscroll"
Disable horizontal scroll Disable horizontal scroll
.TP .TP

View File

@ -44,6 +44,7 @@ const usage = `usage: fzf [options]
--no-mouse Disable mouse --no-mouse Disable mouse
--bind=KEYBINDS Custom key bindings. Refer to the man page. --bind=KEYBINDS Custom key bindings. Refer to the man page.
--cycle Enable cyclic scroll --cycle Enable cyclic scroll
--keep-right Keep the right end of the line visible on overflow
--no-hscroll Disable horizontal scroll --no-hscroll Disable horizontal scroll
--hscroll-off=COL Number of screen columns to keep to the right of the --hscroll-off=COL Number of screen columns to keep to the right of the
highlighted substring (default: 10) highlighted substring (default: 10)
@ -187,6 +188,7 @@ type Options struct {
MinHeight int MinHeight int
Layout layoutType Layout layoutType
Cycle bool Cycle bool
KeepRight bool
Hscroll bool Hscroll bool
HscrollOff int HscrollOff int
FileWord bool FileWord bool
@ -242,6 +244,7 @@ func defaultOptions() *Options {
MinHeight: 10, MinHeight: 10,
Layout: layoutDefault, Layout: layoutDefault,
Cycle: false, Cycle: false,
KeepRight: false,
Hscroll: true, Hscroll: true,
HscrollOff: 10, HscrollOff: 10,
FileWord: false, FileWord: false,
@ -1174,6 +1177,10 @@ func parseOptions(opts *Options, allArgs []string) {
opts.Cycle = true opts.Cycle = true
case "--no-cycle": case "--no-cycle":
opts.Cycle = false opts.Cycle = false
case "--keep-right":
opts.KeepRight = true
case "--no-keep-right":
opts.KeepRight = false
case "--hscroll": case "--hscroll":
opts.Hscroll = true opts.Hscroll = true
case "--no-hscroll": case "--no-hscroll":

View File

@ -25,6 +25,8 @@ import (
var placeholder *regexp.Regexp var placeholder *regexp.Regexp
var activeTempFiles []string var activeTempFiles []string
const ellipsis string = ".."
func init() { func init() {
placeholder = regexp.MustCompile(`\\?(?:{[+sf]*[0-9,-.]*}|{q}|{\+?f?nf?})`) placeholder = regexp.MustCompile(`\\?(?:{[+sf]*[0-9,-.]*}|{q}|{\+?f?nf?})`)
activeTempFiles = []string{} activeTempFiles = []string{}
@ -73,6 +75,7 @@ type Terminal struct {
queryLen [2]int queryLen [2]int
layout layoutType layout layoutType
fullscreen bool fullscreen bool
keepRight bool
hscroll bool hscroll bool
hscrollOff int hscrollOff int
wordRubout string wordRubout string
@ -397,6 +400,7 @@ func NewTerminal(opts *Options, eventBox *util.EventBox) *Terminal {
queryLen: [2]int{0, 0}, queryLen: [2]int{0, 0},
layout: opts.Layout, layout: opts.Layout,
fullscreen: fullscreen, fullscreen: fullscreen,
keepRight: opts.KeepRight,
hscroll: opts.Hscroll, hscroll: opts.Hscroll,
hscrollOff: opts.HscrollOff, hscrollOff: opts.HscrollOff,
wordRubout: wordRubout, wordRubout: wordRubout,
@ -1000,14 +1004,17 @@ func (t *Terminal) printHighlighted(result Result, attr tui.Attr, col1 tui.Color
displayWidth := t.displayWidthWithLimit(text, 0, maxWidth) displayWidth := t.displayWidthWithLimit(text, 0, maxWidth)
if displayWidth > maxWidth { if displayWidth > maxWidth {
if t.hscroll { if t.hscroll {
// Stri.. if t.keepRight && pos == nil {
if !t.overflow(text[:maxe], maxWidth-2) { text, _ = t.trimLeft(text, maxWidth-2)
text = append([]rune(ellipsis), text...)
} else if !t.overflow(text[:maxe], maxWidth-2) {
// Stri..
text, _ = t.trimRight(text, maxWidth-2) text, _ = t.trimRight(text, maxWidth-2)
text = append(text, []rune("..")...) text = append(text, []rune(ellipsis)...)
} else { } else {
// Stri.. // Stri..
if t.overflow(text[maxe:], 2) { if t.overflow(text[maxe:], 2) {
text = append(text[:maxe], []rune("..")...) text = append(text[:maxe], []rune(ellipsis)...)
} }
// ..ri.. // ..ri..
var diff int32 var diff int32
@ -1022,11 +1029,11 @@ func (t *Terminal) printHighlighted(result Result, attr tui.Attr, col1 tui.Color
offsets[idx].offset[0] = b offsets[idx].offset[0] = b
offsets[idx].offset[1] = util.Max32(b, e) offsets[idx].offset[1] = util.Max32(b, e)
} }
text = append([]rune(".."), text...) text = append([]rune(ellipsis), text...)
} }
} else { } else {
text, _ = t.trimRight(text, maxWidth-2) text, _ = t.trimRight(text, maxWidth-2)
text = append(text, []rune("..")...) text = append(text, []rune(ellipsis)...)
for idx, offset := range offsets { for idx, offset := range offsets {
offsets[idx].offset[0] = util.Min32(offset.offset[0], int32(maxWidth-2)) offsets[idx].offset[0] = util.Min32(offset.offset[0], int32(maxWidth-2))

View File

@ -1728,6 +1728,11 @@ class TestGoFZF < TestBase
tmux.send_keys :Enter tmux.send_keys :Enter
end end
end end
def test_keep_right
tmux.send_keys("seq 10000 | #{FZF} --read0 --keep-right", :Enter)
tmux.until { |lines| lines.any_include?('9999 10000') }
end
end end
module TestShell module TestShell