Convert BuildWrap to wrapTx

This commit is contained in:
gizak 2016-03-10 15:49:31 -05:00
parent 7dd60de21e
commit 4701bd971f
4 changed files with 26 additions and 22 deletions

View File

@ -1,6 +1,6 @@
package main package main
import ui "github.com/jrmiller82/termui" import ui "github.com/gizak/termui"
func main() { func main() {
@ -12,7 +12,7 @@ func main() {
p := ui.NewPar("Press q to QUIT THE DEMO. [There](fg-blue) are other things [that](fg-red) are going to fit in here I think. What do you think? Now is the time for all good [men to](bg-blue) come to the aid of their country. [This is going to be one really really really long line](fg-green) that is going to go together and stuffs and things. Let's see how this thing renders out.\n Here is a new paragraph and stuffs and things. There should be a tab indent at the beginning of the paragraph. Let's see if that worked as well.") p := ui.NewPar("Press q to QUIT THE DEMO. [There](fg-blue) are other things [that](fg-red) are going to fit in here I think. What do you think? Now is the time for all good [men to](bg-blue) come to the aid of their country. [This is going to be one really really really long line](fg-green) that is going to go together and stuffs and things. Let's see how this thing renders out.\n Here is a new paragraph and stuffs and things. There should be a tab indent at the beginning of the paragraph. Let's see if that worked as well.")
p.WrapLength = 48 // this should be at least p.Width - 2 p.WrapLength = 48 // this should be at least p.Width - 2
p.Height = 30 p.Height = 20
p.Width = 50 p.Width = 50
p.Y = 2 p.Y = 2
p.X = 20 p.X = 20
@ -28,5 +28,4 @@ func main() {
}) })
ui.Loop() ui.Loop()
} }

View File

@ -212,3 +212,11 @@ func DTrimTxCls(cs []Cell, w int) []Cell {
return rt return rt
} }
func CellsToStr(cs []Cell) string {
str := ""
for _, c := range cs {
str += string(c.Ch)
}
return str
}

14
par.go
View File

@ -16,7 +16,7 @@ type Par struct {
Text string Text string
TextFgColor Attribute TextFgColor Attribute
TextBgColor Attribute TextBgColor Attribute
WrapLength uint WrapLength int // words wrap limit. Note it may not work properly with multi-width char
} }
// NewPar returns a new *Par with given text as its content. // NewPar returns a new *Par with given text as its content.
@ -35,11 +35,13 @@ func (p *Par) Buffer() Buffer {
buf := p.Block.Buffer() buf := p.Block.Buffer()
fg, bg := p.TextFgColor, p.TextBgColor fg, bg := p.TextFgColor, p.TextBgColor
cs := []Cell{} cs := DefaultTxBuilder.Build(p.Text, fg, bg)
if p.WrapLength < 1 {
cs = DefaultTxBuilder.Build(p.Text, fg, bg) // wrap if WrapLength set
} else { if p.WrapLength < 0 {
cs = DefaultTxBuilder.BuildWrap(p.Text, fg, bg, p.WrapLength) cs = wrapTx(cs, p.Width-2)
} else if p.WrapLength > 0 {
cs = wrapTx(cs, p.WrapLength)
} }
y, x, n := 0, 0, 0 y, x, n := 0, 0, 0

View File

@ -1,15 +1,15 @@
package termui package termui
import ( import (
"github.com/mitchellh/go-wordwrap" // LEFT UP TO PKG MAINTAINER TO DECIDE HOW TO VENDOR; is MIT LICENSED
"regexp" "regexp"
"strings" "strings"
"github.com/mitchellh/go-wordwrap"
) )
// TextBuilder is a minimal interface to produce text []Cell using specific syntax (markdown). // TextBuilder is a minimal interface to produce text []Cell using specific syntax (markdown).
type TextBuilder interface { type TextBuilder interface {
Build(s string, fg, bg Attribute) []Cell Build(s string, fg, bg Attribute) []Cell
BuildWrap(s string, fg, bg Attribute, wl uint) []Cell
} }
// DefaultTxBuilder is set to be MarkdownTxBuilder. // DefaultTxBuilder is set to be MarkdownTxBuilder.
@ -186,20 +186,15 @@ func (mtb *MarkdownTxBuilder) parse(str string) {
mtb.plainTx = normTx mtb.plainTx = normTx
} }
// BuildWrap implements TextBuilder interface and will naively wrap the plain func wrapTx(cs []Cell, wl int) []Cell {
// text string to length specified by wl while preserving the fg and bg tmpCell := make([]Cell, len(cs))
// attributes copy(tmpCell, cs)
func (mtb MarkdownTxBuilder) BuildWrap(s string, fg, bg Attribute, wl uint) []Cell {
// get the []Cell from plain Build
tmpCell := mtb.Build(s, fg, bg)
// get the plaintext // get the plaintext
mtb.parse(s) plain := CellsToStr(cs)
plain := string(mtb.plainTx)
// wrap // wrap
plainWrapped := wordwrap.WrapString(plain, wl) plainWrapped := wordwrap.WrapString(plain, uint(wl))
// find differences and insert // find differences and insert
finalCell := tmpCell // finalcell will get the inserts and is what is returned finalCell := tmpCell // finalcell will get the inserts and is what is returned
@ -211,7 +206,7 @@ func (mtb MarkdownTxBuilder) BuildWrap(s string, fg, bg Attribute, wl uint) []Ce
for trigger != "stop" { for trigger != "stop" {
plainRune = plainRuneNew plainRune = plainRuneNew
for i, _ := range plainRune { for i := range plainRune {
if plainRune[i] == plainWrappedRune[i] { if plainRune[i] == plainWrappedRune[i] {
trigger = "stop" trigger = "stop"
} else if plainRune[i] != plainWrappedRune[i] && plainWrappedRune[i] == 10 { } else if plainRune[i] != plainWrappedRune[i] && plainWrappedRune[i] == 10 {