diff --git a/_example/wrappar.go b/_example/wrappar.go index 69824e2..23e00a0 100644 --- a/_example/wrappar.go +++ b/_example/wrappar.go @@ -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() - } diff --git a/helper.go b/helper.go index 840c9bb..e563888 100644 --- a/helper.go +++ b/helper.go @@ -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 +} diff --git a/par.go b/par.go index a60262c..354519d 100644 --- a/par.go +++ b/par.go @@ -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 diff --git a/textbuilder.go b/textbuilder.go index 7d45208..2627098 100644 --- a/textbuilder.go +++ b/textbuilder.go @@ -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 {