From 42190678968e292fa63ccd88a8d82109ed99883f Mon Sep 17 00:00:00 2001 From: coloursofnoise Date: Tue, 23 Nov 2021 13:27:30 -0800 Subject: [PATCH] Fix alignment in table example --- _examples/table.go | 6 +++--- view.go | 14 ++++++++------ 2 files changed, 11 insertions(+), 9 deletions(-) diff --git a/_examples/table.go b/_examples/table.go index a97f510..64462ac 100644 --- a/_examples/table.go +++ b/_examples/table.go @@ -43,7 +43,7 @@ func (t *Table) Layout(g *gocui.Gui) error { width, height := view.Size() hOffset := 0 for cid, column := range t.Columns { - size := int(float32(width) * column.Size) + size := int(float32(width + 1) * column.Size) view.SetWritePos(hOffset, 0) view.WriteString(column.Title) @@ -53,7 +53,7 @@ func (t *Table) Layout(g *gocui.Gui) error { view.SetWritePos(hOffset, rid+1) view.WriteString(t.Data[cid][rid]) } - view.SetWritePos(hOffset+size-3, rid) + view.SetWritePos(hOffset + size - 1, rid) view.WriteRunes([]rune{'│'}) } @@ -70,7 +70,7 @@ func main() { } defer g.Close() - table := NewTable("t", 1, 2, 80, 10) + table := NewTable("t", 0, 2, 80, 10) table.Columns = []Column{ {"Column1", 0.25}, {"Column2", 0.25}, diff --git a/view.go b/view.go index f22ddf0..946f679 100644 --- a/view.go +++ b/view.go @@ -204,10 +204,11 @@ func (v *View) Name() string { // setRune sets a rune at the given point relative to the view. It applies the // specified colors, taking into account if the cell must be highlighted. Also, // it checks if the position is valid. -func (v *View) setRune(x, y int, ch rune, fgColor, bgColor Attribute) error { +// Returns the set rune (!may be different from input) +func (v *View) setRune(x, y int, ch rune, fgColor, bgColor Attribute) (rune, error) { maxX, maxY := v.Size() if x < 0 || x >= maxX || y < 0 || y >= maxY { - return ErrInvalidPoint + return 0, ErrInvalidPoint } if v.Mask != 0 { @@ -226,7 +227,7 @@ func (v *View) setRune(x, y int, ch rune, fgColor, bgColor Attribute) error { tcellSetCell(v.x0+x+1, v.y0+y+1, ch, fgColor, bgColor, v.outMode) - return nil + return ch, nil } // SetCursorUnrestricted sets the cursor position of the view at the given point @@ -562,7 +563,7 @@ func (v *View) draw() error { if !v.tainted && v.contentCache != nil { for _, cell := range v.contentCache { - if err := v.setRune(cell.x, cell.y, cell.chr, cell.fgColor, cell.bgColor); err != nil { + if _, err := v.setRune(cell.x, cell.y, cell.chr, cell.fgColor, cell.bgColor); err != nil { return err } } @@ -610,10 +611,11 @@ func (v *View) draw() error { x: x, y: y, }) - if err := v.setRune(x, y, char.chr, fgColor, bgColor); err != nil { + chr, err := v.setRune(x, y, char.chr, fgColor, bgColor) + if err != nil { return err } - x += runewidth.RuneWidth(char.chr) + x += runewidth.RuneWidth(chr) } y++ }