Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extra newlines when combining unconditional and standard word wrapping #43

Open
meowgorithm opened this issue Sep 22, 2021 · 5 comments · Fixed by charmbracelet/x#57 or charmbracelet/lipgloss#275
Labels
bug Something isn't working

Comments

@meowgorithm
Copy link
Contributor

When combining word-wrapping with unconditional wrapping as described in the README, extra linebreaks can sometimes be found in the output.

For example:

const str = "the quick brown foxxxxxxxxxxxxxxxx jumped over the lazy dog."
const limit = 16
wrapped := wrap.String(wordwrap.String(str, limit), limit)
fmt.Println(wrapped)

Outputs:

the quick brown
foxxxxxxxxxxxxxx
xx
jumped over the
lazy dog.

However, I'd expect it to be:

the quick brown
foxxxxxxxxxxxxxx
xx jumped over 
the lazy dog.

Playground Example

@bashbunni
Copy link

We could probably enhance wordwrap to include forceful newlines to break words at the given width. The wrapper is behaving as expected in this case. (New lines are preserved by default)
https://github.com/bashbunni/reflow/blob/master/wrap/wrap.go#L36

@janderland
Copy link

I'm also running into this issue. I may be able to take a stab at implementing what @bashbunni is describing if she isn't doing so already.

@muesli
Copy link
Owner

muesli commented Sep 18, 2023

I'm also running into this issue. I may be able to take a stab at implementing what @bashbunni is describing if she isn't doing so already.

Depending on what you actually need, you can also just initialize a new wrap.Writer and set PreserveNewlines to false.

@janderland
Copy link

I'm also running into this issue. I may be able to take a stab at implementing what @bashbunni is describing if she isn't doing so already.

Depending on what you actually need, you can also just initialize a new wrap.Writer and set PreserveNewlines to false.

By setting wrap.Wrap.PreserveNewlines to false, won't this cause wrap.Wrap to undo the work which wordwrap.Wrap did? wordwrap.Wrap is only adding newlines to the string. If we don't preserve them it will be as if wordwrap.Wrap never modified the original string, right?

@janderland
Copy link

OK, I took a stab at adding this feature the library but ran into some problems while trying to maintain the current functionality. To better wrap my head around the problem, I implemented the wrapping functionality I need in my own project. I'm still open to porting this functionality back into this project if it's something the author wants.

aymanbagabas added a commit to charmbracelet/lipgloss that referenced this issue Mar 28, 2024
aymanbagabas added a commit to charmbracelet/lipgloss that referenced this issue Mar 28, 2024
aymanbagabas added a commit to charmbracelet/lipgloss that referenced this issue Mar 28, 2024
aymanbagabas added a commit to charmbracelet/lipgloss that referenced this issue Mar 28, 2024
aymanbagabas added a commit to charmbracelet/lipgloss that referenced this issue Mar 28, 2024
aymanbagabas added a commit to charmbracelet/lipgloss that referenced this issue Mar 28, 2024
aymanbagabas added a commit to charmbracelet/lipgloss that referenced this issue Mar 28, 2024
aymanbagabas added a commit to charmbracelet/x that referenced this issue Mar 28, 2024
ANSI aware text wrapping that breaks word boundaries only when
necessary.

Fixes: charmbracelet/lipgloss#275
Fixes: muesli/reflow#43
aymanbagabas added a commit to charmbracelet/x that referenced this issue Mar 28, 2024
ANSI aware text wrapping that breaks word boundaries only when
necessary.

Fixes: charmbracelet/lipgloss#275
Fixes: muesli/reflow#43
aymanbagabas added a commit to charmbracelet/x that referenced this issue Mar 28, 2024
ANSI aware text wrapping that breaks word boundaries only when
necessary.

Fixes: charmbracelet/lipgloss#275
Fixes: muesli/reflow#43
aymanbagabas added a commit to charmbracelet/lipgloss that referenced this issue Mar 28, 2024
aymanbagabas added a commit to charmbracelet/x that referenced this issue Mar 28, 2024
ANSI aware text wrapping that breaks word boundaries only when
necessary.

Fixes: charmbracelet/lipgloss#275
Fixes: muesli/reflow#43
aymanbagabas added a commit to charmbracelet/lipgloss that referenced this issue Mar 28, 2024
aymanbagabas added a commit to charmbracelet/x that referenced this issue Mar 28, 2024
ANSI aware text wrapping that breaks word boundaries only when
necessary.

Fixes: charmbracelet/lipgloss#275
Fixes: muesli/reflow#43
aymanbagabas added a commit to charmbracelet/x that referenced this issue Mar 28, 2024
ANSI aware text wrapping that breaks word boundaries only when
necessary.

Fixes: charmbracelet/lipgloss#275
Fixes: muesli/reflow#43
aymanbagabas added a commit to charmbracelet/lipgloss that referenced this issue Mar 28, 2024
aymanbagabas added a commit to charmbracelet/lipgloss that referenced this issue Mar 28, 2024
aymanbagabas added a commit to charmbracelet/x that referenced this issue Mar 29, 2024
* feat(term): ansi: add SmartWrap

ANSI aware text wrapping that breaks word boundaries only when
necessary.

Fixes: charmbracelet/lipgloss#275
Fixes: muesli/reflow#43

* refactor(term): ansi: rename wrap functions
aymanbagabas added a commit to charmbracelet/lipgloss that referenced this issue Mar 29, 2024
* feat: switch to term/ansi for text manipulation

Use ANSI aware, wide characters support, uniseg backed term/ansi package
to calculate string widths, truncate, and wrap strings.

Related: muesli/reflow#71
Fixes: #258
Fixes: #220

* fix: combining both conditional and unconditional wrapping

Uses `ansi.SmartWrap` charmbracelet/x#57

Fixes: muesli/reflow#43

* chore: update deps

* Update get.go
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
4 participants