Compare commits

...

2 Commits

Author SHA1 Message Date
Junegunn Choi
4fdc07927f
Refactor --preview-border=line 2025-01-11 19:34:26 +09:00
Junegunn Choi
9030b67e4f
Fix window sizing with borders on the right 2025-01-11 11:39:51 +09:00
2 changed files with 43 additions and 44 deletions

View File

@ -332,12 +332,21 @@ func (o *previewOpts) Toggle() {
o.hidden = !o.hidden
}
func (o *previewOpts) HasBorderRight() bool {
return o.border.HasRight() || o.border == tui.BorderLine && o.position == posLeft
func (o *previewOpts) Border() tui.BorderShape {
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 o.border.HasTop() || o.border == tui.BorderLine && o.position == posDown
}
return shape
}
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
effectiveMinHeight := minHeight
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) {
effectiveMinHeight--
@ -1521,12 +1521,12 @@ func calculateSize(base int, size sizeSpec, occupied int, minSize int) int {
}
func (t *Terminal) minPreviewSize(opts *previewOpts) (int, int) {
minPreviewWidth := 1 + borderColumns(opts.border, t.borderWidth)
minPreviewHeight := 1 + borderLines(opts.border)
minPreviewWidth := 1 + borderColumns(opts.Border(), t.borderWidth)
minPreviewHeight := 1 + borderLines(opts.Border())
switch opts.position {
case posLeft, posRight:
if len(t.scrollbar) > 0 && !opts.HasBorderRight() {
if len(t.scrollbar) > 0 && !opts.Border().HasRight() {
// Need a column to show scrollbar
minPreviewWidth++
}
@ -1756,11 +1756,14 @@ func (t *Terminal) resizeWindows(forcePreview bool, redrawBorder bool) {
t.areaLines = height
t.areaColumns = width
// If none of the inner borders has the right side, but the outer border does, increase the list width by 1 column
listStickToRight := t.borderShape.HasRight() && !t.listBorderShape.HasRight() && !t.inputBorderShape.HasRight() &&
(!t.headerVisible || !t.headerBorderShape.HasRight() || t.visibleHeaderLines() == 0)
// Set up preview window
noBorder := tui.MakeBorderStyle(tui.BorderNone, t.unicode)
if forcePreview || t.needPreviewWindow() {
var resizePreviewWindows func(previewOpts *previewOpts)
stickToRight := false
resizePreviewWindows = func(previewOpts *previewOpts) {
t.activePreviewOpts = previewOpts
if previewOpts.size.size == 0 {
@ -1770,19 +1773,7 @@ func (t *Terminal) resizeWindows(forcePreview bool, redrawBorder bool) {
createPreviewWindow := func(y int, x int, w int, h int) {
pwidth := w
pheight := h
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
}
}
shape := previewOpts.Border()
previewBorder := tui.MakeBorderStyle(shape, t.unicode)
t.pborder = t.tui.NewWindow(y, x, w, h, tui.WindowPreview, previewBorder, false)
pwidth -= borderColumns(shape, bw)
@ -1826,11 +1817,9 @@ func (t *Terminal) resizeWindows(forcePreview bool, redrawBorder bool) {
if previewOpts.hidden {
return
}
// If none of the inner borders has the right side, but the outer border does, increase the width by 1 column
stickToRight = t.borderShape.HasRight() &&
!previewOpts.HasBorderRight() && !t.listBorderShape.HasRight() && !t.inputBorderShape.HasRight() &&
(!t.headerVisible || !t.headerBorderShape.HasRight() || t.visibleHeaderLines() == 0)
if stickToRight {
listStickToRight = listStickToRight && !previewOpts.Border().HasRight()
if listStickToRight {
innerWidth++
width++
}
@ -1903,9 +1892,12 @@ func (t *Terminal) resizeWindows(forcePreview bool, redrawBorder bool) {
innerBorderFn(marginInt[0], marginInt[3]+pwidth, width-pwidth, height)
createPreviewWindow(marginInt[0], marginInt[3], pwidth, height)
} else {
// NOTE: fzf --preview 'cat {}' --preview-window border-left --border
stickToRight = !previewOpts.HasBorderRight() && t.borderShape.HasRight()
if stickToRight {
// NOTE: Relaxed condition for the following cases
// 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 --input-border
listStickToRight = t.borderShape.HasRight() && !previewOpts.Border().HasRight()
if listStickToRight {
innerWidth++
width++
}
@ -1919,7 +1911,7 @@ func (t *Terminal) resizeWindows(forcePreview bool, redrawBorder bool) {
}
resizePreviewWindows(&t.previewOpts)
if t.borderShape.HasRight() && !stickToRight {
if t.borderShape.HasRight() && !listStickToRight {
// Need to clear the extra margin between the borders
// fzf --preview 'seq 1000' --preview-window border-left --bind space:change-preview-window:border-rounded --border vertical
// fzf --preview 'seq 1000' --preview-window up,hidden --bind space:toggle-preview --border vertical
@ -1942,7 +1934,7 @@ func (t *Terminal) resizeWindows(forcePreview bool, redrawBorder bool) {
// Without preview window
if t.window == nil {
if t.borderShape.HasRight() && !hasListBorder {
if listStickToRight {
// Put scrollbar closer to the right border for consistent look
innerWidth++
width++
@ -2027,9 +2019,7 @@ func (t *Terminal) resizeWindows(forcePreview bool, redrawBorder bool) {
// Print border label
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)
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.headerBorder, t.headerLabel, t.headerLabelOpts, t.headerLabelLen, t.headerBorderShape, false)
}
@ -2044,7 +2034,7 @@ func (t *Terminal) printLabel(window tui.Window, render labelPrinter, opts label
}
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 {
window.DrawHBorder()
}
@ -3227,11 +3217,11 @@ func (t *Terminal) renderPreviewScrollbar(yoff int, barLength int, barStart int)
t.previewer.xw = xw
}
xshift := -1 - t.borderWidth
if !t.activePreviewOpts.HasBorderRight() {
if !t.activePreviewOpts.Border().HasRight() {
xshift = -1
}
yshift := 1
if !t.activePreviewOpts.HasBorderTop() {
if !t.activePreviewOpts.Border().HasTop() {
yshift = 0
}
for i := yoff; i < height; i++ {
@ -3893,13 +3883,13 @@ func (t *Terminal) Loop() error {
if t.activePreviewOpts.aboveOrBelow() {
if t.activePreviewOpts.size.percent {
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 {
contentHeight += int(t.activePreviewOpts.size.size) + borderLines(t.activePreviewOpts.border)
contentHeight += int(t.activePreviewOpts.size.size) + borderLines(t.activePreviewOpts.Border())
}
} else {
// 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)
@ -4253,7 +4243,7 @@ func (t *Terminal) Loop() error {
case reqRedrawBorderLabel:
t.printLabel(t.border, t.borderLabel, t.borderLabelOpts, t.borderLabelLen, t.borderShape, true)
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:
t.tui.Resume(t.fullscreen, true)
t.fullRedraw()