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: handle textinput max-width edge cases #484

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

JaredReisinger
Copy link

@JaredReisinger JaredReisinger commented Feb 28, 2024

I noticed that the cursor seemed to be adding an extra space which the width calculations weren't accounting for. This also led me to discover an edge case with the view (offset/offsetRight) calculation when the cursor (pos) sits at the very end. Additionally, View() was always rendering the cursor, even when focus was false, which the comment suggests should not be the case.

These should all be resolved, such that Width is now fully respected in both View() and placeholderView(): they should never render a string that exceeds the requested width. (Well... with one caveat: I did not touch the suggestion/completion logic, as I've never used it. I did leave a comment, however, that perhaps suggestions shouldn't even render when the component does not have focus.)

Fixes #358, #307

I noticed that the cursor seemed to be adding an extra space which the
width calculations weren't accounting for. This also led me to discover
an edge case with the view (`offset`/`offsetRight`) calculation when the
cursor (`pos`) sits at the very end. Additionally, `View()` was always
rendering the cursor, even when `focus` was false, which the comment
suggests should not be the case.

These should all be resolved, such that Width is now fully respected in
both `View()` and `placeholderView()`: they should _never_ render a
string that exceeds the requested width. (Well... with one caveat: I did
not touch the suggestion logic, as I've never used it. I did leave a
comment, however, that perhaps suggestions shouldn't even render when
the component does not have focus.)

Fixes charmbracelet#358, charmbracelet#307
@JaredReisinger
Copy link
Author

My particular use-case involves dynamically setting the Width as edits are happening (it's a semi-WYSIWYG-style TUI), using len(text.Value()) + 1 when it has focus, and len(text.Value()) when it does not. Without this PR, the textinput was always rendering two extra spaces at the end. With this fix, the textinput only renders the requested Width.

I can provide a minimal repro example if needed.

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.

textinput: placeholder changes component width
1 participant