2019-01-23 21:12:10 -07:00
|
|
|
// Copyright 2017 Zack Guo <zack.y.guo@gmail.com>. All rights reserved.
|
|
|
|
// Use of this source code is governed by a MIT license that can
|
|
|
|
// be found in the LICENSE file.
|
|
|
|
|
|
|
|
package widgets
|
|
|
|
|
|
|
|
import (
|
|
|
|
"image"
|
|
|
|
|
2019-03-07 03:50:20 -07:00
|
|
|
. "github.com/gizak/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())
|
2019-02-23 20:29:35 -07:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|