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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
// src/modules/repl/input.rs
use crossterm::event::{Event, KeyCode, KeyEvent, KeyModifiers};
use std::collections::VecDeque;
use std::io::{self, Write};
use std::time::Duration;
use crate::parser::interpreter::Interpreter;
use super::autocomplete::{gather_completions_for_prefix, last_word_for_autocomplete};
pub struct EphemeralCompletion {
pub candidates: Vec<String>,
pub cycle_idx: usize,
pub prefix_start: usize,
pub prefix_len: usize,
}
impl EphemeralCompletion {
pub fn new(candidates: Vec<String>, prefix_start: usize, prefix_len: usize) -> Self {
EphemeralCompletion {
candidates,
cycle_idx: 0,
prefix_start,
prefix_len,
}
}
pub fn current_suffix(&self) -> String {
let cand = &self.candidates[self.cycle_idx];
if cand.len() > self.prefix_len {
cand[self.prefix_len..].to_string()
} else {
"".to_string()
}
}
pub fn chosen(&self) -> &str {
&self.candidates[self.cycle_idx]
}
}
pub struct ReadLineState {
pub buffer: Vec<char>,
pub cursor_pos: usize,
pub ephemeral: Option<EphemeralCompletion>,
}
impl ReadLineState {
pub fn new() -> Self {
ReadLineState {
buffer: Vec::new(),
cursor_pos: 0,
ephemeral: None,
}
}
pub fn typed_line(&self) -> String {
self.buffer.iter().collect()
}
}
fn user_broke_ephemeral(code: &KeyCode) -> bool {
match code {
KeyCode::Tab | KeyCode::Right => false,
_ => true,
}
}
pub fn read_line_with_arrows(
verbose: bool,
history: &mut VecDeque<String>,
prompt: &str,
interp: &mut Interpreter,
redraw_full: &dyn Fn(&mut io::Stdout, &str, &ReadLineState, bool, &mut Interpreter) -> Result<(), String>,
) -> Result<String, String> {
let mut stdout = io::stdout();
let mut local_state = ReadLineState::new();
let mut history_idx: Option<usize> = None;
let mut partial_new_line = Vec::new();
redraw_full(&mut stdout, prompt, &local_state, verbose, interp)?;
loop {
if crossterm::event::poll(Duration::from_millis(50)).map_err(|e| e.to_string())? {
match crossterm::event::read().map_err(|e| e.to_string())? {
Event::Key(KeyEvent { code, modifiers, .. }) => {
match code {
KeyCode::Char('l') if modifiers.contains(KeyModifiers::CONTROL) => {
use crossterm::terminal::{Clear, ClearType};
use crossterm::queue;
use crossterm::cursor::MoveTo;
queue!(
stdout,
Clear(ClearType::All),
MoveTo(0, 0)
)
.map_err(|e| e.to_string())?;
stdout.flush().map_err(|e| e.to_string())?;
redraw_full(&mut stdout, prompt, &local_state, verbose, interp)?;
continue;
}
KeyCode::Enter => {
let line_str = local_state.typed_line();
if line_str == ":q" || line_str == ":quit" {
redraw_full(&mut stdout, prompt, &local_state, verbose, interp)?;
println!();
return Ok(line_str);
}
redraw_full(&mut stdout, prompt, &local_state, verbose, interp)?;
println!();
return Ok(line_str);
}
KeyCode::Char('d') | KeyCode::Char('c')
if modifiers.contains(KeyModifiers::CONTROL) =>
{
redraw_full(&mut stdout, prompt, &local_state, verbose, interp)?;
return Ok("<<<EXIT>>>".to_string());
}
KeyCode::Tab => {
let typed_str = local_state.typed_line();
let ephemeral_opt = local_state.ephemeral.take();
if let Some(mut ephemeral) = ephemeral_opt {
if ephemeral.candidates.len() == 1 {
let (start_idx, prefix) = last_word_for_autocomplete(
&typed_str,
local_state.cursor_pos
);
for _ in 0..prefix.len() {
local_state.buffer.remove(start_idx);
}
let chosen = ephemeral.chosen();
for (i, ch) in chosen.chars().enumerate() {
local_state.buffer.insert(start_idx + i, ch);
}
local_state.cursor_pos = start_idx + chosen.len();
} else {
ephemeral.cycle_idx =
(ephemeral.cycle_idx + 1) % ephemeral.candidates.len();
local_state.ephemeral = Some(ephemeral);
}
} else {
let (start_idx, prefix) = last_word_for_autocomplete(
&typed_str,
local_state.cursor_pos
);
if let Some(cands) = gather_completions_for_prefix(&prefix) {
if !cands.is_empty() {
let ep = EphemeralCompletion::new(
cands,
start_idx,
prefix.len()
);
if ep.candidates.len() == 1 {
let (start_idx2, prefix2) =
last_word_for_autocomplete(
&typed_str,
local_state.cursor_pos
);
for _ in 0..prefix2.len() {
local_state.buffer.remove(start_idx2);
}
let chosen = ep.chosen();
for (i, ch) in chosen.chars().enumerate() {
local_state.buffer.insert(start_idx2 + i, ch);
}
local_state.cursor_pos = start_idx2 + chosen.len();
} else {
local_state.ephemeral = Some(ep);
}
}
}
}
}
KeyCode::Right => {
let typed_str = local_state.typed_line();
let ephemeral_opt = local_state.ephemeral.take();
if let Some(ephemeral) = ephemeral_opt {
let (start_idx, prefix) = last_word_for_autocomplete(
&typed_str,
local_state.cursor_pos
);
for _ in 0..prefix.len() {
local_state.buffer.remove(start_idx);
}
let chosen = ephemeral.chosen();
for (i, ch) in chosen.chars().enumerate() {
local_state.buffer.insert(start_idx + i, ch);
}
local_state.cursor_pos = start_idx + chosen.len();
} else {
if local_state.cursor_pos < local_state.buffer.len() {
local_state.cursor_pos += 1;
}
}
history_idx = None;
}
_ => {
if user_broke_ephemeral(&code) {
local_state.ephemeral = None;
}
match code {
KeyCode::Left => {
if local_state.cursor_pos > 0 {
local_state.cursor_pos -= 1;
}
history_idx = None;
}
KeyCode::Up => {
if !history.is_empty() {
match history_idx {
None => {
partial_new_line = local_state.buffer.clone();
let new_idx = history.len() - 1;
history_idx = Some(new_idx);
let hline = &history[new_idx];
local_state.buffer = hline.chars().collect();
local_state.cursor_pos =
local_state.buffer.len();
}
Some(old) => {
if old > 0 {
let new_i = old - 1;
history_idx = Some(new_i);
let hline = &history[new_i];
local_state.buffer =
hline.chars().collect();
local_state.cursor_pos =
local_state.buffer.len();
}
}
}
}
}
KeyCode::Down => {
if !history.is_empty() {
match history_idx {
None => {}
Some(old) => {
if old < history.len() - 1 {
let new_i = old + 1;
history_idx = Some(new_i);
let hline = &history[new_i];
local_state.buffer =
hline.chars().collect();
local_state.cursor_pos =
local_state.buffer.len();
} else {
history_idx = None;
local_state.buffer =
partial_new_line.clone();
local_state.cursor_pos =
local_state.buffer.len();
}
}
}
}
}
KeyCode::Backspace => {
if local_state.cursor_pos > 0 {
local_state.buffer.remove(local_state.cursor_pos - 1);
local_state.cursor_pos -= 1;
history_idx = None;
}
}
KeyCode::Char(ch) => {
local_state.buffer.insert(local_state.cursor_pos, ch);
local_state.cursor_pos += 1;
history_idx = None;
}
_ => {}
}
}
}
redraw_full(&mut stdout, prompt, &local_state, verbose, interp)?;
}
_ => {}
}
}
}
}