Skip to content

Commit f04dc58

Browse files
committed
add clear, quit commands; change formatting of output
1 parent 7f8969a commit f04dc58

File tree

4 files changed

+26
-12
lines changed

4 files changed

+26
-12
lines changed

Cargo.lock

Lines changed: 8 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
[package]
22
name = "csc"
3-
version = "0.1.7"
3+
version = "0.1.8"
44
edition = "2021"
55
authors = ["Zahash <zahash.z@gmail.com>"]
66
description = "Command Line Scientific Calculator"
77
license = "MIT"
88
repository = "https://github.com/zahash/csc"
99

1010
[dependencies]
11+
anyhow = "1"
1112
regex = { version = "1" }
1213
lazy_static = { version = "1" }
1314
rustyline = { version = "12" }

src/main.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@ mod lex;
33
mod parse;
44
mod prompt;
55

6-
fn main() {
7-
prompt::run();
6+
fn main() -> anyhow::Result<()> {
7+
prompt::run()
88
}

src/prompt.rs

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,30 +10,34 @@ const LOGO: &'static str = r#"
1010
██████ ███████ ██████
1111
"#;
1212

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

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

26+
println!("To Quit, press CTRL-C or CTRL-D or type 'exit' or 'quit'");
27+
2628
let mut state = State::new();
27-
let mut rl = rustyline::DefaultEditor::new().unwrap();
29+
let mut editor = rustyline::DefaultEditor::new().unwrap();
2830

2931
loop {
30-
match rl.readline("> ") {
32+
match editor.readline("> ").as_deref() {
33+
Ok("clear") | Ok("cls") => editor.clear_screen()?,
34+
Ok("exit") | Ok("quit") => break,
3135
Ok(line) => {
3236
if !line.is_empty() {
33-
let _ = rl.add_history_entry(line.as_str());
34-
match eval(line.as_str(), &mut state) {
35-
Ok(res) => println!("{}", res),
36-
Err(e) => eprintln!("{:?}", e),
37+
let _ = editor.add_history_entry(line);
38+
match eval(line, &mut state) {
39+
Ok(res) => println!(">>> {}", res),
40+
Err(e) => eprintln!("!! {:?}", e),
3741
}
3842
}
3943
}
@@ -51,4 +55,6 @@ pub fn run() {
5155
}
5256
}
5357
}
58+
59+
Ok(())
5460
}

0 commit comments

Comments
 (0)