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
//! Terminal implementation for all non-Redox operating systems.

use crate::{
    event::{Event, Key, MouseButton, MouseEvent, MouseEventKind},
    util::{Color, Point, Size},
    Terminal,
};
use crossterm::{cursor, event, style, terminal, tty::IsTty, QueueableCommand};
use std::{io, time::Duration};

// TODO: return result instead of unwrapping?

// > When I first did this, it was noticeably slower than the termion version(roughly 5-10 fps).
// > This is because calling into the Console API that often (once per character) is going to pull down performance.
// > Luckily, I could work around this by just checking if we were already using the color I wanted to render.
// > If we were, I didn't set the color again.

impl<'a> Terminal<'a> {
    pub fn enter_alternate_dimension(&mut self) {
        self.stdout.queue(terminal::EnterAlternateScreen).unwrap();
    }
    pub fn exit_alternate_dimension(&mut self) {
        self.stdout.queue(terminal::LeaveAlternateScreen).unwrap();
    }

    pub fn set_title(&mut self, title: &str) {
        self.stdout.queue(terminal::SetTitle(title)).unwrap();
    }

    pub fn enable_raw_mode(&self) {
        terminal::enable_raw_mode().unwrap();
    }
    pub fn disable_raw_mode(&self) {
        terminal::disable_raw_mode().unwrap();
    }

    // TODO: use custom escape sequence to be more specific about what mouse events exactly to take
    pub fn enable_mouse_capture(&mut self) {
        self.stdout.queue(event::EnableMouseCapture).unwrap();
        self.with_mouse = true;
    }
    pub fn disable_mouse_capture(&mut self) {
        self.stdout.queue(event::DisableMouseCapture).unwrap();
        self.with_mouse = false;
    }

    pub fn show_cursor(&mut self) {
        self.stdout.queue(cursor::Show).unwrap();
    }
    pub fn hide_cursor(&mut self) {
        self.stdout.queue(cursor::Hide).unwrap();
    }

    /// Reads an event. It also sets the new size if the terminal has been resized, hence a mutable borrow of `self` is required.
    pub fn read_event(&mut self) -> Option<Event> {
        if let Ok(crossterm_event) = event::read() {
            let event = match crossterm_event {
                event::Event::Mouse(event) => {
                    fn translate_button(button: event::MouseButton) -> MouseButton {
                        match button {
                            event::MouseButton::Left => MouseButton::Left,
                            event::MouseButton::Middle => MouseButton::Middle,
                            event::MouseButton::Right => MouseButton::Right,
                        }
                    }

                    let kind = match event.kind {
                        event::MouseEventKind::Moved => MouseEventKind::Move,
                        event::MouseEventKind::Drag(button) => {
                            MouseEventKind::Drag(translate_button(button))
                        }
                        event::MouseEventKind::Down(button) => {
                            MouseEventKind::Press(translate_button(button))
                        }
                        event::MouseEventKind::Up(button) => {
                            MouseEventKind::Release(translate_button(button))
                        }
                        event::MouseEventKind::ScrollUp => MouseEventKind::ScrollUp,
                        event::MouseEventKind::ScrollDown => MouseEventKind::ScrollDown,
                    };

                    let point = Point {
                        x: event.column,
                        y: event.row,
                    };

                    Event::Mouse(MouseEvent { kind, point })
                }
                event::Event::Key(event::KeyEvent { code, modifiers: _ }) => {
                    let key = match code {
                        event::KeyCode::Char(char) => Key::Char(char),
                        event::KeyCode::Up => Key::Up,
                        event::KeyCode::Down => Key::Down,
                        event::KeyCode::Left => Key::Left,
                        event::KeyCode::Right => Key::Right,
                        event::KeyCode::Tab => Key::Tab,
                        event::KeyCode::Enter => Key::Enter,
                        event::KeyCode::F(number) => Key::F(number),
                        event::KeyCode::Backspace => Key::Backspace,
                        event::KeyCode::Esc => Key::Esc,
                        _ => return None,
                    };

                    // let modifier = if modifiers == event::KeyModifiers::ALT {
                    //     Some(KeyModifier::Alt)
                    // } else if modifiers == event::KeyModifiers::CONTROL {
                    //     Some(KeyModifier::Control)
                    // } else if modifiers == event::KeyModifiers::SHIFT {
                    //     Some(KeyModifier::Shift)
                    // } else {
                    //     None
                    // };

                    Event::Key(key)
                }
                event::Event::Resize(width, height) => {
                    self.size = Size { width, height };
                    Event::Resize
                }
            };
            Some(event)
        } else {
            None
        }
    }

