refactor: use a []CellWithX instead a chan of CellWithX to make it more sense

This commit is contained in:
Anson Chan 2019-02-24 11:29:35 +08:00
parent 179acca35e
commit b5e7ca951a
3 changed files with 13 additions and 14 deletions

View File

@ -219,13 +219,12 @@ type CellWithX struct {
Cell Cell
}
func BuildCellChannel(cells []Cell) chan CellWithX {
c := make(chan CellWithX, len(cells))
func BuildCellWithXArray(cells []Cell) []CellWithX {
cellWithXArray := make([]CellWithX, len(cells))
index := 0
for _, cell := range cells {
c <- CellWithX{X: index, Cell: cell}
for i, cell := range cells {
cellWithXArray[i] = CellWithX{X: index, Cell: cell}
index += rw.RuneWidth(cell.Rune)
}
close(c)
return c
return cellWithXArray
}

View File

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

View File

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