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

fix(term): ansi: account for some wrap edge cases #59

Merged
merged 9 commits into from
Apr 8, 2024

Conversation

aymanbagabas
Copy link
Member

Properly count escape codes, better handling of breakpoints, and only break word/breakpoint when necessary.

Fixes: #58

Properly count escape codes, better handling of breakpoints, and only
break word/breakpoint when necessary.

Fixes: #58
exp/term/ansi/wrap.go Outdated Show resolved Hide resolved
exp/term/ansi/wrap.go Outdated Show resolved Hide resolved
exp/term/ansi/wrap.go Outdated Show resolved Hide resolved
exp/term/ansi/wrap.go Outdated Show resolved Hide resolved
exp/term/ansi/wrap_test.go Show resolved Hide resolved
exp/term/ansi/wrap_test.go Show resolved Hide resolved
exp/term/ansi/wrap.go Outdated Show resolved Hide resolved
exp/term/ansi/wrap.go Outdated Show resolved Hide resolved
@aymanbagabas
Copy link
Member Author

@mikelorant Pushed a bunch of fixes for edge cases and support for NBSP (non-breaking space) character.

Breakpoints are now respected and wrapped properly.
Support non-breaking spaces
curWidth++
default:
if wordLen+1 > limit {
if curWidth+wordLen+1 > limit {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider dropping the +1 and making it a >= comparison to simplify the logic?

@@ -199,6 +202,8 @@ func Wordwrap(s string, limit int, breakpoints string) string {
case unicode.IsSpace(r):
addWord()
space.WriteByte(b[i])
case r == '-':

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would there be value in having a small helper function IsDash to be consistent to how it is done for IsSpace?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps, if we add support to other unicode dash characters 🤔

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Something to consider, certainly nothing to stop you merging once you do decide.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thinking more about this, there are many variants of dashes defined in unicode. People who wish to use custom breakpoints can do so using the breakpoints parameter

@mikelorant
Copy link

I'm not sure of the commit best practices used by the Charm team, but I generally recommend at this point to the developers I work with to squash their commits (seeing as there are now 8 commits). Your branch, so your rules 😄

@aymanbagabas
Copy link
Member Author

I'm not sure of the commit best practices used by the Charm team, but I generally recommend at this point to the developers I work with to squash their commits (seeing as there are now 8 commits). Your branch, so your rules 😄

Yes, that was what I'm going for. I don't think the commits should be split since they're all related and address the same issues

@mikelorant
Copy link

I'd say while there might be some minor improvements possible, the current state is worth merging. LGTM.

exp/term/ansi/wrap.go Outdated Show resolved Hide resolved
@@ -199,6 +202,8 @@ func Wordwrap(s string, limit int, breakpoints string) string {
case unicode.IsSpace(r):
addWord()
space.WriteByte(b[i])
case r == '-':
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thinking more about this, there are many variants of dashes defined in unicode. People who wish to use custom breakpoints can do so using the breakpoints parameter

@aymanbagabas aymanbagabas merged commit c2c5fe8 into main Apr 8, 2024
11 checks passed
@aymanbagabas aymanbagabas deleted the term/wrap-reg branch April 8, 2024 01:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Wrap adds a new line to strings containing ANSI codes
2 participants