aboutsummaryrefslogtreecommitdiffstats
path: root/src/readline.rs
blob: d385bd13c043ff36b202e323afcf2a98646eef9f (plain) (blame)
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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
use snafu::ResultExt as _;
use std::io::Write as _;

#[derive(Debug, snafu::Snafu)]
pub enum Error {
    #[snafu(display("failed to write to the terminal: {}", source))]
    WriteToTerminal { source: std::io::Error },

    #[snafu(display("end of input"))]
    EOF,

    #[snafu(display(
        "failed to put the terminal into raw mode: {}",
        source
    ))]
    IntoRawMode { source: std::io::Error },

    #[snafu(display(
        "failed to spawn a background thread to read terminal input: {}",
        source
    ))]
    TerminalInputReadingThread { source: std::io::Error },
}

pub type Result<T> = std::result::Result<T, Error>;

pub fn readline(prompt: &str, echo: bool) -> Result<Readline> {
    Readline::new(prompt, echo)
}

pub struct Readline {
    reader: Option<KeyReader>,
    state: ReadlineState,
    _raw_screen: crossterm::RawScreen,
}

struct ReadlineState {
    prompt: String,
    echo: bool,

    buffer: String,
    cursor: usize,
    wrote_prompt: bool,
}

impl Readline {
    fn new(prompt: &str, echo: bool) -> Result<Self> {
        let screen =
            crossterm::RawScreen::into_raw_mode().context(IntoRawMode)?;

        Ok(Self {
            reader: None,
            state: ReadlineState {
                prompt: prompt.to_string(),
                echo,
                buffer: String::new(),
                cursor: 0,
                wrote_prompt: false,
            },
            _raw_screen: screen,
        })
    }

    fn with_reader<F, T>(&mut self, f: F) -> Result<T>
    where
        F: FnOnce(&KeyReader, &mut ReadlineState) -> Result<T>,
    {
        let mut reader_opt = self.reader.take();
        if reader_opt.is_none() {
            reader_opt = Some(KeyReader::new(futures::task::current())?);
        }
        let ret = f(reader_opt.as_ref().unwrap(), &mut self.state);
        self.reader = reader_opt;
        ret
    }
}

impl ReadlineState {
    fn process_event(
        &mut self,
        event: crossterm::InputEvent,
    ) -> std::result::Result<futures::Async<String>, Error> {
        match event {
            crossterm::InputEvent::Keyboard(e) => {
                return self.process_keyboard_event(&e)
            }
            _ => {}
        }

        Ok(futures::Async::NotReady)
    }

    fn process_keyboard_event(
        &mut self,
        event: &crossterm::KeyEvent,
    ) -> std::result::Result<futures::Async<String>, Error> {
        match *event {
            crossterm::KeyEvent::Char('\n') => {
                self.echo_char('\n').context(WriteToTerminal)?;
                return Ok(futures::Async::Ready(self.buffer.clone()));
            }
            crossterm::KeyEvent::Char(c) => {
                if self.cursor != self.buffer.len() {
                    self.echo(b"\x1b[@").context(WriteToTerminal)?;
                }
                self.echo_char(c).context(WriteToTerminal)?;
                self.buffer.insert(self.cursor, c);
                self.cursor += 1;
            }
            crossterm::KeyEvent::Ctrl(c) => match c {
                'a' => {
                    if self.cursor != 0 {
                        self.echo(
                            &format!("\x1b[{}D", self.cursor).into_bytes(),
                        )
                        .context(WriteToTerminal)?;
                        self.cursor = 0;
                    }
                }
                'c' => {
                    self.buffer = String::new();
                    self.cursor = 0;
                    self.echo_char('\n').context(WriteToTerminal)?;
                    self.prompt().context(WriteToTerminal)?;
                }
                'd' => {
                    if self.buffer.is_empty() {
                        self.echo_char('\n').context(WriteToTerminal)?;
                        return EOF.fail();
                    }
                }
                'e' => {
                    if self.cursor != self.buffer.len() {
                        self.echo(
                            &format!(
                                "\x1b[{}C",
                                self.buffer.len() - self.cursor
                            )
                            .into_bytes(),
                        )
                        .context(WriteToTerminal)?;
                        self.cursor = self.buffer.len();
                    }
                }
                'u' => {
                    if self.cursor != 0 {
                        self.echo(
                            std::iter::repeat(b'\x08')
                                .take(self.cursor)
                                .chain(
                                    format!("\x1b[{}P", self.cursor)
                                        .into_bytes(),
                                )
                                .collect::<Vec<_>>()
                                .as_ref(),
                        )
                        .context(WriteToTerminal)?;
                        self.buffer = self.buffer.split_off(self.cursor);
                        self.cursor = 0;
                    }
                }
                _ => {}
            },
            crossterm::KeyEvent::Backspace => {
                if self.cursor != 0 {
                    self.cursor -= 1;
                    self.buffer.remove(self.cursor);
                    if self.cursor == self.buffer.len() {
                        self.echo(b"\x08 \x08").context(WriteToTerminal)?;
                    } else {
                        self.echo(b"\x08\x1b[P").context(WriteToTerminal)?;
                    }
                }
            }
            crossterm::KeyEvent::Left => {
                if self.cursor != 0 {
                    self.cursor -= 1;
                    self.write(b"\x1b[D").context(WriteToTerminal)?;
                }
            }
            crossterm::KeyEvent::Right => {
                if self.cursor != self.buffer.len() {
                    self.cursor += 1;
                    self.write(b"\x1b[C").context(WriteToTerminal)?;
                }
            }
            crossterm::KeyEvent::Delete => {
                if self.cursor != self.buffer.len() {
                    self.buffer.remove(self.cursor);
                    self.echo(b"\x1b[P").context(WriteToTerminal)?;
                }
            }
            _ => {}
        }

        Ok(futures::Async::NotReady)
    }

