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

Implement cursor #412

Merged
merged 8 commits into from
May 24, 2022
Merged

Implement cursor #412

merged 8 commits into from
May 24, 2022

Conversation

b3nj5m1n
Copy link
Contributor

This PR implements a cursor, meaning you can move around in the input field, delete or add chars in the middle, etc.


Demo

Changes

I added a field called cursor_index to the State struct. Every function interacting with the user input now has to use this value, so it's not a trivial change.

I added the following keybinds:

  • Control + w (delete back one word)
  • Control + a (go to the beginning of the line)
  • Control + e (go to the end of the line)

These are the default bash keybinds for these operations, adding more should be fairly simple.

I also modified what I believe to be all places in the search.rs file affected by this, namely the other keybinds and the draw functions.

Problems

Arrow keys not recognised properly

For some reason, the Key::Right and Key::Left keybinds don't do anything. I looked at the termion docs, and it seems to be the correct value for the right and left arrow keys, but the events don't trigger for some reason. The up & down keys seem to work just fine.

For now, I did all my testing using the alternative vim-style keybinds. (Control + h/l)

Text encoding

The input is a String, so it supports unicode. My current implementation only works properly with ASCII text.

I think unicode should be supported, which is why I'm creating this as a draft PR.

I don't think making it work with unicode would be too hard, but it might be a bit of a hassle, so I want to make sure this is something you're interested in merging first before working on it.

Fixes

This would close #410

@conradludgate
Copy link
Collaborator

What's even harder than just utf8 support is grapheme cluster support. Eg an emoji can be made up of multiple unicode scalar points, so when you press back and scan through to find the previous char boundary, that would be in the middle of an emoji

What's worse is that you can't tell how multiple emojis might interact since all terminals/OSs might be supporting different emojis 😅

@conradludgate
Copy link
Collaborator

This does definitely seem nice to have though, thanks for the effort. We do need to figure out the left/right keys.

Have you been testing it by running atuin search -i or by using your shell binding (ctrl-R or up arrow)

@b3nj5m1n
Copy link
Contributor Author

Have you been testing it by running atuin search -i or by using your shell binding (ctrl-R or up arrow)

I've only been testing it using search -i so far, I just tried installing it and when invoking it with control + r or up, the arrow keys work perfectly fine.

The up & down keys seem to work just fine.

I'm not sure if I just misremembered yesterday, or if that broke in the meantime, but when invoking with search -i, none of the arrow keys work. Everything works as expected when invoking with control + r or up though.

@b3nj5m1n
Copy link
Contributor Author

What's even harder than just utf8 support is grapheme cluster support. Eg an emoji can be made up of multiple unicode scalar points, so when you press back and scan through to find the previous char boundary, that would be in the middle of an emoji

Shouldn't the chars() iterator on strings take care of that? My idea was using that, I can probably try it out sometime in the evening today.

If that solution wouldn't work, let me know if there's any alternative.

@ellie
Copy link
Member

ellie commented May 16, 2022

Ohhh awesome thanks for all the work here!

when invoking with search -i, none of the arrow keys work. Everything works as expected when invoking with control + r or up though.

Yup this will be because of the terminal mode

If you look in our ZSH bindings, we change that: https://github.com/ellie/atuin/blob/main/src/shell/atuin.zsh#L34

Depending on what mode the terminal is in, programs are sent different escape sequences for different characters. Some applications can be a bit messy and change things without changing them back. It was a nightmare to debug when I was first writing this 😂

So long as it works when called from the bindings + when in the correct terminal mode, it's all good.

I can probably try it out sometime in the evening today.

Definitely interested in the results of this one 🙌

@b3nj5m1n
Copy link
Contributor Author

I only managed to do a bit of testing, but it seems like 8a65f41 implements proper unicode support.


Demo

I added a couple of functions, hopefully that's okay with you.

Would be great if you could do a bit of testing yourself and get back to me!

@conradludgate
Copy link
Collaborator

Can you test with a larger width emoji? For instance '👩‍👩‍👦'

See https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=8c71737da77c80a427ae6af6959fb397 for what I mean.

This is an edge case though and I'll be happy to ignore those for the sake of this change

@b3nj5m1n
Copy link
Contributor Author

That breaks it a little, but it doesn't seem to crash an least.

I can try to get it working properly, but no promises.

What would the desired behaviour be in this case? Deleting the whole emoji in one go, or deleting the individual emojis making it up, like in firefox, for example?

@conradludgate
Copy link
Collaborator

I would not be so worried about making it 'correct'. Whichever you choose I think could be argued as unintuitive. This also ignores that not all setups will even render these multiple emojis together (or not be updated in future)

@ellie
Copy link
Member

ellie commented May 17, 2022

Yup I think either way of doing it would be fine, most terminals are unlikely to render things correctly anyway

I'd be happy to revisit this in the future should it become a problem, but if that's the only edge case atm then it looks good to me 😁

