Add more preview window options and reduce vertical padding on noborder

Fix #2138
Fix #2029
This commit is contained in:
Junegunn Choi 2020-08-23 17:05:45 +09:00
parent 1cb19dbf65
commit 9dc4b40d7a
No known key found for this signature in database
GPG Key ID: 254BC280FEF9C627
4 changed files with 38 additions and 14 deletions

View File

@ -10,6 +10,9 @@ CHANGELOG
--preview 'bat --style=numbers --color=always --highlight-line {2} {1}' \
--preview-window +{2}-/2
```
- Added `--preview-window` option for sharp edges (`--preview-window sharp`)
- Reduced vertical padding around the preview window when `--preview-window
noborder` is used
0.22.0
------

View File

@ -381,7 +381,7 @@ Preview window will be updated even when there is no match for the current
query if any of the placeholder expressions evaluates to a non-empty string.
.RE
.TP
.BI "--preview-window=" "[POSITION][:SIZE[%]][:noborder][:wrap][:hidden][:+SCROLL[-OFFSET]]"
.BI "--preview-window=" "[POSITION][:SIZE[%]][:rounded|sharp|noborder][:wrap][:hidden][:+SCROLL[-OFFSET]]"
Determines the layout of the preview window. If the argument contains
\fB:hidden\fR, the preview window will be hidden by default until
\fBtoggle-preview\fR action is triggered. Long lines are truncated by default.
@ -390,6 +390,10 @@ Line wrap can be enabled with \fB:wrap\fR flag.
If size is given as 0, preview window will not be visible, but fzf will still
execute the command in the background.
To change the style of the border of the preview window, specify one of
\fBrounded\fR (border with rounded edges, default), \fBsharp\fR (border with
sharp edges), or \fBnoborder\fR (no border).
\fB+SCROLL[-OFFSET]\fR determines the initial scroll offset of the preview
window. \fBSCROLL\fR can be either a numeric integer or a single-field index
expression that refers to a numeric integer. The optional \fB-OFFSET\fR part is

View File

@ -81,6 +81,7 @@ const usage = `usage: fzf [options]
--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]]
[:rounded|sharp|noborder]
Scripting
-q, --query=STR Start the finder with the given query
@ -162,7 +163,7 @@ type previewOpts struct {
scroll string
hidden bool
wrap bool
border bool
border tui.BorderShape
}
// Options stores the values of command-line options
@ -261,7 +262,7 @@ func defaultOptions() *Options {
ToggleSort: false,
Expect: make(map[int]string),
Keymap: make(map[int][]action),
Preview: previewOpts{"", posRight, sizeSpec{50, true}, "", false, false, true},
Preview: previewOpts{"", posRight, sizeSpec{50, true}, "", false, false, tui.BorderRounded},
PrintQuery: false,
ReadZero: false,
Printer: func(str string) { fmt.Println(str) },
@ -1011,10 +1012,12 @@ func parsePreviewWindow(opts *previewOpts, input string) {
opts.position = posLeft
case "right":
opts.position = posRight
case "border":
opts.border = true
case "rounded", "border":
opts.border = tui.BorderRounded
case "sharp":
opts.border = tui.BorderSharp
case "noborder":
opts.border = false
opts.border = tui.BorderNone
default:
if sizeRegex.MatchString(token) {
opts.size = parseSize(token, 99, "window size")
@ -1274,7 +1277,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[%]][:noborder][:wrap][:hidden][:+SCROLL[-OFFSET]]"))
nextString(allArgs, &i, "preview window layout required: [up|down|left|right][:SIZE[%]][:rounded|sharp|noborder][:wrap][:hidden][:+SCROLL[-OFFSET]]"))
case "--height":
opts.Height = parseHeight(nextString(allArgs, &i, "height required: HEIGHT[%]"))
case "--min-height":

View File

@ -676,6 +676,8 @@ func (t *Terminal) resizeWindows() {
}
if t.pborder != nil {
t.pborder.Close()
}
if t.pwindow != nil {
t.pwindow.Close()
}
@ -700,19 +702,28 @@ func (t *Terminal) resizeWindows() {
noBorder := tui.MakeBorderStyle(tui.BorderNone, t.unicode)
if previewVisible {
createPreviewWindow := func(y int, x int, w int, h int) {
previewBorder := tui.MakeBorderStyle(tui.BorderRounded, t.unicode)
if !t.preview.border {
previewBorder = tui.MakeTransparentBorder()
pwidth := w
pheight := h
if t.preview.border != tui.BorderNone {
previewBorder := tui.MakeBorderStyle(t.preview.border, t.unicode)
t.pborder = t.tui.NewWindow(y, x, w, h, true, previewBorder)
pwidth -= 4
pheight -= 2
x += 2
y += 1
} else {
previewBorder := tui.MakeTransparentBorder()
t.pborder = t.tui.NewWindow(y, x, w, h, true, previewBorder)
pwidth -= 2
x += 1
}
t.pborder = t.tui.NewWindow(y, x, w, h, true, previewBorder)
pwidth := w - 4
// ncurses auto-wraps the line when the cursor reaches the right-end of
// the window. To prevent unintended line-wraps, we use the width one
// column larger than the desired value.
if !t.preview.wrap && t.tui.DoesAutoWrap() {
pwidth += 1
}
t.pwindow = t.tui.NewWindow(y+1, x+2, pwidth, h-2, true, noBorder)
t.pwindow = t.tui.NewWindow(y, x, pwidth, pheight, true, noBorder)
}
switch t.preview.position {
case posUp:
@ -1210,7 +1221,10 @@ func (t *Terminal) refresh() {
windows = append(windows, t.border)
}
if t.hasPreviewWindow() {
windows = append(windows, t.pborder, t.pwindow)
if t.pborder != nil {
windows = append(windows, t.pborder)
}
windows = append(windows, t.pwindow)
}
windows = append(windows, t.window)
t.tui.RefreshWindows(windows)