minitel 0.3.2

Minitel interaction library
Documentation
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
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
use num_enum::{FromPrimitive, IntoPrimitive};
use smallvec::SmallVec;
use unicode_normalization::UnicodeNormalization;

use crate::MinitelMessage;

use super::protocol::ProtocolMessage;

/// Virtual keystroke sequence
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum UserInput {
    /// A single character, G0 or G2
    Char(char),
    /// A single control character
    C0(C0),
    /// ESC C1 control character
    C1(C1),
    /// One of the function keys
    FunctionKey(FunctionKey),
    /// Protocol command
    Protocol(ProtocolMessage),
}

pub struct StringMessage(pub String);

impl MinitelMessage for StringMessage {
    fn message(self) -> Vec<u8> {
        self.0
            .chars()
            .flat_map(SIChar::try_from)
            .flat_map(|c| c.message())
            .collect()
    }
}

pub struct SetPosition(pub u8, pub u8);
impl MinitelMessage for SetPosition {
    fn message(self) -> Vec<u8> {
        vec![C0::US.into(), 0x40 + self.1, 0x40 + self.0 + 1]
    }
}

/// Base control characters
/// <https://jbellue.github.io/stum1b/#2-2-1-2-4-2>
#[repr(u8)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, IntoPrimitive, FromPrimitive)]
pub enum C0 {
    NUL = 0x00,
    SOH = 0x01,
    EOL = 0x04,
    ENQ = 0x05,
    BEL = 0x07,
    /// Move cursor to the left
    BS = 0x08,
    /// Move cursor to the right
    HT = 0x09,
    /// Move the cursor down
    LF = 0x0A,
    /// Move the cursor up
    VT = 0x0B,
    /// Move the cursor at the first position of the first line and clear the screen
    /// Article separator
    FF = 0x0C,
    /// Move the cursor at the beginning of the line
    CR = 0x0D,
    SO = 0x0E,
    SI = 0x0F,
    DLE = 0x10,
    /// Show cursor
    Con = 0x11,
    /// Repetition
    Rep = 0x12,
    Sep = 0x13,
    /// Hide cursor
    Coff = 0x14,
    NACK = 0x15,
    SYN = 0x16,
    CAN = 0x18,
    /// G2
    SS2 = 0x19,
    SUB = 0x1A,
    /// Call C1 control function
    ESC = 0x1B,
    SS3 = 0x1D,
    /// Move the cursor at the first position of the first line
    /// Article separator
    RS = 0x1E,
    /// Sub article separator
    US = 0x1F,
    /// Unkown control character
    #[num_enum(catch_all)]
    Other(u8),
}

impl MinitelMessage for C0 {
    fn message(self) -> Vec<u8> {
        vec![self.into()]
    }
}

/// ESC control character
/// <https://jbellue.github.io/stum1b/#2-2-1-2-4-2>
#[repr(u8)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, IntoPrimitive, FromPrimitive)]
pub enum C1 {
    /// Protocol message with one parameter
    /// Not listed as C1, but used like one after ESC
    /// <https://jbellue.github.io/stum1b/#2-6-2>
    Pro1 = 0x39,
    /// Protocol message with two parameters
    /// Not listed as C1, but used like one after ESC
    /// <https://jbellue.github.io/stum1b/#2-6-2>
    Pro2 = 0x3A,
    /// Protocol message with three parameters
    /// Not listed as C1, but used like one after ESC
    /// <https://jbellue.github.io/stum1b/#2-6-2>
    Pro3 = 0x3B,
    /// 0%
    CharBlack = 0x40,
    /// 50%
    CharRed = 0x41,
    /// 70%
    CharGreen = 0x42,
    /// 90%
    CharYellow = 0x43,
    /// 40%
    CharBlue = 0x44,
    /// 60%
    CharMagenta = 0x45,
    /// 80%
    CharCyan = 0x46,
    /// 100%
    CharWhite = 0x47,
    Blink = 0x48,
    Fixed = 0x49,
    NormalSize = 0x4C,
    DoubleHeight = 0x4D,
    DoubleWidth = 0x4E,
    DoubleSize = 0x4F,
    /// 0%
    BgBlack = 0x50,
    /// 50%
    BgRed = 0x51,
    /// 70%
    BgGreen = 0x52,
    /// 90%
    BgYellow = 0x53,
    /// 40%
    BgBlue = 0x54,
    /// 60%
    BgMagenta = 0x55,
    /// 80%
    BgCyan = 0x56,
    /// 100%
    BgWhite = 0x57,
    Mask = 0x58,
    /// End underline, or disjoint semi-graphic
    EndUnderline = 0x59,
    /// Begin underline, or disjoint semi-graphic
    BeginUnderline = 0x5A,
    Csi = 0x5B,
    NormalBg = 0x5C,
    InvertBg = 0x5D,
    Unmask = 0x5F,