    pub fn poll_event(&mut self, timeout: Duration) -> Option<Event> {
        if let Ok(true) = crossterm::event::poll(timeout) {
            self.read_event()
        } else {
            None
        }
    }

    /// Sets the cursor to the top left corner.
    #[cfg(not(target_os = "windows"))]
    pub fn reset_cursor(&mut self) {
        self.write("\u{1b}[;H");
    }

    /// Sets the cursor to the top left corner.
    #[cfg(target_os = "windows")]
    pub fn reset_cursor(&mut self) {
        self.set_cursor(Point::default());
    }

    /// Sets the cursor to `point`.
    ///
    /// If possible, try to use the `move_cursor_{}_by` and `move_cursor_{}` methods instead for single operations.
    pub fn set_cursor(&mut self, point: Point) {
        self.stdout.queue(cursor::MoveTo(point.x, point.y)).unwrap();
    }

    /// Sets the cursor X-coordinate to `x`.
    pub fn set_cursor_x(&mut self, x: u16) {
        self.stdout.queue(cursor::MoveToColumn(x)).unwrap();
    }

    /// Sets the cursor Y-coordinate to `y`.
    pub fn set_cursor_y(&mut self, y: u16) {
        self.stdout.queue(cursor::MoveToRow(y)).unwrap();
    }

    pub fn move_cursor_up_by(&mut self, cells: u16) {
        self.stdout.queue(cursor::MoveUp(cells)).unwrap();
    }
    pub fn move_cursor_down_by(&mut self, cells: u16) {
        self.stdout.queue(cursor::MoveDown(cells)).unwrap();
    }
    pub fn move_cursor_left_by(&mut self, cells: u16) {
        self.stdout.queue(cursor::MoveLeft(cells)).unwrap();
    }
    pub fn move_cursor_right_by(&mut self, cells: u16) {
        self.stdout.queue(cursor::MoveRight(cells)).unwrap();
    }

    #[cfg(not(target_os = "windows"))]
    pub fn move_cursor_up(&mut self) {
        self.write("\u{1b}[A");
    }
    #[cfg(not(target_os = "windows"))]
    pub fn move_cursor_down(&mut self) {
        self.write("\u{1b}[B");
    }
    #[cfg(not(target_os = "windows"))]
    pub fn move_cursor_left(&mut self) {
        self.write("\u{1b}[D");
    }
    #[cfg(not(target_os = "windows"))]
    pub fn move_cursor_right(&mut self) {
        self.write("\u{1b}[C");
    }

    #[cfg(not(target_os = "windows"))]
    pub fn next_line(&mut self) {
        self.write("\u{1b}[E");
    }
    #[cfg(not(target_os = "windows"))]
    pub fn previous_line(&mut self) {
        self.write("\u{1b}[F");
    }

    #[cfg(target_os = "windows")]
    pub fn move_cursor_up(&mut self) {
        self.move_cursor_up_by(1);
    }
    #[cfg(target_os = "windows")]
    pub fn move_cursor_down(&mut self) {
        self.move_cursor_down_by(1);
    }
    #[cfg(target_os = "windows")]
    pub fn move_cursor_left(&mut self) {
        self.move_cursor_left_by(1);
    }
    #[cfg(target_os = "windows")]
    pub fn move_cursor_right(&mut self) {
        self.move_cursor_right_by(1);
    }

