aboutsummaryrefslogtreecommitdiff
path: root/vendor/anstyle-wincon/examples/set-wincon.rs
diff options
context:
space:
mode:
authorValentin Popov <valentin@popov.link>2024-01-08 00:21:28 +0300
committerValentin Popov <valentin@popov.link>2024-01-08 00:21:28 +0300
commit1b6a04ca5504955c571d1c97504fb45ea0befee4 (patch)
tree7579f518b23313e8a9748a88ab6173d5e030b227 /vendor/anstyle-wincon/examples/set-wincon.rs
parent5ecd8cf2cba827454317368b68571df0d13d7842 (diff)
downloadfparkan-1b6a04ca5504955c571d1c97504fb45ea0befee4.tar.xz
fparkan-1b6a04ca5504955c571d1c97504fb45ea0befee4.zip
Initial vendor packages
Signed-off-by: Valentin Popov <valentin@popov.link>
Diffstat (limited to 'vendor/anstyle-wincon/examples/set-wincon.rs')
-rw-r--r--vendor/anstyle-wincon/examples/set-wincon.rs58
1 files changed, 58 insertions, 0 deletions
diff --git a/vendor/anstyle-wincon/examples/set-wincon.rs b/vendor/anstyle-wincon/examples/set-wincon.rs
new file mode 100644
index 0000000..6febb0a
--- /dev/null
+++ b/vendor/anstyle-wincon/examples/set-wincon.rs
@@ -0,0 +1,58 @@
+#![cfg_attr(not(windows), allow(dead_code))]
+
+#[cfg(not(windows))]
+fn main() {
+ panic!("unsupported");
+}
+
+#[cfg(windows)]
+fn main() -> Result<(), lexopt::Error> {
+ use anstyle_wincon::WinconStream as _;
+
+ let args = Args::parse()?;
+ let stdout = std::io::stdout();
+ let mut stdout = stdout.lock();
+
+ let fg = args.fg.and_then(|c| c.into_ansi());
+ let bg = args.bg.and_then(|c| c.into_ansi());
+
+ let _ = stdout.write_colored(fg, bg, "".as_bytes());
+
+ std::mem::forget(stdout);
+
+ Ok(())
+}
+
+#[derive(Default)]
+struct Args {
+ fg: Option<anstyle::Ansi256Color>,
+ bg: Option<anstyle::Ansi256Color>,
+}
+
+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("fg") => {
+ res.fg = Some(
+ args.value()?
+ .parse_with(|s| s.parse::<u8>().map(anstyle::Ansi256Color))?,
+ );
+ }
+ Long("bg") => {
+ res.fg = Some(
+ args.value()?
+ .parse_with(|s| s.parse::<u8>().map(anstyle::Ansi256Color))?,
+ );
+ }
+ _ => return Err(arg.unexpected()),
+ }
+ }
+ Ok(res)
+ }
+}