]>
git.proxmox.com Git - rustc.git/blob - 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"))?
;
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"[..]);
16 let _
= stdout
.write(None
, None
, &b
"\n"[..]);
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
);
23 let _
= stdout
.write(None
, None
, &b
"\n"[..]);
29 let _
= stdout
.write(None
, None
, &b
"\n"[..]);
32 let style
= style(fixed
, args
.layer
, args
.effects
);
33 let _
= print_number(&mut stdout
, fixed
, style
);
39 fn style(fixed
: u8, layer
: Layer
, effects
: anstyle
::Effects
) -> anstyle
::Style
{
40 let color
= anstyle
::Ansi256Color(fixed
).into();
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
)),
49 stdout
: &mut anstyle_wincon
::Console
<std
::io
::StdoutLock
<'_
>>,
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
,
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
,
65 .write(fg
, bg
, format
!("{:>4}", fixed
).as_bytes())
71 effects
: anstyle
::Effects
,
75 #[derive(Copy, Clone)]
82 impl Default
for Layer
{
83 fn default() -> Self {
89 fn parse() -> Result
<Self, lexopt
::Error
> {
90 use lexopt
::prelude
::*;
92 let mut res
= Args
::default();
94 let mut args
= lexopt
::Parser
::from_env();
95 while let Some(arg
) = args
.next()?
{
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"),
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
),
120 let effect
= args
.value()?
.parse_with(|s
| {
123 .find(|(name
, _
)| *name
== s
)
124 .map(|(_
, effect
)| effect
)
127 "expected one of {}",
136 res
.effects
= res
.effects
.insert(effect
);
138 _
=> return Err(arg
.unexpected()),