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

refactor(textarea): Improve setting width #496

Merged

Conversation

mikelorant
Copy link
Contributor

When setting the width of the textarea there were some issues preventing this from working correctly. These problems included:

  • If the maximum width needed to be used, the width of the textarea did not take into account the prompt and line number width.
  • The viewport width did not take into account the style width.

The entire function was confusing to understand and a refactor was warranted.

As part of this refactor, the bugs mentioned above were fixed and the code was simplified.

To verify that the logic works as expected, unit tests were expanded to validate that setting the width works as expected.

@mikelorant
Copy link
Contributor Author

@maaslalani Can I get a review of this one. Recommend side by side view as it helps see the differences.

@maaslalani
Copy link
Member

@mikelorant Will do soon! Thank you for this PR.

@mikelorant
Copy link
Contributor Author

@maaslalani @meowgorithm Any chance of a review of this one?

@maaslalani
Copy link
Member

Hey @mikelorant, thanks for the reminder on this one, will take a look soon!

@maaslalani
Copy link
Member

@mikelorant, we'll need to enforce a minimum width as well I believe.

Here I set the width to 6 which breaks since it's smaller than the border + line numbers + prompt.

image

@mikelorant
Copy link
Contributor Author

mikelorant commented Apr 3, 2024

@maaslalani Yikes. How much is a reasonable minimum width? It was 2 before and that was silly. Or should we calculate it?

@maaslalani
Copy link
Member

@maaslalani Yikes. How much is a reasonable minimum width? It was 2 before and that was silly. Or should we calculate it?

I think 2 + (horizontal frame + show line numbers && 4) to be the minimum makes sense but I can also see a possibility of respecting the SetWidth no matter what and turning off show line numbers if the width is smaller, etc...

I think a minimum of 10 is reasonable though.

@mikelorant
Copy link
Contributor Author

@maaslalani Will work on an update to this PR. I will try and make this a bit intelligent.

Thanks for the initial review.

@mikelorant
Copy link
Contributor Author

@maaslalani Decided to refactor it to be more logical. I think the code came out much better and we have an adaptive minimum width. Can have a minimum of 1 character width for input (sort of useless but if you want to be a madman 🤷 ).

@mikelorant
Copy link
Contributor Author

Still seeing the issues you reported so likely something else going on.

textarea

When setting the width of the textarea there were some issues
preventing this from working correctly. These problems included:

- If the maximum width needed to be used, the width of the textarea did
  not take into account the prompt and line number width.
- The viewport width did not take into account the style width.

The entire function was confusing to understand and a refactor was
warranted.

As part of this refactor, the bugs mentioned above were fixed and the
code was simplified.

To verify that the logic works as expected, unit tests were expanded to
validate that setting the width works as expected.

Signed-off-by: Michael Lorant <michael.lorant@nine.com.au>
@mikelorant
Copy link
Contributor Author

mikelorant commented Apr 6, 2024

This was caused by adding the ... for the placeholder when truncated.

Solution was to apply truncate twice, once when adding the ... and again to make sure we don't exceed the minimum width.

textarea-fixed

@mikelorant
Copy link
Contributor Author

Will make it clear that this doesn't work perfectly due to the new line being added unnecessarily. This is fixed by the the work that @aymanbagabas is doing with the new word wrap functions.

new-line-bug

Issue: charmbracelet/x/issues/58
Fixed-by: charmbracelet/x/pull/59

@maaslalani
Copy link
Member

Thanks you so much for your hard work on this @mikelorant!

Going to test this out and merge ASAP

@mikelorant
Copy link
Contributor Author

@maaslalani Glad to be able to add this one. It is mostly tests in this PR.

Are you happy with the approach of the double truncate to deal with ... not fitting?

@maaslalani
Copy link
Member

Superb work @mikelorant, this is looking so good from my testing! Thank you so much, especially for all the tests! ❤️

@maaslalani maaslalani merged commit 48dffdd into charmbracelet:master Apr 6, 2024
9 checks passed
@mikelorant
Copy link
Contributor Author

@maaslalani Thanks for your attention to detail to get the highest quality pull requests. Don't settle for average!

@mikelorant mikelorant deleted the fix/text-area-set-width branch April 6, 2024 05:45
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.

None yet

2 participants