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

feat(textarea) Add multiline placeholder #302

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

Conversation

mikelorant
Copy link
Contributor

Add the capability to show a multiline placeholder. Some refactoring was required to improve readability and improve logic.

End of line buffer character was only shown when line numbers were displayed which requires some verification whether this is the intended outcome. This change resolves this issue.

textarea/textarea_test.go Outdated Show resolved Hide resolved
@mikelorant mikelorant force-pushed the feat/text-area-multiline-placeholder branch from 805ce3b to c5687de Compare December 6, 2022 23:01
@mikelorant mikelorant force-pushed the feat/text-area-multiline-placeholder branch 3 times, most recently from 3cff057 to 5585074 Compare January 28, 2024 09:12
@mikelorant
Copy link
Contributor Author

@meowgorithm @maaslalani Can I get approval to run the workflow? Thanks.

@mikelorant
Copy link
Contributor Author

Can I get someone from @charmbracelet to review this. Thanks.

@maaslalani
Copy link
Member

Hey @mikelorant. Thank you so much for this PR.

I'm seeing a bug with how this PR is handling line numbers:

This is what the textarea should look like:
image

Here is what it looks like using this PR (rebased on main):

image

@maaslalani
Copy link
Member

The testing code I'm using can be found here: https://github.com/charmbracelet/bubbletea/tree/master/examples/textarea

@mikelorant
Copy link
Contributor Author

Just to clarify, line numbers only display if there is user input (or cursor) on that line?

@mikelorant
Copy link
Contributor Author

Just did some basic investigation and it seems the aim is that it should look like Vim with line numbers enabled. Which means only lines with content get a number.

@maaslalani
Copy link
Member

Just did some basic investigation and it seems the aim is that it should look like Vim with line numbers enabled. Which means only lines with content get a number.

Yep, that's correct! Similar to vim!

@mikelorant mikelorant force-pushed the feat/text-area-multiline-placeholder branch from 5585074 to 97e8494 Compare January 31, 2024 07:26
@mikelorant
Copy link
Contributor Author

Have fixed the issue you have reported and here is the result using the code you linked:

CleanShot 2024-01-31 at 18 39 20

Using a multiline placeholder shows like this:

CleanShot 2024-01-31 at 18 40 17

I wish to make it clear that line numbers are only for lines that have a cursor or entered text. This is placeholder text, so only the first line will ever have a line number in this case. As soon as you start typing and adding new lines, none of this code is used.

I've had to extensively increase the unit tests because it was far too complex checking all the variations. As part of this process I needed a small helper function to strip ANSI and trailing white spaces to make comparison easier. I would have preferred to use golden files however I think this is a reasonable compromise. Unfortunately no other tests in textarea use testing tables so that will stand out as being different but as you can tell a requirement.

@mikelorant
Copy link
Contributor Author

One more reference image with the cursor visible.

CleanShot 2024-01-31 at 18 51 23

Check the test tables and let me know if that is the output you'd expect. I had to make some executive decisions and tried to base everything I could on how it was displayed in Vim.

@mikelorant
Copy link
Contributor Author

@maaslalani Any chance you could do a review, if this is working correctly I'd like to get it merged otherwise if there are problems, lets me get started on fixing them.

@maaslalani
Copy link
Member

maaslalani commented Feb 2, 2024

Hey @mikelorant, thanks again for your work on this one!

One issue I'm seeing is the CursorLine (if it has a background color, bleeds over to the line number column)

image

Here's what it should look like:

image

@mikelorant
Copy link
Contributor Author

mikelorant commented Feb 2, 2024

The default prompt which is being used here is defined at:
https://github.com/charmbracelet/bubbles/blob/master/textarea/textarea.go#L258C3-L258C59

Code as follows:

Prompt:               lipgloss.ThickBorder().Left + " ",

This has me thinking that both versions are wrong. The background colour should start at position 3 for each line.

|X123 Once upon a time...
|X    ~
|X    ~

The X space is part of that prompt and whatever styling that is applied to it is independent of everything else.

Thoughts?

Additional notes:

Consider the following change:

Prompt:               lipgloss.ThickBorder().Right + " ",

This would put the changed background colour outside the left border which would be very wrong.

