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(examples): cursor can be moved left/right and character at cursor position can be deleted/inserted #302

Merged

Conversation

BoolPurist
Copy link
Contributor

@BoolPurist BoolPurist commented Jul 9, 2023

Changes

Added cursor navigation to the user input example. User can now move cursor left and right. In addition user can also insert/delete characters at the cursor position.

Demo

pr_demo

Note

Could also put this demonstration into a new example file if you prefer it this way.

@codecov
Copy link

codecov bot commented Jul 9, 2023

Codecov Report

Merging #302 (9e20ff0) into main (e66d5cd) will not change coverage.
The diff coverage is n/a.

❗ Current head 9e20ff0 differs from pull request most recent head 0a5ab2c. Consider uploading reports for the commit 0a5ab2c to get more accurate results

@@           Coverage Diff           @@
##             main     #302   +/-   ##
=======================================
  Coverage   83.21%   83.21%           
=======================================
  Files          37       37           
  Lines        7764     7764           
=======================================
  Hits         6461     6461           
  Misses       1303     1303           

examples/user_input.rs Outdated Show resolved Hide resolved
examples/user_input.rs Outdated Show resolved Hide resolved
examples/user_input.rs Outdated Show resolved Hide resolved
examples/user_input.rs Outdated Show resolved Hide resolved
examples/user_input.rs Outdated Show resolved Hide resolved
examples/user_input.rs Outdated Show resolved Hide resolved
examples/user_input.rs Outdated Show resolved Hide resolved
examples/user_input.rs Outdated Show resolved Hide resolved
examples/user_input.rs Outdated Show resolved Hide resolved
@joshka
Copy link
Member

joshka commented Jul 9, 2023

Let's not get too much more complicated with this example. An example that uses the tui-textarea or tui-input libraries might be worth considering.

@BoolPurist BoolPurist force-pushed the example_cursor_navigation_user_input branch from c409d45 to 9741ce2 Compare July 9, 2023 10:22
@mindoodoo
Copy link
Member

Let's not get too much more complicated with this example. An example that uses the tui-textarea or tui-input libraries might be worth considering.

This example in it's current state also has a couple bugs with unicode characters width, so might be interesting to look into how those crates handle unicode char width and if their solution might be better.

I'm unsure though about including examples using external crate widgets. Would you add another example alongside this one or would it take it's place ?

@BoolPurist BoolPurist force-pushed the example_cursor_navigation_user_input branch from 9741ce2 to 515aa0d Compare July 9, 2023 10:45
@sayanarijit
Copy link
Member

sayanarijit commented Jul 9, 2023

Agree. Examples should be as simple as possible, and not deviate from demonstrating its primary use case.

Related/unrelated, we should probably have unicode handling built-in. I believe this falls under this crate's primary objectives.

EDIT: Sorry, I see now, ratatui already handles unicode width where possible... but it's not abstracted enough.

@BoolPurist
Copy link
Contributor Author

BoolPurist commented Jul 9, 2023

I realize now that the previously mentioned crates, tui-input and tui-textarea, are meant for that purpose xD

In my application I often end up needing some kind of input field.

In my opinion an input field example with a non-adjustable cursor seems not particular useful to me and the lack of Unicode width support appears like a trap which bites me back later.

As a new user of ratatui I would appreciate 2 kind of examples in the area of input boxes.

  1. A simple one line input field.
  2. A multi-line input field.

Both example should include input with unicode width support and the ability to move the cursor around.

if this is out of the scope for this crate, then I find examples for user input more useful if they at least show that you use these external crates for input boxes.

That said, I would also not mind if my solution is replaced by such an example with the usage of an external crate.

@joshka
Copy link
Member

joshka commented Jul 9, 2023

I think eventually we should combine the two crates and bring them into ratatui as a core widget (or perhaps as a separate widgets crate?)

@joshka
Copy link
Member

joshka commented Jul 9, 2023

@mindoodoo wrote:

I'm unsure though about including examples using external crate widgets. Would you add another example alongside this one or would it take it's place ?

My intent was to communicate that we don't need to try to re-implement those crates in this example. Perhaps it would be a good idea to add a small comment in the current example pointing readers in the correct direction of the crates.

@mindoodoo
Copy link
Member

Ah I see what you mean now. I think it might be interesting to provide examples for those external crates in the meantime, but bringing one or a combination of the two into ratatui would be interesting I think.

Copy link
Member

@joshka joshka left a comment

Choose a reason for hiding this comment

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

Approved subject to the small requested change (document the reason for not using input.remove())
Also, please rebase this (there's some small changes to the imports in the main branch.

examples/user_input.rs Outdated Show resolved Hide resolved
@BoolPurist BoolPurist force-pushed the example_cursor_navigation_user_input branch from 515aa0d to 9e20ff0 Compare July 12, 2023 21:30
Copy link
Member

@joshka joshka left a comment

Choose a reason for hiding this comment

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

LGTM - thanks for submitting this PR. :)
I'll leave this to @mindoodoo to merge

Copy link
Member

@mindoodoo mindoodoo left a comment

Choose a reason for hiding this comment

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

LGTM, would just like to see a mention that this example does not try to handle unicode characters and then will merge !

examples/user_input.rs Show resolved Hide resolved
The user_input example now responds to left/right and allows the
character at the cursor position to be deleted / inserted.

Co-authored-by: Leon Sautour <[email protected]>
@joshka joshka enabled auto-merge July 13, 2023 10:37
@joshka joshka force-pushed the example_cursor_navigation_user_input branch from 2b35f1c to 0a5ab2c Compare July 13, 2023 10:37
@joshka
Copy link
Member

joshka commented Jul 13, 2023

force push has the comment update, a rebase on main and a cargo fmt of the new comment

@joshka joshka added this pull request to the merge queue Jul 13, 2023
Merged via the queue into ratatui:main with commit 57678a5 Jul 13, 2023
@joshka
Copy link
Member

joshka commented Jul 13, 2023

Hey @BoolPurist thanks for your contribution. The changes are merged.

a-kenji pushed a commit to a-kenji/ratatui that referenced this pull request Jul 16, 2023
The user_input example now responds to left/right and allows the
character at the cursor position to be deleted / inserted.

Co-authored-by: Leon Sautour <[email protected]>
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.

4 participants