aboutsummaryrefslogtreecommitdiffstats
path: root/src/readline.rs
blob: 73b51952e4f77ea22ef9a795dfe9f33a529b1ab7 (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
use std::io::Write;

#[derive(Debug)]
pub enum Error {
    EOF,
    IOError(std::io::Error),
}

pub struct Readline {
    reader: Option<KeyReader>,
    buffer: String,
    prompt: String,
    wrote_prompt: bool,
    echo: bool,
}

impl Readline {
    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)
            }
            _ => {}
        }
        return 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(c) => {
                self.echo(c).map_err(|e| Error::IOError(e))?;

                if c == '\n' {
                    return Ok(futures::Async::Ready(self.buffer.clone()));
                }
                self.buffer.push(c);
            }
            crossterm::KeyEvent::Ctrl(c) => {
                if c == 'd' {
                    if self.buffer.is_empty() {
                        self.echo('\n').map_err(|e| Error::IOError(e))?;
                        return Err(Error::EOF);
                    }
                }
                if c == 'c' {
                    self.buffer = String::new();
                    self.echo('\n').map_err(|e| Error::IOError(e))?;
                    self.prompt().map_err(|e| Error::IOError(e))?;
                }
            }
            _ => {}
        }
        return Ok(futures::Async::NotReady);
    }

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

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

    fn echo(&self, c: char) -> std::io::Result<()> {
        if c == '\n' {
            self.write(b"\r\n")?;
            return Ok(());
        }

        if !self.echo {
            return Ok(());
        }

        let mut buf = [0u8; 4];
        self.write(c.encode_utf8(&mut buf[..]).as_bytes())
    }
}

impl futures::future::Future for Readline {
    type Item = String;
    type Error = Error;

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

        let reader = self.reader.get_or_insert_with(|| {
            KeyReader::new(tokio::prelude::task::current())
        });
        if let Some(event) = reader.poll() {
            self.process_event(event)
        } else {
            Ok(futures::Async::NotReady)
        }
    }
}

pub fn readline(prompt: &str, echo: bool) -> Readline {
    Readline {
        reader: None,
        buffer: String::new(),
        prompt: prompt.to_string(),
        wrote_prompt: false,
        echo,
    }
}

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

impl KeyReader {
    fn new(task: tokio::prelude::task::Task) -> 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();
        std::thread::spawn(move || {
            for event in reader {
                let newline = event
                    == crossterm::InputEvent::Keyboard(
                        crossterm::KeyEvent::Char('\n'),
                    );
                events_tx.send(event).unwrap();
                task.notify();
                if newline {
                    break;
                }
                if let Ok(_) = quit_rx.try_recv() {
                    break;
                }
            }
        });

        KeyReader {
            events: events_rx,
            quit: quit_tx,
        }
    }

    fn poll(&self) -> Option<crossterm::InputEvent> {
        if let Ok(event) = self.events.try_recv() {
            return Some(event);
        }
        None
    }
}

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
        match self.quit.send(()) {
            _ => {}
        }
    }
}