@mikelorant
Copy link
Contributor Author

mikelorant commented Feb 2, 2024

@maaslalani I'll quickly do the update to this pull request providing the original functionality, but I still believe both implementations have it wrong.

Using Vim as the reference, highlighted cursor line goes as far as the beginning of the line number or end of buffer character. It is hard to know what we should do if we put a border around it since we cant do that in Vim.

@mikelorant
Copy link
Contributor Author

mikelorant commented Feb 2, 2024

Here are the two possibilities in one image:

CleanShot 2024-02-02 at 17 56 33

The top version is how it is currently implemented and the bottom version is how I think it should be. However as a lowly minion here to serve the wonderful Charm team 😄 I have pushed up the version that conforms with how it is also displayed when text is entered.

Thanks for helping make sure we catch all the little subtle things, very much appreciated @maaslalani.

@mikelorant mikelorant force-pushed the feat/text-area-multiline-placeholder branch from 97e8494 to 5f819d1 Compare February 2, 2024 07:02
@mikelorant
Copy link
Contributor Author

mikelorant commented Feb 2, 2024

Heres the example showing the line style for the cursor line leaking out of the prompt "border".
CleanShot 2024-02-02 at 18 06 47

Prompt to use to see this case:

ti.Prompt = lipgloss.ThickBorder().Right + " "

@mikelorant mikelorant force-pushed the feat/text-area-multiline-placeholder branch 2 times, most recently from fc81153 to a2668fb Compare February 2, 2024 07:37
@maaslalani
Copy link
Member

@mikelorant Thanks for the care you're putting into this PR! Yes, I see what you mean by the border. However, when the cursor line is defined with a background color I think it looks more correct (but maybe we will change this later since you are actually right about the correctness technically)

Also when a text area has a multiline placeholder I believe the cursor line background should be applied to the entire placeholder. In your screenshots it only applies to the first line.

@mikelorant
Copy link
Contributor Author

Here is a comparison of the two:

CleanShot 2024-02-03 at 08 58 06

I think it is very confusing to highlight more than the cursor line where there is a multiline placeholder. However, on the flip side it may help indicate that any typing will clear all that text away by keeping it "grouped".

How should I proceed?

@maaslalani
Copy link
Member

In my opinion the second option is correct here as the entire line is highlighted with the cursor line and if the user types that exact placeholder then the same highlighting will occur.

@mikelorant mikelorant force-pushed the feat/text-area-multiline-placeholder branch from a2668fb to bf2cc79 Compare February 2, 2024 22:17
@mikelorant
Copy link
Contributor Author

@maaslalani Commit updated to highlight all lines of the placeholder.

@mikelorant mikelorant force-pushed the feat/text-area-multiline-placeholder branch from bf2cc79 to 699fb3e Compare February 3, 2024 00:42
@maaslalani
Copy link
Member

That is, long placeholders should behave as multi-line placeholders.

@mikelorant
Copy link
Contributor Author

That is, long placeholders should behave as multi-line placeholders.

Agreed, let me add a test case and fix for this one.

@mikelorant mikelorant force-pushed the feat/text-area-multiline-placeholder branch from e7d2b21 to 0c30b26 Compare February 28, 2024 23:03
@mikelorant
Copy link
Contributor Author

@maaslalani This started to get complicated so I had to use muesli/reflow to handle wrap and wordwrap.

Have lots of test cases (include some edge cases) to verify this is working as expected.

@mikelorant mikelorant force-pushed the feat/text-area-multiline-placeholder branch from 0c30b26 to 8aee984 Compare February 28, 2024 23:11
@mikelorant
Copy link
Contributor Author

Added a few more test cases because I am paranoid of all the edge cases 😄

@mikelorant mikelorant force-pushed the feat/text-area-multiline-placeholder branch from 8aee984 to 1e8da29 Compare February 29, 2024 22:12
@mikelorant
Copy link
Contributor Author

@maaslalani Don't believe we have any issues left with this pull request. Would love to get this merged as my own app depends on it.

@meowgorithm
Copy link
Member

We're waiting on muesli/reflow#71 for this one, correct?

@mikelorant
Copy link
Contributor Author

@meowgorithm Not at all. This one is ready to go.

