mirror of
https://github.com/RustPython/RustPython.git
synced 2026-10-28 10:44:36 +08:00
5539 - CTRL+Z then Enter will now close shell on Windows. (#6223)
* CTRL+Z then Enter will now close shell on Windows. * Additional comment. * Use EOF const * Add cfg(windows) for EOF_CHAR --------- Co-authored-by: Jeong, YunWon <69878+youknowone@users.noreply.github.com>
This commit is contained in:
1 parent
0e15e771c3
commit
6b25fe5c95
1 file changed
+26
-1
+26
-1
@@ -76,6 +76,9 @@ mod rustyline_readline {
|
||||
repl: rustyline::Editor<H, rustyline::history::DefaultHistory>,
|
||||
}
|
||||
|
||||
#[cfg(windows)]
|
||||
const EOF_CHAR: &str = "\u{001A}";
|
||||
|
||||
impl<H: Helper> Readline<H> {
|
||||
pub fn new(helper: H) -> Self {
|
||||
use rustyline::*;
|
||||
@@ -88,6 +91,16 @@ mod rustyline_readline {
|
||||
)
|
||||
.expect("failed to initialize line editor");
|
||||
repl.set_helper(Some(helper));
|
||||
|
||||
// Bind CTRL + Z to insert EOF character on Windows
|
||||
#[cfg(windows)]
|
||||
{
|
||||
repl.bind_sequence(
|
||||
KeyEvent::new('z', Modifiers::CTRL),
|
||||
EventHandler::Simple(Cmd::Insert(1, EOF_CHAR.into())),
|
||||
);
|
||||
}
|
||||
|
||||
Self { repl }
|
||||
}
|
||||
|
||||
@@ -115,7 +128,19 @@ mod rustyline_readline {
|
||||
use rustyline::error::ReadlineError;
|
||||
loop {
|
||||
break match self.repl.readline(prompt) {
|
||||
Ok(line) => ReadlineResult::Line(line),
|
||||
Ok(line) => {
|
||||
// Check for CTRL + Z on Windows
|
||||
#[cfg(windows)]
|
||||
{
|
||||
use std::io::IsTerminal;
|
||||
|
||||
let trimmed = line.trim_end_matches(&['\r', '\n'][..]);
|
||||
if trimmed == EOF_CHAR && io::stdin().is_terminal() {
|
||||
return ReadlineResult::Eof;
|
||||
}
|
||||
}
|
||||
ReadlineResult::Line(line)
|
||||
}
|
||||
Err(ReadlineError::Interrupted) => ReadlineResult::Interrupt,
|
||||
Err(ReadlineError::Eof) => ReadlineResult::Eof,
|
||||
Err(ReadlineError::Io(e)) => ReadlineResult::Io(e),
|
||||
|
||||
Reference in new issue
Block a user