Add P tag

This commit is contained in:
gizak 2015-02-03 14:13:51 -05:00
parent 5bf8a442e7
commit ddc86587d8
5 changed files with 214 additions and 31 deletions

26
box.go
View File

@ -14,7 +14,6 @@ type Box struct {
Y int
Width int
Height int
Border bool
FgColor tm.Attribute
BgColor tm.Attribute
}
@ -96,3 +95,28 @@ func (b Box) Buffer() []Point{
return pts
}
type LabeledBox struct {
Box
Label string
LabelFgColor tm.Attribute
LabelBgColor tm.Attribute
}
func (lb LabeledBox) Buffer() []Point {
ps := lb.Box.Buffer()
maxTxtW := lb.Width - 2
rs := trimStr2Runes(lb.Label, maxTxtW)
for i := 0; i < len(rs); i++ {
p := Point{}
p.X = lb.X + 1 + i
p.Y = lb.Y
p.Code.Ch = rs[i]
p.Code.Fg = lb.LabelFgColor
p.Code.Bg = lb.LabelBgColor
ps = append(ps, p)
}
return ps
}

75
div.go Normal file
View File

@ -0,0 +1,75 @@
package termui
import tm "github.com/nsf/termbox-go"
type Div struct {
X int
Y int
Border LabeledBox
IsDisplay bool
HasBorder bool
BgColor tm.Attribute
Width int
Height int
innerWidth int
innerHeight int
innerX int
innerY int
}
func NewDiv() Div {
d := Div{}
d.Border.BgColor = tm.ColorDefault
d.Border.FgColor = tm.ColorDefault
d.Border.LabelFgColor = tm.ColorDefault
d.Border.LabelBgColor = tm.ColorDefault
d.IsDisplay = true
d.HasBorder = true
d.Width = 2
d.Height = 2
d.BgColor = tm.ColorDefault
return d
}
func (d *Div) sync() {
d.innerWidth = d.Width
d.innerHeight = d.Height
d.innerX = d.X
d.innerY = d.Y
if d.HasBorder {
d.innerHeight -= 2
d.innerWidth -= 2
d.Border.X = d.X
d.Border.Y = d.Y
d.Border.Width = d.Width
d.Border.Height = d.Height
d.innerX += 1
d.innerY += 1
}
}
func (d Div) Buffer() []Point {
(&d).sync()
ps := []Point{}
if !d.IsDisplay {
return ps
}
if d.HasBorder {
ps = d.Border.Buffer()
}
for i := 0; i < d.innerWidth; i++ {
for j := 0; j < d.innerHeight; j++ {
p := Point{}
p.X = d.X + 1 + i
p.Y = d.Y + 1 + j
p.Code.Ch = ' '
p.Code.Bg = d.BgColor
ps = append(ps, p)
}
}
return ps
}

28
helper.go Normal file
View File

@ -0,0 +1,28 @@
package termui
import "unicode/utf8"
import "strings"
func str2runes(s string) []rune {
n := utf8.RuneCountInString(s)
ss := strings.Split(s, "")
rs := make([]rune, n)
for i := 0; i < n; i++ {
r, _ := utf8.DecodeRuneInString(ss[i])
rs[i] = r
}
return rs
}
func trimStr2Runes(s string, w int) []rune {
rs := str2runes(s)
if w <= 0 {
return []rune{}
}
if len(rs) > w {
rs = rs[:w]
rs[w-1] = '…'
}
return rs
}

11
helper_test.go Normal file
View File

@ -0,0 +1,11 @@
package termui
import "testing"
func TestStr2Rune(t *testing.T) {
s := "你好,世界."
rs := str2runes(s)
if len(rs) != 6 {
t.Error()
}
}

45
p.go Normal file
View File

@ -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
}