repline/
editor.rs

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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
//! The [Editor] is a multi-line buffer of [`char`]s which operates on an ANSI-compatible terminal.

use crossterm::{cursor::*, execute, queue, style::*, terminal::*};
use std::{collections::VecDeque, fmt::Display, io::Write};

use super::error::{Error, ReplResult};

fn is_newline(c: &char) -> bool {
    *c == '\n'
}

fn write_chars<'a, W: Write>(
    c: impl IntoIterator<Item = &'a char>,
    w: &mut W,
) -> std::io::Result<()> {
    for c in c {
        write!(w, "{c}")?;
    }
    Ok(())
}

/// A multi-line editor which operates on an un-cleared ANSI terminal.
#[derive(Clone, Debug)]
pub struct Editor<'a> {
    head: VecDeque<char>,
    tail: VecDeque<char>,

    pub color: &'a str,
    begin: &'a str,
    again: &'a str,
}

impl<'a> Editor<'a> {
    /// Constructs a new Editor with the provided prompt color, begin prompt, and again prompt.
    pub fn new(color: &'a str, begin: &'a str, again: &'a str) -> Self {
        Self { head: Default::default(), tail: Default::default(), color, begin, again }
    }

    /// Returns an iterator over characters in the editor.
    pub fn iter(&self) -> impl Iterator<Item = &char> {
        let Self { head, tail, .. } = self;
        head.iter().chain(tail.iter())
    }

    /// Moves up to the first line of the editor, and clears the screen.
    ///
    /// This assumes the screen hasn't moved since the last draw.
    pub fn undraw<W: Write>(&self, w: &mut W) -> ReplResult<()> {
        let Self { head, .. } = self;
        match head.iter().copied().filter(is_newline).count() {
            0 => write!(w, "\x1b[0G"),
            lines => write!(w, "\x1b[{}F", lines),
        }?;
        queue!(w, Clear(ClearType::FromCursorDown))?;
        // write!(w, "\x1b[0J")?;
        Ok(())
    }

    /// Redraws the entire editor
    pub fn redraw<W: Write>(&self, w: &mut W) -> ReplResult<()> {
        let Self { head, tail, color, begin, again } = self;
        write!(w, "{color}{begin}\x1b[0m ")?;
        // draw head
        for c in head {
            match c {
                '\n' => write!(w, "\r\n{color}{again}\x1b[0m "),
                _ => w.write_all({ *c as u32 }.to_le_bytes().as_slice()),
            }?
        }
        // save cursor
        execute!(w, SavePosition)?;
        // draw tail
        for c in tail {
            match c {
                '\n' => write!(w, "\r\n{color}{again}\x1b[0m "),
                _ => write!(w, "{c}"),
            }?
        }
        // restore cursor
        execute!(w, RestorePosition)?;
        Ok(())
    }

    /// Prints a context-sensitive prompt (either `begin` if this is the first line,
    /// or `again` for subsequent lines)
    pub fn prompt<W: Write>(&self, w: &mut W) -> ReplResult<()> {
        let Self { head, color, begin, again, .. } = self;
        queue!(
            w,
            MoveToColumn(0),
            Print(color),
            Print(if head.is_empty() { begin } else { again }),
            ResetColor,
            Print(' '),
        )?;
        Ok(())
    }

    /// Prints the characters before the cursor on the current line.
    pub fn print_head<W: Write>(&self, w: &mut W) -> ReplResult<()> {
        self.prompt(w)?;
        write_chars(
            self.head.iter().skip(
                self.head
                    .iter()
                    .rposition(is_newline)
                    .unwrap_or(self.head.len())
                    + 1,
            ),
            w,
        )?;
        Ok(())
    }

    pub fn print_err<W: Write>(&self, w: &mut W, err: impl Display) -> ReplResult<()> {
        queue!(
            w,
            SavePosition,
            Clear(ClearType::UntilNewLine),
            Print(err),
            RestorePosition
        )?;
        Ok(())
    }

    /// Prints the characters after the cursor on the current line.
    pub fn print_tail<W: Write>(&self, w: &mut W) -> ReplResult<()> {
        let Self { tail, .. } = self;
        queue!(w, SavePosition, Clear(ClearType::UntilNewLine))?;
        write_chars(tail.iter().take_while(|&c| !is_newline(c)), w)?;
        queue!(w, RestorePosition)?;
        Ok(())
    }

    /// Writes a character at the cursor, shifting the text around as necessary.
    pub fn push<W: Write>(&mut self, c: char, w: &mut W) -> ReplResult<()> {
        // Tail optimization: if the tail is empty,
        //we don't have to undraw and redraw on newline
        if self.tail.is_empty() {
            self.head.push_back(c);
            match c {
                '\n' => {
                    write!(w, "\r\n")?;
                    self.print_head(w)?;
                }
                c => {
                    queue!(w, Print(c))?;
                }
            };
            return Ok(());
        }

        if '\n' == c {
            self.undraw(w)?;
        }
        self.head.push_back(c);
        match c {
            '\n' => self.redraw(w)?,
            _ => {
                write!(w, "{c}")?;
                self.print_tail(w)?;
            }
        }
        Ok(())
    }

