Skip to content
This repository was archived by the owner on Oct 13, 2021. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 75 additions & 0 deletions border.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package tui

const (
runeMinus = '-'
runePlusSmall = '+'
runeVLineSmall = '|'

runePlus = '┼'
runeHLine = '─'
runeVLine = '│'
runeTTee = '┬'
runeRTee = '┤'
runeLTee = '├'
runeBTee = '┴'
runeULCorner = '┌'
runeURCorner = '┐'
runeLLCorner = '└'
runeLRCorner = '┘'
)

var runeFallbacks = map[rune]rune{}

func init() {
defaultBorder()
}

// default
// ┌──────────────────┐
// │┌────────┬───────┐│
// ││A │apple ││
// │└────────┴───────┘│
// └──────────────────┘
func defaultBorder() {
runeFallbacks = map[rune]rune{
runePlus: runePlus,
runeHLine: runeHLine,
runeVLine: runeVLine,
runeTTee: runeTTee,
runeRTee: runeRTee,
runeLTee: runeLTee,
runeBTee: runeBTee,
runeULCorner: runeULCorner,
runeURCorner: runeURCorner,
runeLLCorner: runeLLCorner,
runeLRCorner: runeLRCorner,
}
}

// small
// +------------------+
// |+--------+-------+|
// ||A |apple ||
// |+--------+-------+|
// +------------------+
func simpleBorder() {
runeFallbacks = map[rune]rune{
runeHLine: runeMinus,
runeVLine: runeVLineSmall,

runePlus: runePlusSmall,
runeLLCorner: runePlusSmall,
runeLRCorner: runePlusSmall,
runeTTee: runePlusSmall,
runeRTee: runePlusSmall,
runeLTee: runePlusSmall,
runeBTee: runePlusSmall,
runeULCorner: runePlusSmall,
runeURCorner: runePlusSmall,
}
}

