2015-03-20 14:21:50 -06: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-03 12:13:51 -07:00
|
|
|
package termui
|
|
|
|
|
2015-03-24 15:16:43 -06:00
|
|
|
// Par displays a paragraph.
|
|
|
|
/*
|
|
|
|
par := termui.NewPar("Simple Text")
|
|
|
|
par.Height = 3
|
|
|
|
par.Width = 17
|
|
|
|
par.Border.Label = "Label"
|
|
|
|
*/
|
2015-03-11 14:15:59 -06:00
|
|
|
type Par struct {
|
2015-02-03 18:56:49 -07:00
|
|
|
Block
|
2015-02-03 12:13:51 -07:00
|
|
|
Text string
|
2015-02-03 18:56:49 -07:00
|
|
|
TextFgColor Attribute
|
|
|
|
TextBgColor Attribute
|
2015-02-03 12:13:51 -07:00
|
|
|
}
|
|
|
|
|
2015-03-24 15:16:43 -06:00
|
|
|
// NewPar returns a new *Par with given text as its content.
|
2015-03-11 14:15:59 -06:00
|
|
|
func NewPar(s string) *Par {
|
|
|
|
return &Par{
|
|
|
|
Block: *NewBlock(),
|
|
|
|
Text: s,
|
|
|
|
TextFgColor: theme.ParTextFg,
|
|
|
|
TextBgColor: theme.ParTextBg}
|
2015-02-03 12:13:51 -07:00
|
|
|
}
|
|
|
|
|
2015-03-24 15:16:43 -06:00
|
|
|
// Buffer implements Bufferer interface.
|
2015-03-11 14:15:59 -06:00
|
|
|
func (p *Par) Buffer() []Point {
|
2015-02-03 18:56:49 -07:00
|
|
|
ps := p.Block.Buffer()
|
2015-02-03 12:13:51 -07:00
|
|
|
|
|
|
|
rs := str2runes(p.Text)
|
|
|
|
i, j, k := 0, 0, 0
|
|
|
|
for i < p.innerHeight && k < len(rs) {
|
2015-03-25 16:04:15 -06:00
|
|
|
// the width of char is about to print
|
|
|
|
w := charWidth(rs[k])
|
|
|
|
|
|
|
|
if rs[k] == '\n' || j+w > p.innerWidth {
|
2015-02-03 12:13:51 -07:00
|
|
|
i++
|
2015-03-25 16:04:15 -06:00
|
|
|
j = 0 // set x = 0
|
2015-02-03 12:13:51 -07:00
|
|
|
if rs[k] == '\n' {
|
|
|
|
k++
|
|
|
|
}
|
2015-03-13 11:20:17 -06:00
|
|
|
|
|
|
|
if i >= p.innerHeight {
|
|
|
|
ps = append(ps, newPointWithAttrs('…',
|
|
|
|
p.innerX+p.innerWidth-1,
|
|
|
|
p.innerY+p.innerHeight-1,
|
|
|
|
p.TextFgColor, p.TextBgColor))
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
2015-02-03 12:13:51 -07:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
pi := Point{}
|
|
|
|
pi.X = p.innerX + j
|
|
|
|
pi.Y = p.innerY + i
|
|
|
|
|
2015-03-03 11:28:09 -07:00
|
|
|
pi.Ch = rs[k]
|
|
|
|
pi.Bg = p.TextBgColor
|
|
|
|
pi.Fg = p.TextFgColor
|
2015-02-03 12:13:51 -07:00
|
|
|
|
|
|
|
ps = append(ps, pi)
|
2015-03-13 11:20:17 -06:00
|
|
|
|
2015-02-03 12:13:51 -07:00
|
|
|
k++
|
2015-03-25 16:04:15 -06:00
|
|
|
j += w
|
2015-02-03 12:13:51 -07:00
|
|
|
}
|
|
|
|
return ps
|
|
|
|
}
|