1 use std::io::Write;
2 
main() -> Result<(), lexopt::Error>3 fn main() -> Result<(), lexopt::Error> {
4     let args = Args::parse()?;
5     let stdout = std::io::stdout();
6     let mut stdout = stdout.lock();
7 
8     for fixed in 0..16 {
9         let style = style(fixed, args.layer, args.effects);
10         let _ = print_number(&mut stdout, fixed, style);
11         if fixed == 7 || fixed == 15 {
12             let _ = writeln!(&mut stdout);
13         }
14     }
15 
16     for r in 0..6 {
17         let _ = writeln!(stdout);
18         for g in 0..6 {
19             for b in 0..6 {
20                 let fixed = r * 36 + g * 6 + b + 16;
21                 let style = style(fixed, args.layer, args.effects);
22                 let _ = print_number(&mut stdout, fixed, style);
23             }
24             let _ = writeln!(stdout);
25         }
26     }
27 
28     for c in 0..24 {
29         if 0 == c % 8 {
30             let _ = writeln!(stdout);
31         }
32         let fixed = 232 + c;
33         let style = style(fixed, args.layer, args.effects);
34         let _ = print_number(&mut stdout, fixed, style);
35     }
36 
37     let _ = writeln!(stdout);
38 
39     Ok(())
40 }
41 
style(fixed: u8, layer: Layer, effects: anstyle::Effects) -> anstyle::Style42 fn style(fixed: u8, layer: Layer, effects: anstyle::Effects) -> anstyle::Style {
43     let color = anstyle::Ansi256Color(fixed).into();
44     (match layer {
45         Layer::Fg => anstyle::Style::new().fg_color(Some(color)),
46         Layer::Bg => anstyle::Style::new().bg_color(Some(color)),
47         Layer::Underline => anstyle::Style::new().underline_color(Some(color)),
48     }) | effects
49 }
50 
print_number( stdout: &mut std::io::StdoutLock<'_>, fixed: u8, style: anstyle::Style, ) -> std::io::Result<()>51 fn print_number(
52     stdout: &mut std::io::StdoutLock<'_>,
53     fixed: u8,
54     style: anstyle::Style,
55 ) -> std::io::Result<()> {
56     write!(
57         stdout,
58         "{}{:>4}{}",
59         style.render(),
60         fixed,
61         anstyle::Reset.render()
62     )
63 }
64 
65 #[derive(Default)]
66 struct Args {
67     effects: anstyle::Effects,
68     layer: Layer,
69 }
70 
71 #[derive(Copy, Clone, Default)]
72 enum Layer {
73     #[default]
74     Fg,
75     Bg,
76     Underline,
77 }
78 
79 impl Args {
parse() -> Result<Self, lexopt::Error>80     fn parse() -> Result<Self, lexopt::Error> {
81         use lexopt::prelude::*;
82 
83         let mut res = Args::default();
84 
85         let mut args = lexopt::Parser::from_env();
86         while let Some(arg) = args.next()? {
87             match arg {
88                 Long("layer") => {
89                     res.layer = args.value()?.parse_with(|s| match s {
90                         "fg" => Ok(Layer::Fg),
91                         "bg" => Ok(Layer::Bg),
92                         "underline" => Ok(Layer::Underline),
93                         _ => Err("expected values fg, bg, underline"),
94                     })?;
95                 }
96                 Long("effect") => {
97                     const EFFECTS: [(&str, anstyle::Effects); 12] = [
98                         ("bold", anstyle::Effects::BOLD),
99                         ("dimmed", anstyle::Effects::DIMMED),
100                         ("italic", anstyle::Effects::ITALIC),
101                         ("underline", anstyle::Effects::UNDERLINE),
102                         ("double_underline", anstyle::Effects::DOUBLE_UNDERLINE),
103                         ("curly_underline", anstyle::Effects::CURLY_UNDERLINE),
104                         ("dotted_underline", anstyle::Effects::DOTTED_UNDERLINE),
105                         ("dashed_underline", anstyle::Effects::DASHED_UNDERLINE),
106                         ("blink", anstyle::Effects::BLINK),
107                         ("invert", anstyle::Effects::INVERT),
108                         ("hidden", anstyle::Effects::HIDDEN),
109                         ("strikethrough", anstyle::Effects::STRIKETHROUGH),
110                     ];
111                     let effect = args.value()?.parse_with(|s| {
112                         EFFECTS
113                             .into_iter()
114                             .find(|(name, _)| *name == s)
115                             .map(|(_, effect)| effect)
116                             .ok_or_else(|| {
117                                 format!(
118                                     "expected one of {}",
119                                     EFFECTS
120                                         .into_iter()
121                                         .map(|(n, _)| n)
122                                         .collect::<Vec<_>>()
123                                         .join(", ")
124                                 )
125                             })
126                     })?;
127                     res.effects = res.effects.insert(effect);
128                 }
129                 _ => return Err(arg.unexpected()),
130             }
131         }
132         Ok(res)
133     }
134 }
135