Add WrapText option to Paragraph

This commit is contained in:
Caleb Bassi
2019-01-26 02:47:47 -08:00
parent a5828eb2da
commit 5b78b896c9
5 changed files with 97 additions and 59 deletions

View File

@@ -39,7 +39,7 @@ func (self *List) Draw(buf *Buffer) {
point = image.Pt(self.Inner.Min.X, point.Y+1)
} else {
if point.X+1 == self.Inner.Max.X+1 && len(cells) > self.Inner.Dx() {
buf.SetCell(NewCell(DOTS, cells[j].Style), point.Add(image.Pt(-1, 0)))
buf.SetCell(NewCell(ELLIPSES, cells[j].Style), point.Add(image.Pt(-1, 0)))
break
} else {
buf.SetCell(cells[j], point)

View File

@@ -14,27 +14,34 @@ type Paragraph struct {
Block
Text string
TextStyle Style
WrapText bool
}
func NewParagraph() *Paragraph {
return &Paragraph{
Block: *NewBlock(),
TextStyle: Theme.Paragraph.Text,
WrapText: true,
}
}
func (self *Paragraph) Draw(buf *Buffer) {
self.Block.Draw(buf)
point := self.Inner.Min
cells := WrapCells(ParseText(self.Text, self.TextStyle), uint(self.Inner.Dx()))
cells := ParseText(self.Text, self.TextStyle)
if self.WrapText {
cells = WrapCells(cells, uint(self.Inner.Dx()))
}
for i := 0; i < len(cells) && point.Y < self.Inner.Max.Y; i++ {
if cells[i].Rune == '\n' {
point = image.Pt(self.Inner.Min.X, point.Y+1)
} else {
buf.SetCell(cells[i], point)
point = point.Add(image.Pt(1, 0))
rows := SplitCells(cells, '\n')
for y, row := range rows {
if y+self.Inner.Min.Y >= self.Inner.Max.Y {
break
}
row = TrimCells(row, self.Inner.Dx())
for x, cell := range row {
buf.SetCell(cell, image.Pt(x, y).Add(self.Inner.Min))
}
}
}

View File

@@ -61,7 +61,7 @@ func (self *Table) Draw(buf *Buffer) {
if len(col) > columnWidths[j] || self.TextAlign == AlignLeft {
for k, cell := range col {
if k == columnWidths[j] || colXCoordinate+k == self.Inner.Max.X {
cell.Rune = DOTS
cell.Rune = ELLIPSES
buf.SetCell(cell, image.Pt(colXCoordinate+k-1, yCoordinate))
break
} else {