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

textinput: Prompt length not taken into account for width #307

Closed
mikelorant opened this issue Jan 8, 2023 · 3 comments
Closed

textinput: Prompt length not taken into account for width #307

mikelorant opened this issue Jan 8, 2023 · 3 comments

Comments

@mikelorant
Copy link
Contributor

mikelorant commented Jan 8, 2023

When creating a textinput, the prompt width is added to the width of the entire textinput. This means that two text inputs with a width of 20, will have different lengths based on the length of the prompt. This is visible when adding a border.

func initialModel() model {
	tiA := makeTextInput("1234")
	tiB := makeTextInput("1234567890")

	return model{
		textInputA: tiA,
		textInputB: tiB,
	}
}

func makeTextInput(str string) textinput.Model {
	ti := textinput.New()
	ti.Prompt = str
	ti.Focus()
	ti.CharLimit = 156
	ti.Width = 20

	return ti
}

I have an example of this bug here:
https://github.com/mikelorant/bubbles-textinput-prompt

Looking at the textinput code, it is clear that the prompt length is never subtracted from the width. I believe the width is actually shorter than it should be but I am unclear what the intended width is meant to represent.

@mikelorant
Copy link
Contributor Author

mikelorant commented Jan 28, 2024

@meowgorithm Can we get some clarification of what the width is meant to mean. You were the last person to touch this field so I am hoping you know the reasons behind why 😄

// Width is the maximum number of characters that can be displayed at once.
// It essentially treats the text field like a horizontally scrolling
// viewport. If 0 or less this setting is ignored.
Width int

https://github.com/charmbracelet/bubbles/blob/master/textinput/textinput.go#L113-L116

I have two possible interpretations:

  1. The width is the entire textinput component.
  2. The width is just the visible area for user input space. Prompts and extra padding add to this width.

Whatever the answer is, I believe we should update the documentation clarifying this field. If there is a bug, I will put up the appropriate pull request to fix this.

JaredReisinger added a commit to JaredReisinger/bubbles that referenced this issue 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 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
@meowgorithm
Copy link
Member

Okay, jumping back in on this one: Width here is the width of the textinput input area only — so not including margins, borders, padding and so on.

@mikelorant
Copy link
Contributor Author

@meowgorithm Thanks for the clarification. Will close this issue as it seems everything is working as expected.

@mikelorant mikelorant closed this as not planned Won't fix, can't repro, duplicate, stale Mar 13, 2024
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

No branches or pull requests

2 participants