diff --git a/cli/src/main.rs b/cli/src/main.rs index 9905ae4..eec8cae 100644 --- a/cli/src/main.rs +++ b/cli/src/main.rs @@ -37,6 +37,10 @@ fn main() { .flag( Flag::new("max-recursion-depth", FlagType::Int) .description("The maximum allowed recursion depth. This is used to avoid crashes."), + ) + .flag( + Flag::new("no-leading-eq", FlagType::Bool) + .description("Don't include an equal sign at the start of results") ); app.run(args); @@ -83,7 +87,12 @@ fn default_action(context: &Context) { if context.args.is_empty() { // REPL - repl::start(&mut parser_context, precision, format); + repl::start( + &mut parser_context, + precision, + format, + context.bool_flag("no-leading-eq") + ); } else { // Direct output output::eval( @@ -92,6 +101,7 @@ fn default_action(context: &Context) { precision, 10u8, format, + context.bool_flag("no-leading-eq") ); } } diff --git a/cli/src/output.rs b/cli/src/output.rs index a2ef5c4..380c8bb 100644 --- a/cli/src/output.rs +++ b/cli/src/output.rs @@ -3,7 +3,14 @@ use kalk::{kalk_value::ScientificNotationFormat, parser}; pub(crate) const DEFAULT_PRECISION: u32 = 1024; -pub fn eval(parser: &mut parser::Context, input: &str, precision: u32, base: u8, format: ScientificNotationFormat) { +pub fn eval( + parser: &mut parser::Context, + input: &str, + precision: u32, + base: u8, + format: ScientificNotationFormat, + no_leading_equal: bool +) { match parser::eval(parser, input, precision) { Ok(Some(mut result)) => { if base != 10 && !result.set_radix(base) { @@ -13,7 +20,16 @@ pub fn eval(parser: &mut parser::Context, input: &str, precision: u32, base: u8, } if precision == DEFAULT_PRECISION { - println!("{}", result.to_string_pretty_format(format)); + let mut result_str = result.to_string_pretty_format(format); + if no_leading_equal { + result_str = result_str + .trim_start_matches('=') + .trim_start_matches('≈') + .trim_start() + .to_string(); + } + + println!("{}", result_str); return; } diff --git a/cli/src/repl.rs b/cli/src/repl.rs index 589d9ec..5d8aa77 100644 --- a/cli/src/repl.rs +++ b/cli/src/repl.rs @@ -27,7 +27,12 @@ struct Context { mode: ScientificNotationFormat, } -pub fn start(parser: &mut parser::Context, precision: u32, format: ScientificNotationFormat) { +pub fn start( + parser: &mut parser::Context, + precision: u32, + format: ScientificNotationFormat, + no_leading_equal: bool +) { let mut editor = Editor::::new(); editor.set_helper(Some(RLHelper { highlighter: LineHighlighter {}, @@ -73,7 +78,7 @@ pub fn start(parser: &mut parser::Context, precision: u32, format: ScientificNot match readline { Ok(input) => { editor.add_history_entry(input.as_str()); - eval_repl(&mut repl, parser, &input, precision); + eval_repl(&mut repl, parser, &input, precision, no_leading_equal); } Err(ReadlineError::Interrupted) => break, _ => break, @@ -85,7 +90,13 @@ pub fn start(parser: &mut parser::Context, precision: u32, format: ScientificNot } } -fn eval_repl(repl: &mut self::Context, parser: &mut parser::Context, input: &str, precision: u32) { +fn eval_repl( + repl: &mut self::Context, + parser: &mut parser::Context, + input: &str, + precision: u32, + no_leading_equal: bool +) { if let Some(file_name) = input.strip_prefix("load ") { if let Some(file_path) = crate::get_input_file_by_name(file_name) { crate::load_input_file(&file_path, precision, parser); @@ -133,7 +144,7 @@ fn eval_repl(repl: &mut self::Context, parser: &mut parser::Context, input: &str "clear" => print!("\x1B[2J"), "exit" => process::exit(0), "help" => print_cli_help(), - _ => output::eval(parser, input, precision, repl.base, repl.mode), + _ => output::eval(parser, input, precision, repl.base, repl.mode, no_leading_equal), } }