-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #148 from epage/example
feat(anstream): Add debugging 'examples'
- Loading branch information
Showing
4 changed files
with
150 additions
and
0 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
use std::io::Write; | ||
|
||
fn main() -> Result<(), lexopt::Error> { | ||
let args = Args::parse()?; | ||
let stdout = anstream::stdout(); | ||
let mut stdout = stdout.lock(); | ||
|
||
for fixed in 0..16 { | ||
let style = style(fixed, args.layer, args.effects); | ||
let _ = print_number(&mut stdout, fixed, style); | ||
if fixed == 7 || fixed == 15 { | ||
let _ = writeln!(&mut stdout); | ||
} | ||
} | ||
|
||
for r in 0..6 { | ||
let _ = writeln!(stdout); | ||
for g in 0..6 { | ||
for b in 0..6 { | ||
let fixed = r * 36 + g * 6 + b + 16; | ||
let style = style(fixed, args.layer, args.effects); | ||
let _ = print_number(&mut stdout, fixed, style); | ||
} | ||
let _ = writeln!(stdout); | ||
} | ||
} | ||
|
||
for c in 0..24 { | ||
if 0 == c % 8 { | ||
let _ = writeln!(stdout); | ||
} | ||
let fixed = 232 + c; | ||
let style = style(fixed, args.layer, args.effects); | ||
let _ = print_number(&mut stdout, fixed, style); | ||
} | ||
|
||
Ok(()) | ||
} | ||
|
||
fn style(fixed: u8, layer: Layer, effects: anstyle::Effects) -> anstyle::Style { | ||
let color = anstyle::Ansi256Color(fixed).into(); | ||
(match layer { | ||
Layer::Fg => anstyle::Style::new().fg_color(Some(color)), | ||
Layer::Bg => anstyle::Style::new().bg_color(Some(color)), | ||
Layer::Underline => anstyle::Style::new().underline_color(Some(color)), | ||
}) | effects | ||
} | ||
|
||
fn print_number(stdout: &mut impl Write, fixed: u8, style: anstyle::Style) -> std::io::Result<()> { | ||
write!( | ||
stdout, | ||
"{}{:>4}{}", | ||
style.render(), | ||
fixed, | ||
anstyle::Reset.render() | ||
) | ||
} | ||
|
||
#[derive(Default)] | ||
struct Args { | ||
effects: anstyle::Effects, | ||
layer: Layer, | ||
} | ||
|
||
#[derive(Copy, Clone, Default)] | ||
enum Layer { | ||
#[default] | ||
Fg, | ||
Bg, | ||
Underline, | ||
} | ||
|
||
impl Args { | ||
fn parse() -> Result<Self, lexopt::Error> { | ||
use lexopt::prelude::*; | ||
|
||
let mut res = Args::default(); | ||
|
||
let mut args = lexopt::Parser::from_env(); | ||
while let Some(arg) = args.next()? { | ||
match arg { | ||
Long("layer") => { | ||
res.layer = args.value()?.parse_with(|s| match s { | ||
"fg" => Ok(Layer::Fg), | ||
"bg" => Ok(Layer::Bg), | ||
"underline" => Ok(Layer::Underline), | ||
_ => Err("expected values fg, bg, underline"), | ||
})?; | ||
} | ||
Long("effect") => { | ||
const EFFECTS: [(&str, anstyle::Effects); 12] = [ | ||
("bold", anstyle::Effects::BOLD), | ||
("dimmed", anstyle::Effects::DIMMED), | ||
("italic", anstyle::Effects::ITALIC), | ||
("underline", anstyle::Effects::UNDERLINE), | ||
("double_underline", anstyle::Effects::DOUBLE_UNDERLINE), | ||
("curly_underline", anstyle::Effects::CURLY_UNDERLINE), | ||
("dotted_underline", anstyle::Effects::DOTTED_UNDERLINE), | ||
("dashed_underline", anstyle::Effects::DASHED_UNDERLINE), | ||
("blink", anstyle::Effects::BLINK), | ||
("invert", anstyle::Effects::INVERT), | ||
("hidden", anstyle::Effects::HIDDEN), | ||
("strikethrough", anstyle::Effects::STRIKETHROUGH), | ||
]; | ||
let effect = args.value()?.parse_with(|s| { | ||
EFFECTS | ||
.into_iter() | ||
.find(|(name, _)| *name == s) | ||
.map(|(_, effect)| effect) | ||
.ok_or_else(|| { | ||
format!( | ||
"expected one of {}", | ||
EFFECTS | ||
.into_iter() | ||
.map(|(n, _)| n) | ||
.collect::<Vec<_>>() | ||
.join(", ") | ||
) | ||
}) | ||
})?; | ||
res.effects = res.effects.insert(effect); | ||
} | ||
_ => return Err(arg.unexpected()), | ||
} | ||
} | ||
Ok(res) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
fn main() { | ||
println!("stdout:"); | ||
println!( | ||
" choice: {:?}", | ||
anstream::AutoStream::choice(&std::io::stdout()) | ||
); | ||
println!( | ||
" choice: {:?}", | ||
anstream::AutoStream::auto(std::io::stdout()).current_choice() | ||
); | ||
println!("stderr:"); | ||
println!( | ||
" choice: {:?}", | ||
anstream::AutoStream::choice(&std::io::stderr()) | ||
); | ||
println!( | ||
" choice: {:?}", | ||
anstream::AutoStream::auto(std::io::stderr()).current_choice() | ||
); | ||
} |