// GetBorder get a simple or default border in a compatible way
func GetBorder(k rune) rune {
return runeFallbacks[k]
}
20 changes: 15 additions & 5 deletions grid.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ func (g *Grid) Draw(p *Painter) {
for i := 0; i < g.cols-1; i++ {
x := g.colWidths[i] + coloff + border
p.DrawVerticalLine(x, 0, s.Y-1)
p.DrawRune(x, 0, '┬')
p.DrawRune(x, s.Y-1, '┴')
p.DrawRune(x, 0, GetBorder('┬'))
p.DrawRune(x, s.Y-1, GetBorder('┴'))
coloff = x
}

Expand All @@ -59,8 +59,8 @@ func (g *Grid) Draw(p *Painter) {
for j := 0; j < g.rows-1; j++ {
y := g.rowHeights[j] + rowoff + border
p.DrawHorizontalLine(0, s.X-1, y)
p.DrawRune(0, y, '├')
p.DrawRune(s.X-1, y, '┤')
p.DrawRune(0, y, GetBorder('├'))
p.DrawRune(s.X-1, y, GetBorder('┤'))
rowoff = y
}

Expand All @@ -71,7 +71,7 @@ func (g *Grid) Draw(p *Painter) {
coloff = 0
for i := 0; i < g.cols-1; i++ {
x := g.colWidths[i] + coloff + border
p.DrawRune(x, y, '┼')
p.DrawRune(x, y, GetBorder('┼'))
coloff = x
}
rowoff = y
Expand Down Expand Up @@ -409,6 +409,16 @@ func (g *Grid) SetBorder(enabled bool) {
g.hasBorder = enabled
}

// SetSimpleBorder sets the border use [+ - |]. This is a global config.
func (g *Grid) SetSimpleBorder() {
simpleBorder()
}

// SetDefaultBorder sets the border use [┼ ─ │ ┬ ┤ ├ ┴ ┌ ┐ └ ┘]. This is a global config.
func (g *Grid) SetDefaultBorder() {
defaultBorder()
}

// AppendRow adds a new row at the end.
func (g *Grid) AppendRow(row ...Widget) {
g.rows++
Expand Down
19 changes: 19 additions & 0 deletions grid_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,23 @@ var drawGridTests = []struct {
┌─┬─────────┬──────────────────┬─┐
│f│bar......│baz...............│t│
└─┴─────────┴──────────────────┴─┘
`,
},
{
test: "Empty grid with simple border",
size: image.Point{15, 5},
setup: func() *Grid {
g := NewGrid(0, 0)
g.SetBorder(true)
g.SetSimpleBorder()
return g
},
want: `
+-------------+
|.............|
|.............|
|.............|
+-------------+
`,
},
}
Expand All @@ -232,6 +249,8 @@ func TestGrid_Draw(t *testing.T) {
painter := NewPainter(surface, NewTheme())
painter.Repaint(tt.setup())

// Reset
tt.setup().SetDefaultBorder()
if diff := surfaceEquals(surface, tt.want); diff != "" {
t.Error(diff)
}
Expand Down
16 changes: 8 additions & 8 deletions painter.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,14 +106,14 @@ func (p *Painter) DrawText(x, y int, text string) {
// DrawHorizontalLine paints a horizontal line using box characters.
func (p *Painter) DrawHorizontalLine(x1, x2, y int) {
for x := x1; x < x2; x++ {
p.DrawRune(x, y, '─')
p.DrawRune(x, y, GetBorder('─'))
}
}

// DrawVerticalLine paints a vertical line using box characters.
func (p *Painter) DrawVerticalLine(x, y1, y2 int) {
for y := y1; y < y2; y++ {
p.DrawRune(x, y, '│')
p.DrawRune(x, y, GetBorder('│'))
}
}

Expand All @@ -126,17 +126,17 @@ func (p *Painter) DrawRect(x, y, w, h int) {

switch {
case i == 0 && j == 0:
p.DrawRune(m, n, '┌')
p.DrawRune(m, n, GetBorder('┌'))
case i == w-1 && j == 0:
p.DrawRune(m, n, '┐')
p.DrawRune(m, n, GetBorder('┐'))
case i == 0 && j == h-1:
p.DrawRune(m, n, '└')
p.DrawRune(m, n, GetBorder('└'))
case i == w-1 && j == h-1:
p.DrawRune(m, n, '┘')
p.DrawRune(m, n, GetBorder('┘'))
case i == 0 || i == w-1:
p.DrawRune(m, n, '│')
p.DrawRune(m, n, GetBorder('│'))
case j == 0 || j == h-1:
p.DrawRune(m, n, '─')
p.DrawRune(m, n, GetBorder('─'))
}
}
}
Expand Down
10 changes: 5 additions & 5 deletions table.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ func (t *Table) Draw(p *Painter) {
for i := 0; i < t.cols-1; i++ {
x := t.colWidths[i] + coloff + border
p.DrawVerticalLine(x, 0, s.Y-1)
p.DrawRune(x, 0, '┬')
p.DrawRune(x, s.Y-1, '┴')
p.DrawRune(x, 0, GetBorder('┬'))
p.DrawRune(x, s.Y-1, GetBorder('┴'))
coloff = x
}

Expand All @@ -45,8 +45,8 @@ func (t *Table) Draw(p *Painter) {
for j := 0; j < t.rows-1; j++ {
y := t.rowHeights[j] + rowoff + border
p.DrawHorizontalLine(0, s.X-1, y)
p.DrawRune(0, y, '├')
p.DrawRune(s.X-1, y, '┤')
p.DrawRune(0, y, GetBorder('├'))
p.DrawRune(s.X-1, y, GetBorder('┤'))
rowoff = y
}

Expand All @@ -57,7 +57,7 @@ func (t *Table) Draw(p *Painter) {
coloff = 0
for i := 0; i < t.cols-1; i++ {
x := t.colWidths[i] + coloff + border
p.DrawRune(x, y, '┼')
p.DrawRune(x, y, GetBorder('┼'))
coloff = x
}
rowoff = y
Expand Down