crossterm icon indicating copy to clipboard operation
crossterm copied to clipboard

Make cursor screen specific

Open TimonPost opened this issue 6 years ago • 4 comments

Task Description

let ct = Crossterm::from_screen(&alternative.screen);
let cursor = ct.cursor();
cursor.hide();

When switching back -- dropping the new screen that was created just for the alternative screen; the terminal still has the cursor hidden.

Notice how the code, upon closing, re-establishes some default state like showing the cursor back into stdout. Not sure if Unix-y systems has an equivalent to winapi's GetConsoleState, but it would be preferable to restore the preexisting state, rather than overwriting state (even if they are sane defaults)

Task Todo

This issue is due to not restoring the previous console/terminal settings on shutdown. When applying syscalls to hide or show cursor, it needs to be reversed on exit -- checkout termbox-go's implementation. https://github.com/nsf/termbox-go/blob/master/api.go#L129

TimonPost avatar Jun 22 '19 21:06 TimonPost

Does this still apply with crossterm's current API? Either way, the title should be changed.

Kestrer avatar May 02 '20 15:05 Kestrer

I believe it is. It is related to the alternate screen and main screen. If one disables the cursor on an alternate screen it will still be disabled if it leaves the alternate screen.

TimonPost avatar May 02 '20 19:05 TimonPost

Can't reproduce this on Windows 10, tried both cmd and powershell.

Here's the code I used to test it

use crossterm::cursor::Hide;
use crossterm::style::Print;
use crossterm::terminal::{EnterAlternateScreen, LeaveAlternateScreen};
use crossterm::{ExecutableCommand, Result};
use std::io::stdout;
use std::{thread, time};

pub fn main() -> Result<()> {
    let mut stdout = stdout();
    let two_secs = time::Duration::from_secs(2);
    stdout.execute(Print("Primary"))?;
    thread::sleep(two_secs);
    stdout
        .execute(EnterAlternateScreen)?
        .execute(Print("Alternate"))?;
    thread::sleep(two_secs);
    stdout.execute(Hide)?;
    thread::sleep(two_secs);
    stdout.execute(LeaveAlternateScreen)?;
    thread::sleep(two_secs);

    Ok(())
}

kallekankaanpaa avatar Feb 04 '21 15:02 kallekankaanpaa

Ah so that's why it wasn't added yet, I guess we can track my change here https://github.com/crossterm-rs/crossterm/pull/775 for a reminder to revert it once this gets added.

lesleyrs avatar Apr 06 '23 02:04 lesleyrs