    /// Enquiry cursor position
    EnqCursor = 0x61,

    /// Unkown control character
    #[num_enum(catch_all)]
    Other(u8),
}

impl MinitelMessage for C1 {
    fn message(self) -> Vec<u8> {
        vec![C0::ESC.into(), self.into()]
    }
}

/// G0 characters (nearly ascii)
///
/// <https://jbellue.github.io/stum1b/#2-2-1-2-8>
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct G0(pub u8);

impl From<G0> for u8 {
    fn from(val: G0) -> Self {
        val.0
    }
}

impl MinitelMessage for G0 {
    fn message(self) -> Vec<u8> {
        vec![self.into()]
    }
}

#[rustfmt::skip]
const G0_TO_CHAR: [char; 95] = [
    ' ', '!', '"', '#', '$', '%', '&', '\'', '(', ')', '*', '+', ',', '-', '.', '/',
    '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', ':', ';', '<', '=', '>', '?',
    '@', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O',
    'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '[', '\\', ']', '', '_',
    '', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o',
    'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '', '|', '', ''
];

impl From<G0> for char {
    fn from(val: G0) -> Self {
        G0_TO_CHAR[val.0 as usize - 0x20]
    }
}

impl TryFrom<u8> for G0 {
    type Error = ();

    fn try_from(value: u8) -> Result<Self, Self::Error> {
        match value {
            0x20..=0x7E => Ok(G0(value)),
            _ => Err(()),
        }
    }
}

impl TryFrom<char> for G0 {
    type Error = ();

    fn try_from(value: char) -> Result<Self, Self::Error> {
        match value {
            // Ranges matching ascii
            '\u{0020}'..='\u{005D}' | '\u{005F}' | '\u{0061}'..='\u{007A}' | '\u{007C}' => {
                Ok(G0(value as u8))
            }
            // Drawing characters
            '' => Ok(G0(0x5F)),
            '' => Ok(G0(0x60)),
            '' => Ok(G0(0x7B)),
            '' => Ok(G0(0x7D)),
            '' => Ok(G0(0x7E)),
            _ => Err(()),
        }
    }
}

/// Semi-graphic sextant characters
///
/// <https://jbellue.github.io/stum1b/#2-2-1-2-8>
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct G1(pub u8);

impl From<G1> for u8 {
    fn from(g1: G1) -> u8 {
        g1.0
    }
}

impl MinitelMessage for G1 {
    fn message(self) -> Vec<u8> {
        vec![self.into()]
    }
}

impl G1 {
    // Sextant from the unicode Symbols for Legacy Computing (U+1FB0x...)
    // https://en.wikipedia.org/wiki/Symbols_for_Legacy_Computing
    // Some values are skipped (zero, full, vertical bars)...
    // To simplify, use braille as intermediate
    #[rustfmt::skip]
    const SEXTANT_TO_BRAILLE: [char; 60] = [
        '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '',
        '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '',
        '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '',
        '', '', '', '', '', '', '', '', '', '', '', ''
    ];