@b3nj5m1n
Copy link
Contributor Author

I tried out an alternative approach, and it didn't fix the problem. (It actually made it worse)

If you're okay with these edge cases not being handled properly for now, I think this is probably the best compromise.

Let me know if there's any other changes you want me to make!

@b3nj5m1n b3nj5m1n marked this pull request as ready for review May 18, 2022 21:58
@ellie
Copy link
Member

ellie commented May 19, 2022

I'm happy with this! I don't think the fancy emoji are used often enough for it to be a problem - I doubt many people have a terminal setup that renders them right anyway

Should it be a larger problem we can revisit in the future

Thank you so much for the work here, this is great 🚀

@ellie ellie enabled auto-merge (squash) May 19, 2022 20:46
ellie
ellie previously approved these changes May 19, 2022
@ellie
Copy link
Member

ellie commented May 19, 2022

@b3nj5m1n also, do you mind if I use one of your GIFs in the next release?

@b3nj5m1n
Copy link
Contributor Author

b3nj5m1n commented May 19, 2022

@b3nj5m1n also, do you mind if I use one of your GIFs in the next release?

Sure, that'd be great.


Cippy has been failing this entire time because the function for handling keys has too many lines, what would you like to do about that? I had tried to use more compact syntax before, but then fmt was complaining. The only thing I could think of would be to separate all the events into their own functions, but there's not really a point to doing that except for pleasing clippy.

Alternatively, we could also add #[allow(clippy::too_many_lines)] to the function.

Just let me know what you would prefer!

@b3nj5m1n
Copy link
Contributor Author

@ellie Pinging in case you missed my last comment.

@ellie
Copy link
Member

ellie commented May 24, 2022

Sure, that'd be great.

Thanks!!

Alternatively, we could also add #[allow(clippy::too_many_lines)] to the function.

I think in this case I'd be happy for us to use this approach

auto-merge was automatically disabled May 24, 2022 18:08

Head branch was pushed to by a user without write access

@b3nj5m1n
Copy link
Contributor Author

Should be good to go now!

Copy link
Member

@ellie ellie left a comment

Choose a reason for hiding this comment

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

Nice work, thank you! Great to get this in 🚀

@ellie ellie enabled auto-merge (squash) May 24, 2022 20:43
@ellie ellie merged commit 9ac0c60 into atuinsh:main May 24, 2022
@b3nj5m1n
Copy link
Contributor Author

Awesome, thank you so much!

