2017-01-14 14:07:43 +08:00
|
|
|
// Copyright 2017 Zack Guo <zack.y.guo@gmail.com>. All rights reserved.
|
2015-03-21 04:21:50 +08:00
|
|
|
// 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
|
2016-10-14 19:48:50 +08:00
|
|
|
par.BorderLabel = "Label"
|
2015-03-25 05:16:43 +08:00
|
|
|
*/
|
2015-03-12 04:15:59 +08:00
|
|
|
type Par struct {
|
2015-02-04 09:56:49 +08:00
|
|
|
Block
|
2015-10-09 10:11:26 +08:00
|
|
|
Text string
|
|
|
|
TextFgColor Attribute
|
|
|
|
TextBgColor Attribute
|
2016-03-11 04:49:31 +08:00
|
|
|
WrapLength int // words wrap limit. Note it may not work properly with multi-width char
|
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-10-09 10:11:26 +08:00
|
|
|
Block: *NewBlock(),
|
|
|
|
Text: s,
|
|
|
|
TextFgColor: ThemeAttr("par.text.fg"),
|
|
|
|
TextBgColor: ThemeAttr("par.text.bg"),
|
2015-10-30 10:55:30 +08:00
|
|
|
WrapLength: 0,
|
2015-04-17 02:19:44 +08:00
|
|
|
}
|
2015-02-04 03:13:51 +08:00
|
|
|
}
|
|
|
|
|
2015-03-25 05:16:43 +08:00
|
|
|
// Buffer implements Bufferer interface.
|
2015-10-09 10:11:26 +08:00
|
|
|
func (p *Par) Buffer() Buffer {
|
|
|
|
buf := 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
|
2016-03-11 04:49:31 +08:00
|
|
|
cs := DefaultTxBuilder.Build(p.Text, fg, bg)
|
|
|
|
|
|
|
|
// wrap if WrapLength set
|
|
|
|
if p.WrapLength < 0 {
|
|
|
|
cs = wrapTx(cs, p.Width-2)
|
|
|
|
} else if p.WrapLength > 0 {
|
|
|
|
cs = wrapTx(cs, p.WrapLength)
|
2015-10-30 10:55:30 +08:00
|
|
|
}
|
2015-04-17 02:19:44 +08:00
|
|
|
|
|
|
|
y, x, n := 0, 0, 0
|
2015-10-09 10:11:26 +08:00
|
|
|
for y < p.innerArea.Dy() && n < len(cs) {
|
|
|
|
w := cs[n].Width()
|
|
|
|
if cs[n].Ch == '\n' || x+w > p.innerArea.Dx() {
|
2015-04-17 02:19:44 +08:00
|
|
|
y++
|
|
|
|
x = 0 // set x = 0
|
2015-10-09 10:11:26 +08:00
|
|
|
if cs[n].Ch == '\n' {
|
2015-04-17 02:19:44 +08:00
|
|
|
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-10-09 10:11:26 +08:00
|
|
|
buf.Set(p.innerArea.Min.X+p.innerArea.Dx()-1,
|
2015-10-08 02:25:59 +08:00
|
|
|
p.innerArea.Min.Y+p.innerArea.Dy()-1,
|
2015-10-09 10:11:26 +08:00
|
|
|
Cell{Ch: '…', Fg: p.TextFgColor, Bg: p.TextBgColor})
|
2015-03-14 01:20:17 +08:00
|
|
|
break
|
|
|
|
}
|
2015-02-04 03:13:51 +08:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2015-10-09 10:11:26 +08:00
|
|
|
buf.Set(p.innerArea.Min.X+x, p.innerArea.Min.Y+y, cs[n])
|
|
|
|
|
2015-04-17 02:19:44 +08:00
|
|
|
n++
|
2015-10-09 10:11:26 +08:00
|
|
|
x += w
|
2015-02-04 03:13:51 +08:00
|
|
|
}
|
2015-04-17 02:19:44 +08:00
|
|
|
|
2015-10-09 10:11:26 +08:00
|
|
|
return buf
|
2015-02-04 03:13:51 +08:00
|
|
|
}
|