    pub fn new(val: u8) -> Self {
        G1(val)
    }

    /// Convert from the 3 rows of 2 bits into a G1 character
    /// [[1, 2],
    /// [3, 4],
    /// [5, 6]]
    pub fn from_bits(bits: [[bool; 2]; 3]) -> Self {
        let val: u8 = (bits[0][0] as u8)
            | ((bits[0][1] as u8) << 1)
            | ((bits[1][0] as u8) << 2)
            | ((bits[1][1] as u8) << 3)
            | ((bits[2][0] as u8) << 4)
            | ((true as u8) << 5)
            | ((bits[2][1] as u8) << 6);
        G1(val)
    }

    /// Render the approximate semi graphic character matching the unicode value
    pub fn approximate_char(c: char) -> Option<Self> {
        let c = match c {
            // sextants: use braille as intermediate
            '\u{1FB00}'..='\u{1FB3C}' => Self::SEXTANT_TO_BRAILLE[c as usize - 0x1FB00],
            _ => c,
        };
        match c {
            // braille
            '\u{2800}'..'\u{2900}' => {
                let val = c as u32 - 0x2800;
                let mut bits = [[false; 2]; 3];
                bits[0][0] = val & 0b00000001 != 0;
                bits[1][0] = val & 0b00000010 != 0;
                bits[2][0] = val & 0b00000100 != 0;
                bits[0][1] = val & 0b00001000 != 0;
                bits[1][1] = val & 0b00010000 != 0;
                bits[2][1] = val & 0b00100000 != 0;
                Some(G1::from_bits(bits))
            }
            ' ' => Some(G1(0x20)),
            // quadrants
            '' => Some(G1(0x21)),
            '' => Some(G1(0x22)),
            '' => Some(G1(0x30)),
            '' => Some(G1(0x60)),
            '' => Some(G1(0x23)),
            '' => Some(G1(0x70)),
            '' => Some(G1(0x35)),
            '' => Some(G1(0x6A)),
            '' => Some(G1(0x75)),
            '' => Some(G1(0x37)),
            '' => Some(G1(0x6B)),
            '' => Some(G1(0x7A)),
            '' => Some(G1(0x64)),
            '' => Some(G1(0x26)),
            '' => Some(G1(0x7F)),
            // horizontal bars
            '' => Some(G1(0x7F)),
            '' => Some(G1(0x7F)),
            '' => Some(G1(0x35)),
            '' => Some(G1(0x35)),
            '' => Some(G1(0x20)),
            '' => Some(G1(0x20)),
            // vertical bars
            '' => Some(G1(0x7F)),
            '' => Some(G1(0x7C)),
            '' => Some(G1(0x7C)),
            '' => Some(G1(0x70)),
            '' => Some(G1(0x70)),
            '' => Some(G1(0x20)),
            _ => None,
        }
    }
}

/// <https://jbellue.github.io/stum1b/#2-2-1-2-8>
#[repr(u8)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, IntoPrimitive, FromPrimitive)]
pub enum G2 {
    Pound = 0x23,
    Dollar = 0x24,
    Hash = 0x26,
    Section = 0x27,
    LeftArrow = 0x2C,
    UpArrow = 0x2D,
    RightArrow = 0x2E,
    DownArrow = 0x2F,
    Degree = 0x30,
    PlusMinus = 0x31,
    Division = 0x38,
    OneQuarter = 0x3C,
    OneHalf = 0x3D,
    ThreeQuarters = 0x3E,
    Grave = 0x41,
    Acute = 0x42,
    Circumflex = 0x43,
    Diaeresis = 0x48,
    Cedille = 0x4B,
    OeMaj = 0x6A,
    OeMin = 0x7A,
    Beta = 0x7B,
    #[num_enum(catch_all)]
    Unknown(u8),
}

