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 a function to get the cursor style (shape, blinking) #646

Closed
Cl00e9ment opened this issue Apr 2, 2022 · 8 comments · Fixed by #742
Closed

Add a function to get the cursor style (shape, blinking) #646

Cl00e9ment opened this issue Apr 2, 2022 · 8 comments · Fixed by #742

Comments

@Cl00e9ment
Copy link
Contributor

Cl00e9ment commented Apr 2, 2022

Currently, it's possible to modify the cursor shape and whether it's blinking or not, but the previous state can't be restored. A function that can retrieve the cursor style would be useful, because one could save the old style and restore it when the program exits, so users can keep their terminal the way they prefer it.

I don't know if this is possible. For now, I forked the project and I've a proof of concept that works only on Xterm compatible terminals using DECRQSS with DECSCUSR (see this).

What works:

  • GNOME Terminal (Manjaro Linux)
  • Xterm (Manjaro Linux)

What doesn't:

  • CLion terminal (Manjaro Linux)
  • cmd.exe (Windows 10)
  • PowerSheel (Windows 10)
  • iTerm2 (macOS)

I've 2 questions:

  • Do anyone know how to implement something like that on other terminals?
  • If it's not possible to make it work on other terminals, should this be included in crossterm?
@sigmaSd
Copy link
Contributor

sigmaSd commented Apr 3, 2022

According to this pr microsoft/terminal#11152 requesting DECSCUSR via DECRQSS might land in a future pr.

Personally I think xterm should be the reference to judge if a feature should be added or not, windows terminal is catching up with the spec, and I imagine macos have other terminals where this work.

@TimonPost
Copy link
Member

TimonPost commented Apr 3, 2022

Maybe for Windows systems, we could get some kind of idea by querying: https://docs.microsoft.com/en-us/windows/console/console-cursor-info-str. Otherwise, might be nice to have an CursorShape::Unknown as a default for when some platform doesnt support this feature.

@Cl00e9ment
Copy link
Contributor Author

It's interesting to note that, on Windows (tested with cmd.exe and PowerShell), if the cursor style was modified in the alternate screen, it is automatically restored upon leaving the alternate screen. Maybe we should implement that on other terminals, when possible, to have an uniform behavior.

@Cl00e9ment
Copy link
Contributor Author

Maybe for Windows systems, we could get some kind of idea by querying: https://docs.microsoft.com/en-us/windows/console/console-cursor-info-str.

Idk why, but whatever the shape of the cursor, the reported dwSize is always 25 (see this).

@TimonPost
Copy link
Member

TimonPost commented Apr 3, 2022

25% seems to be the normal behavior. Doesn't this change if you hide the terminal cursor for example?

image

@Cl00e9ment
Copy link
Contributor Author

Cl00e9ment commented Apr 3, 2022

25% seems to be the normal behavior. Doesn't this change if you hide the terminal cursor for example?

The reported dwSize is always 25, whatever the cursor shape or if it's hidden or not. However bVisible seems to be set correctly.

Here is the code that I ran (using https://github.com/Cl00e9ment/crossterm.git). I didn't check the return statement of cursor::style since I haven't finished to implement the function. Instead, I'm looking at the output of the println statement in the cursor::style function that I had left there for debugging purpose.

execute!(io::stdout(), SetCursorShape(CursorShape::Block)).unwrap();
let _ = crossterm::cursor::style();
execute!(io::stdout(), SetCursorShape(CursorShape::Line)).unwrap();
let _ = crossterm::cursor::style();
execute!(io::stdout(), SetCursorShape(CursorShape::UnderScore)).unwrap();
let _ = crossterm::cursor::style();
execute!(io::stdout(), Hide).unwrap();
let _ = crossterm::cursor::style();
execute!(io::stdout(), Show).unwrap();
let _ = crossterm::cursor::style();

@Cl00e9ment
Copy link
Contributor Author

The way I understand it, is that Windows terminals have their own cursor which is a block with variable height (default 25%). And on top of that, they support ANSI cursors (block, line and underscore) where dwSize is irrelevant.

@mb21
Copy link

mb21 commented Sep 7, 2023

This was closed in #742 although that PR only added SetCursorStyle and no command to get the cursor style?

(And SetCursorStyle::DefaultUserShape apparently sets the cursor to a default and not to what it used to be before entering reedline...)

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 a pull request may close this issue.

4 participants