Refactor --preview-border=line

This commit is contained in:
Junegunn Choi 2025-01-11 19:34:13 +09:00
parent 9030b67e4f
commit 4fdc07927f
No known key found for this signature in database
GPG Key ID: 254BC280FEF9C627
2 changed files with 30 additions and 35 deletions

View File

@ -332,12 +332,21 @@ func (o *previewOpts) Toggle() {
o.hidden = !o.hidden o.hidden = !o.hidden
} }
func (o *previewOpts) HasBorderRight() bool { func (o *previewOpts) Border() tui.BorderShape {
return o.border.HasRight() || o.border == tui.BorderLine && o.position == posLeft shape := o.border
if shape == tui.BorderLine {
switch o.position {
case posUp:
shape = tui.BorderBottom
case posDown:
shape = tui.BorderTop
case posLeft:
shape = tui.BorderRight
case posRight:
shape = tui.BorderLeft
} }
}
func (o *previewOpts) HasBorderTop() bool { return shape
return o.border.HasTop() || o.border == tui.BorderLine && o.position == posDown
} }
func defaultTmuxOptions(index int) *tmuxOptions { func defaultTmuxOptions(index int) *tmuxOptions {

View File

@ -782,7 +782,7 @@ func NewTerminal(opts *Options, eventBox *util.EventBox, executor *util.Executor
// Minimum height required to render fzf excluding margin and padding // Minimum height required to render fzf excluding margin and padding
effectiveMinHeight := minHeight effectiveMinHeight := minHeight
if previewBox != nil && opts.Preview.aboveOrBelow() { if previewBox != nil && opts.Preview.aboveOrBelow() {
effectiveMinHeight += 1 + borderLines(opts.Preview.border) effectiveMinHeight += 1 + borderLines(opts.Preview.Border())
} }
if noSeparatorLine(opts.InfoStyle, opts.Separator == nil || uniseg.StringWidth(*opts.Separator) > 0) { if noSeparatorLine(opts.InfoStyle, opts.Separator == nil || uniseg.StringWidth(*opts.Separator) > 0) {
effectiveMinHeight-- effectiveMinHeight--
@ -1521,12 +1521,12 @@ func calculateSize(base int, size sizeSpec, occupied int, minSize int) int {
} }
func (t *Terminal) minPreviewSize(opts *previewOpts) (int, int) { func (t *Terminal) minPreviewSize(opts *previewOpts) (int, int) {
minPreviewWidth := 1 + borderColumns(opts.border, t.borderWidth) minPreviewWidth := 1 + borderColumns(opts.Border(), t.borderWidth)
minPreviewHeight := 1 + borderLines(opts.border) minPreviewHeight := 1 + borderLines(opts.Border())
switch opts.position { switch opts.position {
case posLeft, posRight: case posLeft, posRight:
if len(t.scrollbar) > 0 && !opts.HasBorderRight() { if len(t.scrollbar) > 0 && !opts.Border().HasRight() {
// Need a column to show scrollbar // Need a column to show scrollbar
minPreviewWidth++ minPreviewWidth++
} }
@ -1773,19 +1773,7 @@ func (t *Terminal) resizeWindows(forcePreview bool, redrawBorder bool) {
createPreviewWindow := func(y int, x int, w int, h int) { createPreviewWindow := func(y int, x int, w int, h int) {
pwidth := w pwidth := w
pheight := h pheight := h
shape := previewOpts.border shape := previewOpts.Border()
if shape == tui.BorderLine {
switch previewOpts.position {
case posUp:
shape = tui.BorderBottom
case posDown:
shape = tui.BorderTop
case posLeft:
shape = tui.BorderRight
case posRight:
shape = tui.BorderLeft
}
}
previewBorder := tui.MakeBorderStyle(shape, t.unicode) previewBorder := tui.MakeBorderStyle(shape, t.unicode)
t.pborder = t.tui.NewWindow(y, x, w, h, tui.WindowPreview, previewBorder, false) t.pborder = t.tui.NewWindow(y, x, w, h, tui.WindowPreview, previewBorder, false)
pwidth -= borderColumns(shape, bw) pwidth -= borderColumns(shape, bw)
@ -1830,7 +1818,7 @@ func (t *Terminal) resizeWindows(forcePreview bool, redrawBorder bool) {
return return
} }
listStickToRight = listStickToRight && !previewOpts.HasBorderRight() listStickToRight = listStickToRight && !previewOpts.Border().HasRight()
if listStickToRight { if listStickToRight {
innerWidth++ innerWidth++
width++ width++
@ -1908,7 +1896,7 @@ func (t *Terminal) resizeWindows(forcePreview bool, redrawBorder bool) {
// fzf --preview 'seq 500' --preview-window border-left --border // fzf --preview 'seq 500' --preview-window border-left --border
// fzf --preview 'seq 500' --preview-window border-left --border --list-border // fzf --preview 'seq 500' --preview-window border-left --border --list-border
// fzf --preview 'seq 500' --preview-window border-left --border --input-border // fzf --preview 'seq 500' --preview-window border-left --border --input-border
listStickToRight = t.borderShape.HasRight() && !previewOpts.HasBorderRight() listStickToRight = t.borderShape.HasRight() && !previewOpts.Border().HasRight()
if listStickToRight { if listStickToRight {
innerWidth++ innerWidth++
width++ width++
@ -2031,9 +2019,7 @@ func (t *Terminal) resizeWindows(forcePreview bool, redrawBorder bool) {
// Print border label // Print border label
t.printLabel(t.wborder, t.listLabel, t.listLabelOpts, t.listLabelLen, t.listBorderShape, false) t.printLabel(t.wborder, t.listLabel, t.listLabelOpts, t.listLabelLen, t.listBorderShape, false)
t.printLabel(t.border, t.borderLabel, t.borderLabelOpts, t.borderLabelLen, t.borderShape, false) t.printLabel(t.border, t.borderLabel, t.borderLabelOpts, t.borderLabelLen, t.borderShape, false)
if t.pborder != nil && t.pwindow.Height() != t.pborder.Height() { // To address --preview-border=line with different positions t.printLabel(t.pborder, t.previewLabel, t.previewLabelOpts, t.previewLabelLen, t.activePreviewOpts.Border(), false)
t.printLabel(t.pborder, t.previewLabel, t.previewLabelOpts, t.previewLabelLen, t.activePreviewOpts.border, false)
}
t.printLabel(t.inputBorder, t.inputLabel, t.inputLabelOpts, t.inputLabelLen, t.inputBorderShape, false) t.printLabel(t.inputBorder, t.inputLabel, t.inputLabelOpts, t.inputLabelLen, t.inputBorderShape, false)
t.printLabel(t.headerBorder, t.headerLabel, t.headerLabelOpts, t.headerLabelLen, t.headerBorderShape, false) t.printLabel(t.headerBorder, t.headerLabel, t.headerLabelOpts, t.headerLabelLen, t.headerBorderShape, false)
} }
@ -2048,7 +2034,7 @@ func (t *Terminal) printLabel(window tui.Window, render labelPrinter, opts label
} }
switch borderShape { switch borderShape {
case tui.BorderHorizontal, tui.BorderTop, tui.BorderBottom, tui.BorderRounded, tui.BorderSharp, tui.BorderBold, tui.BorderBlock, tui.BorderThinBlock, tui.BorderDouble, tui.BorderLine: case tui.BorderHorizontal, tui.BorderTop, tui.BorderBottom, tui.BorderRounded, tui.BorderSharp, tui.BorderBold, tui.BorderBlock, tui.BorderThinBlock, tui.BorderDouble:
if redrawBorder { if redrawBorder {
window.DrawHBorder() window.DrawHBorder()
} }
@ -3231,11 +3217,11 @@ func (t *Terminal) renderPreviewScrollbar(yoff int, barLength int, barStart int)
t.previewer.xw = xw t.previewer.xw = xw
} }
xshift := -1 - t.borderWidth xshift := -1 - t.borderWidth
if !t.activePreviewOpts.HasBorderRight() { if !t.activePreviewOpts.Border().HasRight() {
xshift = -1 xshift = -1
} }
yshift := 1 yshift := 1
if !t.activePreviewOpts.HasBorderTop() { if !t.activePreviewOpts.Border().HasTop() {
yshift = 0 yshift = 0
} }
for i := yoff; i < height; i++ { for i := yoff; i < height; i++ {
@ -3897,13 +3883,13 @@ func (t *Terminal) Loop() error {
if t.activePreviewOpts.aboveOrBelow() { if t.activePreviewOpts.aboveOrBelow() {
if t.activePreviewOpts.size.percent { if t.activePreviewOpts.size.percent {
newContentHeight := int(float64(contentHeight) * 100. / (100. - t.activePreviewOpts.size.size)) newContentHeight := int(float64(contentHeight) * 100. / (100. - t.activePreviewOpts.size.size))
contentHeight = util.Max(contentHeight+1+borderLines(t.activePreviewOpts.border), newContentHeight) contentHeight = util.Max(contentHeight+1+borderLines(t.activePreviewOpts.Border()), newContentHeight)
} else { } else {
contentHeight += int(t.activePreviewOpts.size.size) + borderLines(t.activePreviewOpts.border) contentHeight += int(t.activePreviewOpts.size.size) + borderLines(t.activePreviewOpts.Border())
} }
} else { } else {
// Minimum height if preview window can appear // Minimum height if preview window can appear
contentHeight = util.Max(contentHeight, 1+borderLines(t.activePreviewOpts.border)) contentHeight = util.Max(contentHeight, 1+borderLines(t.activePreviewOpts.Border()))
} }
} }
return util.Min(termHeight, contentHeight+pad) return util.Min(termHeight, contentHeight+pad)
@ -4257,7 +4243,7 @@ func (t *Terminal) Loop() error {
case reqRedrawBorderLabel: case reqRedrawBorderLabel:
t.printLabel(t.border, t.borderLabel, t.borderLabelOpts, t.borderLabelLen, t.borderShape, true) t.printLabel(t.border, t.borderLabel, t.borderLabelOpts, t.borderLabelLen, t.borderShape, true)
case reqRedrawPreviewLabel: case reqRedrawPreviewLabel:
t.printLabel(t.pborder, t.previewLabel, t.previewLabelOpts, t.previewLabelLen, t.activePreviewOpts.border, true) t.printLabel(t.pborder, t.previewLabel, t.previewLabelOpts, t.previewLabelLen, t.activePreviewOpts.Border(), true)
case reqReinit: case reqReinit:
t.tui.Resume(t.fullscreen, true) t.tui.Resume(t.fullscreen, true)
t.fullRedraw() t.fullRedraw()