Use Has{Top,Right,Bottom,Left}() where possible
Before adoption: this commit came in with the history the project adopted, so no landing records it.
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().
authored by Julian Prein · committed by Junegunn Choi · parent 66dbee1 · Browse files · 2 files·+39 added−65 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+39 added−65 removed
- other:+39 added−65 removed
Provenance
- Commit
cdf641f- Tree
bf73a6b- Author
Julian Prein <julian@druckdev.xyz>- Committer
Junegunn Choi <junegunn.c@gmail.com>- Parent
66dbee1
2 files changed+39 added−65 removed
modified:src/tui/tui.go+8 added
385 unmodified lines386 }387 return true388 }389 390+func (s BorderShape) HasBottom() bool {391+ switch s {392+ case BorderNone, BorderLeft, BorderRight, BorderTop, BorderVertical: // No bottom393+ return false394+ }395+ return true396+}397+398 type BorderStyle struct {399 shape BorderShape400 top rune401 bottom rune486 unmodified linesmodified:src/terminal.go+31 added-65 removed
993 unmodified lines994 return env995 }996 997 func borderLines(shape tui.BorderShape) int {998- switch shape {999- case tui.BorderHorizontal, tui.BorderRounded, tui.BorderSharp, tui.BorderBold, tui.BorderBlock, tui.BorderThinBlock, tui.BorderDouble:1000- return 21001- case tui.BorderTop, tui.BorderBottom:1002- return 1998+ lines := 0999+ if shape.HasTop() {1000+ lines++1001 }1002+ if shape.HasBottom() {1003+ lines++1004+ }1004- return 01005+ return lines1006 }1007 1008 func borderColumns(shape tui.BorderShape, borderWidth int) int {1009 columns := 0531 unmodified lines1541 // Reset preview version so that full redraw occurs1542 t.previewed.version = 01543 1544 bw := t.borderWidth1544- switch t.borderShape {1545- case tui.BorderHorizontal:1546- t.border = t.tui.NewWindow(1547- marginInt[0]-1, marginInt[3], width, height+2,1548- false, tui.MakeBorderStyle(tui.BorderHorizontal, t.unicode))1549- case tui.BorderVertical:1550- t.border = t.tui.NewWindow(1551- marginInt[0], marginInt[3]-(1+bw), width+(1+bw)*2, height,1552- false, tui.MakeBorderStyle(tui.BorderVertical, t.unicode))1553- case tui.BorderTop:1554- t.border = t.tui.NewWindow(1555- marginInt[0]-1, marginInt[3], width, height+1,1556- false, tui.MakeBorderStyle(tui.BorderTop, t.unicode))1557- case tui.BorderBottom:1558- t.border = t.tui.NewWindow(1559- marginInt[0], marginInt[3], width, height+1,1560- false, tui.MakeBorderStyle(tui.BorderBottom, t.unicode))1561- case tui.BorderLeft:1562- t.border = t.tui.NewWindow(1563- marginInt[0], marginInt[3]-(1+bw), width+(1+bw), height,1564- false, tui.MakeBorderStyle(tui.BorderLeft, t.unicode))1565- case tui.BorderRight:1566- t.border = t.tui.NewWindow(1567- marginInt[0], marginInt[3], width+(1+bw), height,1568- false, tui.MakeBorderStyle(tui.BorderRight, t.unicode))1569- case tui.BorderRounded, tui.BorderSharp, tui.BorderBold, tui.BorderBlock, tui.BorderThinBlock, tui.BorderDouble:1570- t.border = t.tui.NewWindow(1571- marginInt[0]-1, marginInt[3]-(1+bw), width+(1+bw)*2, height+2,1572- false, tui.MakeBorderStyle(t.borderShape, t.unicode))1545+ offsets := [4]int{} // TRWH1546+ if t.borderShape.HasTop() {1547+ offsets[0] -= 11548+ offsets[3] += 11549 }1550+ if t.borderShape.HasRight() {1551+ offsets[2] += 1 + bw1552+ }1553+ if t.borderShape.HasBottom() {1554+ offsets[3] += 11555+ }1556+ if t.borderShape.HasLeft() {1557+ offsets[1] -= 1 + bw1558+ offsets[2] += 1 + bw1559+ }1560+ t.border = t.tui.NewWindow(1561+ marginInt[0]+offsets[0], marginInt[3]+offsets[1], width+offsets[2], height+offsets[3],1562+ false, tui.MakeBorderStyle(t.borderShape, t.unicode))1563 1564 // Add padding to margin1565 for idx, val := range paddingInt {1566 marginInt[idx] += val23 unmodified lines1590 } else {1591 previewBorder = tui.MakeBorderStyle(previewOpts.border, t.unicode)1592 }1593 t.pborder = t.tui.NewWindow(y, x, w, h, true, previewBorder)1605- switch previewOpts.border {1606- case tui.BorderSharp, tui.BorderRounded, tui.BorderBold, tui.BorderBlock, tui.BorderThinBlock, tui.BorderDouble:1607- pwidth -= (1 + bw) * 21608- pheight -= 21594+ pwidth -= borderColumns(previewOpts.border, bw)1595+ pheight -= borderLines(previewOpts.border)1596+ if t.previewOpts.border.HasLeft() {1597 x += 1 + bw1598+ }1599+ if t.previewOpts.border.HasTop() {1600 y += 11611- case tui.BorderLeft:1612- pwidth -= 1 + bw1613- x += 1 + bw1614- case tui.BorderRight:1615- pwidth -= 1 + bw1616- case tui.BorderTop:1617- pheight -= 11618- y += 11619- case tui.BorderBottom:1620- pheight -= 11621- case tui.BorderHorizontal:1622- pheight -= 21623- y += 11624- case tui.BorderVertical:1625- pwidth -= (1 + bw) * 21626- x += 1 + bw1601 }1602 if len(t.scrollbar) > 0 && !previewOpts.border.HasRight() {1603 // Need a column to show scrollbar1604 pwidth -= 14 unmodified lines1609 if !hadPreviewWindow {1610 t.pwindow.Erase()1611 }1612 }1639- verticalPad := 21640- minPreviewHeight := 31641- switch previewOpts.border {1642- case tui.BorderNone, tui.BorderVertical, tui.BorderLeft, tui.BorderRight:1643- verticalPad = 01644- minPreviewHeight = 11645- case tui.BorderTop, tui.BorderBottom:1646- verticalPad = 11647- minPreviewHeight = 21648- }1613+ verticalPad := 0 + borderLines(previewOpts.border)1614+ minPreviewHeight := 1 + borderLines(previewOpts.border)1615 switch previewOpts.position {1616 case posUp, posDown:1617 minWindowHeight := minHeight1618 if t.noSeparatorLine() {3647 unmodified lines