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