| 1 | //! Write ANSI escape code colored text |
| 2 | |
| 3 | use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Writerust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Write#; |
| 4 | |
| 5 | fn mainrust-analyzer cargo anstyle 1.0.14 main().() -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<(), lexoptrust-analyzer cargo lexopt 0.3.1 crate/::Errorrust-analyzer cargo lexopt 0.3.1 Error#> { |
| 6 | let args = Argsrust-analyzer cargo anstyle 1.0.14 Args#::parserust-analyzer cargo anstyle 1.0.14 impl#[Args]parse().()?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 7 | let stdout = stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::stdoutrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/stdio/stdout().(); |
| 8 | let mut stdout = stdout.lockrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/stdio/impl#[Stdout]lock().(); |
| 9 | |
| 10 | for fixed in 0..rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/Range#16 { |
| 11 | let color = anstylerust-analyzer cargo anstyle 1.0.14 crate/::Ansi256Colorrust-analyzer cargo anstyle 1.0.14 color/Ansi256Color#(fixed) |
| 12 | .into_ansirust-analyzer cargo anstyle 1.0.14 color/impl#[Ansi256Color]into_ansi().() |
| 13 | .expectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]expect().("4-bit range used"); |
| 14 | let style = stylerust-analyzer cargo anstyle 1.0.14 style().(color, args.layerrust-analyzer cargo anstyle 1.0.14 Args#layer., args.effectsrust-analyzer cargo anstyle 1.0.14 Args#effects.); |
| 15 | let _ = print_numberrust-analyzer cargo anstyle 1.0.14 print_number().(&mut stdout, fixed, style); |
| 16 | if fixed rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u8][`PartialEq<Self>`]eq().==rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u8][`PartialEq<Self>`]eq(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u8][`PartialEq<Self>`]eq().7 || fixed rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u8][`PartialEq<Self>`]eq().==rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u8][`PartialEq<Self>`]eq(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u8][`PartialEq<Self>`]eq().15 { |
| 17 | let _ = writelnrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/writeln!!(&mut stdout); |
| 18 | } |
| 19 | } |
| 20 | |
| 21 | for fixed in 16..rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/Range#232 { |
| 22 | let col = (fixed rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u8][`Sub<Self>`]sub().-rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u8][`Sub<Self>`]sub(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u8][`Sub<Self>`]sub().16) rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u8][`Rem<Self>`]rem().%rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u8][`Rem<Self>`]rem(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u8][`Rem<Self>`]rem().36; |
| 23 | if col rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u8][`PartialEq<Self>`]eq().==rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u8][`PartialEq<Self>`]eq(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u8][`PartialEq<Self>`]eq().0 { |
| 24 | let _ = writelnrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/writeln!!(stdout); |
| 25 | } |
| 26 | let color = anstylerust-analyzer cargo anstyle 1.0.14 crate/::Ansi256Colorrust-analyzer cargo anstyle 1.0.14 color/Ansi256Color#(fixed); |
| 27 | let style = stylerust-analyzer cargo anstyle 1.0.14 style().(color, args.layerrust-analyzer cargo anstyle 1.0.14 Args#layer., args.effectsrust-analyzer cargo anstyle 1.0.14 Args#effects.); |
| 28 | let _ = print_numberrust-analyzer cargo anstyle 1.0.14 print_number().(&mut stdout, fixed, style); |
| 29 | } |
| 30 | |
| 31 | let _ = writelnrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/writeln!!(stdout); |
| 32 | let _ = writelnrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/writeln!!(stdout); |
| 33 | for fixed in 232..=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeInclusive#255 { |
| 34 | let color = anstylerust-analyzer cargo anstyle 1.0.14 crate/::Ansi256Colorrust-analyzer cargo anstyle 1.0.14 color/Ansi256Color#(fixed); |
| 35 | let style = stylerust-analyzer cargo anstyle 1.0.14 style().(color, args.layerrust-analyzer cargo anstyle 1.0.14 Args#layer., args.effectsrust-analyzer cargo anstyle 1.0.14 Args#effects.); |
| 36 | let _ = print_numberrust-analyzer cargo anstyle 1.0.14 print_number().(&mut stdout, fixed, style); |
| 37 | } |
| 38 | |
| 39 | let _ = writelnrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/writeln!!(stdout); |
| 40 | |
| 41 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(()) |
| 42 | } |
| 43 | |
| 44 | fn stylerust-analyzer cargo anstyle 1.0.14 style().( |
| 45 | color: impl Intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/Into#<anstylerust-analyzer cargo anstyle 1.0.14 crate/::Colorrust-analyzer cargo anstyle 1.0.14 color/Color#>, |
| 46 | layer: Layerrust-analyzer cargo anstyle 1.0.14 Layer#, |
| 47 | effects: anstylerust-analyzer cargo anstyle 1.0.14 crate/::Effectsrust-analyzer cargo anstyle 1.0.14 effect/Effects#, |
| 48 | ) -> anstylerust-analyzer cargo anstyle 1.0.14 crate/::Stylerust-analyzer cargo anstyle 1.0.14 style/Style# { |
| 49 | let color = color.intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/Into#into().(); |
| 50 | (match layer { |
| 51 | Layerrust-analyzer cargo anstyle 1.0.14 Layer#::Fgrust-analyzer cargo anstyle 1.0.14 Layer#Fg# => anstylerust-analyzer cargo anstyle 1.0.14 crate/::Stylerust-analyzer cargo anstyle 1.0.14 style/Style#::newrust-analyzer cargo anstyle 1.0.14 style/impl#[Style]new().().fg_colorrust-analyzer cargo anstyle 1.0.14 style/impl#[Style]fg_color().(Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(color)), |
| 52 | Layerrust-analyzer cargo anstyle 1.0.14 Layer#::Bgrust-analyzer cargo anstyle 1.0.14 Layer#Bg# => anstylerust-analyzer cargo anstyle 1.0.14 crate/::Stylerust-analyzer cargo anstyle 1.0.14 style/Style#::newrust-analyzer cargo anstyle 1.0.14 style/impl#[Style]new().().bg_colorrust-analyzer cargo anstyle 1.0.14 style/impl#[Style]bg_color().(Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(color)), |
| 53 | Layerrust-analyzer cargo anstyle 1.0.14 Layer#::Underlinerust-analyzer cargo anstyle 1.0.14 Layer#Underline# => anstylerust-analyzer cargo anstyle 1.0.14 crate/::Stylerust-analyzer cargo anstyle 1.0.14 style/Style#::newrust-analyzer cargo anstyle 1.0.14 style/impl#[Style]new().().underline_colorrust-analyzer cargo anstyle 1.0.14 style/impl#[Style]underline_color().(Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(color)), |
| 54 | }) rust-analyzer cargo anstyle 1.0.14 style/impl#[Style][`BitOr<crate::Effects>`]bitor().|rust-analyzer cargo anstyle 1.0.14 style/impl#[Style][`BitOr<crate::Effects>`]bitor(). rust-analyzer cargo anstyle 1.0.14 style/impl#[Style][`BitOr<crate::Effects>`]bitor().effects |
| 55 | } |
| 56 | |
| 57 | fn print_numberrust-analyzer cargo anstyle 1.0.14 print_number().( |
| 58 | stdout: &mut stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::StdoutLockrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/stdio/StdoutLock#<'_>, |
| 59 | fixed: u8, |
| 60 | style: anstylerust-analyzer cargo anstyle 1.0.14 crate/::Stylerust-analyzer cargo anstyle 1.0.14 style/Style#, |
| 61 | ) -> stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Resultrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Result#<()> { |
| 62 | writerust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/write!!(stdout, "{style}{fixed:>3X}{style:#}",) |
| 63 | } |
| 64 | |
| 65 | #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Defaultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core default/Default:)] |
| 66 | struct Argsrust-analyzer cargo anstyle 1.0.14 Args# { |
| 67 | effectsrust-analyzer cargo anstyle 1.0.14 Args#effects.: anstylerust-analyzer cargo anstyle 1.0.14 crate/::Effectsrust-analyzer cargo anstyle 1.0.14 effect/Effects#, |
| 68 | layerrust-analyzer cargo anstyle 1.0.14 Args#layer.: Layerrust-analyzer cargo anstyle 1.0.14 Layer#, |
| 69 | } |
| 70 | |
| 71 | #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Copyrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Copy:, Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone:, Defaultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core default/Default:)] |
| 72 | enum Layerrust-analyzer cargo anstyle 1.0.14 Layer# { |
| 73 | #[defaultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core default/default:] |
| 74 | Fgrust-analyzer cargo anstyle 1.0.14 Layer#Fg#, |
| 75 | Bgrust-analyzer cargo anstyle 1.0.14 Layer#Bg#, |
| 76 | Underlinerust-analyzer cargo anstyle 1.0.14 Layer#Underline#, |
| 77 | } |
| 78 | |
| 79 | impl Argsrust-analyzer cargo anstyle 1.0.14 Args# { |
| 80 | fn parserust-analyzer cargo anstyle 1.0.14 impl#[Args]parse().() -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<Selfrust-analyzer cargo anstyle 1.0.14 impl#[Args], lexoptrust-analyzer cargo lexopt 0.3.1 crate/::Errorrust-analyzer cargo lexopt 0.3.1 Error#> { |
| 81 | use lexoptrust-analyzer cargo lexopt 0.3.1 crate/::preluderust-analyzer cargo lexopt 0.3.1 prelude/::*; |
| 82 | |
| 83 | let mut res = Argsrust-analyzer cargo anstyle 1.0.14 Args#::defaultrust-analyzer cargo anstyle 1.0.14 impl#[Args][Default]default().(); |
| 84 | |
| 85 | let mut args = lexoptrust-analyzer cargo lexopt 0.3.1 crate/::Parserrust-analyzer cargo lexopt 0.3.1 Parser#::from_envrust-analyzer cargo lexopt 0.3.1 impl#[Parser]from_env().(); |
| 86 | while let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(arg) = args.nextrust-analyzer cargo lexopt 0.3.1 impl#[Parser]next().()?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch(). { |
| 87 | match arg { |
| 88 | Longrust-analyzer cargo lexopt 0.3.1 Arg#Long#("layer") => { |
| 89 | res.layerrust-analyzer cargo anstyle 1.0.14 Args#layer. = args.valuerust-analyzer cargo lexopt 0.3.1 impl#[Parser]value().()?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch()..parse_withrust-analyzer cargo lexopt 0.3.1 impl#[OsString][ValueExt]parse_with().(|s| match s { |
| 90 | "fg" => Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(Layerrust-analyzer cargo anstyle 1.0.14 Layer#::Fgrust-analyzer cargo anstyle 1.0.14 Layer#Fg#), |
| 91 | "bg" => Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(Layerrust-analyzer cargo anstyle 1.0.14 Layer#::Bgrust-analyzer cargo anstyle 1.0.14 Layer#Bg#), |
| 92 | "underline" => Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(Layerrust-analyzer cargo anstyle 1.0.14 Layer#::Underlinerust-analyzer cargo anstyle 1.0.14 Layer#Underline#), |
| 93 | _ => Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#("expected values fg, bg, underline"), |
| 94 | })?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 95 | } |
| 96 | Longrust-analyzer cargo lexopt 0.3.1 Arg#Long#("effect") => { |
| 97 | const EFFECTSrust-analyzer cargo anstyle 1.0.14 EFFECTS.: [(&str, anstylerust-analyzer cargo anstyle 1.0.14 crate/::Effectsrust-analyzer cargo anstyle 1.0.14 effect/Effects#); 12] = [ |
| 98 | ("bold", anstylerust-analyzer cargo anstyle 1.0.14 crate/::Effectsrust-analyzer cargo anstyle 1.0.14 effect/Effects#::BOLDrust-analyzer cargo anstyle 1.0.14 effect/impl#[Effects]BOLD.), |
| 99 | ("dimmed", anstylerust-analyzer cargo anstyle 1.0.14 crate/::Effectsrust-analyzer cargo anstyle 1.0.14 effect/Effects#::DIMMEDrust-analyzer cargo anstyle 1.0.14 effect/impl#[Effects]DIMMED.), |
| 100 | ("italic", anstylerust-analyzer cargo anstyle 1.0.14 crate/::Effectsrust-analyzer cargo anstyle 1.0.14 effect/Effects#::ITALICrust-analyzer cargo anstyle 1.0.14 effect/impl#[Effects]ITALIC.), |
| 101 | ("underline", anstylerust-analyzer cargo anstyle 1.0.14 crate/::Effectsrust-analyzer cargo anstyle 1.0.14 effect/Effects#::UNDERLINErust-analyzer cargo anstyle 1.0.14 effect/impl#[Effects]UNDERLINE.), |
| 102 | ("double_underline", anstylerust-analyzer cargo anstyle 1.0.14 crate/::Effectsrust-analyzer cargo anstyle 1.0.14 effect/Effects#::DOUBLE_UNDERLINErust-analyzer cargo anstyle 1.0.14 effect/impl#[Effects]DOUBLE_UNDERLINE.), |
| 103 | ("curly_underline", anstylerust-analyzer cargo anstyle 1.0.14 crate/::Effectsrust-analyzer cargo anstyle 1.0.14 effect/Effects#::CURLY_UNDERLINErust-analyzer cargo anstyle 1.0.14 effect/impl#[Effects]CURLY_UNDERLINE.), |
| 104 | ("dotted_underline", anstylerust-analyzer cargo anstyle 1.0.14 crate/::Effectsrust-analyzer cargo anstyle 1.0.14 effect/Effects#::DOTTED_UNDERLINErust-analyzer cargo anstyle 1.0.14 effect/impl#[Effects]DOTTED_UNDERLINE.), |
| 105 | ("dashed_underline", anstylerust-analyzer cargo anstyle 1.0.14 crate/::Effectsrust-analyzer cargo anstyle 1.0.14 effect/Effects#::DASHED_UNDERLINErust-analyzer cargo anstyle 1.0.14 effect/impl#[Effects]DASHED_UNDERLINE.), |
| 106 | ("blink", anstylerust-analyzer cargo anstyle 1.0.14 crate/::Effectsrust-analyzer cargo anstyle 1.0.14 effect/Effects#::BLINKrust-analyzer cargo anstyle 1.0.14 effect/impl#[Effects]BLINK.), |
| 107 | ("invert", anstylerust-analyzer cargo anstyle 1.0.14 crate/::Effectsrust-analyzer cargo anstyle 1.0.14 effect/Effects#::INVERTrust-analyzer cargo anstyle 1.0.14 effect/impl#[Effects]INVERT.), |
| 108 | ("hidden", anstylerust-analyzer cargo anstyle 1.0.14 crate/::Effectsrust-analyzer cargo anstyle 1.0.14 effect/Effects#::HIDDENrust-analyzer cargo anstyle 1.0.14 effect/impl#[Effects]HIDDEN.), |
| 109 | ("strikethrough", anstylerust-analyzer cargo anstyle 1.0.14 crate/::Effectsrust-analyzer cargo anstyle 1.0.14 effect/Effects#::STRIKETHROUGHrust-analyzer cargo anstyle 1.0.14 effect/impl#[Effects]STRIKETHROUGH.), |
| 110 | ]; |
| 111 | let effect = args.valuerust-analyzer cargo lexopt 0.3.1 impl#[Parser]value().()?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch()..parse_withrust-analyzer cargo lexopt 0.3.1 impl#[OsString][ValueExt]parse_with().(|s| { |
| 112 | EFFECTSrust-analyzer cargo anstyle 1.0.14 EFFECTS. |
| 113 | .into_iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/iter/impl#[`[T; N]`][IntoIterator]into_iter().() |
| 114 | .findrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#find().(|(name, _)| *rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&T`][Deref]deref().name rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[`&A`][`PartialEq<&B>`]eq().==rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[`&A`][`PartialEq<&B>`]eq(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[`&A`][`PartialEq<&B>`]eq().s) |
| 115 | .maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]map().(|(_, effect)| effect) |
| 116 | .ok_or_elserust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]ok_or_else().(|| { |
| 117 | formatrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/format!!( |
| 118 | "expected one of {}", |
| 119 | EFFECTSrust-analyzer cargo anstyle 1.0.14 EFFECTS. |
| 120 | .into_iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/iter/impl#[`[T; N]`][IntoIterator]into_iter().() |
| 121 | .maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#map().(|(n, _)| n) |
| 122 | .collectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#collect().::<Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<_>>() |
| 123 | .joinrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc slice/impl#[`[T]`]join().(", ") |
| 124 | ) |
| 125 | }) |
| 126 | })?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 127 | res.effectsrust-analyzer cargo anstyle 1.0.14 Args#effects. = res.effectsrust-analyzer cargo anstyle 1.0.14 Args#effects..insertrust-analyzer cargo anstyle 1.0.14 effect/impl#[Effects]insert().(effect); |
| 128 | } |
| 129 | _ => return Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(arg.unexpectedrust-analyzer cargo lexopt 0.3.1 impl#[`Arg<'_>`]unexpected().()), |
| 130 | } |
| 131 | } |
| 132 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(res) |
| 133 | } |
| 134 | } |