From e847ec25553f767271a301cefea03a9cf858387d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=91=A8=E5=BB=BA=E5=88=9A?= Date: Sat, 24 Mar 2018 23:43:58 +0800 Subject: [PATCH 1/4] Add simple border --- border.go | 74 ++++++++++++++++++++++++++++++++++++++++++++++++++++ grid.go | 22 ++++++++++++---- grid_test.go | 19 ++++++++++++++ painter.go | 16 ++++++------ table.go | 10 +++---- 5 files changed, 123 insertions(+), 18 deletions(-) create mode 100644 border.go diff --git a/border.go b/border.go new file mode 100644 index 0000000..27a29d7 --- /dev/null +++ b/border.go @@ -0,0 +1,74 @@ +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, + } +} + +func GetBorder(k rune) rune { + return runeFallbacks[k] +} diff --git a/grid.go b/grid.go index dbb379e..dc93514 100644 --- a/grid.go +++ b/grid.go @@ -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 } @@ -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 } @@ -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 @@ -409,6 +409,18 @@ func (g *Grid) SetBorder(enabled bool) { g.hasBorder = enabled } +// This is a global config. +// SetSmallBorder sets the border use [+ - |]. +func (g *Grid) SetSimpleBorder() { + SimpleBorder() +} + +// This is a global config. +// SetSmallBorder sets the border use [┼ ─ │ ┬ ┤ ├ ┴ ┌ ┐ └ ┘]. +func (g *Grid) SetDefaultBorder() { + DefaultBorder() +} + // AppendRow adds a new row at the end. func (g *Grid) AppendRow(row ...Widget) { g.rows++ diff --git a/grid_test.go b/grid_test.go index 979e2c5..14dac96 100644 --- a/grid_test.go +++ b/grid_test.go @@ -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: ` ++-------------+ +|.............| +|.............| +|.............| ++-------------+ `, }, } @@ -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) } diff --git a/painter.go b/painter.go index eea1add..3db1bad 100644 --- a/painter.go +++ b/painter.go @@ -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('│')) } } @@ -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('─')) } } } diff --git a/table.go b/table.go index 3f8865f..006e36a 100644 --- a/table.go +++ b/table.go @@ -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 } @@ -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 } @@ -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 From 6122f17a86bb996f85e49a4c96649d7b07d61aa7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=91=A8=E5=BB=BA=E5=88=9A?= Date: Sun, 25 Mar 2018 00:24:23 +0800 Subject: [PATCH 2/4] Fix function's comment --- border.go | 79 ++++++++++++++++++++++++++++--------------------------- grid.go | 8 +++--- 2 files changed, 44 insertions(+), 43 deletions(-) diff --git a/border.go b/border.go index 27a29d7..3d69363 100644 --- a/border.go +++ b/border.go @@ -1,27 +1,27 @@ package tui const ( - RuneMinus = '-' - RunePlusSmall = '+' - RuneVLineSmall = '|' + runeMinus = '-' + runePlusSmall = '+' + runeVLineSmall = '|' - RunePlus = '┼' - RuneHLine = '─' - RuneVLine = '│' - RuneTTee = '┬' - RuneRTee = '┤' - RuneLTee = '├' - RuneBTee = '┴' - RuneULCorner = '┌' - RuneURCorner = '┐' - RuneLLCorner = '└' - RuneLRCorner = '┘' + runePlus = '┼' + runeHLine = '─' + runeVLine = '│' + runeTTee = '┬' + runeRTee = '┤' + runeLTee = '├' + runeBTee = '┴' + runeULCorner = '┌' + runeURCorner = '┐' + runeLLCorner = '└' + runeLRCorner = '┘' ) var runeFallbacks = map[rune]rune{} func init() { - DefaultBorder() + defaultBorder() } // default @@ -30,19 +30,19 @@ func init() { // ││A │apple ││ // │└────────┴───────┘│ // └──────────────────┘ -func DefaultBorder() { +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, + runePlus: runePlus, + runeHLine: runeHLine, + runeVLine: runeVLine, + runeTTee: runeTTee, + runeRTee: runeRTee, + runeLTee: runeLTee, + runeBTee: runeBTee, + runeULCorner: runeULCorner, + runeURCorner: runeURCorner, + runeLLCorner: runeLLCorner, + runeLRCorner: runeLRCorner, } } @@ -52,23 +52,24 @@ func DefaultBorder() { // ||A |apple || // |+--------+-------+| // +------------------+ -func SimpleBorder() { +func simpleBorder() { runeFallbacks = map[rune]rune{ - RuneHLine: RuneMinus, - RuneVLine: RuneVLineSmall, + runeHLine: runeMinus, + runeVLine: runeVLineSmall, - RunePlus: RunePlusSmall, - RuneLLCorner: RunePlusSmall, - RuneLRCorner: RunePlusSmall, - RuneTTee: RunePlusSmall, - RuneRTee: RunePlusSmall, - RuneLTee: RunePlusSmall, - RuneBTee: RunePlusSmall, - RuneULCorner: RunePlusSmall, - RuneURCorner: RunePlusSmall, + 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] } diff --git a/grid.go b/grid.go index dc93514..bc14cfa 100644 --- a/grid.go +++ b/grid.go @@ -410,15 +410,15 @@ func (g *Grid) SetBorder(enabled bool) { } // This is a global config. -// SetSmallBorder sets the border use [+ - |]. +// SetSimpleBorder sets the border use [+ - |]. func (g *Grid) SetSimpleBorder() { - SimpleBorder() + simpleBorder() } // This is a global config. -// SetSmallBorder sets the border use [┼ ─ │ ┬ ┤ ├ ┴ ┌ ┐ └ ┘]. +// SetDefaultBorder sets the border use [┼ ─ │ ┬ ┤ ├ ┴ ┌ ┐ └ ┘]. func (g *Grid) SetDefaultBorder() { - DefaultBorder() + defaultBorder() } // AppendRow adds a new row at the end. From 7dea5e0d0ae91d22ba18814218d3d32a97e5b8f1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=91=A8=E5=BB=BA=E5=88=9A?= Date: Sun, 25 Mar 2018 00:27:50 +0800 Subject: [PATCH 3/4] Fix function's comment --- grid.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/grid.go b/grid.go index bc14cfa..4d4868c 100644 --- a/grid.go +++ b/grid.go @@ -409,14 +409,14 @@ func (g *Grid) SetBorder(enabled bool) { g.hasBorder = enabled } -// This is a global config. // SetSimpleBorder sets the border use [+ - |]. +// This is a global config. func (g *Grid) SetSimpleBorder() { simpleBorder() } -// This is a global config. // SetDefaultBorder sets the border use [┼ ─ │ ┬ ┤ ├ ┴ ┌ ┐ └ ┘]. +// This is a global config. func (g *Grid) SetDefaultBorder() { defaultBorder() } From abdf20f91aa770a3239b1f894e9ee20e0e060f87 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=91=A8=E5=BB=BA=E5=88=9A?= Date: Sun, 25 Mar 2018 00:30:51 +0800 Subject: [PATCH 4/4] Fix function's comment --- grid.go | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/grid.go b/grid.go index 4d4868c..74afc6f 100644 --- a/grid.go +++ b/grid.go @@ -409,14 +409,12 @@ func (g *Grid) SetBorder(enabled bool) { g.hasBorder = enabled } -// SetSimpleBorder sets the border use [+ - |]. -// This is a global config. +// 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. +// SetDefaultBorder sets the border use [┼ ─ │ ┬ ┤ ├ ┴ ┌ ┐ └ ┘]. This is a global config. func (g *Grid) SetDefaultBorder() { defaultBorder() }