box-cli-maker icon indicating copy to clipboard operation
box-cli-maker copied to clipboard

[BUG] long text fails to wrap

Open gennaro-tedesco opened this issue 3 years ago • 12 comments

Describe the bug Text lines that are longer than terminal width fail to wrap inside the box

To Reproduce You can reproduce the behaviour by simply creating a string that is longer than terminal width: notice that you can retrieve the current terminal length with

import (
    "golang.org/x/term"
)

width, _, err := term.GetSize(0)
if err != nil {
    return
}

and replicate a string of given width.

Then simply

Box.Print("title", long_string)

produces effects like this.

Expected behavior The box wraps text automatically. Ideally the user can specify wrap after a certain length.

Workaround At the moment I am making use of go-wordwrap to wrap the text myself before feeding it into the box.

import (
	"github.com/mitchellh/go-wordwrap"
)
Box.Print("title", wordwrap.WrapString(sb.String(), uint((2*width)/3)))

Versions (please complete the following information, if relevant):

Machine:   MacBookPro15,2
Kernel:     Darwin 21.2.0
OS         macOS 12.1.0 Monterey
Terminal   iTerm2 (Version 3.4.14)
Shell      /bin/zsh

gennaro-tedesco avatar Feb 04 '22 07:02 gennaro-tedesco

Thanks for raising the issue. I will see what I can do.

Delta456 avatar Feb 04 '22 08:02 Delta456

Hmm I am thinking of making a flag like AllowWrapping when set to true will allow wrapping of lines else the lines won't be wrapped. I also would like to know if I should also let the user provide the number or uint((2*width)/3)) should be used instead?

Delta456 avatar Feb 04 '22 15:02 Delta456

I am thinking of making a flag like AllowWrapping when set to true will allow wrapping of lines

Yes, this sounds like the best solution.

if I should also let the user provide the number or uint((2*width)/3)) should be used instead?

another idea could be, instead of AllowWrapping = true/false, to have a numerical parameter, say WrappingWidth = 0,...,1 between 0 and 1 that directly specifies the size to wrap at, with 1 meaning "no wrap" or so. Which begs the question: is there any use case where users do not want to wrap text? I cannot imagine any scenario where users may want to leave the text pending off terminal width 🤔

In any case this probably depends on the implementation complexity and I would be happy already to have a boolean parameter that allows wrapping at a fixed length by default, just to avoid the boxes to be messed up in some cases.

gennaro-tedesco avatar Feb 04 '22 19:02 gennaro-tedesco

By the way, just for reference, this is the project I am working on and here is where I am invoking your library.

gennaro-tedesco avatar Feb 04 '22 21:02 gennaro-tedesco

In some scenarios people may want to pad according to their own needs so I think I will let the user allow it. By default it would be uint((width*2)/3).

What you think about this? 🤔

Also it looks like you are using a very old version of the library. v2.2.2 is the latest version.

Delta456 avatar Feb 05 '22 05:02 Delta456

After several testing, I will be using muesli/reflow instead of go-wordwrap because the former also has support for CJK, tabs and emojis and it provides several options.

Delta456 avatar Feb 05 '22 07:02 Delta456

Great idea, I am looking forward to the new release! :)

gennaro-tedesco avatar Feb 05 '22 11:02 gennaro-tedesco

In some scenarios people may want to pad according to their own needs so I think I will let the user allow it. By default it would be uint((width*2)/3).

What you think about this? 🤔

Also it looks like you are using a very old version of the library. v2.2.2 is the latest version.

Please what you think about this

Delta456 avatar Feb 05 '22 11:02 Delta456

In some scenarios people may want to pad according to their own needs so I think I will let the user allow it.

yes, I agree that giving the user more flexibility may help in many cases. Moreover 2/3 is also a good default!

Also it looks like you are using a very old version of the library. v2.2.2 is the latest version

oooops, I just noticed it too. I will update the modules! What are the main differences (or do you have a page with new release notes I can read)?

gennaro-tedesco avatar Feb 05 '22 11:02 gennaro-tedesco

oooops, I just noticed it too. I will update the modules! What are the main differences (or do you have a page with new release notes I can read)?

Yes, it is here.

Delta456 avatar Feb 05 '22 11:02 Delta456

Heads up. I have implemented this feature in this commit https://github.com/Delta456/box-cli-maker/pull/22/commits/d7a6d036685c0736876fb03222984296dc87dde7 you can read how the future API would be.

Delta456 avatar Feb 06 '22 11:02 Delta456

I am having a look, thank you!

gennaro-tedesco avatar Feb 06 '22 12:02 gennaro-tedesco