Skip to content

Commit

Permalink
parse stdin (with a flag)
Browse files Browse the repository at this point in the history
  • Loading branch information
geremachek committed Sep 14, 2021
1 parent 846c60a commit 6513bd7
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 9 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "merlin"
version = "1.3.5"
version = "1.3.6"
authors = ["geremachek <[email protected]>"]
edition = "2018"

Expand Down
3 changes: 2 additions & 1 deletion src/error.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::fmt;
use crate::util::ERROR_PREFIX;

// our error structure

Expand Down Expand Up @@ -33,6 +34,6 @@ impl fmt::Display for MerlinError {
MerlinError::UnknownNomen => "unknown nomen",
};

write!(f, "merlin: {}", msg)
write!(f, "{} {}", ERROR_PREFIX, msg)
}
}
12 changes: 11 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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();
}
29 changes: 25 additions & 4 deletions src/plane/input.rs
Original file line number Diff line number Diff line change
@@ -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) {
Expand All @@ -14,16 +22,29 @@ 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

self.parse_line(strip_nl(&input));

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
Expand Down
4 changes: 3 additions & 1 deletion src/util.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
// print a nice error message on an error event

pub const ERROR_PREFIX: &str = "merlin:";

pub fn err_msg<T, E>(res: Result<T, E>, msg: &str) {
if let Err(_) = res {
eprintln!("merlin: {}", msg);
eprintln!("{} {}", ERROR_PREFIX, msg);
}
}

0 comments on commit 6513bd7

Please sign in to comment.