Merge pull request #227 from anson0370/unicode_string_display

fix: #226
This commit is contained in:
Caleb Bassi 2019-02-24 10:10:52 -08:00
commit 63c2a0d709
5 changed files with 35 additions and 8 deletions

View File

@ -6,6 +6,8 @@ package termui
import ( import (
"image" "image"
rw "github.com/mattn/go-runewidth"
) )
// Cell represents a viewable terminal cell // Cell represents a viewable terminal cell
@ -65,7 +67,10 @@ func (self *Buffer) Fill(c Cell, rect image.Rectangle) {
} }
func (self *Buffer) SetString(s string, style Style, p image.Point) { func (self *Buffer) SetString(s string, style Style, p image.Point) {
for i, char := range s { runes := []rune(s)
self.SetCell(Cell{char, style}, image.Pt(p.X+i, p.Y)) x := 0
for _, char := range runes {
self.SetCell(Cell{char, style}, image.Pt(p.X+x, p.Y))
x += rw.RuneWidth(char)
} }
} }

View File

@ -189,8 +189,9 @@ func CellsToString(cells []Cell) string {
func TrimCells(cells []Cell, w int) []Cell { func TrimCells(cells []Cell, w int) []Cell {
s := CellsToString(cells) s := CellsToString(cells)
s = TrimString(s, w) s = TrimString(s, w)
runes := []rune(s)
newCells := []Cell{} newCells := []Cell{}
for i, r := range s { for i, r := range runes {
newCells = append(newCells, Cell{r, cells[i].Style}) newCells = append(newCells, Cell{r, cells[i].Style})
} }
return newCells return newCells
@ -212,3 +213,18 @@ func SplitCells(cells []Cell, r rune) [][]Cell {
} }
return splitCells return splitCells
} }
type CellWithX struct {
X int
Cell Cell
}
func BuildCellWithXArray(cells []Cell) []CellWithX {
cellWithXArray := make([]CellWithX, len(cells))
index := 0
for i, cell := range cells {
cellWithXArray[i] = CellWithX{X: index, Cell: cell}
index += rw.RuneWidth(cell.Rune)
}
return cellWithXArray
}

View File

@ -7,6 +7,8 @@ package widgets
import ( import (
"image" "image"
rw "github.com/mattn/go-runewidth"
. "github.com/gizak/termui" . "github.com/gizak/termui"
) )
@ -57,7 +59,7 @@ func (self *List) Draw(buf *Buffer) {
break break
} else { } else {
buf.SetCell(NewCell(cells[j].Rune, style), point) buf.SetCell(NewCell(cells[j].Rune, style), point)
point = point.Add(image.Pt(1, 0)) point = point.Add(image.Pt(rw.RuneWidth(cells[j].Rune), 0))
} }
} }
} }

View File

@ -40,7 +40,8 @@ func (self *Paragraph) Draw(buf *Buffer) {
break break
} }
row = TrimCells(row, self.Inner.Dx()) row = TrimCells(row, self.Inner.Dx())
for x, cell := range row { for _, cx := range BuildCellWithXArray(row) {
x, cell := cx.X, cx.Cell
buf.SetCell(cell, image.Pt(x, y).Add(self.Inner.Min)) buf.SetCell(cell, image.Pt(x, y).Add(self.Inner.Min))
} }
} }

View File

@ -74,7 +74,8 @@ func (self *Table) Draw(buf *Buffer) {
col := ParseText(row[j], rowStyle) col := ParseText(row[j], rowStyle)
// draw row cell // draw row cell
if len(col) > columnWidths[j] || self.TextAlign == AlignLeft { if len(col) > columnWidths[j] || self.TextAlign == AlignLeft {
for k, cell := range col { for _, cx := range BuildCellWithXArray(col) {
k, cell := cx.X, cx.Cell
if k == columnWidths[j] || colXCoordinate+k == self.Inner.Max.X { if k == columnWidths[j] || colXCoordinate+k == self.Inner.Max.X {
cell.Rune = ELLIPSES cell.Rune = ELLIPSES
buf.SetCell(cell, image.Pt(colXCoordinate+k-1, yCoordinate)) buf.SetCell(cell, image.Pt(colXCoordinate+k-1, yCoordinate))
@ -86,12 +87,14 @@ func (self *Table) Draw(buf *Buffer) {
} else if self.TextAlign == AlignCenter { } else if self.TextAlign == AlignCenter {
xCoordinateOffset := (columnWidths[j] - len(col)) / 2 xCoordinateOffset := (columnWidths[j] - len(col)) / 2
stringXCoordinate := xCoordinateOffset + colXCoordinate stringXCoordinate := xCoordinateOffset + colXCoordinate
for k, cell := range col { for _, cx := range BuildCellWithXArray(col) {
k, cell := cx.X, cx.Cell
buf.SetCell(cell, image.Pt(stringXCoordinate+k, yCoordinate)) buf.SetCell(cell, image.Pt(stringXCoordinate+k, yCoordinate))
} }
} else if self.TextAlign == AlignRight { } else if self.TextAlign == AlignRight {
stringXCoordinate := MinInt(colXCoordinate+columnWidths[j], self.Inner.Max.X) - len(col) stringXCoordinate := MinInt(colXCoordinate+columnWidths[j], self.Inner.Max.X) - len(col)
for k, cell := range col { for _, cx := range BuildCellWithXArray(col) {
k, cell := cx.X, cx.Cell
buf.SetCell(cell, image.Pt(stringXCoordinate+k, yCoordinate)) buf.SetCell(cell, image.Pt(stringXCoordinate+k, yCoordinate))
} }
} }