Use Has{Top,Right,Bottom,Left}() where possible

De-duplicate code and reduce the amount of code that has to be changed
when new BorderShapes are being added. This also adds and uses the
missing HasBottom().
This commit is contained in:
Julian Prein authored and Junegunn Choi committed 2024-12-12 13:53:08 +09:00
1 parent 66dbee10f5
commit cdf641fa3e
2 files changed
+39 -65

No files matched your search

+31 -65
View File
@@ -995,13 +995,14 @@ func (t *Terminal) environ() []string {
} }
func borderLines(shape tui.BorderShape) int { func borderLines(shape tui.BorderShape) int {
switch shape { lines := 0
case tui.BorderHorizontal, tui.BorderRounded, tui.BorderSharp, tui.BorderBold, tui.BorderBlock, tui.BorderThinBlock, tui.BorderDouble: if shape.HasTop() {
return 2 lines++
case tui.BorderTop, tui.BorderBottom:
return 1
} }
return 0 if shape.HasBottom() {
lines++
}
return lines
} }
func borderColumns(shape tui.BorderShape, borderWidth int) int { func borderColumns(shape tui.BorderShape, borderWidth int) int {
@@ -1541,36 +1542,24 @@ func (t *Terminal) resizeWindows(forcePreview bool) {
t.previewed.version = 0 t.previewed.version = 0
bw := t.borderWidth bw := t.borderWidth
switch t.borderShape { offsets := [4]int{} // TRWH
case tui.BorderHorizontal: if t.borderShape.HasTop() {
t.border = t.tui.NewWindow( offsets[0] -= 1
marginInt[0]-1, marginInt[3], width, height+2, offsets[3] += 1
false, tui.MakeBorderStyle(tui.BorderHorizontal, t.unicode))
case tui.BorderVertical:
t.border = t.tui.NewWindow(
marginInt[0], marginInt[3]-(1+bw), width+(1+bw)*2, height,
false, tui.MakeBorderStyle(tui.BorderVertical, t.unicode))
case tui.BorderTop:
t.border = t.tui.NewWindow(
marginInt[0]-1, marginInt[3], width, height+1,
false, tui.MakeBorderStyle(tui.BorderTop, t.unicode))
case tui.BorderBottom:
t.border = t.tui.NewWindow(
marginInt[0], marginInt[3], width, height+1,
false, tui.MakeBorderStyle(tui.BorderBottom, t.unicode))
case tui.BorderLeft:
t.border = t.tui.NewWindow(
marginInt[0], marginInt[3]-(1+bw), width+(1+bw), height,
false, tui.MakeBorderStyle(tui.BorderLeft, t.unicode))
case tui.BorderRight:
t.border = t.tui.NewWindow(
marginInt[0], marginInt[3], width+(1+bw), height,
false, tui.MakeBorderStyle(tui.BorderRight, t.unicode))
case tui.BorderRounded, tui.BorderSharp, tui.BorderBold, tui.BorderBlock, tui.BorderThinBlock, tui.BorderDouble:
t.border = t.tui.NewWindow(
marginInt[0]-1, marginInt[3]-(1+bw), width+(1+bw)*2, height+2,
false, tui.MakeBorderStyle(t.borderShape, t.unicode))
} }
if t.borderShape.HasRight() {
offsets[2] += 1 + bw
}
if t.borderShape.HasBottom() {
offsets[3] += 1
}
if t.borderShape.HasLeft() {
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))
// Add padding to margin // Add padding to margin
for idx, val := range paddingInt { for idx, val := range paddingInt {
@@ -1602,28 +1591,13 @@ func (t *Terminal) resizeWindows(forcePreview bool) {
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) t.pborder = t.tui.NewWindow(y, x, w, h, true, previewBorder)
switch previewOpts.border { pwidth -= borderColumns(previewOpts.border, bw)
case tui.BorderSharp, tui.BorderRounded, tui.BorderBold, tui.BorderBlock, tui.BorderThinBlock, tui.BorderDouble: pheight -= borderLines(previewOpts.border)
pwidth -= (1 + bw) * 2 if t.previewOpts.border.HasLeft() {
pheight -= 2
x += 1 + bw x += 1 + bw
}
if t.previewOpts.border.HasTop() {
y += 1 y += 1
case tui.BorderLeft:
pwidth -= 1 + bw
x += 1 + bw
case tui.BorderRight:
pwidth -= 1 + bw
case tui.BorderTop:
pheight -= 1
y += 1
case tui.BorderBottom:
pheight -= 1
case tui.BorderHorizontal:
pheight -= 2
y += 1
case tui.BorderVertical:
pwidth -= (1 + bw) * 2
x += 1 + bw
} }
if len(t.scrollbar) > 0 && !previewOpts.border.HasRight() { if len(t.scrollbar) > 0 && !previewOpts.border.HasRight() {
// Need a column to show scrollbar // Need a column to show scrollbar
@@ -1636,16 +1610,8 @@ func (t *Terminal) resizeWindows(forcePreview bool) {
t.pwindow.Erase() t.pwindow.Erase()
} }
} }
verticalPad := 2 verticalPad := 0 + borderLines(previewOpts.border)
minPreviewHeight := 3 minPreviewHeight := 1 + borderLines(previewOpts.border)
switch previewOpts.border {
case tui.BorderNone, tui.BorderVertical, tui.BorderLeft, tui.BorderRight:
verticalPad = 0
minPreviewHeight = 1
case tui.BorderTop, tui.BorderBottom:
verticalPad = 1
minPreviewHeight = 2
}
switch previewOpts.position { switch previewOpts.position {
case posUp, posDown: case posUp, posDown:
minWindowHeight := minHeight minWindowHeight := minHeight
+8
View File
@@ -387,6 +387,14 @@ func (s BorderShape) HasTop() bool {
return true return true
} }
func (s BorderShape) HasBottom() bool {
switch s {
case BorderNone, BorderLeft, BorderRight, BorderTop, BorderVertical: // No bottom
return false
}
return true
}
type BorderStyle struct { type BorderStyle struct {
shape BorderShape shape BorderShape
top rune top rune