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