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
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
//! # Console Traits
//!
//! Contains a trait which describes a console. A console is a rectangular monospaced text display, of a certain width and height. You can write Unicode text to it.
//!
//! Currently we assume UNIX LF sematics - that is a sole LF implies a new
//! line *and* carriage return (as distinct to Windows semantics where you
//! would need to send a CRLF pair).
//!
//! Implementors should handle the following Unicode characters specially:
//!
//! * 0x08 (BS)  - Backspaces one character (and erases it)
//! * 0x09 (TAB) - Move to next tab stop, or the end of the line if no tab stops left.
//! * 0x0A (LF)  - Line feed.
//! * 0x0D (CR)  - Carriage return.
//! * 0x7F (DEL) - Ignored.
#![cfg_attr(not(test), no_std)]

#[cfg(test)]
use std as core;

/// Identifies a horizontal row on the screen. Zero is at the top.
#[derive(Debug, PartialOrd, Ord, PartialEq, Eq, Copy, Clone)]
pub struct Row(pub u8);

/// Describes a vertical column on the screen. Zero is on the left.
#[derive(Debug, PartialOrd, Ord, PartialEq, Eq, Copy, Clone)]
pub struct Col(pub u8);

/// Describes a place on the screen. (0, 0) is the top left.
#[derive(Debug, Copy, Clone)]
pub struct Position {
    /// The horizontal row (zero at the top)
    pub row: Row,
    /// The vertical column (zero on the left)
    pub col: Col,
}

#[derive(Debug, Copy, Clone)]
pub enum EscapeCharMode {
    Waiting,
    Seen
}

/// How to handle Control Characters
#[derive(Debug, Copy, Clone)]
pub enum ControlCharMode {
    Interpret,
    Display,
}

#[derive(Debug, Copy, Clone)]
/// Special types of character we need to interpret
pub enum SpecialChar {
    Linefeed,
    CarriageReturn,
    Tab,
    Backspace,
    Delete,
    Escape
}

/// Abstraction for our console. We can move the cursor around and write text to it.
pub trait Console {
    type Error;

    /// Gets the last col on the screen.
    fn get_width(&self) -> Col;

    /// Gets the last row on the screen.
    fn get_height(&self) -> Row;

    /// Set the horizontal position for the next text output.
    fn set_col(&mut self, col: Col) -> Result<(), Self::Error>;

    /// Set the vertical position for the next text output.
    fn set_row(&mut self, row: Row) -> Result<(), Self::Error>;

    /// Set the horizontal and vertical position for the next text output.
    fn set_pos(&mut self, pos: Position) -> Result<(), Self::Error>;

    /// Set the horizontal and vertical position for the next text output.
    /// Don't bounds check the value, we've already done it.
    fn set_pos_unbounded(&mut self, pos: Position) {
        let _ = self.set_pos(pos);
    }

    /// Get the current screen position.
    fn get_pos(&self) -> Position;

    /// Set the control char mode
    fn set_control_char_mode(&mut self, mode: ControlCharMode);

    /// Get the current control char mode
    fn get_control_char_mode(&self) -> ControlCharMode;

    /// Set the escape char mode
    fn set_escape_char_mode(&mut self, mode: EscapeCharMode);

    /// Get the current escape char mode
    fn get_escape_char_mode(&self) -> EscapeCharMode;

    /// Called when the screen needs to scroll up one row.
    fn scroll_screen(&mut self) -> Result<(), Self::Error>;

    /// Write a single Unicode char to the screen at the given position
    /// without updating the current position.
    fn write_char_at(&mut self, ch: char, pos: Position) -> Result<(), Self::Error>;

    /// Called when they've used an escape character in the string.
    /// Currently you can only escape a single byte.
    /// The escape character is '\u{001B}'
    /// This function returns 'true' when the escape sequence is complete.
    fn handle_escape(&mut self, escaped_char: char) -> bool;

    /// Write a string to the screen at the given position. Updates the
    /// current position to the end of the string. Strings will wrap across
    /// the end of the screen and scroll the screen if they reach the bottom.
    fn write_string(&mut self, str: &str) -> Result<(), Self::Error> {
        for ch in str.chars() {
            self.write_char(ch)?;
        }
        Ok(())
    }

