termui/widgets/paragraph.go

49 lines
973 B
Go
Raw Permalink Normal View History

2019-01-23 21:12:10 -07:00
// Copyright 2017 Zack Guo <zack.y.guo@gmail.com>. All rights reserved.
2022-08-28 10:46:41 -06:00
// Use of this source code is governed by a GPLv3 license that can
2019-01-23 21:12:10 -07:00
// be found in the LICENSE file.
package widgets
import (
"image"
. "git.tebitea.media/sashakoshka/termui/v3"
2019-01-23 21:12:10 -07:00
)
type Paragraph struct {
Block
Text string
TextStyle Style
2019-01-26 03:47:47 -07:00
WrapText bool
2019-01-23 21:12:10 -07:00
}
func NewParagraph() *Paragraph {
return &Paragraph{
Block: *NewBlock(),
TextStyle: Theme.Paragraph.Text,
2019-01-26 03:47:47 -07:00
WrapText: true,
2019-01-23 21:12:10 -07:00
}
}
func (self *Paragraph) Draw(buf *Buffer) {
self.Block.Draw(buf)
2019-02-23 17:54:20 -07:00
cells := ParseStyles(self.Text, self.TextStyle)
2019-01-26 03:47:47 -07:00
if self.WrapText {
cells = WrapCells(cells, uint(self.Inner.Dx()))
}
rows := SplitCells(cells, '\n')
2019-01-23 21:12:10 -07:00
2019-01-26 03:47:47 -07:00
for y, row := range rows {
if y+self.Inner.Min.Y >= self.Inner.Max.Y {
break
}
row = TrimCells(row, self.Inner.Dx())
for _, cx := range BuildCellWithXArray(row) {
x, cell := cx.X, cx.Cell
2019-01-26 03:47:47 -07:00
buf.SetCell(cell, image.Pt(x, y).Add(self.Inner.Min))
2019-01-23 21:12:10 -07:00
}
}
}