1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
use crossterm::event::{Event, KeyCode, KeyModifiers};
use nu_engine::CallExt;
use nu_protocol::ast::Call;
use nu_protocol::engine::{Command, EngineState, Stack};
use nu_protocol::{
Category, Example, IntoPipelineData, PipelineData, ShellError, Signature, SyntaxShape, Value,
};
use std::io::{Read, Write};
#[derive(Clone)]
pub struct Input;
impl Command for Input {
fn name(&self) -> &str {
"input"
}
fn usage(&self) -> &str {
"Get input from the user."
}
fn search_terms(&self) -> Vec<&str> {
vec!["prompt", "interactive"]
}
fn signature(&self) -> Signature {
Signature::build("input")
.optional("prompt", SyntaxShape::String, "prompt to show the user")
.named(
"bytes-until",
SyntaxShape::String,
"read bytes (not text) until a stop byte",
Some('u'),
)
.switch("suppress-output", "don't print keystroke values", Some('s'))
.category(Category::Platform)
}
fn run(
&self,
engine_state: &EngineState,
stack: &mut Stack,
call: &Call,
_input: PipelineData,
) -> 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();
if let Some(prompt) = prompt {
print!("{}", prompt);
let _ = std::io::stdout().flush();
}
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 buf[0] == c {
let _ = crossterm::terminal::disable_raw_mode();
break;
}
}
Ok(Value::Binary {
val: buffer,
span: call.head,
}
.into_pipeline_data())
} else {
let _ = crossterm::terminal::disable_raw_mode();
Err(ShellError::IOError(
"input can't stop on this byte".to_string(),
))
}
} else {
if let Some(prompt) = prompt {
print!("{}", prompt);
let _ = std::io::stdout().flush();
}
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 {
KeyCode::Char(_)
if k.modifiers == KeyModifiers::ALT
|| k.modifiers == KeyModifiers::CONTROL =>
{
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());
}
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())),
}
}
}
fn examples(&self) -> Vec<Example> {
vec![Example {
description: "Get input from the user, and assign to a variable",
example: "let user-input = (input)",
result: None,
}]
}
}
#[cfg(test)]
mod tests {
use super::Input;
#[test]
fn examples_work_as_expected() {
use crate::test_examples;
test_examples(Input {})
}
}