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
//! Atari 8-bit BBSes: ATASCII, on the 40x24 screen of the Atari's text
//! mode, light text on blue.
//!
//! Bytes 0x00-0x7F are characters (in ATASCII order, which BadTerm's Atari
//! font uses), the same with the top bit set are shown in inverse, and the
//! Atari's screen editor codes (EOL, cursor keys, clear, delete and insert)
//! are carried out. ESC shows the next byte as a character instead.
use crate::ansi::TermEvent;
use crate::screen::{Color, Screen};
pub const COLS: usize = 40;
pub const ROWS: usize = 24;
/// The CTerm font ID of the Atari font (as SyncTERM numbers it).
pub const FONT: u8 = 36;
/// The Atari's default text colors.
pub const BACKGROUND: Color = Color::Rgb(0x00_52_A5);
pub const TEXT: Color = Color::Rgb(0xA8_D0_FF);
#[derive(Debug, Default)]
pub struct Atascii {
/// The last byte was ESC: show this one as it is.
literal: bool,
}
impl Atascii {
pub fn start(&mut self, s: &mut Screen) {
self.literal = false;
s.pen.fg = TEXT;
s.pen.bg = BACKGROUND;
s.look.fonts[0] = FONT;
s.base_font = FONT;
s.autowrap = true;
s.erase_display(2);
s.move_to(0, 0);
}
pub fn byte(&mut self, s: &mut Screen, b: u8, events: &mut Vec<TermEvent>) {
if std::mem::take(&mut self.literal) {
s.put(b);
return;
}
match b {
0x1B => self.literal = true,
0x9B => {
s.carriage_return();
s.line_feed();
}
// The cursor keys wrap around within the screen, as on an Atari.
0x1C => {
let (x, y) = s.cursor();
s.move_to(x, (y + s.rows() - 1) % s.rows());
}
0x1D => {
let (x, y) = s.cursor();
s.move_to(x, (y + 1) % s.rows());
}
0x1E => {
let (x, y) = s.cursor();
s.move_to((x + s.cols() - 1) % s.cols(), y);
}
0x1F => {
let (x, y) = s.cursor();
s.move_to((x + 1) % s.cols(), y);
}
0x7D => {
s.erase_display(2);
s.move_to(0, 0);
}
// Backspace: back one and blank it.
0x7E => {
if s.cursor().0 > 0 {
s.move_by(-1, 0);
s.erase_chars(1);
}
}
0x7F => s.tab(),
0x9C => s.delete_lines(1),
0x9D => s.insert_lines(1),
0xFD => events.push(TermEvent::Bell),
0xFE => s.delete_chars(1),
0xFF => s.insert_chars(1),
// Tab stops: not kept; tabs go to the next multiple of 8.
0x9E | 0x9F => {}
_ => s.put(b),
}
}
}
/// What a typed character sends to an Atari BBS: ATASCII is ASCII for the
/// letters, digits and punctuation.
pub fn encode_char(c: char) -> Option<u8> {
matches!(c, ' '..='z' | '|').then_some(c as u8)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn eol_inverse_and_editing() {
let mut s = Screen::new(COLS, ROWS, 0);
let mut a = Atascii::default();
a.start(&mut s);
let mut ev = Vec::new();
for &b in b"HI\xc1\x9bAB\x7e\x1b\x1c\x9b\x7dX\xfd" {
a.byte(&mut s, b, &mut ev);
}
// Cleared by 0x7D, then X.
assert_eq!(s.row(0)[0].ch, b'X');
assert_eq!(ev, vec![TermEvent::Bell]);
let mut s = Screen::new(COLS, ROWS, 0);
a.start(&mut s);
for &b in b"HI\xc1\x9bAB\x7e\x1b\x1c" {
a.byte(&mut s, b, &mut ev);
}
assert_eq!(&[s.row(0)[0].ch, s.row(0)[1].ch, s.row(0)[2].ch], b"HI\xc1");
// B rubbed out, then ESC shows 0x1C (an arrow) instead of moving.
assert_eq!(&[s.row(1)[0].ch, s.row(1)[1].ch], b"A\x1c");
assert_eq!(s.row(0)[0].bg, BACKGROUND);
}
}