mirror of
https://github.com/Llewellynvdm/fzf.git
synced 2024-12-22 10:58:59 +00:00
Refactor
This commit is contained in:
parent
db44cbdff0
commit
289997e373
@ -1417,7 +1417,7 @@ const (
|
||||
minHeight = 3
|
||||
)
|
||||
|
||||
func calculateSize(base int, size sizeSpec, occupied int, minSize int, pad int) int {
|
||||
func calculateSize(base int, size sizeSpec, occupied int, minSize int) int {
|
||||
max := base - occupied
|
||||
if max < minSize {
|
||||
max = minSize
|
||||
@ -1425,7 +1425,22 @@ func calculateSize(base int, size sizeSpec, occupied int, minSize int, pad int)
|
||||
if size.percent {
|
||||
return util.Constrain(int(float64(base)*0.01*size.size), minSize, max)
|
||||
}
|
||||
return util.Constrain(int(size.size)+pad, minSize, max)
|
||||
return util.Constrain(int(size.size)+minSize-1, minSize, max)
|
||||
}
|
||||
|
||||
func (t *Terminal) minPreviewSize(opts *previewOpts) (int, int) {
|
||||
minPreviewWidth := 1 + borderColumns(opts.border, t.borderWidth)
|
||||
minPreviewHeight := 1 + borderLines(opts.border)
|
||||
|
||||
switch opts.position {
|
||||
case posLeft, posRight:
|
||||
if len(t.scrollbar) > 0 && !opts.border.HasRight() {
|
||||
// Need a column to show scrollbar
|
||||
minPreviewWidth++
|
||||
}
|
||||
}
|
||||
|
||||
return minPreviewWidth, minPreviewHeight
|
||||
}
|
||||
|
||||
func (t *Terminal) adjustMarginAndPadding() (int, int, [4]int, [4]int) {
|
||||
@ -1499,8 +1514,7 @@ func (t *Terminal) adjustMarginAndPadding() (int, int, [4]int, [4]int) {
|
||||
minAreaHeight -= 1
|
||||
}
|
||||
if t.needPreviewWindow() {
|
||||
minPreviewHeight := 1 + borderLines(t.activePreviewOpts.border)
|
||||
minPreviewWidth := 5
|
||||
minPreviewWidth, minPreviewHeight := t.minPreviewSize(t.activePreviewOpts)
|
||||
switch t.activePreviewOpts.position {
|
||||
case posUp, posDown:
|
||||
minAreaHeight += minPreviewHeight
|
||||
@ -1558,9 +1572,11 @@ func (t *Terminal) resizeWindows(forcePreview bool) {
|
||||
offsets[1] -= 1 + bw
|
||||
offsets[2] += 1 + bw
|
||||
}
|
||||
t.border = t.tui.NewWindow(
|
||||
marginInt[0]+offsets[0], marginInt[3]+offsets[1], width+offsets[2], height+offsets[3],
|
||||
false, tui.MakeBorderStyle(t.borderShape, t.unicode))
|
||||
if t.borderShape != tui.BorderNone {
|
||||
t.border = t.tui.NewWindow(
|
||||
marginInt[0]+offsets[0], marginInt[3]+offsets[1], width+offsets[2], height+offsets[3],
|
||||
false, tui.MakeBorderStyle(t.borderShape, t.unicode))
|
||||
}
|
||||
|
||||
// Add padding to margin
|
||||
for idx, val := range paddingInt {
|
||||
@ -1585,12 +1601,7 @@ func (t *Terminal) resizeWindows(forcePreview bool) {
|
||||
createPreviewWindow := func(y int, x int, w int, h int) {
|
||||
pwidth := w
|
||||
pheight := h
|
||||
var previewBorder tui.BorderStyle
|
||||
if previewOpts.border == tui.BorderNone {
|
||||
previewBorder = tui.MakeTransparentBorder()
|
||||
} else {
|
||||
previewBorder = tui.MakeBorderStyle(previewOpts.border, t.unicode)
|
||||
}
|
||||
previewBorder := tui.MakeBorderStyle(previewOpts.border, t.unicode)
|
||||
t.pborder = t.tui.NewWindow(y, x, w, h, true, previewBorder)
|
||||
pwidth -= borderColumns(previewOpts.border, bw)
|
||||
pheight -= borderLines(previewOpts.border)
|
||||
@ -1611,15 +1622,14 @@ func (t *Terminal) resizeWindows(forcePreview bool) {
|
||||
t.pwindow.Erase()
|
||||
}
|
||||
}
|
||||
verticalPad := 0 + borderLines(previewOpts.border)
|
||||
minPreviewHeight := 1 + borderLines(previewOpts.border)
|
||||
minPreviewWidth, minPreviewHeight := t.minPreviewSize(previewOpts)
|
||||
switch previewOpts.position {
|
||||
case posUp, posDown:
|
||||
minWindowHeight := minHeight
|
||||
if t.noSeparatorLine() {
|
||||
minWindowHeight--
|
||||
}
|
||||
pheight := calculateSize(height, previewOpts.size, minWindowHeight, minPreviewHeight, verticalPad)
|
||||
pheight := calculateSize(height, previewOpts.size, minWindowHeight, minPreviewHeight)
|
||||
if hasThreshold && pheight < previewOpts.threshold {
|
||||
t.activePreviewOpts = previewOpts.alternative
|
||||
if forcePreview {
|
||||
@ -1646,13 +1656,7 @@ func (t *Terminal) resizeWindows(forcePreview bool) {
|
||||
createPreviewWindow(marginInt[0]+height-pheight, marginInt[3], width, pheight)
|
||||
}
|
||||
case posLeft, posRight:
|
||||
pad := borderColumns(previewOpts.border, t.borderWidth)
|
||||
if len(t.scrollbar) > 0 && !previewOpts.border.HasRight() {
|
||||
// Need a column to show scrollbar
|
||||
pad += 1
|
||||
}
|
||||
minPreviewWidth := 1 + pad
|
||||
pwidth := calculateSize(width, previewOpts.size, minWidth, minPreviewWidth, pad)
|
||||
pwidth := calculateSize(width, previewOpts.size, minWidth, minPreviewWidth)
|
||||
if hasThreshold && pwidth < previewOpts.threshold {
|
||||
t.activePreviewOpts = previewOpts.alternative
|
||||
if forcePreview {
|
||||
@ -4722,13 +4726,7 @@ func (t *Terminal) Loop() error {
|
||||
if pborderDragging {
|
||||
previewWidth := t.pwindow.Width() + borderColumns(t.activePreviewOpts.border, t.borderWidth)
|
||||
previewHeight := t.pwindow.Height() + borderLines(t.activePreviewOpts.border)
|
||||
minPreviewWidth := 1 + borderColumns(t.activePreviewOpts.border, t.borderWidth)
|
||||
minPreviewHeight := 1 + borderLines(t.activePreviewOpts.border)
|
||||
|
||||
if len(t.scrollbar) > 0 && t.activePreviewOpts.position == posLeft && !t.activePreviewOpts.border.HasRight() {
|
||||
// Need a column to show scrollbar
|
||||
minPreviewWidth++
|
||||
}
|
||||
minPreviewWidth, minPreviewHeight := t.minPreviewSize(t.activePreviewOpts)
|
||||
|
||||
// Decrement, so the cursor drags the last column/row of the
|
||||
// preview window (i.e. in most cases the border) and not
|
||||
|
@ -410,6 +410,18 @@ type BorderStyle struct {
|
||||
type BorderCharacter int
|
||||
|
||||
func MakeBorderStyle(shape BorderShape, unicode bool) BorderStyle {
|
||||
if shape == BorderNone {
|
||||
return BorderStyle{
|
||||
shape: BorderRounded,
|
||||
top: ' ',
|
||||
bottom: ' ',
|
||||
left: ' ',
|
||||
right: ' ',
|
||||
topLeft: ' ',
|
||||
topRight: ' ',
|
||||
bottomLeft: ' ',
|
||||
bottomRight: ' '}
|
||||
}
|
||||
if !unicode {
|
||||
return BorderStyle{
|
||||
shape: shape,
|
||||
@ -506,19 +518,6 @@ func MakeBorderStyle(shape BorderShape, unicode bool) BorderStyle {
|
||||
}
|
||||
}
|
||||
|
||||
func MakeTransparentBorder() BorderStyle {
|
||||
return BorderStyle{
|
||||
shape: BorderRounded,
|
||||
top: ' ',
|
||||
bottom: ' ',
|
||||
left: ' ',
|
||||
right: ' ',
|
||||
topLeft: ' ',
|
||||
topRight: ' ',
|
||||
bottomLeft: ' ',
|
||||
bottomRight: ' '}
|
||||
}
|
||||
|
||||
type TermSize struct {
|
||||
Lines int
|
||||
Columns int
|
||||
|
Loading…
Reference in New Issue
Block a user