brewery
notashelf /
f29038f592f186426ab2a5529cf07c90ce20fb3f

beer

public

A terminal worth pouring time into

Star0tarball
clone
ssh://git@git.notashelf.dev:33/notashelf/beer.git
crates/beer-protocols/src/charset.rsRust61 lines1.5 KB
1
//! G0/G1 character-set designation and the DEC special graphics (line-drawing)
2
//! translation.
3
4
/// A designated character set (`ESC ( c` / `ESC ) c`).
5
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
6
pub enum Charset {
7
    Ascii,
8
    DecSpecial,
9
}
10
11
/// Map a designation byte to a [`Charset`]. `0` selects DEC special graphics;
12
/// everything else falls back to ASCII.
13
pub fn charset(byte: u8) -> Charset {
14
    match byte {
15
        b'0' => Charset::DecSpecial,
16
        _ => Charset::Ascii,
17
    }
18
}
19
20
/// Translate a byte under the DEC special graphics set (VT100 line drawing).
21
pub fn dec_special(c: char) -> char {
22
    match c {
23
        '`' => '◆',
24
        'a' => '▒',
25
        'f' => '°',
26
        'g' => '±',
27
        'j' => '┘',
28
        'k' => '┐',
29
        'l' => '┌',
30
        'm' => '└',
31
        'n' => '┼',
32
        'o' => '⎺',
33
        'p' => '⎻',
34
        'q' => '─',
35
        'r' => '⎼',
36
        's' => '⎽',
37
        't' => '├',
38
        'u' => '┤',
39
        'v' => '┴',
40
        'w' => '┬',
41
        'x' => '│',
42
        'y' => '≤',
43
        'z' => '≥',
44
        '~' => '·',
45
        _ => c,
46
    }
47
}
48
49
#[cfg(test)]
50
mod tests {
51
    use super::*;
52
53
    #[test]
54
    fn line_drawing_translates() {
55
        assert_eq!(charset(b'0'), Charset::DecSpecial);
56
        assert_eq!(charset(b'B'), Charset::Ascii);
57
        assert_eq!(dec_special('q'), '─');
58
        assert_eq!(dec_special('x'), '│');
59
        assert_eq!(dec_special('A'), 'A'); // unmapped passes through
60
    }
61
}