gizak / termui

Golang terminal dashboard
MIT License
13.14k stars 787 forks source link

Feature textwrapping #60

Closed jrmiller82 closed 8 years ago

jrmiller82 commented 8 years ago

Gizak:

Here's a quick and dirty addition of text wrapping to anything called with NewPar. If the Par has a Par.WrapLength greater than x, a new varation of Build called BuildWrap is called. It still retains your neat markdown color syntax while getting nice naive wrapping of the text.

It's less efficient than it could be; I'm self taught so my logic might not be the best. I used a loop that breaks and re-reruns a lot to make sure the unwrapped and wrapped texts were properly compared and to avoid index errors. There is likely a more efficient way to do this; but, since we're just talking about wrapping small amounts of text, and with Go's inherent speed, I'm not too worried about it.

It imports (but does not vendor) github.com/mitchellh/go-wordwrap (MIT licensed). The import is called in textbuilder.go. The src is fairly short if you want to bring it inside of termui so you're not having to import it. But, I'll leave that decision up to you.

I also added the BuildWrap func to the TextBuilder interface as it was the only way to get around not having to entirely rewrite the whole call stack. Maybe in the future it would be best to just rewrite Build to take into account the wrapping logic in BuildWrap; but, I couldn't exactly grok your build stuff...

Let me know what you think. Termui rocks!

Oh, after the PR, the import statement in the example file points to my fork, and not the main repo; it will need to be changed.

gizak commented 8 years ago

:+1: Merged but with some modification: BuildWrap will break compatibility so I convert it to wrapTx as a normal function call.

jrmiller82 commented 8 years ago

Wonderful. Glad you found it useful.