    fn write(&self, buf: &[u8]) -> std::io::Result<()> {
        let stdout = std::io::stdout();
        let mut stdout = stdout.lock();
        stdout.write_all(buf)?;
        stdout.flush()
    }

    fn prompt(&self) -> std::io::Result<()> {
        self.write(self.prompt.as_bytes())
    }

    fn echo(&self, bytes: &[u8]) -> std::io::Result<()> {
        let bytes: Vec<_> = bytes
            .iter()
            // replace \n with \r\n
            .fold(vec![], |mut acc, &c| {
                if c == b'\n' {
                    acc.push(b'\r');
                    acc.push(b'\n');
                } else {
                    if self.echo {
                        acc.push(c);
                    }
                }
                acc
            });
        self.write(&bytes)
    }

    fn echo_char(&self, c: char) -> std::io::Result<()> {
        let mut buf = [0_u8; 4];
        self.echo(c.encode_utf8(&mut buf[..]).as_bytes())
    }
}

#[must_use = "futures do nothing unless polled"]
impl futures::future::Future for Readline {
    type Item = String;
    type Error = Error;

    fn poll(&mut self) -> futures::Poll<Self::Item, Self::Error> {
        if !self.state.wrote_prompt {
            self.state.prompt().context(WriteToTerminal)?;
            self.state.wrote_prompt = true;
        }

        self.with_reader(|reader, state| {
            loop {
                match reader.events.try_recv() {
                    Ok(event) => {
                        let a = state.process_event(event)?;
                        if a.is_ready() {
                            return Ok(a);
                        }
                    }
                    Err(std::sync::mpsc::TryRecvError::Empty) => {
                        return Ok(futures::Async::NotReady)
                    }
                    Err(std::sync::mpsc::TryRecvError::Disconnected) => {
                        // is EOF correct here?
                        return EOF.fail();
                    }
                }
            }
        })
    }
}

struct KeyReader {
    events: std::sync::mpsc::Receiver<crossterm::InputEvent>,
    quit: std::sync::mpsc::Sender<()>,
}

impl KeyReader {
    fn new(task: futures::task::Task) -> Result<Self> {
        let reader = crossterm::input().read_sync();
        let (events_tx, events_rx) = std::sync::mpsc::channel();
        let (quit_tx, quit_rx) = std::sync::mpsc::channel();
        // TODO: this is pretty janky - it'd be better to build in more useful
        // support to crossterm directly
        std::thread::Builder::new()
            .spawn(move || {
                for event in reader {
                    let newline = event
                        == crossterm::InputEvent::Keyboard(
                            crossterm::KeyEvent::Char('\n'),
                        );
                    // unwrap is unpleasant, but so is figuring out how to
                    // propagate the error back to the main thread
                    events_tx.send(event).unwrap();
                    task.notify();
                    if newline {
                        break;
                    }
                    if quit_rx.try_recv().is_ok() {
                        break;
                    }
                }
            })
            .context(TerminalInputReadingThread)?;

        Ok(Self {
            events: events_rx,
            quit: quit_tx,
        })
    }
}

impl Drop for KeyReader {
    fn drop(&mut self) {
        // don't care if it fails to send, this can happen if the thread
        // terminates due to seeing a newline before the keyreader goes out of
        // scope
        let _ = self.quit.send(());
    }
}