feat: add --suppress-output (-s) to input command (#5017)

* feat: add --suppress-output (-s) to input command

* Don't handle cursor position since existing impl doesn't

* Handle all raw mode outcomes
This commit is contained in:
msp 2022-03-31 15:20:31 -04:00 committed by GitHub
parent 05f7d7d38b
commit 6a6471b04b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,3 +1,4 @@
use crossterm::event::{Event, KeyCode, KeyModifiers};
use nu_engine::CallExt;
use nu_protocol::ast::Call;
use nu_protocol::engine::{Command, EngineState, Stack};
@ -27,6 +28,7 @@ impl Command for Input {
"read bytes (not text) until a stop byte",
Some('u'),
)
.switch("suppress-output", "don't print keystroke values", Some('s'))
.category(Category::Platform)
}
@ -39,6 +41,7 @@ impl Command for Input {
) -> Result<PipelineData, ShellError> {
let prompt: Option<String> = call.opt(engine_state, stack, 0)?;
let bytes_until: Option<String> = call.get_flag(engine_state, stack, "bytes-until")?;
let suppress_output = call.has_flag("suppress-output");
if let Some(bytes_until) = bytes_until {
let _ = crossterm::terminal::enable_raw_mode();
@ -83,8 +86,38 @@ impl Command for Input {
let _ = std::io::stdout().flush();
}
// Just read a normal line of text
let mut buf = String::new();
if suppress_output {
crossterm::terminal::enable_raw_mode()?;
loop {
match crossterm::event::read() {
Ok(Event::Key(k)) => match k.code {
// TODO: maintain keycode parity with existing command
KeyCode::Char(_) if k.modifiers != KeyModifiers::NONE => continue,
KeyCode::Char(c) => buf.push(c),
KeyCode::Backspace => {
let _ = buf.pop();
}
KeyCode::Enter => break,
_ => continue,
},
Ok(_) => continue,
Err(event_error) => {
crossterm::terminal::disable_raw_mode()?;
return Err(event_error.into());
}
}
}
crossterm::terminal::disable_raw_mode()?;
return Ok(Value::String {
val: buf,
span: call.head,
}
.into_pipeline_data());
}
// Just read a normal line of text
let input = std::io::stdin().read_line(&mut buf);
match input {