aboutsummaryrefslogtreecommitdiffstats
path: root/Godeps/_workspace/src/github.com/gizak/termui/p.go
blob: e327d7489712e904b3948cc1026a363e1e15b083 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
// 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.

package termui

// Par displays a paragraph.
/*
  par := termui.NewPar("Simple Text")
  par.Height = 3
  par.Width = 17
  par.Border.Label = "Label"
*/
type Par struct {
    Block
    Text        string
    TextFgColor Attribute
    TextBgColor Attribute
}

// NewPar returns a new *Par with given text as its content.
func NewPar(s string) *Par {
    return &Par{
        Block:       *NewBlock(),
        Text:        s,
        TextFgColor: theme.ParTextFg,
        TextBgColor: theme.ParTextBg}
}

// Buffer implements Bufferer interface.
func (p *Par) Buffer() []Point {
    ps := p.Block.Buffer()

    rs := str2runes(p.Text)
    i, j, k := 0, 0, 0
    for i < p.innerHeight && k < len(rs) {
        // the width of char is about to print
        w := charWidth(rs[k])

        if rs[k] == '\n' || j+w > p.innerWidth {
            i++
            j = 0 // set x = 0
            if rs[k] == '\n' {
                k++
            }

            if i >= p.innerHeight {
                ps = append(ps, newPointWithAttrs('…',
                    p.innerX+p.innerWidth-1,
                    p.innerY+p.innerHeight-1,
                    p.TextFgColor, p.TextBgColor))
                break
            }

            continue
        }
        pi := Point{}
        pi.X = p.innerX + j
        pi.Y = p.innerY + i

        pi.Ch = rs[k]
        pi.Bg = p.TextBgColor
        pi.Fg = p.TextFgColor

        ps = append(ps, pi)

        k++
        j += w
    }
    return p.Block.chopOverflow(ps)
}