Skip to content

Commit 6b25fe5

Browse files
5539 - CTRL+Z then Enter will now close shell on Windows. (RustPython#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>
1 parent 0e15e77 commit 6b25fe5

File tree

1 file changed

+26
-1
lines changed

1 file changed

+26
-1
lines changed

vm/src/readline.rs

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,9 @@ mod rustyline_readline {
7676
repl: rustyline::Editor<H, rustyline::history::DefaultHistory>,
7777
}
7878

79+
#[cfg(windows)]
80+
const EOF_CHAR: &str = "\u{001A}";
81+
7982
impl<H: Helper> Readline<H> {
8083
pub fn new(helper: H) -> Self {
8184
use rustyline::*;
@@ -88,6 +91,16 @@ mod rustyline_readline {
8891
)
8992
.expect("failed to initialize line editor");
9093
repl.set_helper(Some(helper));
94+
95+
// Bind CTRL + Z to insert EOF character on Windows
96+
#[cfg(windows)]
97+
{
98+
repl.bind_sequence(
99+
KeyEvent::new('z', Modifiers::CTRL),
100+
EventHandler::Simple(Cmd::Insert(1, EOF_CHAR.into())),
101+
);
102+
}
103+
91104
Self { repl }
92105
}
93106

@@ -115,7 +128,19 @@ mod rustyline_readline {
115128
use rustyline::error::ReadlineError;
116129
loop {
117130
break match self.repl.readline(prompt) {
118-
Ok(line) => ReadlineResult::Line(line),
131+
Ok(line) => {
132+
// Check for CTRL + Z on Windows
133+
#[cfg(windows)]
134+
{
135+
use std::io::IsTerminal;
136+
137+
let trimmed = line.trim_end_matches(&['\r', '\n'][..]);
138+
if trimmed == EOF_CHAR && io::stdin().is_terminal() {
139+
return ReadlineResult::Eof;
140+
}
141+
}
142+
ReadlineResult::Line(line)
143+
}
119144
Err(ReadlineError::Interrupted) => ReadlineResult::Interrupt,
120145
Err(ReadlineError::Eof) => ReadlineResult::Eof,
121146
Err(ReadlineError::Io(e)) => ReadlineResult::Io(e),

0 commit comments

Comments
 (0)