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

64
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
}
@ -35,10 +34,10 @@ type VLine struct {
BgColor tm.Attribute
}
func (l HLine) Buffer() []Point{
pts := make([]Point,l.Length)
for i:=0;i<l.Length;i++{
pts[i].X = l.X+i
func (l HLine) Buffer() []Point {
pts := make([]Point, l.Length)
for i := 0; i < l.Length; i++ {
pts[i].X = l.X + i
pts[i].Y = l.Y
pts[i].Code.Ch = HORIZONTAL_LINE
pts[i].Code.Bg = l.BgColor
@ -47,11 +46,11 @@ func (l HLine) Buffer() []Point{
return pts
}
func (l VLine) Buffer() []Point{
pts := make([]Point,l.Length)
for i:=0;i<l.Length;i++{
func (l VLine) Buffer() []Point {
pts := make([]Point, l.Length)
for i := 0; i < l.Length; i++ {
pts[i].X = l.X
pts[i].Y = l.Y+i
pts[i].Y = l.Y + i
pts[i].Code.Ch = VERTICAL_LINE
pts[i].Code.Bg = l.BgColor
pts[i].Code.Fg = l.FgColor
@ -59,11 +58,11 @@ func (l VLine) Buffer() []Point{
return pts
}
func (b Box) Buffer() []Point{
if b.Width<2 || b.Height<2 {
func (b Box) Buffer() []Point {
if b.Width < 2 || b.Height < 2 {
return nil
}
pts := make([]Point,2*b.Width+2*b.Height-4)
pts := make([]Point, 2*b.Width+2*b.Height-4)
pts[0].X = b.X
pts[0].Y = b.Y
@ -71,28 +70,53 @@ func (b Box) Buffer() []Point{
pts[0].Code.Bg = b.BgColor
pts[0].Code.Ch = TOP_LEFT
pts[1].X = b.X+b.Width-1
pts[1].X = b.X + b.Width - 1
pts[1].Y = b.Y
pts[1].Code.Fg = b.FgColor
pts[1].Code.Bg = b.BgColor
pts[1].Code.Ch = TOP_RIGHT
pts[2].X = b.X
pts[2].Y = b.Y+b.Height-1
pts[2].Y = b.Y + b.Height - 1
pts[2].Code.Fg = b.FgColor
pts[2].Code.Bg = b.BgColor
pts[2].Code.Ch = BOTTOM_LEFT
pts[3].X = b.X+b.Width-1
pts[3].Y = b.Y+b.Height-1
pts[3].X = b.X + b.Width - 1
pts[3].Y = b.Y + b.Height - 1
pts[3].Code.Fg = b.FgColor
pts[3].Code.Bg = b.BgColor
pts[3].Code.Ch = BOTTOM_RIGHT
copy(pts[4:],(HLine{b.X+1,b.Y,b.Width-2,b.FgColor,b.BgColor}).Buffer())
copy(pts[4+b.Width-2:],(HLine{b.X+1,b.Y+b.Height-1,b.Width-2,b.FgColor,b.BgColor}).Buffer())
copy(pts[4+2*b.Width-4:],(VLine{b.X,b.Y+1,b.Height-2,b.FgColor,b.BgColor}).Buffer())
copy(pts[4+2*b.Width-4+b.Height-2:],(VLine{b.X+b.Width-1,b.Y+1,b.Height-2,b.FgColor,b.BgColor}).Buffer())
copy(pts[4:], (HLine{b.X + 1, b.Y, b.Width - 2, b.FgColor, b.BgColor}).Buffer())
copy(pts[4+b.Width-2:], (HLine{b.X + 1, b.Y + b.Height - 1, b.Width - 2, b.FgColor, b.BgColor}).Buffer())
copy(pts[4+2*b.Width-4:], (VLine{b.X, b.Y + 1, b.Height - 2, b.FgColor, b.BgColor}).Buffer())
copy(pts[4+2*b.Width-4+b.Height-2:], (VLine{b.X + b.Width - 1, b.Y + 1, b.Height - 2, b.FgColor, b.BgColor}).Buffer())
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
}