diff --git a/Cargo.lock b/Cargo.lock index 1a5b10f..1fafeb8 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -96,7 +96,7 @@ dependencies = [ [[package]] name = "merlin" -version = "1.3.4" +version = "1.3.6" dependencies = [ "clap", "ctrlc", diff --git a/Cargo.toml b/Cargo.toml index f8ef63d..a15e7f6 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "merlin" -version = "1.3.5" +version = "1.3.6" authors = ["geremachek "] edition = "2018" diff --git a/src/error.rs b/src/error.rs index 79f7867..8a7e1d6 100644 --- a/src/error.rs +++ b/src/error.rs @@ -1,4 +1,5 @@ use std::fmt; +use crate::util::ERROR_PREFIX; // our error structure @@ -33,6 +34,6 @@ impl fmt::Display for MerlinError { MerlinError::UnknownNomen => "unknown nomen", }; - write!(f, "merlin: {}", msg) + write!(f, "{} {}", ERROR_PREFIX, msg) } } diff --git a/src/main.rs b/src/main.rs index cf2f5fe..58a5439 100644 --- a/src/main.rs +++ b/src/main.rs @@ -21,6 +21,10 @@ fn main() { .short("i") .long("interrupt") .help("Don't Block interrupts (Ctrl-C)")) + .arg(Arg::with_name("stdin") + .short("s") + .long("stdin") + .help("Parse stdin as merlin notation")) .arg(Arg::with_name("NOTATION") .index(1) .multiple(true) @@ -41,7 +45,13 @@ fn main() { util::err_msg(ctrlc::set_handler(|| ()), "can't handle Ctrl-C events"); } - // start the shell + // check to see if we should parse stdin... + + if merlin_args.is_present("stdin") { + p.parse_stdin() + } + + // otherwise start the REPL p.repl(); } diff --git a/src/plane/input.rs b/src/plane/input.rs index 6417634..a304842 100644 --- a/src/plane/input.rs +++ b/src/plane/input.rs @@ -1,6 +1,14 @@ use crate::util; use super::{Plane, Vision}; -use std::io::{self, Write}; +use std::io::{self, Write, BufRead}; + +// flush stdout, handling errors + +macro_rules! flush_stdout { + () => { + util::err_msg(io::stdout().flush(), "unable to flush stdout") + } +} impl Plane { pub fn repl(&mut self) { @@ -14,8 +22,8 @@ impl Plane { Vision::Scribe => self.parse_line(";scribe-prompt"), }; - util::err_msg(io::stdout().flush(), "unable to flush stdout"); // flush stdout, handling any errors - util::err_msg(io::stdin().read_line(&mut input), "unable to readline"); // read a line of input (handle any errors) + flush_stdout!(); // flush stdout, handling any errors + util::err_msg(io::stdin().read_line(&mut input), "unable to read line"); // read a line of input (handle any errors) // parse our line, stripping newlines @@ -23,7 +31,20 @@ impl Plane { input.clear(); } - } + } + + pub fn parse_stdin(&mut self) { + for line in io::stdin().lock().lines() { + self.parse_line(strip_nl(&line + .expect(&format!("{} {}", util::ERROR_PREFIX, "can't read stdin")))); + + flush_stdout!(); + + if !self.running { + break; + } + } + } } // strip newlines diff --git a/src/util.rs b/src/util.rs index 8af857b..a3a232b 100644 --- a/src/util.rs +++ b/src/util.rs @@ -1,7 +1,9 @@ // print a nice error message on an error event +pub const ERROR_PREFIX: &str = "merlin:"; + pub fn err_msg(res: Result, msg: &str) { if let Err(_) = res { - eprintln!("merlin: {}", msg); + eprintln!("{} {}", ERROR_PREFIX, msg); } }