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

Add cursor to TextInputComponent for better commit message support (#46) #117

Merged
merged 10 commits into from
Jun 13, 2020

Conversation

alistaircarscadden
Copy link
Contributor

This is one of the changes for #46 .

I've added a movable cursor to the TextInputComponent that is used to enter commit messages. This allows the user to use the arrow keys to move the cursor left and right, insert characters at the cursor. The cursor behaves naturally, moving with the text.

This change does not address the part of #46 that says characters after 50 should be red, it is very possible to make that edit to the code but will make the branching quite messy, and I will consider for a while how to best handle that logic.

This change does not address the part about multi line commit messages, and opening another editor.

I tested my changes on the commit window, I'm not sure what other functions use that TextInputComponent, and they have not been tested.

@alistaircarscadden
Copy link
Contributor Author

alistaircarscadden commented Jun 12, 2020

Example of the cursor as you type a commit message:

image

Example of inserting text in to the middle of the content:

image

I've just thought of an edge case I somehow missed, backspacing with the cursor in the middle. Committing that fix soon.

@extrawurst
Copy link
Collaborator

Cool @alistaircarscadden thanks for tackling this! I just had a few minor comments, would be cool if u could look into that and then it will be merged!

Also please add a changelog entry

@extrawurst
Copy link
Collaborator

I gave it another round of review and the biggest issue is that right now unicode is not handled and crashes

@alistaircarscadden
Copy link
Contributor Author

alistaircarscadden commented Jun 13, 2020

@extrawurst Ok, fixed the behaviour when accents etc are used. The new operations walk thru the string using the &str.chars() and &str.char_indices() functions to get the next char boundary. In order to move the cursor backwards I'm walking thru the string until the previous position, which since these strings are very short takes no time at all, but if this TextInputComponent was used for some 20,000 word essay we'd want to be caching the values or something! 😀

I didn't add the changelog item because I'm currently working on a previous release. I merged the master into my branch, but can't compile anymore due to

error[E0723]: heap allocations are not allowed in const fn
   --> src\components\help.rs:163:19
    |
163 |             cmds: vec![],
    |                   ^^^^^^
    |
    = note: see issue #57563 <https://github.com/rust-lang/rust/issues/57563> for more information
    = help: add `#![feature(const_fn)]` to the crate attributes to enable
    = note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)

And finally, a lot of my code here makes me wish that the if and while let chains were not experimental. Some of these things could be simplified a bit.

@extrawurst
Copy link
Collaborator

looks like you are not using the latest rust (1.44) then this should be fine.

@extrawurst
Copy link
Collaborator

Awesome stuff man! Thanks for this ❤️

@extrawurst extrawurst merged commit 09c3fe9 into gitui-org:master Jun 13, 2020
@extrawurst extrawurst mentioned this pull request Jun 13, 2020
3 tasks
@extrawurst
Copy link
Collaborator

extrawurst commented Jun 13, 2020

@alistaircarscadden I found is_char_boundary to be what we are looking for to remove the loop to find the previous index: https://doc.rust-lang.org/std/primitive.str.html#method.is_char_boundary
Will change that tomorrow to support editing 20k character novels with this 😂

@alistaircarscadden
Copy link
Contributor Author

I found is_char_boundary

Oh I actually saw that, and for some reason it didn't click. That would be super easy to use for this.

I'll update my rust, thanks.

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.

2 participants