impl MinitelMessage for G2 {
    fn message(self) -> Vec<u8> {
        vec![C0::SS2.into(), self.into()]
    }
}

impl G2 {
    pub fn char(self) -> char {
        match self {
            G2::Pound => '£',
            G2::Dollar => '$',
            G2::Hash => '#',
            G2::Section => '§',
            G2::LeftArrow => '',
            G2::UpArrow => '',
            G2::RightArrow => '',
            G2::DownArrow => '',
            G2::Degree => '°',
            G2::PlusMinus => '±',
            G2::Division => '÷',
            G2::OneQuarter => '¼',
            G2::OneHalf => '½',
            G2::ThreeQuarters => '¾',
            G2::Grave => '`',
            G2::Acute => '´',
            G2::Circumflex => '^',
            G2::Diaeresis => '¨',
            G2::Cedille => '¸',
            G2::OeMaj => 'Œ',
            G2::OeMin => 'œ',
            G2::Beta => 'β',
            G2::Unknown(_) => ' ',
        }
    }

    pub fn unicode_diacritic(self) -> Option<char> {
        match self {
            G2::Grave => Some('\u{0300}'),
            G2::Acute => Some('\u{0301}'),
            G2::Circumflex => Some('\u{0302}'),
            G2::Diaeresis => Some('\u{0308}'),
            G2::Cedille => Some('\u{0327}'),
            _ => None,
        }
    }

    pub fn try_from_diactric(c: char) -> Option<Self> {
        match c {
            '\u{0300}' => Some(G2::Grave),
            '\u{0301}' => Some(G2::Acute),
            '\u{0302}' => Some(G2::Circumflex),
            '\u{0308}' => Some(G2::Diaeresis),
            '\u{0327}' => Some(G2::Cedille),
            _ => None,
        }
    }
}

impl TryFrom<char> for G2 {
    type Error = ();

    fn try_from(value: char) -> Result<Self, Self::Error> {
        match value {
            '£' => Ok(G2::Pound),
            '$' => Ok(G2::Dollar),
            '#' => Ok(G2::Hash),
            '§' => Ok(G2::Section),
            '' => Ok(G2::LeftArrow),
            '' => Ok(G2::UpArrow),
            '' => Ok(G2::RightArrow),
            '' => Ok(G2::DownArrow),
            '°' => Ok(G2::Degree),
            '±' => Ok(G2::PlusMinus),
            '÷' => Ok(G2::Division),
            '¼' => Ok(G2::OneQuarter),
            '½' => Ok(G2::OneHalf),
            '¾' => Ok(G2::ThreeQuarters),
            //'`' => Ok(G2::Grave),
            //'´' => Ok(G2::Acute),
            //'^' => Ok(G2::Circumflex),
            //'¨' => Ok(G2::Diaeresis),
            //'¸' => Ok(G2::Cedille),
            'Œ' => Ok(G2::OeMaj),
            'œ' => Ok(G2::OeMin),
            'β' => Ok(G2::Beta),
            _ => Err(()),
        }
    }
}

/// Normal characters ("code")
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum SIChar {
    /// Basic character, nearly ascii
    G0(G0),
    /// Accentuated character
    G0Diacritic(G0, G2),
    /// Special character ($, £, ...)
    G2(G2),
}

impl MinitelMessage for SIChar {
    fn message(self) -> Vec<u8> {
        match self {
            SIChar::G0(g0) => g0.message(),
            SIChar::G0Diacritic(g0, g2) => [g2.message(), g0.message()].concat(),
            SIChar::G2(g2) => g2.message(),
        }
    }
}

impl TryFrom<char> for SIChar {
    type Error = ();