    /// Erases a character at the cursor, shifting the text around as necessary.
    pub fn pop<W: Write>(&mut self, w: &mut W) -> ReplResult<Option<char>> {
        if let Some('\n') = self.head.back() {
            self.undraw(w)?;
        }
        let c = self.head.pop_back();
        // if the character was a newline, we need to go back a line
        match c {
            Some('\n') => self.redraw(w)?,
            Some(_) => {
                // go back a char
                queue!(w, MoveLeft(1), Print(' '), MoveLeft(1))?;
                self.print_tail(w)?;
            }
            None => {}
        }
        Ok(c)
    }

    /// Writes characters into the editor at the location of the cursor.
    pub fn extend<T: IntoIterator<Item = char>, W: Write>(
        &mut self,
        iter: T,
        w: &mut W,
    ) -> ReplResult<()> {
        for c in iter {
            self.push(c, w)?;
        }
        Ok(())
    }

    /// Sets the editor to the contents of a string, placing the cursor at the end.
    pub fn restore(&mut self, s: &str) {
        self.clear();
        self.head.extend(s.chars())
    }

    /// Clears the editor, removing all characters.
    pub fn clear(&mut self) {
        self.head.clear();
        self.tail.clear();
    }

    /// Pops the character after the cursor, redrawing if necessary
    pub fn delete<W: Write>(&mut self, w: &mut W) -> ReplResult<char> {
        match self.tail.front() {
            Some('\n') => {
                self.undraw(w)?;
                let out = self.tail.pop_front();
                self.redraw(w)?;
                out
            }
            _ => {
                let out = self.tail.pop_front();
                self.print_tail(w)?;
                out
            }
        }
        .ok_or(Error::EndOfInput)
    }

    /// Erases a word from the buffer, where a word is any non-whitespace characters
    /// preceded by a single whitespace character
    pub fn erase_word<W: Write>(&mut self, w: &mut W) -> ReplResult<()> {
        while self.pop(w)?.filter(|c| !c.is_whitespace()).is_some() {}
        Ok(())
    }

    /// Returns the number of characters in the buffer
    pub fn len(&self) -> usize {
        self.head.len() + self.tail.len()
    }

    /// Returns true if the buffer is empty.
    pub fn is_empty(&self) -> bool {
        self.head.is_empty() && self.tail.is_empty()
    }

    /// Returns true if the buffer ends with a given pattern
    pub fn ends_with(&self, iter: impl DoubleEndedIterator<Item = char>) -> bool {
        let mut iter = iter.rev();
        let mut head = self.head.iter().rev();
        loop {
            match (iter.next(), head.next()) {
                (None, _) => break true,
                (Some(_), None) => break false,
                (Some(a), Some(b)) if a != *b => break false,
                (Some(_), Some(_)) => continue,
            }
        }
    }

    /// Moves the cursor back `steps` steps
    pub fn cursor_back<W: Write>(&mut self, steps: usize, w: &mut W) -> ReplResult<()> {
        for _ in 0..steps {
            if let Some('\n') = self.head.back() {
                self.undraw(w)?;
            }
            let Some(c) = self.head.pop_back() else {
                return Ok(());
            };
            self.tail.push_front(c);
            match c {
                '\n' => self.redraw(w)?,
                _ => queue!(w, MoveLeft(1))?,
            }
        }
        Ok(())
    }

    /// Moves the cursor forward `steps` steps
    pub fn cursor_forward<W: Write>(&mut self, steps: usize, w: &mut W) -> ReplResult<()> {
        for _ in 0..steps {
            if let Some('\n') = self.tail.front() {
                self.undraw(w)?
            }
            let Some(c) = self.tail.pop_front() else {
                return Ok(());
            };
            self.head.push_back(c);
            match c {
                '\n' => self.redraw(w)?,
                _ => queue!(w, MoveRight(1))?,
            }
        }
        Ok(())
    }

    /// Moves the cursor to the beginning of the current line
    pub fn home<W: Write>(&mut self, w: &mut W) -> ReplResult<()> {
        loop {
            match self.head.back() {
                Some('\n') | None => break Ok(()),
                Some(_) => self.cursor_back(1, w)?,
            }
        }
    }

    /// Moves the cursor to the end of the current line
    pub fn end<W: Write>(&mut self, w: &mut W) -> ReplResult<()> {
        loop {
            match self.tail.front() {
                Some('\n') | None => break Ok(()),
                Some(_) => self.cursor_forward(1, w)?,
            }
        }
    }
}

impl<'e> IntoIterator for &'e Editor<'_> {
    type Item = &'e char;
    type IntoIter = std::iter::Chain<
        std::collections::vec_deque::Iter<'e, char>,
        std::collections::vec_deque::Iter<'e, char>,
    >;
    fn into_iter(self) -> Self::IntoIter {
        self.head.iter().chain(self.tail.iter())
    }
}

impl Display for Editor<'_> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        use std::fmt::Write;
        for c in self.iter() {
            f.write_char(*c)?;
        }
        Ok(())
    }
}