Handle color fully in rust

This commit is contained in:
Greg Shuflin 2023-07-23 19:52:25 -07:00
parent c5bdc65923
commit b6acbc2484
3 changed files with 61 additions and 4 deletions

View File

@ -2,6 +2,19 @@ default:
just --list just --list
# Build from scratch
build: build:
cargo build --release cargo build --release
make nms 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}}

40
src/color.rs Normal file
View File

@ -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<OsString> for Color {
type Error = ();
fn try_from(s: OsString) -> Result<Self, ()> {
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(()),
})
}
}

View File

@ -1,8 +1,10 @@
mod args; mod args;
mod color;
use libc::{c_int, c_void}; use libc::{c_int, c_void};
use color::Color;
const VERSION: &str = "2.0.0"; const VERSION: &str = "2.0.0";
extern "C" { extern "C" {
fn nmseffect_set_autodecrypt(_: c_int) -> c_void; fn nmseffect_set_autodecrypt(_: c_int) -> c_void;
@ -15,15 +17,18 @@ pub extern "C" fn rust_main() {
println!("Hello from rust"); println!("Hello from rust");
let args = args::parse_arguments().unwrap(); let args = args::parse_arguments().unwrap();
println!("{:?}", args);
if args.version { if args.version {
println!("nms version {VERSION}"); println!("nms version {VERSION}");
std::process::exit(0); 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 { unsafe {
foregroundColor = 5; foregroundColor = n;
} }
} }
@ -38,5 +43,4 @@ pub extern "C" fn rust_main() {
nmseffect_set_autodecrypt(1); nmseffect_set_autodecrypt(1);
} }
} }
println!("{:?}", args);
} }