    /// Write a single Unicode char to the screen at the current position.
    fn write_char(&mut self, ch: char) -> Result<(), Self::Error> {
        match self.get_escape_char_mode() {
            EscapeCharMode::Seen => {
                if self.handle_escape(ch) {
                    self.set_escape_char_mode(EscapeCharMode::Waiting);
                }
            }
            EscapeCharMode::Waiting => {
                let mut pos = self.get_pos();
                match self.is_special(ch) {
                    // Go to start of next row
                    Some(SpecialChar::Linefeed) => {
                        pos.col = Col::origin();
                        if pos.row == self.get_height() {
                            self.set_pos_unbounded(pos);
                            self.scroll_screen()?;
                        } else {
                            pos.row.incr();
                            self.set_pos_unbounded(pos);
                        }
                    }
                    // Go to start of this row
                    Some(SpecialChar::CarriageReturn) => {
                        pos.col = Col::origin();
                        self.set_pos_unbounded(pos);
                    }
                    // Go to next tab stop
                    Some(SpecialChar::Tab) => {
                        let tabs = pos.col.0 / 9;
                        pos.col.0 = (tabs + 1) * 9;
                        pos.col.bound(self.get_width());
                        self.set_pos_unbounded(pos);
                    }
                    // Go back one space (but don't erase anything there)
                    Some(SpecialChar::Backspace) => {
                        if pos.col > Col::origin() {
                            pos.col.decr();
                            self.set_pos_unbounded(pos);
                        }
                    }
                    // Delete is ignored
                    Some(SpecialChar::Delete) => {}
                    // Escape the next char
                    Some(SpecialChar::Escape) => {
                        self.set_escape_char_mode(EscapeCharMode::Seen);
                    }
                    None => {
                        self.write_char_at(ch, pos)?;
                        self.move_cursor_right()?;
                    }
                }
            }
        }
        Ok(())
    }

    /// Write a string to the screen at the given position. Updates the
    /// current position to the end of the string. Strings will wrap across
    /// the end of the screen and scroll the screen if they reach the bottom.
    fn write_string_at(&mut self, str: &str, pos: Position) -> Result<(), Self::Error> {
        self.set_pos(pos)?;
        self.write_string(str)?;
        Ok(())
    }

    /// Move the current cursor right one position. Wraps at the end of the
    /// line. Returns Ok(true) if the screen needs to scroll, or Ok(false)
    /// if it does not.
    fn move_cursor_right(&mut self) -> Result<(), Self::Error> {
        let mut pos = self.get_pos();
        if pos.col < self.get_width() {
            // we'll still be on screen
            pos.col.incr();
            // no scroll needed
            self.set_pos_unbounded(pos);
        } else {
            // We're going off the right hand edge
            pos.col = Col::origin();
            if pos.row == self.get_height() {
                // We're at the bottom
                self.set_pos_unbounded(pos);
                self.scroll_screen()?;
            } else {
                // We're not at the bottom (yet)
                pos.row.incr();
                self.set_pos_unbounded(pos);
            }
        }
        Ok(())
    }

    /// Check if a char is special
    fn is_special(&self, ch: char) -> Option<SpecialChar> {
        match self.get_control_char_mode() {
            ControlCharMode::Interpret => match ch {
                '\n' => Some(SpecialChar::Linefeed),
                '\r' => Some(SpecialChar::CarriageReturn),
                '\t' => Some(SpecialChar::Tab),
                '\u{001b}' => Some(SpecialChar::Escape),
                '\u{007f}' => Some(SpecialChar::Delete),
                '\u{0008}' => Some(SpecialChar::Backspace),
                _ => None,
            },
            _ => None,
        }
    }
}

impl Position {
    /// Create a new position
    pub fn new(row: Row, col: Col) -> Position {
        Position { row, col }
    }

    /// Get the origin (0, 0)
    pub fn origin() -> Position {
        Position {
            row: Row::origin(),
            col: Col::origin(),
        }
    }
}

impl Col {
    /// Get the origin
    pub fn origin() -> Col {
        Col(0)
    }

    pub fn incr(&mut self) -> &mut Self {
        self.0 += 1;
        self
    }

    pub fn decr(&mut self) -> &mut Self {
        self.0 -= 1;
        self
    }

    pub fn bound(&mut self, other: Col) -> &mut Self {
        if self.0 > other.0 {
            self.0 = other.0;
        }
        self
    }
}

impl Row {
    /// Get the origin
    pub fn origin() -> Row {
        Row(0)
    }

    pub fn incr(&mut self) -> &mut Self {
        self.0 += 1;
        self
    }

    pub fn decr(&mut self) -> &mut Self {
        self.0 -= 1;
        self
    }

    pub fn bound(&mut self, other: Row) -> &mut Self {
        if self.0 > other.0 {
            self.0 = other.0;
        }
        self
    }
}

impl core::convert::From<u8> for Row {
    fn from(num: u8) -> Row {
        Row(num)
    }
}

impl core::convert::From<usize> for Row {
    fn from(num: usize) -> Row {
        Row(num as u8)
    }
}

impl core::convert::From<u8> for Col {
    fn from(num: u8) -> Col {
        Col(num)
    }
}

impl core::convert::From<usize> for Col {
    fn from(num: usize) -> Col {
        Col(num as u8)
    }
}

#[cfg(test)]
mod test {
    use super::*;
    use core::fmt::Write;

    const WIDTH: u8 = 80;
    const HEIGHT: u8 = 25;

    #[derive(Copy, Clone)]
    struct Line {
        chars: [char; WIDTH as usize],
    }

