Debian Codegraph @

debian/missing-sources/anstream/examples/dump-stream.rs

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