From 62105f6883c6ed4bf99c871f63e1ded4d9625fdc Mon Sep 17 00:00:00 2001 From: gizak Date: Sat, 2 May 2015 23:35:06 -0400 Subject: [PATCH] WIP MarkdownTxBuilder --- block.go | 16 ++++------- block_main.go | 19 +++++++++++++ grid.go | 10 +++---- textRender.go | 77 +++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 107 insertions(+), 15 deletions(-) create mode 100644 block_main.go diff --git a/block.go b/block.go index c479140..9fc5f5f 100644 --- a/block.go +++ b/block.go @@ -6,10 +6,6 @@ package termui import "image" -// Copyright 2015 Zack Guo . All rights reserved. -// Use of this source code is governed by a MIT license that can -// be found in the LICENSE file. - // Hline is a horizontal line. type Hline struct { X int @@ -33,7 +29,7 @@ func (l Hline) Buffer() Buffer { if l.Len <= 0 { return NewBuffer() } - return NewFilledBuffer(l.X, l.Y, l.X+l.Len, l.Y, HORIZONTAL_LINE, l.Fg, l.Bg) + return NewFilledBuffer(l.X, l.Y, l.X+l.Len, l.Y+1, HORIZONTAL_LINE, l.Fg, l.Bg) } // Buffer draws a vertical line. @@ -41,7 +37,7 @@ func (l Vline) Buffer() Buffer { if l.Len <= 0 { return NewBuffer() } - return NewFilledBuffer(l.X, l.Y, l.X, l.Y+l.Len, VERTICAL_LINE, l.Fg, l.Bg) + return NewFilledBuffer(l.X, l.Y, l.X+1, l.Y+l.Len, VERTICAL_LINE, l.Fg, l.Bg) } // Buffer draws a box border. @@ -55,8 +51,8 @@ func (b Block) drawBorder(buf Buffer) { x0 := min.X y0 := min.Y - x1 := max.X - y1 := max.Y + x1 := max.X - 1 + y1 := max.Y - 1 // draw lines if b.BorderTop { @@ -148,8 +144,8 @@ func NewBlock() *Block { func (b *Block) Align() { b.area.Min.X = b.X b.area.Min.Y = b.Y - b.area.Max.X = b.X + b.Width - 1 - b.area.Max.Y = b.Y + b.Height - 1 + b.area.Max.X = b.X + b.Width + b.area.Max.Y = b.Y + b.Height b.innerArea.Min.X = b.X + b.PaddingLeft b.innerArea.Min.Y = b.Y + b.PaddingTop diff --git a/block_main.go b/block_main.go new file mode 100644 index 0000000..a55160c --- /dev/null +++ b/block_main.go @@ -0,0 +1,19 @@ +// +build ignore + +package main + +import "github.com/gizak/termui" + +func main() { + termui.Init() + + termui.UseTheme("helloworld") + b := termui.NewBlock() + b.Width = 20 + b.Height = 30 + b.BorderLabel = "HELLO WORLD" + + termui.Render(b) + <-termui.EventCh() + termui.Close() +} diff --git a/grid.go b/grid.go index 1b15b92..cd6f896 100644 --- a/grid.go +++ b/grid.go @@ -161,7 +161,7 @@ func (r *Row) SetWidth(w int) { // Buffer implements Bufferer interface, // recursively merge all widgets buffer func (r *Row) Buffer() Buffer { - merged := Buffer{} + merged := NewBuffer() if r.isRenderableLeaf() { return r.Widget.Buffer() @@ -169,13 +169,13 @@ func (r *Row) Buffer() Buffer { // for those are not leaves but have a renderable widget if r.Widget != nil { - merged.Union(r.Widget.Buffer()) + merged.Merge(r.Widget.Buffer()) } // collect buffer from children if !r.isLeaf() { for _, c := range r.Cols { - merged.Union(c.Buffer()) + merged.Merge(c.Buffer()) } } @@ -268,10 +268,10 @@ func (g *Grid) Align() { // Buffer implments Bufferer interface. func (g Grid) Buffer() Buffer { - buf := Buffer{} + buf := NewBuffer() for _, r := range g.Rows { - buf.Union(r.Buffer()) + buf.Merge(r.Buffer()) } return buf } diff --git a/textRender.go b/textRender.go index 8a0f1ca..97a5aba 100644 --- a/textRender.go +++ b/textRender.go @@ -9,6 +9,83 @@ import ( "strings" ) +// Minial interface +type TextBuilder interface { + Build(s string, fg, bg Attribute) []Cells +} + +type MarkdownTxBuilder struct { + regex string + pattern *regexp.Regexp + baseFg Attribute + baseBg Attribute +} + +var colorMap = map[string]Attribute{ + "red": ColorRed, + "blue": ColorBlue, + "black": ColorBlack, + "cyan": ColorCyan, + "white": ColorWhite, + "default": ColorDefault, + "green": ColorGreen, + "magenta": ColorMagenta, +} + +var attrMap = map[string]Attribute{ + "bold": AttrBold, + "underline": AttrUnderline, + "reverse": AttrReverse, +} + +func rmSpc(s string) string { + reg := regexp.MustCompile(`\s+`) + return reg.ReplaceAllString(s, "") +} + +// readAttr translates strings like `fg-red,fg-bold,bg-white` to fg and bg Attribute +func (mtb MarkdownTxBuilder) readAttr(s string) (Attribute, Attribute) { + fg := mtb.baseFg + bg := mtb.baseBg + + updateAttr := func(a Attribute, attrs []string) Attribute { + for _, s := range attrs { + if c, ok := colorMap[s]; ok { + a &= ^(1<<9 - 1) //erase clr 0 ~ 1<<9-1 + a |= c // set clr + } + if c, ok := attrMap[s]; ok { + a |= c + } + } + return a + } + + ss := strings.Split(s, ",") + fgs := []string{} + bgs := []string{} + for _, v := range ss { + subs := strings.Split(ss, "-") + if len(subs) > 1 { + if subs[0] == "fg" { + fgs := append(fgs, subs[1]) + } + if subs[0] == "bg" { + bgs := append(bgs, subs[1]) + } + } + } + + fg = updateAttr(fg) + bg = updateAttr(bg) + return fg, bg +} + +type EscCodeTxBuilder struct { + regex string + pattern *regexp.Regexp +} + // TextRenderer adds common methods for rendering a text on screeen. type TextRenderer interface { NormalizedText() string