termui/widgets/paragraph.go

48 lines
906 B
Go
Raw Normal View History

2019-01-24 12:12:10 +08: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"
. "github.com/gizak/termui"
)
type Paragraph struct {
Block
Text string
TextStyle Style
2019-01-26 18:47:47 +08:00
WrapText bool
2019-01-24 12:12:10 +08:00
}
func NewParagraph() *Paragraph {
return &Paragraph{
Block: *NewBlock(),
TextStyle: Theme.Paragraph.Text,
2019-01-26 18:47:47 +08:00
WrapText: true,
2019-01-24 12:12:10 +08:00
}
}
func (self *Paragraph) Draw(buf *Buffer) {
self.Block.Draw(buf)
2019-01-26 18:47:47 +08:00
cells := ParseText(self.Text, self.TextStyle)
if self.WrapText {
cells = WrapCells(cells, uint(self.Inner.Dx()))
}
rows := SplitCells(cells, '\n')
2019-01-24 12:12:10 +08:00
2019-01-26 18:47:47 +08:00
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))
2019-01-24 12:12:10 +08:00
}
}
}