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
import ui "github.com/jrmiller82/termui"
import ui "github.com/gizak/termui"
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.WrapLength = 48 // this should be at least p.Width - 2
p.Height = 30
p.Height = 20
p.Width = 50
p.Y = 2
p.X = 20
@ -28,5 +28,4 @@ func main() {
})
ui.Loop()
}

View File

@ -212,3 +212,11 @@ func DTrimTxCls(cs []Cell, w int) []Cell {
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
TextFgColor 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.
@ -35,11 +35,13 @@ func (p *Par) Buffer() Buffer {
buf := p.Block.Buffer()
fg, bg := p.TextFgColor, p.TextBgColor
cs := []Cell{}
if p.WrapLength < 1 {
cs = DefaultTxBuilder.Build(p.Text, fg, bg)
} else {
cs = DefaultTxBuilder.BuildWrap(p.Text, fg, bg, p.WrapLength)
cs := DefaultTxBuilder.Build(p.Text, fg, bg)
// wrap if WrapLength set
if p.WrapLength < 0 {
cs = wrapTx(cs, p.Width-2)
} else if p.WrapLength > 0 {
cs = wrapTx(cs, p.WrapLength)
}
y, x, n := 0, 0, 0

View File

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