Implemented RendererFactory in Par.

This commit is contained in:
Matteo Kloiber 2015-04-16 20:19:44 +02:00
parent a3f1384a3b
commit e9e3e4084e
3 changed files with 30 additions and 31 deletions

View File

@ -29,7 +29,8 @@ func main() {
par1.X = 20 par1.X = 20
par1.Border.Label = "标签" par1.Border.Label = "标签"
par2 := termui.NewPar("Simple text\nwith label. It can be multilined with \\n or break automatically") par2 := termui.NewPar("Simple colored text\nwith label. It [can be](RED) multilined with \\n or [break automatically](GREEN, BOLD)")
par2.RendererFactory = termui.MarkdownTextRendererFactory{}
par2.Height = 5 par2.Height = 5
par2.Width = 37 par2.Width = 37
par2.Y = 4 par2.Y = 4

View File

@ -123,7 +123,7 @@ func main() {
ui.Render(p, list, g, sp, lc, bc, lc1, p1) ui.Render(p, list, g, sp, lc, bc, lc1, p1)
} }
evt := EventCh() evt := ui.EventCh()
i := 0 i := 0
for { for {
select { select {

56
p.go
View File

@ -13,38 +13,43 @@ package termui
*/ */
type Par struct { type Par struct {
Block Block
Text string Text string
TextFgColor Attribute TextFgColor Attribute
TextBgColor Attribute TextBgColor Attribute
RendererFactory TextRendererFactory
} }
// NewPar returns a new *Par with given text as its content. // NewPar returns a new *Par with given text as its content.
func NewPar(s string) *Par { func NewPar(s string) *Par {
return &Par{ return &Par{
Block: *NewBlock(), Block: *NewBlock(),
Text: s, Text: s,
TextFgColor: theme.ParTextFg, TextFgColor: theme.ParTextFg,
TextBgColor: theme.ParTextBg} TextBgColor: theme.ParTextBg,
RendererFactory: PlainRendererFactory{},
}
} }
// Buffer implements Bufferer interface. // Buffer implements Bufferer interface.
func (p *Par) Buffer() []Point { func (p *Par) Buffer() []Point {
ps := p.Block.Buffer() ps := p.Block.Buffer()
rs := str2runes(p.Text) fg, bg := p.TextFgColor, p.TextBgColor
i, j, k := 0, 0, 0 sequence := p.RendererFactory.TextRenderer(p.Text).Render(fg, bg)
for i < p.innerHeight && k < len(rs) { runes := []rune(sequence.NormalizedText)
// the width of char is about to print
w := charWidth(rs[k])
if rs[k] == '\n' || j+w > p.innerWidth { y, x, n := 0, 0, 0
i++ for y < p.innerHeight && n < len(runes) {
j = 0 // set x = 0 point, width := sequence.PointAt(n, x+p.innerX, y+p.innerY)
if rs[k] == '\n' {
k++ if runes[n] == '\n' || x+width > p.innerWidth {
y++
x = 0 // set x = 0
if runes[n] == '\n' {
n++
} }
if i >= p.innerHeight { if y >= p.innerHeight {
ps = append(ps, newPointWithAttrs('…', ps = append(ps, newPointWithAttrs('…',
p.innerX+p.innerWidth-1, p.innerX+p.innerWidth-1,
p.innerY+p.innerHeight-1, p.innerY+p.innerHeight-1,
@ -54,18 +59,11 @@ func (p *Par) Buffer() []Point {
continue continue
} }
pi := Point{}
pi.X = p.innerX + j
pi.Y = p.innerY + i
pi.Ch = rs[k] ps = append(ps, point)
pi.Bg = p.TextBgColor n++
pi.Fg = p.TextFgColor x += width
ps = append(ps, pi)
k++
j += w
} }
return p.Block.chopOverflow(ps) return p.Block.chopOverflow(ps)
} }