    #[cfg(target_os = "windows")]
    pub fn next_line(&mut self) {
        self.stdout.queue(cursor::MoveToNextLine(1)).unwrap();
    }
    #[cfg(target_os = "windows")]
    pub fn previous_line(&mut self) {
        self.stdout.queue(cursor::MoveToPreviousLine(1)).unwrap();
    }

    pub fn save_cursor_point(&mut self) {
        self.stdout.queue(cursor::SavePosition).unwrap();
    }
    pub fn restore_cursor_point(&mut self) {
        self.stdout.queue(cursor::RestorePosition).unwrap();
    }

    pub fn set_foreground_color(&mut self, color: Color) {
        self.stdout
            .queue(style::SetForegroundColor(Self::convert_color(color)))
            .unwrap();
    }
    pub fn set_background_color(&mut self, color: Color) {
        self.stdout
            .queue(style::SetBackgroundColor(Self::convert_color(color)))
            .unwrap();
    }

    //
    // TODO for the following methods: Do they work on Windows?
    //

    // Reference: https://invisible-island.net/xterm/ctlseqs/ctlseqs.html#h3-Operating-System-Commands
    // NOTE: clipboard functionality can be added: https://github.com/alacritty/alacritty/blob/3e867a056018c507d79396cb5c5b4b8309c609c2/alacritty_terminal/src/ansi.rs#L440

    /// Changes the terminal's foreground text color to `hex_color`.
    ///
    /// `hex_color` must be a hexadecimal color such as `"FF0000"`.
    pub fn change_foreground_color(&mut self, hex_color: &str) {
        self.write(&format!("\u{1b}]10;#{}\u{7}", hex_color));
    }
    pub fn reset_foreground_color(&mut self) {
        self.write("\u{1b}]110\u{7}");
    }

    /// Changes the terminal's background text color to `hex_color`.
    ///
    /// `hex_color` must be a hexadecimal color such as `FF0000`.
    pub fn change_background_color(&mut self, hex_color: &str) {
        self.write(&format!("\u{1b}]11;#{}\u{7}", hex_color));
    }
    pub fn reset_background_color(&mut self) {
        self.write("\u{1b}]111\u{7}");
    }

    /// Changes the terminal's cursor color to `hex_color`.
    ///
    /// `hex_color` must be a hexadecimal color such as `FF0000`.
    pub fn change_cursor_color(&mut self, hex_color: &str) {
        self.write(&format!("\u{1b}]12;#{}\u{7}", hex_color));
    }
    pub fn reset_cursor_color(&mut self) {
        self.write("\u{1b}]112\u{7}");
    }

    pub fn enable_italic(&mut self) {
        self.write(&format!("{}", style::Attribute::Italic));
    }
    pub fn disable_italic(&mut self) {
        self.write(&format!("{}", style::Attribute::NoItalic));
    }

    pub fn reset_colors(&mut self) {
        self.stdout.queue(style::ResetColor).unwrap();
    }

    pub fn clear(&mut self) {
        self.stdout
            .queue(terminal::Clear(terminal::ClearType::All))
            .unwrap();
    }
    pub fn clear_from_cursor_to_end(&mut self) {
        self.stdout
            .queue(terminal::Clear(terminal::ClearType::FromCursorUp))
            .unwrap();
    }

    fn convert_color(color: Color) -> style::Color {
        match color {
            Color::Black => style::Color::Black,
            Color::DarkGray => 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::Gray => style::Color::Grey,
            Color::Rgb { r, g, b } => style::Color::Rgb { r, g, b },
            Color::Byte(rgb) => style::Color::AnsiValue(rgb),
        }
    }

    pub(crate) fn size() -> Size {
        let size = terminal::size().unwrap();
        Size {
            width: size.0,
            height: size.1,
        }
    }

    pub(crate) fn is_tty(stdout: &io::StdoutLock) -> bool {
        stdout.is_tty()
    }
}