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

change cursor shape depending on edit mode #494

Merged
merged 1 commit into from Oct 21, 2022
Merged

change cursor shape depending on edit mode #494

merged 1 commit into from Oct 21, 2022

Conversation

cschierig
Copy link
Contributor

Modal editors such as vim/neovim/etc. often use different cursors for different edit modes.
In it's current state, this pr changes the cursor to a line if the user is in insert mode, and uses a block cursor for all other modes (including custom and emacs).

Unfortunately, there is no way to retrieve the current cursor shape in crossterm that I know of, so restoring the previous cursor when exiting reedline could prove to be quite difficult. I currently can think of two solutions for this:

  1. Use the block or a configurable cursor as "standard" and set that whenever reedline exits
  2. Somehow retrieve the cursor upon starting reedline. This could also be quite useful as it would allow to use that as the default cursor for modes which don't need an indicator, I'm specifically thinking of custom modes. I believe there are ways to retrieve the cursor shape, but I will have to research those before either submitting an upstream pr to crossterm or adding them to this pr.

Regarding configurability, I think it should be possible to configure the cursor depending on the mode, this would be quite useful for nushell as it would allow customizing the behaviour in the config record. Depending on how the implementation ends up looking, it might also be a good idea to hide this behind a with_custom_cursors() function.

Once I've resolved all those problems, I might also look into enabling/disabling cursor blinking. Uni is starting next week so it could take a while until this is in a polished state, but I will try to come back to this regularly.

@sholderbach
Copy link
Member

I love it!

@sholderbach
Copy link
Member

@CozyPenguin I gave this a test drive on different terminal emulators across Windows and Linux and I didn't encounter unforeseen problems. I know this overrides the previously set cursor. I think there might be a way to read out the set cursor from a ANSI device (https://invisible-island.net/xterm/ctlseqs/ctlseqs.html#h3-Device-Control-functions) but I am not sure if that is worth it for a minimal viable product if not every terminal emulator implements that protocol.

I would be open to land it in this stage and accept further improvements down the line.

@cschierig
Copy link
Contributor Author

I am fine with it being merged in its current form.

There is crossterm-rs/crossterm#646 where someone has a fork in which retrieving the cursor shape is implement, but I don't know what the status of that one is and if it will be merged due to concerns with windows compatibility. I will try to come back to this and implement the additional functionality when I have a bit more time left over.

@cschierig cschierig marked this pull request as ready for review October 20, 2022 21:05
@sholderbach sholderbach merged commit da27f00 into nushell:main Oct 21, 2022
@fdncred
Copy link
Collaborator

fdncred commented Nov 14, 2022

I wondered why my cursor recently changed to a block. I change it in Windows Terminal to be a bar. Hit enter and it's a block again. LOL. It would be nice if this were configurable somewhere.

@cschierig
Copy link
Contributor Author

I'm going to add an option to configure this, I just don't have that much time right now.

@fdncred
Copy link
Collaborator

fdncred commented Nov 14, 2022

It seems like this change also overrides ansi escape sequences to change the cursor, which is unfortunate.

CSI Ps SP q
          Set cursor style (DECSCUSR), VT520.
            Ps = 0  ⇒  blinking block.
            Ps = 1  ⇒  blinking block (default).
            Ps = 2  ⇒  steady block.
            Ps = 3  ⇒  blinking underline.
            Ps = 4  ⇒  steady underline.
            Ps = 5  ⇒  blinking bar, xterm.
            Ps = 6  ⇒  steady bar, xterm.

# Example $"(ansi csi)5 q" should change it to a blinking bar, and it does momentarily, and then crossterm resets it

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