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

feat: add --interactive option to prompt for each change #1119

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

Bnyro
Copy link

@Bnyro Bnyro commented Oct 10, 2024

The code is probably far from perfect, I'm hoping for some help at improving the code quality in the review :)

closes #397

crates/typos-cli/src/file.rs Outdated Show resolved Hide resolved
crates/typos-cli/src/file.rs Outdated Show resolved Hide resolved
crates/typos-cli/src/file.rs Outdated Show resolved Hide resolved
crates/typos-cli/src/file.rs Outdated Show resolved Hide resolved
crates/typos-cli/src/file.rs Outdated Show resolved Hide resolved
crates/typos-cli/src/file.rs Outdated Show resolved Hide resolved
crates/typos-cli/src/file.rs Outdated Show resolved Hide resolved
Ok(())
}

fn select_fix(typo: &typos::Typo<'_>) -> Option<usize> {
Copy link
Collaborator

Choose a reason for hiding this comment

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

When I run this locally, I get weird output on the right item

image

Copy link
Collaborator

Choose a reason for hiding this comment

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

Something still isn't right
image

And if I ctrl-c,. my cursor no longer shows up

Copy link
Author

Choose a reason for hiding this comment

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

I can reproduce the issue with the cursor (but thought something in my setup is broken, apparently it's not my setup), I've really no idea what causes it. I've tried the inquire library as well because I thought something is wrong with the dialoguer library but that one worked even worse...

Copy link
Author

Choose a reason for hiding this comment

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

Oh well, apparently we have to handle Ctrl+C issues ourselves as per console-rs/dialoguer#294.

Copy link
Author

Choose a reason for hiding this comment

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

It seems like the available cli libraries for prompts are not really that mature yet.

@Bnyro Bnyro force-pushed the prompt-to-write branch 2 times, most recently from 5c2fec3 to b0bdd7e Compare October 11, 2024 11:17
@Bnyro Bnyro changed the title feat: add --write-ask option to prompt for each change feat: add --interactive option to prompt for each change Oct 11, 2024
@Bnyro
Copy link
Author

Bnyro commented Oct 11, 2024

Thanks for your review so far, I'm way more comfortable with the code now than before the review.

crates/typos-cli/src/file.rs Outdated Show resolved Hide resolved
@Bnyro Bnyro force-pushed the prompt-to-write branch 3 times, most recently from b4cabe8 to 5fc0015 Compare October 11, 2024 13:40
@Bnyro
Copy link
Author

Bnyro commented Oct 19, 2024

I've added a custom ctrlc handler now to handle manual program exits and added proper error handling to the dialoguer confirm and selection prompts.

I've no idea why the position of the prompt is weird sometimes.

.default(true)
.show_default(true)
.interact();
return confirmation.map(|_| 0).ok();

Check failure

Code scanning / clippy

unneeded `return` statement

unneeded `return` statement
Comment on lines 782 to 791
return match selection {
Ok(selection) => {
if selection != 0 {
Some(selection - 1)
} else {
None
}
}
Err(_) => None,
};

Check failure

Code scanning / clippy

unneeded `return` statement

unneeded `return` statement
crates/typos-cli/src/file.rs Outdated Show resolved Hide resolved
crates/typos-cli/src/file.rs Outdated Show resolved Hide resolved
@@ -32,6 +32,13 @@ fn run() -> proc_exit::ExitResult {

init_logging(args.verbose.log_level());

// https://github.com/console-rs/dialoguer/issues/294
ctrlc::set_handler(move || {
let _ = console::Term::stdout().show_cursor();
Copy link
Collaborator

Choose a reason for hiding this comment

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

I now see the cursor but I can't see anything I type

Copy link
Author

Choose a reason for hiding this comment

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

That's strange, for me everything behaves normal after that change.

// https://github.com/console-rs/dialoguer/issues/294
ctrlc::set_handler(move || {
let _ = console::Term::stdout().show_cursor();
std::process::exit(0);
Copy link
Collaborator

Choose a reason for hiding this comment

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

Is exit(0) the correct way of exiting on ctrl-c to mirror behavior before this change?

Copy link
Author

Choose a reason for hiding this comment

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

You're right, the previous exit code for ctrl-c was 130, and according to my quick research 130 is preferred over 0 when ctrl-c killed the process via SIGTERM.

@epage
Copy link
Collaborator

epage commented Oct 22, 2024

I've no idea why the position of the prompt is weird sometimes.

For me, its always weird for the first prompt. This would be a blocker for merging

@Bnyro
Copy link
Author

Bnyro commented Oct 22, 2024

For me, its always weird for the first prompt. This would be a blocker for merging

I see, but unfortunately I don't see any way to fix this except for trying other libraries?

@epage
Copy link
Collaborator

epage commented Oct 23, 2024

How about we switch to showing prompts that require hitting <enter> like git add -p does?

@Bnyro
Copy link
Author

Bnyro commented Oct 23, 2024

How about we switch to showing prompts that require hitting <enter> like git add -p does?

That could fix the issue for the confirm prompt. If I run typos --interactive on this repository (crate-ci/typos), the first prompt is a selection prompt which is getting skipped immediately (due to the bug mentioned), although the select prompts should in theory require pressing <enter> as well. That's quite odd.

If I do however run this on different source code folders, for some everything works normally, and for some it behaves as described above, which doesn't make a lot of sense to me.

The only two considerable crates I found are the dialoguer crate (in use right now) and the inquire crate, which wouldn't even let me use the Confirm prompt properly, I wasn't able to actually enter yes or no when I tested 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.

Allow interactive usage with prompt to confirm that changes are written
2 participants