(rust) very basic command handler

This commit is contained in:
2025-04-29 10:46:08 +02:00
parent 115cc27152
commit d1297d6e83
2 changed files with 64 additions and 1 deletions
+1
View File
@@ -6,4 +6,5 @@ edition = "2021"
[dependencies]
clap = { version = "4.5.0", features = ["derive"] }
dirs = "5.0.1"
ctrlc = "3.4"
+63 -1
View File
@@ -1,8 +1,70 @@
#![allow(dead_code, unused)]
use std::io::{Error, Result};
use std::{
io::{stdin, stdout, Error, Result, Write},
sync::{
atomic::{AtomicBool, Ordering}, Arc
},
};
fn main() -> Result<()> {
let running = Arc::new(AtomicBool::new(true));
let signal_running = running.clone();
let mut input: String = String::new();
let mut length: usize;
let mut tokens: Vec<&str>;
ctrlc::set_handler(move || {
signal_running.swap(false, Ordering::SeqCst);
});
while running.load(Ordering::SeqCst) {
print!("> ");
_ = stdout().flush();
length = match stdin().read_line(&mut input) {
Ok(value) => value,
Err(error) => {
println!("\nError: {:#?}\n", error);
continue;
}
};
if 0 == length {
continue;
}
input = input.trim_matches('\n').to_owned();
tokens = input.splitn(2,[' ']).collect();
if tokens.len() != 2 {
continue;
}
let cmd = tokens.remove(0);
let data = tokens.remove(0);
if cmd == "raw" {
println!("{:02x?}", data.as_bytes());
} else if cmd == "str" {
println!("{}", data);
}
input.clear();
}
Ok(())
}
// NOTE: not required, but might be fun to implement at some point
// fn tokenise(input: &str, separator: Vec<&str>) -> Vec<String> {
// let separator = separator.join("");
// let mut tokens: Vec<String> = Vec::new();
// let mut quoted = false;
//
// for char in input.chars() {
// input.len
// if separator.contains(char) {
// }
// }
//
// tokens
// }