From ddc86587d8b394da8369ca2a5356c1bf4885f673 Mon Sep 17 00:00:00 2001 From: gizak Date: Tue, 3 Feb 2015 14:13:51 -0500 Subject: [PATCH] Add P tag --- box.go | 86 ++++++++++++++++++++++++++++++++------------------ div.go | 75 +++++++++++++++++++++++++++++++++++++++++++ helper.go | 28 ++++++++++++++++ helper_test.go | 11 +++++++ p.go | 45 ++++++++++++++++++++++++++ 5 files changed, 214 insertions(+), 31 deletions(-) create mode 100644 div.go create mode 100644 helper.go create mode 100644 helper_test.go create mode 100644 p.go diff --git a/box.go b/box.go index a9274b8..48ebc1a 100644 --- a/box.go +++ b/box.go @@ -10,35 +10,34 @@ const BOTTOM_RIGHT = '┘' const BOTTOM_LEFT = '└' type Box struct { - X int - Y int - Width int - Height int - Border bool + X int + Y int + Width int + Height int FgColor tm.Attribute BgColor tm.Attribute } type HLine struct { - X int - Y int - Length int + X int + Y int + Length int FgColor tm.Attribute BgColor tm.Attribute } type VLine struct { - X int - Y int - Length int + X int + Y int + Length int FgColor tm.Attribute BgColor tm.Attribute } -func (l HLine) Buffer() []Point{ - pts := make([]Point,l.Length) - for i:=0;i w { + rs = rs[:w] + rs[w-1] = '…' + } + return rs +} diff --git a/helper_test.go b/helper_test.go new file mode 100644 index 0000000..d54c976 --- /dev/null +++ b/helper_test.go @@ -0,0 +1,11 @@ +package termui + +import "testing" + +func TestStr2Rune(t *testing.T) { + s := "你好,世界." + rs := str2runes(s) + if len(rs) != 6 { + t.Error() + } +} diff --git a/p.go b/p.go new file mode 100644 index 0000000..3ab4877 --- /dev/null +++ b/p.go @@ -0,0 +1,45 @@ +package termui + +import tm "github.com/nsf/termbox-go" + +type P struct { + Div + Text string + TextFgColor tm.Attribute + TextBgColor tm.Attribute +} + +func NewP(s string) P { + return P{Div: NewDiv(), Text: s} +} + +func (p P) Buffer() []Point { + ps := p.Div.Buffer() + + (&p).sync() + + rs := str2runes(p.Text) + i, j, k := 0, 0, 0 + for i < p.innerHeight && k < len(rs) { + if rs[k] == '\n' || j == p.innerWidth { + i++ + j = 0 + if rs[k] == '\n' { + k++ + } + continue + } + pi := Point{} + pi.X = p.innerX + j + pi.Y = p.innerY + i + + pi.Code.Ch = rs[k] + pi.Code.Bg = p.TextBgColor + pi.Code.Fg = p.TextFgColor + + ps = append(ps, pi) + k++ + j++ + } + return ps +}