Handle reading input in Rust
This commit is contained in:
parent
4906e70f49
commit
1d4d5f0520
35
src/lib.rs
35
src/lib.rs
@ -1,14 +1,17 @@
|
||||
mod args;
|
||||
mod color;
|
||||
|
||||
use color::Color;
|
||||
use libc::{c_int, c_void};
|
||||
use libc::{c_char, c_int, c_uchar, c_void};
|
||||
use std::ffi::CString;
|
||||
use std::process;
|
||||
|
||||
use color::Color;
|
||||
|
||||
const VERSION: &str = "2.0.0";
|
||||
|
||||
extern "C" {
|
||||
fn nmseffect_set_clearscr(_: c_int) -> c_void;
|
||||
fn nmseffect_exec(input: *const c_char, len: c_int) -> c_char;
|
||||
static mut foregroundColor: c_int;
|
||||
static mut maskBlank: c_int;
|
||||
static mut autoDecrypt: c_int;
|
||||
@ -60,4 +63,32 @@ pub extern "C" fn rust_main() {
|
||||
autoDecrypt = 0;
|
||||
}
|
||||
}
|
||||
|
||||
let output = get_input("Enter input: ");
|
||||
if output.len() == 0 {
|
||||
eprintln!("Input is empty"); //TODO use error_log()/error_print() abstraction
|
||||
process::exit(1);
|
||||
}
|
||||
let output_cstring = CString::new(output).unwrap();
|
||||
let ptr = output_cstring.as_ptr();
|
||||
let len = output_cstring.as_bytes().len();
|
||||
let _r = unsafe { nmseffect_exec(ptr, len as i32) };
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
@ -20,6 +20,7 @@ extern void rust_main();
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
rust_main();
|
||||
/*
|
||||
unsigned char *input = NULL;
|
||||
|
||||
int r = input_get(&input, "Enter input: ");
|
||||
@ -39,6 +40,7 @@ int main(int argc, char *argv[])
|
||||
r = nmseffect_exec(input, r);
|
||||
|
||||
free(input);
|
||||
*/
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user