    fn try_from(value: char) -> Result<Self, Self::Error> {
        // Check for basic characters
        if let Ok(g0) = G0::try_from(value) {
            return Ok(SIChar::G0(g0));
        }

        // Check for special characters
        if let Ok(g2) = G2::try_from(value) {
            return Ok(SIChar::G2(g2));
        }

        // Diacritics
        let parts: SmallVec<[char; 2]> = value.nfd().take(2).collect();
        if let (Some(base), Some(diacritic)) = (parts.first(), parts.get(1)) {
            if let (Ok(g0), Some(diacritic)) =
                (G0::try_from(*base), G2::try_from_diactric(*diacritic))
            {
                return Ok(SIChar::G0Diacritic(g0, diacritic));
            }
        }
        Err(())
    }
}

/// Function keys, preceeded with C0::SEP
///
/// <https://jbellue.github.io/stum1b/#2-3-6>
#[repr(u8)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, IntoPrimitive, FromPrimitive)]
pub enum FunctionKey {
    Envoi = 0x41,
    Retour = 0x42,
    Repetition = 0x43,
    Guide = 0x44,
    Annulation = 0x45,
    Sommaire = 0x46,
    Correction = 0x47,
    Suite = 0x48,
    ConnexionFin = 0x49,
    #[num_enum(catch_all)]
    Unknown(u8),
}

impl MinitelMessage for FunctionKey {
    fn message(self) -> Vec<u8> {
        vec![C0::Sep.into(), self.into()]
    }
}

/// Convenience for black&white minitels
///
/// <https://jbellue.github.io/stum1b/#1-3-2-4-3>
pub enum GrayScale {
    Black,
    Gray40,
    Gray50,
    Gray60,
    Gray70,
    Gray80,
    Gray90,
    White,
}

impl GrayScale {
    pub fn char(&self) -> C1 {
        match self {
            GrayScale::Black => C1::CharBlack,
            GrayScale::Gray40 => C1::CharBlue,
            GrayScale::Gray50 => C1::CharRed,
            GrayScale::Gray60 => C1::CharMagenta,
            GrayScale::Gray70 => C1::CharGreen,
            GrayScale::Gray80 => C1::CharCyan,
            GrayScale::Gray90 => C1::CharYellow,
            GrayScale::White => C1::CharWhite,
        }
    }

    pub fn bg(&self) -> C1 {
        match self {
            GrayScale::Black => C1::BgBlack,
            GrayScale::Gray40 => C1::BgBlue,
            GrayScale::Gray50 => C1::BgRed,
            GrayScale::Gray60 => C1::BgMagenta,
            GrayScale::Gray70 => C1::BgGreen,
            GrayScale::Gray80 => C1::BgCyan,
            GrayScale::Gray90 => C1::BgYellow,
            GrayScale::White => C1::BgWhite,
        }
    }
}

/// Repeat the character
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Repeat(pub u8);

impl MinitelMessage for Repeat {
    fn message(self) -> Vec<u8> {
        vec![C0::Rep.into(), 0x40 + self.0]
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    #[test]
    pub fn semigraphic_from_bits() {
        assert_eq!(
            0x20,
            G1::from_bits([[false, false], [false, false], [false, false]]).0
        );
        assert_eq!(
            0x7F,
            G1::from_bits([[true, true], [true, true], [true, true]]).0
        );
        assert_eq!(
            0x2C,
            G1::from_bits([[false, false], [true, true], [false, false]]).0
        );
    }

    #[test]
    pub fn semigraphic_from_char() {
        assert_eq!(G1::approximate_char(''), Some(G1(0x23)));
        assert_eq!(G1::approximate_char(''), Some(G1(0x77)));
        assert_eq!(G1::approximate_char(''), Some(G1(0x77)));
        assert_eq!(G1::approximate_char(''), Some(G1(0x77)));
        assert_eq!(G1::approximate_char(''), Some(G1(0x77)));
        assert_eq!(G1::approximate_char(''), Some(G1(0x7F)));
        assert_eq!(G1::approximate_char('\u{1FB00}'), Some(G1(0x21)));
        assert_eq!(G1::approximate_char('\u{1FB28}'), Some(G1(0x6B)));
    }
}