refactor input command (#10150)

close #8074 

I attempted to refactor the "input" command. The reason for this is that
the current implementation of the "input" command lacks consistency for
different options. For instance, some parts use `std::io::stdin` while
others use `crossterm::event::read`.

In this pull request, I have made changes to use crossterm consistently:
- Detection of the -u option is now done using `crossterm`'s
`KeyCode::Char`.
- The current input is displayed when using `crossterm` for input (it
won't be displayed when -s is present).
- Ctrl-C triggers SIGINT. 


# User-Facing Changes

Users can interrupt "input" with ctrl-c.
This commit is contained in:
Yuto 2023-09-03 04:09:26 +09:00 committed by GitHub
parent 844f541719
commit e9d4730099
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,4 +1,10 @@
use crossterm::event::{Event, KeyCode, KeyEventKind, KeyModifiers};
use crossterm::{
cursor,
event::{Event, KeyCode, KeyEventKind, KeyModifiers},
execute,
style::Print,
terminal::{self, ClearType},
};
use nu_engine::CallExt;
use nu_protocol::ast::Call;
use nu_protocol::engine::{Command, EngineState, Stack};
@ -6,7 +12,7 @@ use nu_protocol::{
Category, Example, IntoPipelineData, PipelineData, ShellError, Signature, Spanned, SyntaxShape,
Type, Value,
};
use std::io::{Read, Write};
use std::io::Write;
use std::time::Duration;
#[derive(Clone)]
@ -60,7 +66,6 @@ impl Command for Input {
let bytes_until: Option<String> = call.get_flag(engine_state, stack, "bytes-until")?;
let suppress_output = call.has_flag("suppress-output");
let numchar: Option<Spanned<i64>> = call.get_flag(engine_state, stack, "numchar")?;
let numchar_exists = numchar.is_some();
let numchar: Spanned<i64> = numchar.unwrap_or(Spanned {
item: i64::MAX,
span: call.head,
@ -75,63 +80,26 @@ impl Command for Input {
));
}
if let Some(bytes_until) = bytes_until {
let _ = crossterm::terminal::enable_raw_mode();
if let Some(prompt) = prompt {
print!("{prompt}");
let _ = std::io::stdout().flush();
}
let byte_until = if let Some(bytes_until) = bytes_until {
if let Some(c) = bytes_until.bytes().next() {
let mut buf = [0u8; 1];
let mut buffer = vec![];
let mut stdin = std::io::stdin();
loop {
if let Err(err) = stdin.read_exact(&mut buf) {
let _ = crossterm::terminal::disable_raw_mode();
return Err(ShellError::IOError(err.to_string()));
}
buffer.push(buf[0]);
if i64::try_from(buffer.len()).unwrap_or(0) >= numchar.item {
let _ = crossterm::terminal::disable_raw_mode();
break;
}
// 03 symbolizes SIGINT/Ctrl+C
if buf.contains(&3) {
let _ = crossterm::terminal::disable_raw_mode();
return Err(ShellError::IOError("SIGINT".to_string()));
}
if buf[0] == c {
let _ = crossterm::terminal::disable_raw_mode();
break;
}
}
Ok(Value::Binary {
val: buffer,
span: call.head,
}
.into_pipeline_data())
Some(c)
} else {
let _ = crossterm::terminal::disable_raw_mode();
Err(ShellError::IOError(
return Err(ShellError::IOError(
"input can't stop on this byte".to_string(),
))
));
}
} else {
if let Some(prompt) = prompt {
None
};
if let Some(prompt) = &prompt {
print!("{prompt}");
let _ = std::io::stdout().flush();
}
let mut buf = String::new();
if suppress_output || numchar_exists {
crossterm::terminal::enable_raw_mode()?;
// clear terminal events
while crossterm::event::poll(Duration::from_secs(0))? {
@ -141,7 +109,6 @@ impl Command for Input {
loop {
if i64::try_from(buf.len()).unwrap_or(0) >= numchar.item {
let _ = crossterm::terminal::disable_raw_mode();
break;
}
match crossterm::event::read() {
@ -155,13 +122,16 @@ impl Command for Input {
{
if k.modifiers == KeyModifiers::CONTROL && c == 'c' {
crossterm::terminal::disable_raw_mode()?;
return Err(ShellError::IOError(
"SIGINT".to_string(),
));
return Err(ShellError::IOError("SIGINT".to_string()));
}
continue;
}
if let Some(byte_until) = byte_until {
if c == byte_until as char {
break;
}
}
buf.push(c);
}
KeyCode::Backspace => {
@ -181,33 +151,28 @@ impl Command for Input {
return Err(event_error.into());
}
}
if !suppress_output {
// clear the current line and print the current buffer
execute!(
std::io::stdout(),
terminal::Clear(ClearType::CurrentLine),
cursor::MoveToColumn(0),
)?;
if let Some(prompt) = &prompt {
execute!(std::io::stdout(), Print(prompt.to_string()))?;
}
execute!(std::io::stdout(), Print(buf.to_string()))?;
}
}
crossterm::terminal::disable_raw_mode()?;
return Ok(Value::String {
if !suppress_output {
std::io::stdout().write_all(b"\n")?;
}
Ok(Value::String {
val: buf,
span: call.head,
}
.into_pipeline_data());
}
// Just read a normal line of text, and trim the newline at the end
let input = std::io::stdin().read_line(&mut buf);
if buf.ends_with('\n') {
buf.pop();
if buf.ends_with('\r') {
buf.pop();
}
}
match input {
Ok(_) => Ok(Value::String {
val: buf,
span: call.head,
}
.into_pipeline_data()),
Err(err) => Err(ShellError::IOError(err.to_string())),
}
}
.into_pipeline_data())
}
fn examples(&self) -> Vec<Example> {