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
//! Backend using the pure-rust crossplatform crossterm library.
//!
//! Requires the `crossterm-backend` feature.
#![cfg(feature = "crossterm")]
#![cfg_attr(feature = "doc-cfg", doc(cfg(feature = "crossterm-backend")))]

use std::{
    cell::{Cell, RefCell, RefMut},
    io::{self, BufWriter, Write},
    time::Duration,
};

pub use crossterm;

use crossterm::{
    cursor::{Hide, MoveTo, Show},
    event::{
        poll, read, DisableMouseCapture, EnableMouseCapture, Event as CEvent,
        KeyCode, KeyEvent as CKeyEvent, KeyModifiers,
        MouseButton as CMouseButton, MouseEvent as CMouseEvent,
        MouseEventKind,
    },
    execute, queue,
    style::{
        Attribute, Color, Print, SetAttribute, SetBackgroundColor,
        SetForegroundColor,
    },
    terminal::{
        self, disable_raw_mode, enable_raw_mode, Clear, ClearType,
        EnterAlternateScreen, LeaveAlternateScreen,
    },
};

use crate::{
    backend,
    event::{Event, Key, MouseButton, MouseEvent},
    theme, Vec2,
};

#[cfg(windows)]
type Stdout = io::Stdout;

#[cfg(unix)]
type Stdout = std::fs::File;

/// Backend using crossterm
pub struct Backend {
    current_style: Cell<theme::ColorPair>,

    stdout: RefCell<BufWriter<Stdout>>,
}

fn translate_button(button: CMouseButton) -> MouseButton {
    match button {
        CMouseButton::Left => MouseButton::Left,
        CMouseButton::Right => MouseButton::Right,
        CMouseButton::Middle => MouseButton::Middle,
    }
}

fn translate_key(code: KeyCode) -> Key {
    match code {
        KeyCode::Esc => Key::Esc,
        KeyCode::Backspace => Key::Backspace,
        KeyCode::Left => Key::Left,
        KeyCode::Right => Key::Right,
        KeyCode::Up => Key::Up,
        KeyCode::Down => Key::Down,
        KeyCode::Home => Key::Home,
        KeyCode::End => Key::End,
        KeyCode::PageUp => Key::PageUp,
        KeyCode::PageDown => Key::PageDown,
        KeyCode::Delete => Key::Del,
        KeyCode::Insert => Key::Ins,
        KeyCode::Enter => Key::Enter,
        KeyCode::Tab => Key::Tab,
        KeyCode::F(n) => Key::from_f(n),
        KeyCode::BackTab => Key::Tab, /* not supported */
        KeyCode::Char(_) => Key::Tab, /* is handled at `Event` level, use tab as default */
        KeyCode::Null => Key::Tab, /* is handled at `Event` level, use tab as default */
    }
}

fn translate_event(event: CKeyEvent) -> Event {
    const CTRL_ALT: KeyModifiers = KeyModifiers::from_bits_truncate(
        KeyModifiers::CONTROL.bits() | KeyModifiers::ALT.bits(),
    );
    const CTRL_SHIFT: KeyModifiers = KeyModifiers::from_bits_truncate(
        KeyModifiers::CONTROL.bits() | KeyModifiers::SHIFT.bits(),
    );
    const ALT_SHIFT: KeyModifiers = KeyModifiers::from_bits_truncate(
        KeyModifiers::ALT.bits() | KeyModifiers::SHIFT.bits(),
    );

    match event {
        // Handle Char + modifier.
        CKeyEvent {
            modifiers: KeyModifiers::CONTROL,
            code: KeyCode::Char(c),
        } => Event::CtrlChar(c),
        CKeyEvent {
            modifiers: KeyModifiers::ALT,
            code: KeyCode::Char(c),
        } => Event::AltChar(c),
        CKeyEvent {
            modifiers: KeyModifiers::SHIFT,
            code: KeyCode::Char(c),
        } => Event::Char(c),

        // Handle key + multiple modifiers
        CKeyEvent {
            modifiers: CTRL_ALT,
            code,
        } => Event::CtrlAlt(translate_key(code)),
        CKeyEvent {
            modifiers: CTRL_SHIFT,
            code,
        } => Event::CtrlShift(translate_key(code)),
        CKeyEvent {
            modifiers: ALT_SHIFT,
            code,
        } => Event::AltShift(translate_key(code)),

        // Handle key + single modifier
        CKeyEvent {
            modifiers: KeyModifiers::CONTROL,
            code,
        } => Event::Ctrl(translate_key(code)),
        CKeyEvent {
            modifiers: KeyModifiers::ALT,
            code,
        } => Event::Alt(translate_key(code)),
        CKeyEvent {
            modifiers: KeyModifiers::SHIFT,
            code,
        } => Event::Shift(translate_key(code)),

        CKeyEvent {
            code: KeyCode::Char(c),
            ..
        } => Event::Char(c),

        // Explicitly handle 'backtab' since crossterm does not sent SHIFT alongside the back tab key.
        CKeyEvent {
            code: KeyCode::BackTab,
            ..
        } => Event::Shift(Key::Tab),

        // All other keys.
        CKeyEvent { code, .. } => Event::Key(translate_key(code)),
    }
}

