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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
use std::mem::swap;

use crossterm::{
    cursor,
    style::{self, Attribute, ContentStyle, StyledContent},
    terminal, QueueableCommand,
};
use unicode_segmentation::UnicodeSegmentation;

use crate::{pos, Cell, Color, Device, Position, Result, State, Style, Vector};

/// A TTY-based user-interface providing optimized update rendering.
pub struct Interface<'a> {
    device: &'a mut dyn Device,
    size: Vector,
    current: State,
    alternate: Option<State>,
    staged_cursor: Option<Position>,
    cursor: Position,
    relative: bool,
}

impl Interface<'_> {
    /// Create a new interface for the specified device on the alternate screen.
    ///
    /// # Examples
    /// ```
    /// # use tty_interface::{Error, test::VirtualDevice};
    /// # let mut device = VirtualDevice::new();
    /// use tty_interface::Interface;
    ///
    /// let interface = Interface::new_alternate(&mut device)?;
    /// # Ok::<(), Error>(())
    /// ```
    pub fn new_alternate<'a>(device: &'a mut dyn Device) -> Result<Interface<'a>> {
        let size = device.get_terminal_size()?;

        let mut interface = Interface {
            device,
            size,
            current: State::new(),
            alternate: None,
            staged_cursor: None,
            cursor: pos!(0, 0),
            relative: false,
        };

        let device = &mut interface.device;
        device.enable_raw_mode()?;
        device.queue(terminal::EnterAlternateScreen)?;
        device.queue(terminal::Clear(terminal::ClearType::All))?;
        device.queue(cursor::Hide)?;
        device.queue(cursor::MoveTo(0, 0))?;
        device.flush()?;

        Ok(interface)
    }

    /// Create a new interface for the specified device which renders relatively in the buffer.
    ///
    /// # Examples
    /// ```
    /// # use tty_interface::{Error, test::VirtualDevice};
    /// # let mut device = VirtualDevice::new();
    /// use tty_interface::Interface;
    ///
    /// let interface = Interface::new_relative(&mut device)?;
    /// # Ok::<(), Error>(())
    /// ```
    pub fn new_relative<'a>(device: &'a mut dyn Device) -> Result<Interface<'a>> {
        let size = device.get_terminal_size()?;

        let mut interface = Interface {
            device,
            size,
            current: State::new(),
            alternate: None,
            staged_cursor: None,
            cursor: pos!(0, 0),
            relative: true,
        };

        let device = &mut interface.device;
        device.enable_raw_mode()?;

        Ok(interface)
    }

    /// When finished using this interface, uninitialize its terminal configuration.
    ///
    /// # Examples
    /// ```
    /// # use tty_interface::{Error, test::VirtualDevice};
    /// # let mut device = VirtualDevice::new();
    /// use tty_interface::Interface;
    ///
    /// let interface = Interface::new_alternate(&mut device)?;
    /// interface.exit()?;
    /// # Ok::<(), Error>(())
    /// ```
    pub fn exit(mut self) -> Result<()> {
        if !self.relative {
            self.device.queue(terminal::LeaveAlternateScreen)?;
            self.device.flush()?;
        } else {
            if let Some(last_position) = self.current.get_last_position() {
                self.move_cursor_to(pos!(0, last_position.y()))?;
            }
        }

        self.device.disable_raw_mode()?;

        println!();
        Ok(())
    }

    /// Update the interface's text at the specified position. Changes are staged until applied.
    ///
    /// # Examples
    /// ```
    /// # use tty_interface::{Error, test::VirtualDevice};
    /// # let mut device = VirtualDevice::new();
    /// use tty_interface::{Interface, Position, pos};
    ///
    /// let mut interface = Interface::new_alternate(&mut device)?;
    /// interface.set(pos!(1, 1), "Hello, world!");
    /// # Ok::<(), Error>(())
    /// ```
    pub fn set(&mut self, position: Position, text: &str) {
        self.stage_text(position, text, None)
    }

    /// Update the interface's text at the specified position. Changes are staged until applied.
    ///
    /// # Examples
    /// ```
    /// # use tty_interface::{Error, test::VirtualDevice};
    /// # let mut device = VirtualDevice::new();
    /// use tty_interface::{Interface, Style, Position, pos};
    ///
    /// let mut interface = Interface::new_alternate(&mut device)?;
    /// interface.set_styled(pos!(1, 1), "Hello, world!", Style::new().set_bold(true));
    /// # Ok::<(), Error>(())
    /// ```
    pub fn set_styled(&mut self, position: Position, text: &str, style: Style) {
        self.stage_text(position, text, Some(style))
    }

    /// Clear all text on the specified line. Changes are staged until applied.
    ///
    /// # Examples
    /// ```
    /// # use tty_interface::{Error, test::VirtualDevice};
    /// # let mut device = VirtualDevice::new();
    /// use tty_interface::{Interface, Style, Position, pos};
    ///
    /// let mut interface = Interface::new_alternate(&mut device)?;
    ///
    /// // Write "Hello," and "world!" on two different lines
    /// interface.set(pos!(0, 0), "Hello,");
    /// interface.set(pos!(0, 1), "world!");
    /// interface.apply()?;
    ///
    /// // Clear the second line, "world!"
    /// interface.clear_line(1);
    /// interface.apply()?;
    /// # Ok::<(), Error>(())
    /// ```
    pub fn clear_line(&mut self, line: u16) {
        let alternate = self.alternate.get_or_insert_with(|| self.current.clone());
        alternate.clear_line(line);
    }

    /// Clear the remainder of the line from the specified position. Changes are staged until
    /// applied.
    ///
    /// # Examples
    /// ```
    /// # use tty_interface::{Error, test::VirtualDevice};
    /// # let mut device = VirtualDevice::new();
    /// use tty_interface::{Interface, Style, Position, pos};
    ///
    /// let mut interface = Interface::new_alternate(&mut device)?;
    ///
    /// // Write "Hello, world!" to the first line
    /// interface.set(pos!(0, 0), "Hello, world!");
    /// interface.apply()?;
    ///
    /// // Clear everything after "Hello"
    /// interface.clear_rest_of_line(pos!(5, 0));
    /// interface.apply()?;
    /// # Ok::<(), Error>(())
    /// ```
    pub fn clear_rest_of_line(&mut self, from: Position) {
        let alternate = self.alternate.get_or_insert_with(|| self.current.clone());
        alternate.clear_rest_of_line(from);
    }

    /// Clear the remainder of the interface from the specified position. Changes are staged until
    /// applied.
    ///
    /// # Examples
    /// ```
    /// # use tty_interface::{Error, test::VirtualDevice};
    /// # let mut device = VirtualDevice::new();
    /// use tty_interface::{Interface, Style, Position, pos};
    ///
    /// let mut interface = Interface::new_alternate(&mut device)?;
    ///
    /// // Write two lines of content
    /// interface.set(pos!(0, 0), "Hello, world!");
    /// interface.set(pos!(0, 1), "Another line");
    /// interface.apply()?;
    ///
    /// // Clear everything after "Hello", including the second line
    /// interface.clear_rest_of_interface(pos!(5, 0));
    /// interface.apply()?;
    /// # Ok::<(), Error>(())
    /// ```
    pub fn clear_rest_of_interface(&mut self, from: Position) {
        let alternate = self.alternate.get_or_insert_with(|| self.current.clone());
        alternate.clear_rest_of_interface(from);
    }

    /// Update the interface's cursor to the specified position, or hide it if unspecified.
    ///
    /// # Examples
    /// ```
    /// # use tty_interface::{Error, test::VirtualDevice};
    /// # let mut device = VirtualDevice::new();
    /// use tty_interface::{Interface, Position, pos};
    ///
    /// let mut interface = Interface::new_alternate(&mut device)?;
    /// interface.set_cursor(Some(pos!(1, 2)));
    /// # Ok::<(), Error>(())
    /// ```
    pub fn set_cursor(&mut self, position: Option<Position>) {
        self.alternate.get_or_insert_with(|| self.current.clone());
        self.staged_cursor = position;
    }

    /// Stages the specified text and optional style at a position in the terminal.
    fn stage_text(&mut self, position: Position, text: &str, style: Option<Style>) {
        let alternate = self.alternate.get_or_insert_with(|| self.current.clone());

        let mut line = position.y().into();
        let mut column = position.x().into();

        for grapheme in text.graphemes(true) {
            if column > self.size.x().into() {
                column = 0;
                line += 1;
            }

            let cell_position = pos!(column, line);
            match style {
                Some(style) => alternate.set_styled_text(cell_position, grapheme, style),
                None => alternate.set_text(cell_position, grapheme),
            }

            column += 1;
        }
    }

    /// Applies staged changes to the terminal.
    ///
    /// # Examples
    /// ```
    /// # use tty_interface::{Error, test::VirtualDevice};
    /// # let mut device = VirtualDevice::new();
    /// use tty_interface::{Interface, Position, pos};
    ///
    /// let mut interface = Interface::new_alternate(&mut device)?;
    /// interface.set(pos!(1, 1), "Hello, world!");
    /// interface.apply()?;
    /// # Ok::<(), Error>(())
    /// ```
    pub fn apply(&mut self) -> Result<()> {
        if self.alternate.is_none() {
            return Ok(());
        }

        let mut alternate = self.alternate.take().unwrap();
        swap(&mut self.current, &mut alternate);

        let dirty_cells: Vec<(Position, Option<Cell>)> = self.current.dirty_iter().collect();

        self.device.queue(cursor::Hide)?;

        for (position, cell) in dirty_cells {
            if self.cursor != position {
                self.move_cursor_to(position)?;
            }

            match cell {
                Some(cell) => {
                    let mut content_style = ContentStyle::default();
                    if let Some(style) = cell.style() {
                        content_style = get_content_style(*style);
                    }

                    let styled_content = StyledContent::new(content_style, cell.grapheme());
                    let print_styled_content = style::PrintStyledContent(styled_content);
                    self.device.queue(print_styled_content)?;
                }
                None => {
                    let clear_content = style::Print(' ');
                    self.device.queue(clear_content)?;
                }
            }

            self.cursor = self.cursor.translate(1, 0);
        }

        if let Some(position) = self.staged_cursor {
            self.move_cursor_to(position)?;
            self.device.queue(cursor::Show)?;
        }

        self.device.flush()?;

        self.current.clear_dirty();

        Ok(())
    }

    /// Move the cursor to the specified position and update it in state.
    fn move_cursor_to(&mut self, position: Position) -> Result<()> {
        if self.relative {
            let diff_x = position.x() as i32 - self.cursor.x() as i32;
            let diff_y = position.y() as i32 - self.cursor.y() as i32;

            if diff_x > 0 {
                self.device.queue(cursor::MoveRight(diff_x as u16))?;
            } else if diff_x < 0 {
                self.device.queue(cursor::MoveLeft(diff_x.abs() as u16))?;
            }

            if diff_y > 0 {
                self.device
                    .queue(style::Print("\n".repeat(diff_y as usize)))?;
            } else if diff_y < 0 {
                self.device.queue(cursor::MoveUp(diff_y.abs() as u16))?;
            }
        } else {
            let move_cursor = cursor::MoveTo(position.x(), position.y());
            self.device.queue(move_cursor)?;
        }

        self.cursor = position;

        Ok(())
    }
}

