Skip to content

Commit

Permalink
Fix alignment in table example
Browse files Browse the repository at this point in the history
  • Loading branch information
coloursofnoise committed Nov 23, 2021
1 parent 8865903 commit 4219067
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 9 deletions.
6 changes: 3 additions & 3 deletions _examples/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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{'│'})
}

Expand All @@ -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},
Expand Down
14 changes: 8 additions & 6 deletions view.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
Expand Down Expand Up @@ -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
}
}
Expand Down Expand Up @@ -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++
}
Expand Down

0 comments on commit 4219067

Please sign in to comment.