From b6acbc2484351e16e4984d744daf52b5fb032299 Mon Sep 17 00:00:00 2001 From: Greg Shuflin Date: Sun, 23 Jul 2023 19:52:25 -0700 Subject: [PATCH] Handle color fully in rust --- justfile | 13 +++++++++++++ src/color.rs | 40 ++++++++++++++++++++++++++++++++++++++++ src/lib.rs | 12 ++++++++---- 3 files changed, 61 insertions(+), 4 deletions(-) create mode 100644 src/color.rs diff --git a/justfile b/justfile index 88ce7cc..665be9e 100644 --- a/justfile +++ b/justfile @@ -2,6 +2,19 @@ default: just --list +# Build from scratch build: cargo build --release make nms + + +# Clean all project files +clean: + rm -rf obj + rm -rf bin + cargo clean + + +# A sample run of the program +sample_run *args: + cat Cargo.toml | ./bin/nms {{args}} diff --git a/src/color.rs b/src/color.rs new file mode 100644 index 0000000..76fce95 --- /dev/null +++ b/src/color.rs @@ -0,0 +1,40 @@ +use std::{convert::TryFrom, ffi::OsString}; + +pub(crate) enum Color { + Black = 0, + Red = 1, + Green = 2, + Yellow = 3, + Blue = 4, + Magenta = 5, + Cyan = 6, + White = 7, +} + +impl Default for Color { + fn default() -> Self { + Color::Blue + } +} + +impl TryFrom for Color { + type Error = (); + + fn try_from(s: OsString) -> Result { + let s = match s.to_str() { + Some(s) => s, + None => return Err(()), + }; + Ok(match s { + "black" => Color::Black, + "red" => Color::Red, + "green" => Color::Green, + "yellow" => Color::Yellow, + "blue" => Color::Blue, + "magenta" => Color::Magenta, + "cyan" => Color::Cyan, + "white" => Color::White, + _ => return Err(()), + }) + } +} diff --git a/src/lib.rs b/src/lib.rs index 3bc8beb..40a8ec1 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,8 +1,10 @@ mod args; +mod color; use libc::{c_int, c_void}; +use color::Color; -const VERSION: &str = "2.0.0"; +const VERSION: &str = "2.0.0"; extern "C" { fn nmseffect_set_autodecrypt(_: c_int) -> c_void; @@ -15,15 +17,18 @@ pub extern "C" fn rust_main() { println!("Hello from rust"); let args = args::parse_arguments().unwrap(); + println!("{:?}", args); if args.version { println!("nms version {VERSION}"); std::process::exit(0); } - if let Some(ref _color) = args.foreground { + if let Some(color) = args.foreground { + let color = Color::try_from(color).unwrap_or_default(); + let n = color as c_int; unsafe { - foregroundColor = 5; + foregroundColor = n; } } @@ -38,5 +43,4 @@ pub extern "C" fn rust_main() { nmseffect_set_autodecrypt(1); } } - println!("{:?}", args); }