/// Converts a style from its internal representation to crossterm's.
fn get_content_style(style: Style) -> ContentStyle {
    let mut content_style = ContentStyle::default();

    if let Some(color) = style.foreground() {
        content_style.foreground_color = Some(get_crossterm_color(color));
    }

    if let Some(color) = style.background() {
        content_style.background_color = Some(get_crossterm_color(color));
    }

    if style.is_bold() {
        content_style.attributes.set(Attribute::Bold);
    }

    if style.is_italic() {
        content_style.attributes.set(Attribute::Italic);
    }

    if style.is_underlined() {
        content_style.attributes.set(Attribute::Underlined);
    }

    content_style
}

fn get_crossterm_color(color: Color) -> crossterm::style::Color {
    match color {
        Color::Black => style::Color::Black,
        Color::DarkGrey => style::Color::DarkGrey,
        Color::Red => style::Color::Red,
        Color::DarkRed => style::Color::DarkRed,
        Color::Green => style::Color::Green,
        Color::DarkGreen => style::Color::DarkGreen,
        Color::Yellow => style::Color::Yellow,
        Color::DarkYellow => style::Color::DarkYellow,
        Color::Blue => style::Color::Blue,
        Color::DarkBlue => style::Color::DarkBlue,
        Color::Magenta => style::Color::Magenta,
        Color::DarkMagenta => style::Color::DarkMagenta,
        Color::Cyan => style::Color::Cyan,
        Color::DarkCyan => style::Color::DarkCyan,
        Color::White => style::Color::White,
        Color::Grey => style::Color::Grey,
        Color::Reset => style::Color::Reset,
    }
}