Compare commits

...

4 Commits

Author SHA1 Message Date
Greg Shuflin
4f74c2f8cd Cargo fmt 2023-07-23 20:01:02 -07:00
Greg Shuflin
a1e8fb7aec Remove C code for foreground color 2023-07-23 20:00:52 -07:00
Greg Shuflin
b6acbc2484 Handle color fully in rust 2023-07-23 19:56:31 -07:00
Greg Shuflin
c5bdc65923 Start doing color handling in rust 2023-07-23 19:35:15 -07:00
10 changed files with 72 additions and 72 deletions

View File

@ -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}}

View File

@ -6,6 +6,11 @@ use lexopt::prelude::*;
pub(crate) struct Args {
pub(crate) version: bool,
pub(crate) clear_screen: bool,
///Pass the 'color' argument to the nmstermio module where it will set the
///foreground color of the unencrypted characters as they are
///revealed. Valid arguments are "white", "yellow", "magenta", "blue",
///"green", "red", and "cyan".
pub(crate) foreground: Option<OsString>,
pub(crate) autodecrypt: bool,
pub(crate) mask_blanks: bool,

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,5 +1,7 @@
mod args;
mod color;
use color::Color;
use libc::{c_int, c_void};
const VERSION: &str = "2.0.0";
@ -7,6 +9,7 @@ const VERSION: &str = "2.0.0";
extern "C" {
fn nmseffect_set_autodecrypt(_: c_int) -> c_void;
fn nmseffect_set_clearscr(_: c_int) -> c_void;
static mut foregroundColor: c_int;
}
#[no_mangle]
@ -14,12 +17,21 @@ 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(color) = args.foreground {
let color = Color::try_from(color).unwrap_or_default();
let n = color as c_int;
unsafe {
foregroundColor = n;
}
}
if args.clear_screen {
unsafe {
nmseffect_set_clearscr(1);
@ -31,5 +43,4 @@ pub extern "C" fn rust_main() {
nmseffect_set_autodecrypt(1);
}
}
println!("{:?}", args);
}

View File

@ -29,9 +29,6 @@ int main(int argc, char *argv[])
{
switch (o)
{
case 'f':
nmseffect_set_foregroundcolor(optarg);
break;
case 's':
nmseffect_set_maskblank(1);
break;

View File

@ -290,16 +290,6 @@ char nmseffect_exec(unsigned char *string, int string_len) {
return ret;
}
/*
* Pass the 'color' argument to the nmstermio module where it will set the
* foreground color of the unencrypted characters as they are
* revealed. Valid arguments are "white", "yellow", "magenta", "blue",
* "green", "red", and "cyan".
*/
void nmseffect_set_foregroundcolor(char *color) {
nmstermio_set_foregroundcolor(color);
}
/*
* Set the autoDecrypt flag according to the true/false value of the
* 'setting' argument. When set to true, nmseffect_exec() will not

View File

@ -10,7 +10,6 @@
// Function prototypes
char nmseffect_exec(unsigned char *, int string_len);
void nmseffect_set_foregroundcolor(char *);
void nmseffect_set_returnopts(char *);
void nmseffect_set_autodecrypt(int);
void nmseffect_set_maskblank(int);

View File

@ -50,7 +50,7 @@
// Terminal IO settings
static int clearScr = 0; // clearScr flag
static int foregroundColor = COLOR_BLUE; // Foreground color setting
int foregroundColor = COLOR_BLUE; // Foreground color setting
// Function prototypes
static void nmstermio_set_terminal(int);
@ -224,33 +224,6 @@ void nmstermio_set_clearscr(int s) {
clearScr = 0;
}
/*
* Set the desired foreground color of the unencrypted characters as they
* are revealed by nmstermio_print_reveal_string(). Valid arguments are
* "white", "yellow", "magenta", "blue", "green", "red", and "cyan".
*/
void nmstermio_set_foregroundcolor(char *c) {
if(strcmp("white", c) == 0)
foregroundColor = COLOR_WHITE;
else if(strcmp("yellow", c) == 0)
foregroundColor = COLOR_YELLOW;
else if(strcmp("black", c) == 0)
foregroundColor = COLOR_BLACK;
else if(strcmp("magenta", c) == 0)
foregroundColor = COLOR_MAGENTA;
else if(strcmp("blue", c) == 0)
foregroundColor = COLOR_BLUE;
else if(strcmp("green", c) == 0)
foregroundColor = COLOR_GREEN;
else if(strcmp("red", c) == 0)
foregroundColor = COLOR_RED;
else if(strcmp("cyan", c) == 0)
foregroundColor = COLOR_CYAN;
else
foregroundColor = COLOR_BLUE;
}
/*
* Get and returns the current row position of the cursor. This involves
* sending the appropriate ANSI escape code to the terminal and

View File

@ -24,7 +24,6 @@ void nmstermio_show_cursor(void);
void nmstermio_beep(void);
int nmstermio_get_clearscr(void);
void nmstermio_set_clearscr(int);
void nmstermio_set_foregroundcolor(char *);
int nmstermio_get_cursor_row(void);

View File

@ -158,33 +158,6 @@ void nmstermio_set_clearscr(int s) {
clearScr = 0;
}
/*
* Set the desired foreground color of the unencrypted characters as they
* are revealed by nmstermio_print_reveal_string(). Valid arguments are
* "white", "yellow", "magenta", "blue", "green", "red", and "cyan".
*/
void nmstermio_set_foregroundcolor(char *c) {
if(strcmp("white", c) == 0)
foregroundColor = COLOR_WHITE;
else if(strcmp("yellow", c) == 0)
foregroundColor = COLOR_YELLOW;
else if(strcmp("black", c) == 0)
foregroundColor = COLOR_BLACK;
else if(strcmp("magenta", c) == 0)
foregroundColor = COLOR_MAGENTA;
else if(strcmp("blue", c) == 0)
foregroundColor = COLOR_BLUE;
else if(strcmp("green", c) == 0)
foregroundColor = COLOR_GREEN;
else if(strcmp("red", c) == 0)
foregroundColor = COLOR_RED;
else if(strcmp("cyan", c) == 0)
foregroundColor = COLOR_CYAN;
else
foregroundColor = COLOR_BLUE;
}
/*
* Get and returns the current row position of the cursor.
*/