use anyhow::Result;
use clap::Parser as ClapParser;
use mathr::eval::Context;
#[derive(ClapParser)]
#[command(
name = "mathr",
version,
about = "Mathematical computation in your terminal — just give it an expression.",
long_about = None
)]
struct Cli {
input: Option<String>,
}
fn main() -> Result<()> {
let cli = Cli::parse();
let input = match cli.input {
Some(s) => s,
None => {
use std::io::IsTerminal;
if std::io::stdin().is_terminal() {
return mathr::repl::run().map_err(Into::into);
}
use std::io::Read;
let mut buf = String::new();
std::io::stdin().read_to_string(&mut buf)?;
buf.trim().to_string()
}
};
if input.is_empty() {
return mathr::repl::run().map_err(Into::into);
}
let ctx = Context::standard();
match mathr::repl::dispatch_str(&input, ctx) {
Ok(Some(s)) => { println!("{}", s); Ok(()) }
Ok(None) => Ok(()),
Err(e) => Err(anyhow::anyhow!("{}", e)),
}
}