ellie added a commit that referenced this pull request Jun 6, 2022
06ac958 Show current version on server index (#436)
706b1af Disable ARM docker builds (#438)
f2abc23 Update README.md
3c2b055 Noyez fix dir hostname utf8 (#430)
3f5350d [feature] Add scroll wheel support to interactive history search (#435)
dcdde22 Fix text outline for dark mode
9ac0c60 Implement cursor (#412)
119ab9e Adds password prompt for register and login (#424)
e5df809 Noyez zsh histdb import (#393)
b08e254 Improve default fish keybindings (#420)
4096c6e Update README.md
cd2a3ab Add fish shell to key binding docs (#418)
b278211 Bump clap_complete from 3.1.3 to 3.1.4 (#397)
ee66c0a Bump axum from 0.5.5 to 0.5.6 (#415)
4297e26 Bump tokio from 1.18.1 to 1.18.2 (#396)
dbd9ca5 Bump clap from 3.1.16 to 3.1.18 (#401)
a7c9d19 Bump tower-http from 0.3.2 to 0.3.3 (#399)
3b79f68 Bump axum from 0.5.4 to 0.5.5 (#402)
f340771 Cleanup dependencies – disable unnecessary or unused features (#407)
ab294cd Don't pollute shell environment - remove 'id' variable (#408)
14b3060 Allow to build atuin server without client (#404)
5e4e8d1 Don't create config dir for server in default location if not needed (#406)
b7946cc Update Chinese version README.md (#403)
e0291f6 Update README.md
301190e Build ARM docker image in GitHub Actions using QEMU (#400)
1d030b9 Importer V3 (#395)
d3a4ff9 Bump clap from 3.1.15 to 3.1.16 (#392)
e9d2ec4 Add ctrl-k and ctrl-j for up and down (#394)
25afb5b Bump serde_json from 1.0.80 to 1.0.81 (#387)
4a839da Adds stats summary  (#384)
7a394b0 Bump serde from 1.0.136 to 1.0.137 (#375)
edd3f81 Bump clap_complete from 3.1.2 to 3.1.3 (#377)
d85d03d Bump log from 0.4.16 to 0.4.17 (#382)
dc3b7ef Bump tokio from 1.18.0 to 1.18.1 (#383)
12440c1 Bump serde_json from 1.0.79 to 1.0.80 (#376)
731042f Bump tower-http from 0.3.1 to 0.3.2 (#378)
82505e6 Bump clap from 3.1.12 to 3.1.15 (#381)
e05c19d Add Chinese documentation translation & Fix spelling mistakes (#373)
6e280e2 Add Russian documentation translation (#365)
40efdd1 Bump http from 0.2.6 to 0.2.7 (#368)
8bc5bec Bump tower-http from 0.3.0 to 0.3.1 (#367)
172ac8d Create FUNDING.yml
7cdd00b Bump tokio from 1.17.0 to 1.18.0 (#357)
9d2e9ea Search: Allow specifiying the limited of returned entries (#364)
93ab4e7 ignore JetBrains IDEs, tidy-up imports (#348)
2cb4cb3 Bump axum from 0.5.3 to 0.5.4 (#355)
796644e Add created_at column to users (#354)
f8233bc SQLx cannot run this migration OK (#353)
d8ef5dd fix db range query (#351)
5926ea6 fix import auto for bash (#352)
43d299f bump tui (#346)
8ac6571 Remove all select * from the server queries (#347)
4030de4 Add btree index on history table (#345)
b692e0c Bump tower-http from 0.2.5 to 0.3.0 (#343)
3680f4a Bump clap from 3.1.11 to 3.1.12 (#342)
7f5310a history list (#340)
@ellie ellie mentioned this pull request Jun 6, 2022
ellie added a commit that referenced this pull request Jun 6, 2022
06ac958 Show current version on server index (#436)
706b1af Disable ARM docker builds (#438)
f2abc23 Update README.md
3c2b055 Noyez fix dir hostname utf8 (#430)
3f5350d [feature] Add scroll wheel support to interactive history search (#435)
dcdde22 Fix text outline for dark mode
9ac0c60 Implement cursor (#412)
119ab9e Adds password prompt for register and login (#424)
e5df809 Noyez zsh histdb import (#393)
b08e254 Improve default fish keybindings (#420)
4096c6e Update README.md
cd2a3ab Add fish shell to key binding docs (#418)
b278211 Bump clap_complete from 3.1.3 to 3.1.4 (#397)
ee66c0a Bump axum from 0.5.5 to 0.5.6 (#415)
4297e26 Bump tokio from 1.18.1 to 1.18.2 (#396)
dbd9ca5 Bump clap from 3.1.16 to 3.1.18 (#401)
a7c9d19 Bump tower-http from 0.3.2 to 0.3.3 (#399)
3b79f68 Bump axum from 0.5.4 to 0.5.5 (#402)
f340771 Cleanup dependencies – disable unnecessary or unused features (#407)
ab294cd Don't pollute shell environment - remove 'id' variable (#408)
14b3060 Allow to build atuin server without client (#404)
5e4e8d1 Don't create config dir for server in default location if not needed (#406)
b7946cc Update Chinese version README.md (#403)
e0291f6 Update README.md
301190e Build ARM docker image in GitHub Actions using QEMU (#400)
1d030b9 Importer V3 (#395)
d3a4ff9 Bump clap from 3.1.15 to 3.1.16 (#392)
e9d2ec4 Add ctrl-k and ctrl-j for up and down (#394)
25afb5b Bump serde_json from 1.0.80 to 1.0.81 (#387)
4a839da Adds stats summary  (#384)
7a394b0 Bump serde from 1.0.136 to 1.0.137 (#375)
edd3f81 Bump clap_complete from 3.1.2 to 3.1.3 (#377)
d85d03d Bump log from 0.4.16 to 0.4.17 (#382)
dc3b7ef Bump tokio from 1.18.0 to 1.18.1 (#383)
12440c1 Bump serde_json from 1.0.79 to 1.0.80 (#376)
731042f Bump tower-http from 0.3.1 to 0.3.2 (#378)
82505e6 Bump clap from 3.1.12 to 3.1.15 (#381)
e05c19d Add Chinese documentation translation & Fix spelling mistakes (#373)
6e280e2 Add Russian documentation translation (#365)
40efdd1 Bump http from 0.2.6 to 0.2.7 (#368)
8bc5bec Bump tower-http from 0.3.0 to 0.3.1 (#367)
172ac8d Create FUNDING.yml
7cdd00b Bump tokio from 1.17.0 to 1.18.0 (#357)
9d2e9ea Search: Allow specifiying the limited of returned entries (#364)
93ab4e7 ignore JetBrains IDEs, tidy-up imports (#348)
2cb4cb3 Bump axum from 0.5.3 to 0.5.4 (#355)
796644e Add created_at column to users (#354)
f8233bc SQLx cannot run this migration OK (#353)
d8ef5dd fix db range query (#351)
5926ea6 fix import auto for bash (#352)
43d299f bump tui (#346)
8ac6571 Remove all select * from the server queries (#347)
4030de4 Add btree index on history table (#345)
b692e0c Bump tower-http from 0.2.5 to 0.3.0 (#343)
3680f4a Bump clap from 3.1.11 to 3.1.12 (#342)
7f5310a history list (#340)
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.

Allow using Ctrl-a and Ctrl-e to move the cursor
3 participants