2016-02-28 11:26:16 +00:00
|
|
|
use std::collections::BTreeMap;
|
|
|
|
use std::fmt::{Display, Formatter, Error};
|
|
|
|
use std::io::Write;
|
|
|
|
use std::io::stderr;
|
|
|
|
use std::io::stdin;
|
|
|
|
use std::process::exit;
|
|
|
|
use std::result::Result as RResult;
|
|
|
|
|
|
|
|
use libimagcounter::counter::Counter;
|
|
|
|
use libimagcounter::error::CounterError;
|
|
|
|
use libimagrt::runtime::Runtime;
|
|
|
|
use libimagutil::key_value_split::IntoKeyValue;
|
2016-05-16 16:59:02 +00:00
|
|
|
use libimagerror::trace::trace_error;
|
2016-02-28 11:26:16 +00:00
|
|
|
|
|
|
|
type Result<T> = RResult<T, CounterError>;
|
|
|
|
|
|
|
|
pub fn interactive(rt: &Runtime) {
|
|
|
|
let scmd = rt.cli().subcommand_matches("interactive");
|
|
|
|
if scmd.is_none() {
|
2016-04-16 20:39:40 +00:00
|
|
|
debug!("No subcommand");
|
2016-02-28 11:26:16 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
let scmd = scmd.unwrap();
|
|
|
|
debug!("Found 'interactive' command");
|
|
|
|
|
|
|
|
let mut pairs : BTreeMap<char, Binding> = BTreeMap::new();
|
|
|
|
|
|
|
|
for spec in scmd.values_of("spec").unwrap() {
|
|
|
|
match compute_pair(rt, &spec) {
|
|
|
|
Ok((k, v)) => { pairs.insert(k, v); },
|
|
|
|
Err(e) => { trace_error(&e); },
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !has_quit_binding(&pairs) {
|
|
|
|
pairs.insert('q', Binding::Function(String::from("quit"), Box::new(quit)));
|
|
|
|
}
|
|
|
|
|
2016-04-16 20:39:40 +00:00
|
|
|
stderr().flush().ok();
|
2016-02-28 11:26:16 +00:00
|
|
|
loop {
|
|
|
|
println!("---");
|
|
|
|
for (k, v) in &pairs {
|
|
|
|
println!("\t[{}] => {}", k, v);
|
|
|
|
}
|
|
|
|
println!("---");
|
|
|
|
print!("counter > ");
|
|
|
|
|
|
|
|
let mut input = String::new();
|
|
|
|
if let Err(e) = stdin().read_line(&mut input) {
|
|
|
|
trace_error(&e);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2016-05-02 22:57:41 +00:00
|
|
|
let cont = if !input.is_empty() {
|
2016-02-28 11:26:16 +00:00
|
|
|
let increment = match input.chars().next() { Some('-') => false, _ => true };
|
|
|
|
input.chars().all(|chr| {
|
|
|
|
match pairs.get_mut(&chr) {
|
|
|
|
Some(&mut Binding::Counter(ref mut ctr)) => {
|
|
|
|
if increment {
|
|
|
|
debug!("Incrementing");
|
2016-04-16 20:39:40 +00:00
|
|
|
if let Err(e) = ctr.inc() {
|
|
|
|
trace_error(&e);
|
|
|
|
}
|
2016-02-28 11:26:16 +00:00
|
|
|
} else {
|
|
|
|
debug!("Decrementing");
|
2016-04-16 20:39:40 +00:00
|
|
|
if let Err(e) = ctr.dec() {
|
|
|
|
trace_error(&e);
|
|
|
|
}
|
2016-02-28 11:26:16 +00:00
|
|
|
}
|
|
|
|
true
|
|
|
|
},
|
|
|
|
Some(&mut Binding::Function(ref name, ref f)) => {
|
|
|
|
debug!("Calling {}", name);
|
|
|
|
f()
|
|
|
|
},
|
|
|
|
None => true,
|
|
|
|
}
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
println!("No input...");
|
|
|
|
println!("\tUse a single character to increment the counter which is bound to it");
|
|
|
|
println!("\tUse 'q' (or the character bound to quit()) to exit");
|
|
|
|
println!("\tPrefix the line with '-' to decrement instead of increment the counters");
|
|
|
|
println!("");
|
|
|
|
true
|
|
|
|
};
|
|
|
|
|
|
|
|
if !cont {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn has_quit_binding(pairs: &BTreeMap<char, Binding>) -> bool {
|
|
|
|
pairs.iter()
|
|
|
|
.any(|(_, bind)| {
|
2016-05-02 22:57:41 +00:00
|
|
|
match *bind {
|
|
|
|
Binding::Function(ref name, _) => name == "quit",
|
2016-02-28 11:26:16 +00:00
|
|
|
_ => false,
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
enum Binding<'a> {
|
|
|
|
Counter(Counter<'a>),
|
|
|
|
Function(String, Box<Fn() -> bool>),
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> Display for Binding<'a> {
|
|
|
|
|
|
|
|
fn fmt(&self, fmt: &mut Formatter) -> RResult<(), Error> {
|
2016-05-02 22:57:41 +00:00
|
|
|
match *self {
|
|
|
|
Binding::Counter(ref c) => {
|
2016-02-28 11:26:16 +00:00
|
|
|
match c.name() {
|
|
|
|
Ok(name) => {
|
|
|
|
try!(write!(fmt, "{}", name));
|
|
|
|
Ok(())
|
|
|
|
},
|
|
|
|
Err(e) => {
|
|
|
|
trace_error(&e);
|
|
|
|
Ok(()) // TODO: Find a better way to escalate here.
|
|
|
|
},
|
|
|
|
}
|
|
|
|
},
|
2016-05-02 22:57:41 +00:00
|
|
|
Binding::Function(ref name, _) => write!(fmt, "{}()", name),
|
2016-02-28 11:26:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
fn compute_pair<'a>(rt: &'a Runtime, spec: &str) -> Result<(char, Binding<'a>)> {
|
|
|
|
let kv = String::from(spec).into_kv();
|
|
|
|
if kv.is_none() {
|
2016-04-16 20:39:40 +00:00
|
|
|
debug!("Key-Value parsing failed!");
|
2016-02-28 11:26:16 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
let kv = kv.unwrap();
|
|
|
|
|
|
|
|
let (k, v) = kv.into();
|
|
|
|
if !k.len() == 1 {
|
|
|
|
// We have a key which is not only a single character!
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
if v == "quit" {
|
|
|
|
// TODO uncaught unwrap()
|
|
|
|
Ok((k.chars().next().unwrap(), Binding::Function(String::from("quit"), Box::new(quit))))
|
|
|
|
} else {
|
|
|
|
// TODO uncaught unwrap()
|
|
|
|
Counter::load(v, rt.store()).and_then(|ctr| Ok((k.chars().next().unwrap(), Binding::Counter(ctr))))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn quit() -> bool {
|
|
|
|
false
|
|
|
|
}
|
|
|
|
|