diff --git a/Cargo.toml b/Cargo.toml index c1fcc05..ee2f541 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,6 +6,7 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] +rustyline = "9.0.0" serde = "1.0.130" serialport = "4.0.1" [target.'cfg(windows)'.dependencies] diff --git a/src/main.rs b/src/main.rs index 06b1795..8a07cec 100644 --- a/src/main.rs +++ b/src/main.rs @@ -4,6 +4,9 @@ use std::io::{self, Write}; use std::time::Duration; use wmi::{COMLibrary, WMIConnection, WMIError}; +use rustyline::error::ReadlineError; +use rustyline::Editor; + #[cfg(windows)] #[allow(non_camel_case_types)] #[allow(non_snake_case)] @@ -112,37 +115,54 @@ fn main() { println!("{:?}", &builder); //3. Write to port - let msg = "ATI\r\n"; let mut port = builder.open().unwrap_or_else(|e| { eprintln!("Failed to open \"{}\". Error: {}", port_name, e); ::std::process::exit(1); }); - println!( - "Writing {} to {} at {} baud", - &msg, &port_name, &baud_rate - ); + //TODO:Add auto complete with AT command history + //TODO:Colors - match port.write(&msg.as_bytes()) { - Ok(_) => { - print!("{}", &msg); - std::io::stdout().flush().unwrap(); - } - Err(ref e) if e.kind() == io::ErrorKind::TimedOut => (), - Err(e) => eprintln!("{:?}", e), + // `()` can be used when no completer is required + let mut rl = Editor::<()>::new(); + if rl.load_history("history.txt").is_err() { + println!("No previous history."); } + loop { + let readline = rl.readline("❯❯ "); + match readline { + Ok(line) => { + let line = line + "\r"; + rl.add_history_entry(line.as_str()); - //4. Read data - let mut serial_buf: Vec = vec![0; 1000]; - println!("Receiving data on {} at {} baud:", &port_name, &baud_rate); - match port.read(serial_buf.as_mut_slice()) { - Ok(t) => io::stdout().write_all(&serial_buf[..t]).unwrap(), - Err(ref e) if e.kind() == io::ErrorKind::TimedOut => (), - Err(e) => eprintln!("{:?}", e), - } + //3. Write to port + match port.write(&line.as_bytes()) { + Ok(_) => {} + Err(ref e) if e.kind() == io::ErrorKind::TimedOut => (), + Err(e) => eprintln!("{:?}", e), + } - //TODO:Add a loop for data read/write - //TODO:Add promot like a shell - //TODO:Add auto complete with AT command history - //TODO:Colors + //4. Read data + let mut serial_buf: Vec = vec![0; 1000]; + match port.read(serial_buf.as_mut_slice()) { + Ok(t) => io::stdout().write_all(&serial_buf[..t]).unwrap(), + Err(ref e) if e.kind() == io::ErrorKind::TimedOut => (), + Err(e) => eprintln!("{:?}", e), + } + } + Err(ReadlineError::Interrupted) => { + println!("CTRL-C"); + break; + } + Err(ReadlineError::Eof) => { + println!("CTRL-D"); + break; + } + Err(err) => { + println!("Error: {:?}", err); + break; + } + } + } + rl.save_history("history.txt").unwrap(); }