no-more-secrets-rust/src/lib.rs

117 lines
2.8 KiB
Rust
Raw Normal View History

2023-07-23 03:04:59 -07:00
mod args;
2023-07-25 01:44:10 -07:00
mod charset;
2023-07-23 19:52:25 -07:00
mod color;
2023-11-12 13:37:11 -08:00
mod sneakers;
2023-07-23 03:03:23 -07:00
2023-07-25 23:17:17 -07:00
use libc::{c_char, c_int, c_void};
2023-07-24 00:50:34 -07:00
use std::ffi::CString;
2023-07-23 20:05:26 -07:00
use std::process;
2023-07-23 03:14:38 -07:00
2023-07-24 00:50:34 -07:00
use color::Color;
2023-07-23 19:52:25 -07:00
const VERSION: &str = "2.0.0";
2023-07-23 03:19:40 -07:00
2023-07-23 03:14:49 -07:00
extern "C" {
2023-11-12 11:20:13 -08:00
fn nmstermio_set_clearscr(_: c_int);
2023-07-25 23:46:31 -07:00
fn nmseffect_exec(
input: *const c_char,
len: c_int,
autodecrypt_c: c_int,
maskblank_c: c_int,
2023-11-12 11:20:13 -08:00
clear_scr: c_int,
2023-07-25 23:46:31 -07:00
) -> c_char;
2023-07-23 19:35:15 -07:00
static mut foregroundColor: c_int;
2023-07-23 03:14:38 -07:00
}
2023-07-25 23:40:30 -07:00
///Sleep for the number of milliseconds indicated by argument
#[no_mangle]
pub extern "C" fn nmseffect_sleep(t: c_int) {
use std::time::Duration;
let dur = Duration::from_millis(t as u64);
std::thread::sleep(dur);
}
/// Return a random character from charTable[].
#[no_mangle]
pub extern "C" fn nmscharset_get_random() -> *const c_char {
let table = charset::CHAR_TABLE;
let idx: u16 = rand::random::<u16>();
let bytes = table[(idx as usize) % table.len()];
bytes.as_ptr() as *const c_char
}
2023-07-23 03:03:23 -07:00
#[no_mangle]
pub extern "C" fn rust_main() {
println!("Hello from rust");
2023-07-23 03:14:38 -07:00
2023-07-23 20:05:26 -07:00
let args = match args::parse_arguments() {
Ok(args) => args,
Err(e) => {
println!("{e}");
process::exit(1);
}
};
2023-07-23 19:52:25 -07:00
println!("{:?}", args);
2023-07-23 03:14:38 -07:00
2023-07-23 03:19:40 -07:00
if args.version {
println!("nms version {VERSION}");
2023-07-23 20:05:26 -07:00
process::exit(0);
2023-07-23 03:19:40 -07:00
}
2023-07-23 19:52:25 -07:00
if let Some(color) = args.foreground {
let color = Color::try_from(color).unwrap_or_default();
let n = color as c_int;
2023-07-23 19:35:15 -07:00
unsafe {
2023-07-23 19:52:25 -07:00
foregroundColor = n;
2023-07-23 19:35:15 -07:00
}
}
2023-07-23 03:19:40 -07:00
if args.clear_screen {
unsafe {
2023-11-16 21:13:15 -08:00
nmstermio_set_clearscr(1);
2023-07-23 03:19:40 -07:00
}
}
2023-07-24 00:50:34 -07:00
2023-08-12 02:33:52 -07:00
let input = get_input("Enter input: ");
if input.len() == 0 {
2023-07-24 00:50:34 -07:00
eprintln!("Input is empty"); //TODO use error_log()/error_print() abstraction
process::exit(1);
}
2023-08-12 02:33:52 -07:00
2023-11-12 11:20:13 -08:00
exec_effect(input, args.autodecrypt, args.mask_blanks, args.clear_screen);
2023-08-12 02:33:52 -07:00
}
2023-11-15 19:52:55 -08:00
pub(crate) fn exec_effect(input: String, autodecrypt: bool, maskblank: bool, clear_screen: bool) {
2023-08-12 02:33:52 -07:00
let maskblank_c = if maskblank { 1 } else { 0 };
let autodecrypt_c = if autodecrypt { 1 } else { 0};
let output_cstring = CString::new(input).unwrap();
2023-07-24 00:50:34 -07:00
let ptr = output_cstring.as_ptr();
let len = output_cstring.as_bytes().len();
2023-11-12 11:20:13 -08:00
let clear = if clear_screen { 1 } else { 0 };
let _r = unsafe { nmseffect_exec(ptr, len as i32, autodecrypt_c, maskblank_c, clear) };
2023-08-12 02:33:52 -07:00
2023-07-24 00:50:34 -07:00
}
fn get_input(prompt: &str) -> String {
use std::io::{Read, Write};
let mut stdin = std::io::stdin();
let mut stdout = std::io::stdout();
let mut buf = String::new();
if atty::is(atty::Stream::Stdin) {
print!("{prompt}");
stdout.flush().unwrap();
stdin.read_line(&mut buf).unwrap();
} else {
stdin.read_to_string(&mut buf).unwrap();
}
buf
2023-07-23 03:03:23 -07:00
}