ascii_cli/
table.rs

1use std::collections::HashMap;
2
3use colored::Colorize;
4use radix_fmt::radix;
5
6use crate::verification;
7
8pub fn show_table(base: u8) {
9    verification::check_base(base);
10    for row in 0..16 {
11        for col in 0..8 {
12            let char_num = row + col * 16;
13            let char_set = get_char(char_num, true);
14            let char_string = if char_set.1 {
15                char_set.0.italic().bold()
16            } else {
17                char_set.0.bold()
18            };
19            print!("{:>8}", format!("{:#}", radix(char_num, base)).blue());
20            print!(" {:<5}", char_string);
21        }
22        println!();
23    }
24}
25
26pub fn get_char(number: u32, specials_as_str: bool) -> (String, bool) {
27    if specials_as_str {
28        let specials = get_special_chars();
29        if let Some(special_char) = specials.get(&number) {
30            return (special_char.to_string(), true);
31        }
32    }
33
34    (char::from_u32(number).unwrap_or('E').to_string(), false)
35}
36
37pub fn get_special_chars() -> HashMap<u32, String> {
38    HashMap::from([
39        (0, "NUL".to_owned()),
40        (1, "SOH".to_owned()),
41        (2, "STX".to_owned()),
42        (3, "ETX".to_owned()),
43        (4, "EOT".to_owned()),
44        (5, "ENQ".to_owned()),
45        (6, "ACK".to_owned()),
46        (7, "BEL".to_owned()),
47        (8, "BS".to_owned()),
48        (9, "HT".to_owned()),
49        (10, "LF".to_owned()),
50        (11, "VT".to_owned()),
51        (12, "FF".to_owned()),
52        (13, "CR".to_owned()),
53        (14, "SO".to_owned()),
54        (15, "SI".to_owned()),
55        (16, "DLE".to_owned()),
56        (17, "DC1".to_owned()),
57        (18, "DC2".to_owned()),
58        (19, "DC3".to_owned()),
59        (20, "DC4".to_owned()),
60        (21, "NAK".to_owned()),
61        (22, "SYN".to_owned()),
62        (23, "ETB".to_owned()),
63        (24, "CAN".to_owned()),
64        (25, "EM".to_owned()),
65        (26, "SUB".to_owned()),
66        (27, "ESC".to_owned()),
67        (28, "FS".to_owned()),
68        (29, "GS".to_owned()),
69        (30, "RS".to_owned()),
70        (31, "US".to_owned()),
71        (127, "DEL".to_owned()),
72    ])
73}