This has nothing to do with emojis or grapheme clusters.

@mikelorant mikelorant force-pushed the feat/text-area-multiline-placeholder branch from 1e8da29 to 54bf36a Compare March 9, 2024 00:37
@mikelorant
Copy link
Contributor Author

@maaslalani Rebase completed and all unit tests are passing.

🤞 that there are no more issues.

@mikelorant
Copy link
Contributor Author

@meowgorithm @maaslalani Can we please get this reviewed. Want to get this finalised so I can move off using my own fork.

@maaslalani
Copy link
Member

@mikelorant Will take a look today

@maaslalani
Copy link
Member

maaslalani commented Mar 13, 2024

@mikelorant I'm still seeing the bug of long placeholders, let me know if I'm doing something incorrectly.

But, if I set the placeholder to:

	ti.Placeholder = "Once upon a time in a land far far away..."

Then the text is broken.

image

However if I add a new line it works as expected:

	ti.Placeholder = "Once upon a time in a land far far\naway..."
image

I wouldn't mind truncating the placeholder like we currently do if the user has not explicitly opted into the newline, if that easier.

Comment on lines +843 to +1446
view: heredoc.Doc(`
> placeholder the first line that is
> longer than the max width
Copy link
Member

Choose a reason for hiding this comment

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

I think we need to add a test where the CursorLine is styled like we see in the bubbletea example.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Lots more tests coming up.

@mikelorant
Copy link
Contributor Author

I wouldn't mind truncating the placeholder like we currently do if the user has not explicitly opted into the newline, if that easier.

No truncating, will work out what is going on.

Just need lots more tests to find all these weird edge cases that keep popping up. Not sure why this feature has been so difficult to implement 😢

Thanks for finding these problems.

@mikelorant
Copy link
Contributor Author

mikelorant commented Mar 13, 2024

Found the bug and unfortunately it seems like we may have 2 bugs 😢

Firstly, my bug is quite simple, I was using m.MaxWidth to determine the width instead of m.width. Easy fix.

However, this has uncovered something new with all the tests we have added.

Looking into the function SetWidth, there is a lot of work done to make sure the width removes the prompt and line numbers. However the last check causing an edge case.

if m.MaxWidth > 0 {
    m.width = clamp(inputWidth, minWidth, m.MaxWidth)
} else {
    m.width = max(inputWidth, minWidth)
}

If the MaxWidth is smaller than inputWidth we pick MaxWidth. Cool? Cool. However, we never subtracted the prompt and line numbers from this. We need to pick whether we use inputWidth, minWidth or m.MaxWidth first and then subtract the prompt and line numbers.

Hoping my logic here is correct, will keep hacking away and see where this leads.

@mikelorant
Copy link
Contributor Author

Going to add another problem, setting MaxWidth on the model does not call SetWidth() so updating of the model width is not being done.

I believe the order we need to follow is:

ta := textarea.New()
ta.MaxWidth = 25
ta.SetWidth(20)

This might be a case where MaxWidth should have been a function so it then it could call SetWidth() before it returns.

Definitely need some guidance on how this should work.

@mikelorant
Copy link
Contributor Author

Sorted out all the problems, however this is now dependent on #496.

@maaslalani
Copy link
Member

Thanks for debugging the SetWidth and improving it in #496, I can take a look at this one when it is ready to go!

Add the capability to show a multiline placeholder. Some refactoring was
required to improve readability and improve logic.

End of line buffer character was only shown when line numbers were
displayed which requires some verification whether this is the intended
outcome. This change resolves this issue.
@mikelorant mikelorant force-pushed the feat/text-area-multiline-placeholder branch from 54bf36a to ef0557f Compare April 6, 2024 05:51
@mikelorant
Copy link
Contributor Author

Have fixed the conflicts and unit tests now pass.

Will run some manual tests to see if we have all the issues finally resolved 😄

@mikelorant
Copy link
Contributor Author

@maaslalani Actually, everything looks OK from both unit tests and manual tests. Hopefully you see the same results.

@mikelorant
Copy link
Contributor Author

@maaslalani Can we get another review of this one, I am hoping this one passes now that we have the SetWidth method fixed.

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

3 participants