Minor chanages

This commit is contained in:
gizak 2015-05-09 19:29:22 -04:00
parent 7f94c273e5
commit 0042236f53
5 changed files with 25 additions and 32 deletions

View File

@ -85,7 +85,7 @@ func (b Block) drawBorder(buf Buffer) {
func (b Block) drawBorderLabel(buf Buffer) { func (b Block) drawBorderLabel(buf Buffer) {
maxTxtW := b.area.Dx() - 2 maxTxtW := b.area.Dx() - 2
tx := DTrimTxCls(TextCells(b.BorderLabel, b.BorderLabelFg, b.BorderLabelBg), maxTxtW) tx := DTrimTxCls(DefaultTxBuilder.Build(b.BorderLabel, b.BorderLabelFg, b.BorderLabelBg), maxTxtW)
for i, w := 0, 0; i < len(tx); i++ { for i, w := 0, 0; i < len(tx); i++ {
buf.Set(b.area.Min.X+1+w, b.area.Min.Y, tx[i]) buf.Set(b.area.Min.X+1+w, b.area.Min.Y, tx[i])

View File

@ -11,7 +11,7 @@ func main() {
b := termui.NewBlock() b := termui.NewBlock()
b.Width = 20 b.Width = 20
b.Height = 30 b.Height = 30
b.BorderLabel = "HELLO WORLD" b.BorderLabel = "[HELLO](fg-red,bg-white) [WORLD](fg-blue,bg-green)"
termui.Render(b) termui.Render(b)
<-termui.EventCh() <-termui.EventCh()

View File

@ -15,7 +15,7 @@ type Cell struct {
// Buffer is a renderable rectangle cell data container. // Buffer is a renderable rectangle cell data container.
type Buffer struct { type Buffer struct {
Area *image.Rectangle // selected drawing area Area image.Rectangle // selected drawing area
CellMap map[image.Point]Cell CellMap map[image.Point]Cell
} }
@ -50,7 +50,7 @@ func (b Buffer) Bounds() image.Rectangle {
} }
// SetArea assigns a new rect area to Buffer b. // SetArea assigns a new rect area to Buffer b.
func (b Buffer) SetArea(r image.Rectangle) { func (b *Buffer) SetArea(r image.Rectangle) {
b.Area.Max = r.Max b.Area.Max = r.Max
b.Area.Min = r.Min b.Area.Min = r.Min
} }
@ -71,24 +71,15 @@ func (b Buffer) Merge(bs ...Buffer) {
for p, v := range buf.CellMap { for p, v := range buf.CellMap {
b.Set(p.X, p.Y, v) b.Set(p.X, p.Y, v)
} }
b.SetArea(b.Area.Union(*buf.Area)) b.SetArea(b.Area.Union(buf.Area))
} }
} }
// Point for adapting use, will be removed after resolving bridging.
type Point struct {
X int
Y int
Ch rune
Fg Attribute
Bg Attribute
}
// NewBuffer returns a new Buffer // NewBuffer returns a new Buffer
func NewBuffer() Buffer { func NewBuffer() Buffer {
return Buffer{ return Buffer{
CellMap: make(map[image.Point]Cell), CellMap: make(map[image.Point]Cell),
Area: &image.Rectangle{}} Area: image.Rectangle{}}
} }
// Fill fills the Buffer b with ch,fg and bg. // Fill fills the Buffer b with ch,fg and bg.

View File

@ -18,12 +18,13 @@ func Init() error {
Body.X = 0 Body.X = 0
Body.Y = 0 Body.Y = 0
Body.BgColor = theme.BodyBg Body.BgColor = theme.BodyBg
defer func() { if err := tm.Init(); err != nil {
w, _ := tm.Size() return err
Body.Width = w }
evtListen() w, _ := tm.Size()
}() Body.Width = w
return tm.Init() evtListen()
return nil
} }
// Close finalizes termui library, // Close finalizes termui library,
@ -53,7 +54,7 @@ func Render(bs ...Bufferer) {
buf := b.Buffer() buf := b.Buffer()
// set cels in buf // set cels in buf
for p, c := range buf.CellMap { for p, c := range buf.CellMap {
if true { //}p.In(buf.Area) { if p.In(buf.Area) {
tm.SetCell(p.X, p.Y, c.Ch, toTmAttr(c.Fg), toTmAttr(c.Bg)) tm.SetCell(p.X, p.Y, c.Ch, toTmAttr(c.Fg), toTmAttr(c.Bg))
} }
} }

View File

@ -1,7 +1,6 @@
package termui package termui
import ( import (
"fmt"
"regexp" "regexp"
"strings" "strings"
) )
@ -11,6 +10,9 @@ type TextBuilder interface {
Build(s string, fg, bg Attribute) []Cell Build(s string, fg, bg Attribute) []Cell
} }
// DefaultTxBuilder is set to be MarkdownTxBuilder.
var DefaultTxBuilder = NewMarkdownTxBuilder()
// MarkdownTxBuilder implements TextBuilder interface, using markdown syntax. // MarkdownTxBuilder implements TextBuilder interface, using markdown syntax.
type MarkdownTxBuilder struct { type MarkdownTxBuilder struct {
baseFg Attribute baseFg Attribute
@ -55,10 +57,12 @@ func (mtb MarkdownTxBuilder) readAttr(s string) (Attribute, Attribute) {
updateAttr := func(a Attribute, attrs []string) Attribute { updateAttr := func(a Attribute, attrs []string) Attribute {
for _, s := range attrs { for _, s := range attrs {
// replace the color
if c, ok := colorMap[s]; ok { if c, ok := colorMap[s]; ok {
a &= 0xFF00 //erase clr 0 ~ 8 bits a &= 0xFF00 // erase clr 0 ~ 8 bits
a |= c // set clr a |= c // set clr
} }
// add attrs
if c, ok := attrMap[s]; ok { if c, ok := attrMap[s]; ok {
a |= c a |= c
} }
@ -91,7 +95,7 @@ func (mtb *MarkdownTxBuilder) reset() {
mtb.markers = []marker{} mtb.markers = []marker{}
} }
// parse // parse streams and parses text into normalized text and render sequence.
func (mtb *MarkdownTxBuilder) parse(str string) { func (mtb *MarkdownTxBuilder) parse(str string) {
rs := str2runes(str) rs := str2runes(str)
normTx := []rune{} normTx := []rune{}
@ -108,19 +112,14 @@ func (mtb *MarkdownTxBuilder) parse(str string) {
accBrackt = false accBrackt = false
cntSquare = 0 cntSquare = 0
} }
// pipe stacks into normTx and clear
rollback := func() { rollback := func() {
normTx = append(normTx, square...) normTx = append(normTx, square...)
normTx = append(normTx, brackt...) normTx = append(normTx, brackt...)
reset() reset()
} }
// chop first and last
chop := func(s []rune) []rune { chop := func(s []rune) []rune {
defer func() {
if r := recover(); r != nil {
fmt.Println(string(s))
}
}()
return s[1 : len(s)-1] return s[1 : len(s)-1]
} }
@ -185,6 +184,7 @@ func (mtb *MarkdownTxBuilder) parse(str string) {
mtb.plainTx = normTx mtb.plainTx = normTx
} }
// Build implements TextBuilder interface.
func (mtb MarkdownTxBuilder) Build(s string, fg, bg Attribute) []Cell { func (mtb MarkdownTxBuilder) Build(s string, fg, bg Attribute) []Cell {
mtb.baseFg = fg mtb.baseFg = fg
mtb.baseBg = bg mtb.baseBg = bg
@ -204,6 +204,7 @@ func (mtb MarkdownTxBuilder) Build(s string, fg, bg Attribute) []Cell {
return cs return cs
} }
// NewMarkdownTxBuilder returns a TextBuilder employing markdown syntax.
func NewMarkdownTxBuilder() TextBuilder { func NewMarkdownTxBuilder() TextBuilder {
return MarkdownTxBuilder{} return MarkdownTxBuilder{}
} }