fn translate_color(base_color: theme::Color) -> Color {
    match base_color {
        theme::Color::Dark(theme::BaseColor::Black) => Color::Black,
        theme::Color::Dark(theme::BaseColor::Red) => Color::DarkRed,
        theme::Color::Dark(theme::BaseColor::Green) => Color::DarkGreen,
        theme::Color::Dark(theme::BaseColor::Yellow) => Color::DarkYellow,
        theme::Color::Dark(theme::BaseColor::Blue) => Color::DarkBlue,
        theme::Color::Dark(theme::BaseColor::Magenta) => Color::DarkMagenta,
        theme::Color::Dark(theme::BaseColor::Cyan) => Color::DarkCyan,
        theme::Color::Dark(theme::BaseColor::White) => Color::Grey,
        theme::Color::Light(theme::BaseColor::Black) => Color::DarkGrey,
        theme::Color::Light(theme::BaseColor::Red) => Color::Red,
        theme::Color::Light(theme::BaseColor::Green) => Color::Green,
        theme::Color::Light(theme::BaseColor::Yellow) => Color::Yellow,
        theme::Color::Light(theme::BaseColor::Blue) => Color::Blue,
        theme::Color::Light(theme::BaseColor::Magenta) => Color::Magenta,
        theme::Color::Light(theme::BaseColor::Cyan) => Color::Cyan,
        theme::Color::Light(theme::BaseColor::White) => Color::White,
        theme::Color::Rgb(r, g, b) => Color::Rgb { r, g, b },
        theme::Color::RgbLowRes(r, g, b) => {
            debug_assert!(r <= 5,
                              "Red color fragment (r = {}) is out of bound. Make sure r ≤ 5.",
                              r);
            debug_assert!(g <= 5,
                              "Green color fragment (g = {}) is out of bound. Make sure g ≤ 5.",
                              g);
            debug_assert!(b <= 5,
                              "Blue color fragment (b = {}) is out of bound. Make sure b ≤ 5.",
                              b);

            Color::AnsiValue(16 + 36 * r + 6 * g + b)
        }
        theme::Color::TerminalDefault => Color::Reset,
    }
}

impl Backend {
    /// Creates a new crossterm backend.
    pub fn init() -> Result<Box<dyn backend::Backend>, crossterm::ErrorKind>
    where
        Self: Sized,
    {
        enable_raw_mode()?;

        // TODO: Use the stdout we define down there
        execute!(
            io::stdout(),
            EnterAlternateScreen,
            EnableMouseCapture,
            Hide
        )?;

        #[cfg(unix)]
        let stdout =
            RefCell::new(BufWriter::new(std::fs::File::create("/dev/tty")?));
        #[cfg(windows)]
        let stdout = RefCell::new(BufWriter::new(io::stdout()));

        Ok(Box::new(Backend {
            current_style: Cell::new(theme::ColorPair::from_256colors(0, 0)),
            stdout,
        }))
    }

    fn apply_colors(&self, colors: theme::ColorPair) {
        queue!(
            self.stdout_mut(),
            SetForegroundColor(translate_color(colors.front)),
            SetBackgroundColor(translate_color(colors.back))
        )
        .unwrap();
    }

    fn stdout_mut(&self) -> RefMut<BufWriter<Stdout>> {
        self.stdout.borrow_mut()
    }

    fn set_attr(&self, attr: Attribute) {
        queue!(self.stdout_mut(), SetAttribute(attr)).unwrap();
    }

    fn map_key(&mut self, event: CEvent) -> Option<Event> {
        Some(match event {
            CEvent::Key(key_event) => translate_event(key_event),
            CEvent::Mouse(CMouseEvent {
                kind,
                column,
                row,
                modifiers: _,
            }) => {
                let position = (column, row).into();
                let event = match kind {
                    MouseEventKind::Down(button) => {
                        MouseEvent::Press(translate_button(button))
                    }
                    MouseEventKind::Up(button) => {
                        MouseEvent::Release(translate_button(button))
                    }
                    MouseEventKind::Drag(button) => {
                        MouseEvent::Hold(translate_button(button))
                    }
                    MouseEventKind::Moved => {
                        return None;
                    }
                    MouseEventKind::ScrollDown => MouseEvent::WheelDown,
                    MouseEventKind::ScrollUp => MouseEvent::WheelUp,
                };

                Event::Mouse {
                    event,
                    position,
                    offset: Vec2::zero(),
                }
            }
            CEvent::Resize(_, _) => Event::WindowResize,
        })
    }
}

impl Drop for Backend {
    fn drop(&mut self) {
        // We have to execute the show cursor command at the `stdout`.
        execute!(
            io::stdout(),
            LeaveAlternateScreen,
            DisableMouseCapture,
            Show
        )
        .expect("Can not disable mouse capture or show cursor.");

        disable_raw_mode().unwrap();
    }
}

impl backend::Backend for Backend {
    fn poll_event(&mut self) -> Option<Event> {
        match poll(Duration::from_millis(1)) {
            Ok(true) => match read() {
                Ok(event) => match self.map_key(event) {
                    Some(event) => Some(event),
                    None => return self.poll_event(),
                },
                Err(e) => panic!("{:?}", e),
            },
            _ => None,
        }
    }

    fn refresh(&mut self) {
        self.stdout_mut().flush().unwrap();
    }

    fn has_colors(&self) -> bool {
        // TODO: color support detection?
        true
    }

    fn screen_size(&self) -> Vec2 {
        let size = terminal::size().unwrap_or((1, 1));
        Vec2::from(size)
    }

    fn print_at(&self, pos: Vec2, text: &str) {
        queue!(
            self.stdout_mut(),
            MoveTo(pos.x as u16, pos.y as u16),
            Print(text)
        )
        .unwrap();
    }

    fn print_at_rep(&self, pos: Vec2, repetitions: usize, text: &str) {
        if repetitions > 0 {
            let mut out = self.stdout_mut();

            queue!(out, MoveTo(pos.x as u16, pos.y as u16)).unwrap();

            out.write_all(text.as_bytes()).unwrap();

            let mut dupes_left = repetitions - 1;
            while dupes_left > 0 {
                out.write_all(text.as_bytes()).unwrap();
                dupes_left -= 1;
            }
        }
    }

    fn clear(&self, color: theme::Color) {
        self.apply_colors(theme::ColorPair {
            front: color,
            back: color,
        });

        queue!(self.stdout_mut(), Clear(ClearType::All)).unwrap();
    }

    fn set_color(&self, color: theme::ColorPair) -> theme::ColorPair {
        let current_style = self.current_style.get();

        if current_style != color {
            self.apply_colors(color);
            self.current_style.set(color);
        }

        current_style
    }

    fn set_effect(&self, effect: theme::Effect) {
        match effect {
            theme::Effect::Simple => (),
            theme::Effect::Reverse => self.set_attr(Attribute::Reverse),
            theme::Effect::Dim => self.set_attr(Attribute::Dim),
            theme::Effect::Bold => self.set_attr(Attribute::Bold),
            theme::Effect::Blink => self.set_attr(Attribute::SlowBlink),
            theme::Effect::Italic => self.set_attr(Attribute::Italic),
            theme::Effect::Strikethrough => {
                self.set_attr(Attribute::CrossedOut)
            }
            theme::Effect::Underline => self.set_attr(Attribute::Underlined),
        }
    }

    fn unset_effect(&self, effect: theme::Effect) {
        match effect {
            theme::Effect::Simple => (),
            theme::Effect::Reverse => self.set_attr(Attribute::NoReverse),
            theme::Effect::Dim | theme::Effect::Bold => self.set_attr(Attribute::NormalIntensity),
            theme::Effect::Blink => self.set_attr(Attribute::NoBlink),
            theme::Effect::Italic => self.set_attr(Attribute::NoItalic),
            theme::Effect::Strikethrough => {
                self.set_attr(Attribute::NotCrossedOut)
            }
            theme::Effect::Underline => self.set_attr(Attribute::NoUnderline),
        }
    }

    fn name(&self) -> &str {
        "crossterm"
    }
}