diff --git a/block.go b/block.go index 9fc5f5f..85e928d 100644 --- a/block.go +++ b/block.go @@ -85,7 +85,7 @@ func (b Block) drawBorder(buf Buffer) { func (b Block) drawBorderLabel(buf Buffer) { 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++ { buf.Set(b.area.Min.X+1+w, b.area.Min.Y, tx[i]) diff --git a/block_main.go b/block_main.go index a55160c..6e147f0 100644 --- a/block_main.go +++ b/block_main.go @@ -11,7 +11,7 @@ func main() { b := termui.NewBlock() b.Width = 20 b.Height = 30 - b.BorderLabel = "HELLO WORLD" + b.BorderLabel = "[HELLO](fg-red,bg-white) [WORLD](fg-blue,bg-green)" termui.Render(b) <-termui.EventCh() diff --git a/buffer.go b/buffer.go index ea2064a..3cc562f 100644 --- a/buffer.go +++ b/buffer.go @@ -15,7 +15,7 @@ type Cell struct { // Buffer is a renderable rectangle cell data container. type Buffer struct { - Area *image.Rectangle // selected drawing area + Area image.Rectangle // selected drawing area CellMap map[image.Point]Cell } @@ -50,7 +50,7 @@ func (b Buffer) Bounds() image.Rectangle { } // 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.Min = r.Min } @@ -71,24 +71,15 @@ func (b Buffer) Merge(bs ...Buffer) { for p, v := range buf.CellMap { 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 func NewBuffer() Buffer { return Buffer{ CellMap: make(map[image.Point]Cell), - Area: &image.Rectangle{}} + Area: image.Rectangle{}} } // Fill fills the Buffer b with ch,fg and bg. diff --git a/render.go b/render.go index d1a5891..c471238 100644 --- a/render.go +++ b/render.go @@ -18,12 +18,13 @@ func Init() error { Body.X = 0 Body.Y = 0 Body.BgColor = theme.BodyBg - defer func() { - w, _ := tm.Size() - Body.Width = w - evtListen() - }() - return tm.Init() + if err := tm.Init(); err != nil { + return err + } + w, _ := tm.Size() + Body.Width = w + evtListen() + return nil } // Close finalizes termui library, @@ -53,7 +54,7 @@ func Render(bs ...Bufferer) { buf := b.Buffer() // set cels in buf 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)) } } diff --git a/textbuilder.go b/textbuilder.go index 4c7f2af..79271fd 100644 --- a/textbuilder.go +++ b/textbuilder.go @@ -1,7 +1,6 @@ package termui import ( - "fmt" "regexp" "strings" ) @@ -11,6 +10,9 @@ type TextBuilder interface { Build(s string, fg, bg Attribute) []Cell } +// DefaultTxBuilder is set to be MarkdownTxBuilder. +var DefaultTxBuilder = NewMarkdownTxBuilder() + // MarkdownTxBuilder implements TextBuilder interface, using markdown syntax. type MarkdownTxBuilder struct { baseFg Attribute @@ -55,10 +57,12 @@ func (mtb MarkdownTxBuilder) readAttr(s string) (Attribute, Attribute) { updateAttr := func(a Attribute, attrs []string) Attribute { for _, s := range attrs { + // replace the color if c, ok := colorMap[s]; ok { - a &= 0xFF00 //erase clr 0 ~ 8 bits + a &= 0xFF00 // erase clr 0 ~ 8 bits a |= c // set clr } + // add attrs if c, ok := attrMap[s]; ok { a |= c } @@ -91,7 +95,7 @@ func (mtb *MarkdownTxBuilder) reset() { mtb.markers = []marker{} } -// parse +// parse streams and parses text into normalized text and render sequence. func (mtb *MarkdownTxBuilder) parse(str string) { rs := str2runes(str) normTx := []rune{} @@ -108,19 +112,14 @@ func (mtb *MarkdownTxBuilder) parse(str string) { accBrackt = false cntSquare = 0 } - + // pipe stacks into normTx and clear rollback := func() { normTx = append(normTx, square...) normTx = append(normTx, brackt...) reset() } - + // chop first and last chop := func(s []rune) []rune { - defer func() { - if r := recover(); r != nil { - fmt.Println(string(s)) - } - }() return s[1 : len(s)-1] } @@ -185,6 +184,7 @@ func (mtb *MarkdownTxBuilder) parse(str string) { mtb.plainTx = normTx } +// Build implements TextBuilder interface. func (mtb MarkdownTxBuilder) Build(s string, fg, bg Attribute) []Cell { mtb.baseFg = fg mtb.baseBg = bg @@ -204,6 +204,7 @@ func (mtb MarkdownTxBuilder) Build(s string, fg, bg Attribute) []Cell { return cs } +// NewMarkdownTxBuilder returns a TextBuilder employing markdown syntax. func NewMarkdownTxBuilder() TextBuilder { return MarkdownTxBuilder{} }