    struct TestConsole {
        pos: Position,
        lines: [Line; HEIGHT as usize],
        mode: ControlCharMode,
    }

    impl TestConsole {
        fn new() -> TestConsole {
            let line = Line {
                chars: [' '; WIDTH as usize],
            };
            TestConsole {
                lines: [line; HEIGHT as usize],
                pos: Position::origin(),
                mode: ControlCharMode::Interpret,
            }
        }
    }

    impl Console for TestConsole {
        type Error = ();

        /// Gets the last col on the screen.
        fn get_width(&self) -> Col {
            Col(WIDTH - 1)
        }

        /// Gets the last row on the screen.
        fn get_height(&self) -> Row {
            Row(HEIGHT - 1)
        }

        /// Set the horizontal position for the next text output.
        fn set_col(&mut self, col: Col) -> Result<(), Self::Error> {
            self.pos.col = col;
            Ok(())
        }

        /// Set the vertical position for the next text output.
        fn set_row(&mut self, row: Row) -> Result<(), Self::Error> {
            self.pos.row = row;
            Ok(())
        }

        /// Set the horizontal and vertical position for the next text output.
        fn set_pos(&mut self, pos: Position) -> Result<(), Self::Error> {
            self.pos = pos;
            Ok(())
        }

        /// Get the current screen position.
        fn get_pos(&self) -> Position {
            self.pos
        }

        /// Set the control char mode
        fn set_control_char_mode(&mut self, mode: ControlCharMode) {
            self.mode = mode;
        }

        /// Get the current control char mode
        fn get_control_char_mode(&self) -> ControlCharMode {
            self.mode
        }

        /// Called when the screen needs to scroll up one row.
        fn scroll_screen(&mut self) -> Result<(), Self::Error> {
            for row in 0..HEIGHT - 1 {
                self.lines[row as usize] = self.lines[(row as usize) + 1];
                self.lines[(HEIGHT as usize) - 1] = Line {
                    chars: [' '; WIDTH as usize],
                };
            }
            Ok(())
        }

        /// Write a single Unicode char to the screen at the given position
        /// without updating the current position.
        fn write_char_at(&mut self, ch: char, pos: Position) -> Result<(), Self::Error> {
            self.lines[pos.row.0 as usize].chars[pos.col.0 as usize] = ch;
            Ok(())
        }
    }

    impl core::fmt::Write for TestConsole {
        fn write_str(&mut self, s: &str) -> core::fmt::Result {
            self.write_string(s).unwrap();
            Ok(())
        }
    }

    #[test]
    fn test_write() {
        let mut c = TestConsole::new();
        c.write_str("Hello").unwrap();
        assert_eq!(
            &c.lines[0].chars[0..10],
            &"Hello     ".chars().collect::<Vec<char>>()[..]
        );
        assert_eq!(c.pos.row, Row::origin());
        assert_eq!(c.pos.col, Col(5));
    }

    #[test]
    fn test_lf() {
        let mut c = TestConsole::new();
        c.write_str("Hello\n").unwrap();
        assert_eq!(
            &c.lines[0].chars[0..10],
            &"Hello     ".chars().collect::<Vec<char>>()[..]
        );
        assert_eq!(c.pos.row, Row(1));
        assert_eq!(c.pos.col, Col(0));
    }

    #[test]
    fn test_cr() {
        let mut c = TestConsole::new();
        c.write_str("Hello\r123").unwrap();
        assert_eq!(
            &c.lines[0].chars[0..10],
            &"123lo     ".chars().collect::<Vec<char>>()[..]
        );
        assert_eq!(c.pos.row, Row(0));
        assert_eq!(c.pos.col, Col(3));
    }

    #[test]
    fn test_bs() {
        let mut c = TestConsole::new();
        c.write_str("Hello~\u{0008}!").unwrap();
        assert_eq!(
            &c.lines[0].chars[0..10],
            &"Hello!    ".chars().collect::<Vec<char>>()[..]
        );
        assert_eq!(c.pos.row, Row(0));
        assert_eq!(c.pos.col, Col(6));
    }

    #[test]
    fn test_tab() {
        let mut c = TestConsole::new();
        c.write_str("1\t2\tHello\t4").unwrap();
        assert_eq!(
            &c.lines[0].chars[0..28],
            &"1        2        Hello    4"
                .chars()
                .collect::<Vec<char>>()[..]
        );
        assert_eq!(c.pos.row, Row(0));
        assert_eq!(c.pos.col, Col(28));
    }

    #[test]
    fn test_wrap() {
        let mut c = TestConsole::new();
        for line in 0..HEIGHT {
            writeln!(c, "{}", line).unwrap();
        }
        // First line should have a 1 in it, not a 0
        assert_eq!(
            &c.lines[0].chars[0..4],
            &"1   ".chars().collect::<Vec<char>>()[..]
        );
        assert_eq!(c.pos.row, Row(HEIGHT - 1));
        assert_eq!(c.pos.col, Col(0));
    }

}

// End of file