mirror of https://github.com/gizak/termui.git
WIP MarkdownTxBuilder
This commit is contained in:
parent
b65224cdc9
commit
62105f6883
16
block.go
16
block.go
|
@ -6,10 +6,6 @@ package termui
|
|||
|
||||
import "image"
|
||||
|
||||
// Copyright 2015 Zack Guo <gizak@icloud.com>. 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
|
||||
|
|
|
@ -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()
|
||||
}
|
10
grid.go
10
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
|
||||
}
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue