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

fix: ctrl-c doesn't restore cursor #75

Merged
merged 1 commit into from
Dec 18, 2024
Merged

fix: ctrl-c doesn't restore cursor #75

merged 1 commit into from
Dec 18, 2024

Conversation

roele
Copy link
Collaborator

@roele roele commented Dec 17, 2024

Fixes #74

  • Confirm/Dialog currently don't hide the cursor which i think they should
  • IMO behaviour of Escape and CTRL+C should be the same and not clear the terminal
  • not sure about the current Spinner behaviour though

@roele roele force-pushed the issues/74 branch 6 times, most recently from 26bda9a to baa0df0 Compare December 17, 2024 18:10
@roele roele marked this pull request as ready for review December 17, 2024 18:23
@roele roele requested a review from jdx December 17, 2024 18:23
src/lib.rs Outdated Show resolved Hide resolved
@@ -153,6 +154,8 @@ impl<'a> Input<'a> {
/// This function will block until the user submits the input. If the user cancels the input,
/// an error of type `io::ErrorKind::Interrupted` is returned.
pub fn run(mut self) -> io::Result<String> {
let ctrlc_handle = ctrlc::show_cursor_after_ctrlc(&self.term)?;
Copy link
Collaborator Author

@roele roele Dec 18, 2024

Choose a reason for hiding this comment

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

this approach/try with Input seems to work more or less but having to close the handle is a bit cumbersome though. it seems a bit safer than using unsafe operations to register/unregister. signal_hooks also probably won't work on Windows as per docs.

@jdx any ideas?

Copy link
Owner

Choose a reason for hiding this comment

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

can you even hide the cursor to begin with on windows?

Copy link
Owner

Choose a reason for hiding this comment

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

I'll test real quick on my windows laptop

Copy link
Owner

Choose a reason for hiding this comment

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

so in main demand currently does hide the cursor on windows and it doesn't reappear just like in unix, so whatever we do we should fix it for windows too.

This pr doesn't compile on windows at least (maybe on unix too, didn't check)


error[E0599]: no method named `write` found for struct `HANDLE` in the current scope
  --> src\ctrlc.rs:50:39
   |
19 | / lazy_static! {
20 | |     static ref HANDLE: RwLock<Option<Handle>> = RwLock::new(None);
21 | | }
   | |_- method `write` not found for this struct
...
50 |           let mut handle_guard = HANDLE.write().unwrap();
   |                                         ^^^^^ method not found in `HANDLE`
   |
   = help: items from traits can only be used if the trait is implemented and in scope
   = note: the following traits define an item `write`, perhaps you need to implement one of them:
           candidate #1: `Hasher`
           candidate #2: `std::io::Write`

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Windows support for signals seems hopeless at this point. The ctrlc crate goes a long way to make it happen but theres no way to unregister for our use-case. For now i guess we have to accept this as a known issue.

Cargo.toml Outdated Show resolved Hide resolved
let handle_guard = HANDLE.read().unwrap();
return Ok(handle_guard.clone());
}
INIT.store(true, Ordering::Relaxed);
Copy link
Owner

Choose a reason for hiding this comment

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

if I'm reading this right, we will trap sigint the first time demand is run but if a CLI made multiple prompts it seems we would not trap it the second time

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

It should ensure we can only register a handler once.
How would multiple prompts work? We can only render one at a time... if the run method exits the handler should be dropped which should enable you to register another one for another input.

Copy link
Owner

Choose a reason for hiding this comment

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

ok I tested this and it does seem to work as you described.

As far as the multiple prompts things, I handle this with a mutex in mise: https://github.com/jdx/mise/blob/0f9dae377eaa8a527c099310944e75bac60bc3a3/src/ui/prompt.rs#L13

but we should probably put that in here since it never makes sense to show 2 prompts at the same time

src/input.rs Outdated Show resolved Hide resolved
@roele roele force-pushed the issues/74 branch 2 times, most recently from 832cb65 to 0204fdf Compare December 18, 2024 14:32
src/ctrlc.rs Outdated Show resolved Hide resolved
@roele roele force-pushed the issues/74 branch 4 times, most recently from 187d444 to 02c5545 Compare December 18, 2024 18:04
src/ctrlc.rs Outdated Show resolved Hide resolved
src/ctrlc.rs Outdated Show resolved Hide resolved
src/input.rs Outdated Show resolved Hide resolved
@roele roele force-pushed the issues/74 branch 4 times, most recently from a1b1210 to 0f29119 Compare December 18, 2024 22:43
@jdx
Copy link
Owner

jdx commented Dec 18, 2024

you may want to do something like this if you can't get windows to work: https://github.com/jdx/mise/blob/127e1646f80fba36a2b2a1b7956d6d8e77f83c5f/src/ui/mod.rs#L3

@roele roele force-pushed the issues/74 branch 2 times, most recently from 750e3a3 to 3db3c03 Compare December 18, 2024 23:05
@roele
Copy link
Collaborator Author

roele commented Dec 18, 2024

@jdx thanks for the hint, thought i could get it to work within the same file but your approach with a specific stub seems much easier to work with

@roele roele force-pushed the issues/74 branch 2 times, most recently from a227e5c to 1eb2e05 Compare December 18, 2024 23:20
@jdx jdx merged commit ffc5c87 into jdx:main Dec 18, 2024
4 checks passed
@roele roele deleted the issues/74 branch December 19, 2024 09:23
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.

ctrl-c doesn't restore cursor after Select::run
2 participants