Skip to content

Commit

Permalink
add clear, quit commands; change formatting of output
Browse files Browse the repository at this point in the history
  • Loading branch information
zahash committed Mar 26, 2024
1 parent 7f8969a commit f04dc58
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 12 deletions.
9 changes: 8 additions & 1 deletion Cargo.lock

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

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
[package]
name = "csc"
version = "0.1.7"
version = "0.1.8"
edition = "2021"
authors = ["Zahash <[email protected]>"]
description = "Command Line Scientific Calculator"
license = "MIT"
repository = "https://github.com/zahash/csc"

[dependencies]
anyhow = "1"
regex = { version = "1" }
lazy_static = { version = "1" }
rustyline = { version = "12" }
Expand Down
4 changes: 2 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ mod lex;
mod parse;
mod prompt;

fn main() {
prompt::run();
fn main() -> anyhow::Result<()> {
prompt::run()
}
22 changes: 14 additions & 8 deletions src/prompt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,30 +10,34 @@ const LOGO: &'static str = r#"
██████ ███████ ██████
"#;

pub fn run() {
pub fn run() -> anyhow::Result<()> {
let expr = std::env::args().skip(1).collect::<Vec<String>>().join(" ");
if !expr.trim().is_empty() {
match eval(expr.as_str(), &mut State::new()) {
Ok(res) => println!("{}", res),
Err(e) => eprintln!("{:?}", e),
}
return;
return Ok(());
}

println!("{}", LOGO);
println!(env!("CARGO_PKG_VERSION"));

println!("To Quit, press CTRL-C or CTRL-D or type 'exit' or 'quit'");

let mut state = State::new();
let mut rl = rustyline::DefaultEditor::new().unwrap();
let mut editor = rustyline::DefaultEditor::new().unwrap();

loop {
match rl.readline("> ") {
match editor.readline("> ").as_deref() {
Ok("clear") | Ok("cls") => editor.clear_screen()?,
Ok("exit") | Ok("quit") => break,
Ok(line) => {
if !line.is_empty() {
let _ = rl.add_history_entry(line.as_str());
match eval(line.as_str(), &mut state) {
Ok(res) => println!("{}", res),
Err(e) => eprintln!("{:?}", e),
let _ = editor.add_history_entry(line);
match eval(line, &mut state) {
Ok(res) => println!(">>> {}", res),
Err(e) => eprintln!("!! {:?}", e),
}
}
}
Expand All @@ -51,4 +55,6 @@ pub fn run() {
}
}
}

Ok(())
}

0 comments on commit f04dc58

Please sign in to comment.