2015-04-21 21:56:10 +08:00
|
|
|
// +build ignore
|
|
|
|
|
2015-03-21 04:21:50 +08:00
|
|
|
// Copyright 2015 Zack Guo <gizak@icloud.com>. All rights reserved.
|
|
|
|
// Use of this source code is governed by a MIT license that can
|
|
|
|
// be found in the LICENSE file.
|
|
|
|
|
2015-02-04 03:13:51 +08:00
|
|
|
package termui
|
|
|
|
|
2015-03-25 05:16:43 +08:00
|
|
|
// Par displays a paragraph.
|
|
|
|
/*
|
|
|
|
par := termui.NewPar("Simple Text")
|
|
|
|
par.Height = 3
|
|
|
|
par.Width = 17
|
|
|
|
par.Border.Label = "Label"
|
|
|
|
*/
|
2015-03-12 04:15:59 +08:00
|
|
|
type Par struct {
|
2015-02-04 09:56:49 +08:00
|
|
|
Block
|
2015-04-17 02:19:44 +08:00
|
|
|
Text string
|
|
|
|
TextFgColor Attribute
|
|
|
|
TextBgColor Attribute
|
|
|
|
RendererFactory TextRendererFactory
|
2015-02-04 03:13:51 +08:00
|
|
|
}
|
|
|
|
|
2015-03-25 05:16:43 +08:00
|
|
|
// NewPar returns a new *Par with given text as its content.
|
2015-03-12 04:15:59 +08:00
|
|
|
func NewPar(s string) *Par {
|
|
|
|
return &Par{
|
2015-04-17 02:19:44 +08:00
|
|
|
Block: *NewBlock(),
|
|
|
|
Text: s,
|
|
|
|
TextFgColor: theme.ParTextFg,
|
|
|
|
TextBgColor: theme.ParTextBg,
|
|
|
|
RendererFactory: PlainRendererFactory{},
|
|
|
|
}
|
2015-02-04 03:13:51 +08:00
|
|
|
}
|
|
|
|
|
2015-03-25 05:16:43 +08:00
|
|
|
// Buffer implements Bufferer interface.
|
2015-03-12 04:15:59 +08:00
|
|
|
func (p *Par) Buffer() []Point {
|
2015-02-04 09:56:49 +08:00
|
|
|
ps := p.Block.Buffer()
|
2015-02-04 03:13:51 +08:00
|
|
|
|
2015-04-17 02:19:44 +08:00
|
|
|
fg, bg := p.TextFgColor, p.TextBgColor
|
|
|
|
sequence := p.RendererFactory.TextRenderer(p.Text).Render(fg, bg)
|
|
|
|
runes := []rune(sequence.NormalizedText)
|
|
|
|
|
|
|
|
y, x, n := 0, 0, 0
|
2015-10-08 02:25:59 +08:00
|
|
|
for y < p.innerArea.Dy() && n < len(runes) {
|
|
|
|
point, width := sequence.PointAt(n, x+p.innerArea.Min.X, y+p.innerArea.Min.Y)
|
2015-03-26 06:04:15 +08:00
|
|
|
|
2015-10-08 02:25:59 +08:00
|
|
|
if runes[n] == '\n' || x+width > p.innerArea.Dx() {
|
2015-04-17 02:19:44 +08:00
|
|
|
y++
|
|
|
|
x = 0 // set x = 0
|
|
|
|
if runes[n] == '\n' {
|
|
|
|
n++
|
2015-02-04 03:13:51 +08:00
|
|
|
}
|
2015-03-14 01:20:17 +08:00
|
|
|
|
2015-10-08 02:25:59 +08:00
|
|
|
if y >= p.innerArea.Dy() {
|
2015-03-14 01:20:17 +08:00
|
|
|
ps = append(ps, newPointWithAttrs('…',
|
2015-10-08 02:25:59 +08:00
|
|
|
p.innerArea.Min.X+p.innerArea.Dx()-1,
|
|
|
|
p.innerArea.Min.Y+p.innerArea.Dy()-1,
|
2015-03-14 01:20:17 +08:00
|
|
|
p.TextFgColor, p.TextBgColor))
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
2015-02-04 03:13:51 +08:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2015-04-17 02:19:44 +08:00
|
|
|
ps = append(ps, point)
|
|
|
|
n++
|
|
|
|
x += width
|
2015-02-04 03:13:51 +08:00
|
|
|
}
|
2015-04-17 02:19:44 +08:00
|
|
|
|
2015-04-10 04:40:49 +08:00
|
|
|
return p.Block.chopOverflow(ps)
|
2015-02-04 03:13:51 +08:00
|
|
|
}
|