Skip to content

Commit

Permalink
Add --keep-right option to keep the right end of the line visible
Browse files Browse the repository at this point in the history
  • Loading branch information
junegunn committed Mar 11, 2020
1 parent b8fc828 commit 373c6d8
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 6 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ CHANGELOG
- Added `--pointer` and `--marker` options
- `--height` option is now available on Windows binary (@kelleyma49)
- 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
- Vim plugin: Floating windows support
- bash: Various improvements in key bindings (CTRL-T, CTRL-R, ALT-C)
Expand Down
4 changes: 4 additions & 0 deletions man/man1/fzf.1
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,10 @@ the details.
.B "--cycle"
Enable cyclic scroll
.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"
Disable horizontal scroll
.TP
Expand Down
7 changes: 7 additions & 0 deletions src/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ const usage = `usage: fzf [options]
--no-mouse Disable mouse
--bind=KEYBINDS Custom key bindings. Refer to the man page.
--cycle Enable cyclic scroll
--keep-right Keep the right end of the line visible on overflow
--no-hscroll Disable horizontal scroll
--hscroll-off=COL Number of screen columns to keep to the right of the
highlighted substring (default: 10)
Expand Down Expand Up @@ -187,6 +188,7 @@ type Options struct {
MinHeight int
Layout layoutType
Cycle bool
KeepRight bool
Hscroll bool
HscrollOff int
FileWord bool
Expand Down Expand Up @@ -242,6 +244,7 @@ func defaultOptions() *Options {
MinHeight: 10,
Layout: layoutDefault,
Cycle: false,
KeepRight: false,
Hscroll: true,
HscrollOff: 10,
FileWord: false,
Expand Down Expand Up @@ -1174,6 +1177,10 @@ func parseOptions(opts *Options, allArgs []string) {
opts.Cycle = true
case "--no-cycle":
opts.Cycle = false
case "--keep-right":
opts.KeepRight = true
case "--no-keep-right":
opts.KeepRight = false
case "--hscroll":
opts.Hscroll = true
case "--no-hscroll":
Expand Down
19 changes: 13 additions & 6 deletions src/terminal.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ import (
var placeholder *regexp.Regexp
var activeTempFiles []string

const ellipsis string = ".."

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

for idx, offset := range offsets {
offsets[idx].offset[0] = util.Min32(offset.offset[0], int32(maxWidth-2))
Expand Down
5 changes: 5 additions & 0 deletions test/test_go.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1728,6 +1728,11 @@ def test_strip_xterm_osc_sequence
tmux.send_keys :Enter
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

module TestShell
Expand Down

0 comments on commit 373c6d8

Please sign in to comment.