Skip to content

Commit

Permalink
Add --preview-window option for cyclic scrolling
Browse files Browse the repository at this point in the history
  • Loading branch information
junegunn committed Oct 6, 2020
1 parent c0aa5a4 commit 3cc8a74
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 7 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ CHANGELOG
--preview-window +{2}-/2
```
- Added `--preview-window` option for sharp edges (`--preview-window sharp`)
- Added `--preview-window` option for cyclic scrolling (`--preview-window cycle`)
- Reduced vertical padding around the preview window when `--preview-window
noborder` is used
- Added actions for preview window
Expand Down
11 changes: 8 additions & 3 deletions src/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,8 @@ const usage = `usage: fzf [options]
Preview
--preview=COMMAND Command to preview highlighted line ({})
--preview-window=OPT Preview window layout (default: right:50%)
[up|down|left|right][:SIZE[%]][:wrap][:hidden][:+SCROLL[-OFFSET]]
[up|down|left|right][:SIZE[%]][:wrap][:cycle][:hidden]
[:+SCROLL[-OFFSET]]
[:rounded|sharp|noborder]
Scripting
Expand Down Expand Up @@ -163,6 +164,7 @@ type previewOpts struct {
scroll string
hidden bool
wrap bool
cycle bool
border tui.BorderShape
}

Expand Down Expand Up @@ -262,7 +264,7 @@ func defaultOptions() *Options {
ToggleSort: false,
Expect: make(map[int]string),
Keymap: make(map[int][]action),
Preview: previewOpts{"", posRight, sizeSpec{50, true}, "", false, false, tui.BorderRounded},
Preview: previewOpts{"", posRight, sizeSpec{50, true}, "", false, false, false, tui.BorderRounded},
PrintQuery: false,
ReadZero: false,
Printer: func(str string) { fmt.Println(str) },
Expand Down Expand Up @@ -997,6 +999,7 @@ func parsePreviewWindow(opts *previewOpts, input string) {
opts.size = sizeSpec{50, true}
opts.hidden = false
opts.wrap = false
opts.cycle = false

tokens := strings.Split(input, ":")
sizeRegex := regexp.MustCompile("^[0-9]+%?$")
Expand All @@ -1008,6 +1011,8 @@ func parsePreviewWindow(opts *previewOpts, input string) {
opts.hidden = true
case "wrap":
opts.wrap = true
case "cycle":
opts.cycle = true
case "up", "top":
opts.position = posUp
case "down", "bottom":
Expand Down Expand Up @@ -1281,7 +1286,7 @@ func parseOptions(opts *Options, allArgs []string) {
opts.Preview.command = ""
case "--preview-window":
parsePreviewWindow(&opts.Preview,
nextString(allArgs, &i, "preview window layout required: [up|down|left|right][:SIZE[%]][:rounded|sharp|noborder][:wrap][:hidden][:+SCROLL[-OFFSET]]"))
nextString(allArgs, &i, "preview window layout required: [up|down|left|right][:SIZE[%]][:rounded|sharp|noborder][:wrap][:cycle][:hidden][:+SCROLL[-OFFSET]]"))
case "--height":
opts.Height = parseHeight(nextString(allArgs, &i, "height required: HEIGHT[%]"))
case "--min-height":
Expand Down
11 changes: 7 additions & 4 deletions src/terminal.go
Original file line number Diff line number Diff line change
Expand Up @@ -1884,8 +1884,11 @@ func (t *Terminal) Loop() {
if !t.previewer.more {
return
}
newOffset := util.Constrain(
t.previewer.offset+amount, 0, t.previewer.lines-1)
newOffset := t.previewer.offset + amount
if t.preview.cycle {
newOffset = (newOffset + t.previewer.lines) % t.previewer.lines
}
newOffset = util.Constrain(newOffset, 0, t.previewer.lines-1)
if t.previewer.offset != newOffset {
t.previewer.offset = newOffset
req(reqPreviewRefresh)
Expand Down Expand Up @@ -1957,11 +1960,11 @@ func (t *Terminal) Loop() {
}
case actPreviewHalfPageUp:
if t.hasPreviewWindow() {
scrollPreview(-t.pwindow.Height()/2)
scrollPreview(-t.pwindow.Height() / 2)
}
case actPreviewHalfPageDown:
if t.hasPreviewWindow() {
scrollPreview(t.pwindow.Height()/2)
scrollPreview(t.pwindow.Height() / 2)
}
case actBeginningOfLine:
t.cx = 0
Expand Down

0 comments on commit 3cc8a74

Please sign in to comment.