diff options
-rw-r--r-- | man/man1/fzf.1 | 3 | ||||
-rw-r--r-- | src/options.go | 7 | ||||
-rw-r--r-- | src/terminal.go | 52 | ||||
-rw-r--r-- | src/util/util.go | 8 |
4 files changed, 50 insertions, 20 deletions
diff --git a/man/man1/fzf.1 b/man/man1/fzf.1 index d317adb1..0afa4cd9 100644 --- a/man/man1/fzf.1 +++ b/man/man1/fzf.1 @@ -89,6 +89,9 @@ Use black background .B "--reverse" Reverse orientation .TP +.B "--no-hscroll" +Disable horizontal scroll +.TP .BI "--prompt=" "STR" Input prompt (default: '> ') .SS Scripting diff --git a/src/options.go b/src/options.go index fcf30979..c186542d 100644 --- a/src/options.go +++ b/src/options.go @@ -38,6 +38,7 @@ const usage = `usage: fzf [options] +2, --no-256 Disable 256-color --black Use black background --reverse Reverse orientation + --no-hscroll Disable horizontal scroll --prompt=STR Input prompt (default: '> ') Scripting @@ -93,6 +94,7 @@ type Options struct { Color256 bool Black bool Reverse bool + Hscroll bool Prompt string Query string Select1 bool @@ -121,6 +123,7 @@ func defaultOptions() *Options { Color256: strings.Contains(os.Getenv("TERM"), "256"), Black: false, Reverse: false, + Hscroll: true, Prompt: "> ", Query: "", Select1: false, @@ -304,6 +307,10 @@ func parseOptions(opts *Options, allArgs []string) { opts.Reverse = true case "--no-reverse": opts.Reverse = false + case "--hscroll": + opts.Hscroll = true + case "--no-hscroll": + opts.Hscroll = false case "-1", "--select-1": opts.Select1 = true case "+1", "--no-select-1": diff --git a/src/terminal.go b/src/terminal.go index d027d761..b16daeca 100644 --- a/src/terminal.go +++ b/src/terminal.go @@ -22,6 +22,7 @@ import ( type Terminal struct { prompt string reverse bool + hscroll bool cx int cy int offset int @@ -88,6 +89,7 @@ func NewTerminal(opts *Options, eventBox *util.EventBox) *Terminal { return &Terminal{ prompt: opts.Prompt, reverse: opts.Reverse, + hscroll: opts.Hscroll, cx: len(input), cy: 0, offset: 0, @@ -318,7 +320,7 @@ func trimLeft(runes []rune, width int) ([]rune, int32) { return runes, trimmed } -func (*Terminal) printHighlighted(item *Item, bold bool, col1 int, col2 int, current bool) { +func (t *Terminal) printHighlighted(item *Item, bold bool, col1 int, col2 int, current bool) { var maxe int32 for _, offset := range item.offsets { if offset[1] > maxe { @@ -332,30 +334,40 @@ func (*Terminal) printHighlighted(item *Item, bold bool, col1 int, col2 int, cur maxWidth := C.MaxX() - 3 fullWidth := displayWidth(text) if fullWidth > maxWidth { - // Stri.. - matchEndWidth := displayWidth(text[:maxe]) - if matchEndWidth <= maxWidth-2 { - text, _ = trimRight(text, maxWidth-2) - text = append(text, []rune("..")...) - } else { + if t.hscroll { // Stri.. - if matchEndWidth < fullWidth-2 { - text = append(text[:maxe], []rune("..")...) + matchEndWidth := displayWidth(text[:maxe]) + if matchEndWidth <= maxWidth-2 { + text, _ = trimRight(text, maxWidth-2) + text = append(text, []rune("..")...) + } else { + // Stri.. + if matchEndWidth < fullWidth-2 { + text = append(text[:maxe], []rune("..")...) + } + // ..ri.. + var diff int32 + text, diff = trimLeft(text, maxWidth-2) + + // Transform offsets + for idx, offset := range offsets { + b, e := offset.offset[0], offset.offset[1] + b += 2 - diff + e += 2 - diff + b = util.Max32(b, 2) + offsets[idx].offset[0] = b + offsets[idx].offset[1] = util.Max32(b, e) + } + text = append([]rune(".."), text...) } - // ..ri.. - var diff int32 - text, diff = trimLeft(text, maxWidth-2) + } else { + text, _ = trimRight(text, maxWidth-2) + text = append(text, []rune("..")...) - // Transform offsets for idx, offset := range offsets { - b, e := offset.offset[0], offset.offset[1] - b += 2 - diff - e += 2 - diff - b = util.Max32(b, 2) - offsets[idx].offset[0] = b - offsets[idx].offset[1] = util.Max32(b, e) + offsets[idx].offset[0] = util.Min32(offset.offset[0], int32(maxWidth-2)) + offsets[idx].offset[1] = util.Min32(offset.offset[1], int32(maxWidth)) } - text = append([]rune(".."), text...) } } diff --git a/src/util/util.go b/src/util/util.go index 2d680b1a..931b14a8 100644 --- a/src/util/util.go +++ b/src/util/util.go @@ -19,6 +19,14 @@ func Max(first int, items ...int) int { return max } +// Max32 returns the smallest 32-bit integer +func Min32(first int32, second int32) int32 { + if first <= second { + return first + } + return second +} + // Max32 returns the largest 32-bit integer func Max32(first int32, second int32) int32 { if first > second { |