Skip to content

Commit

Permalink
cli: Add --no-leading-eq flag, closes #150
Browse files Browse the repository at this point in the history
  • Loading branch information
PaddiM8 committed Apr 18, 2024
1 parent 504cf11 commit 1ff6813
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 7 deletions.
12 changes: 11 additions & 1 deletion cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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(
Expand All @@ -92,6 +101,7 @@ fn default_action(context: &Context) {
precision,
10u8,
format,
context.bool_flag("no-leading-eq")
);
}
}
Expand Down
20 changes: 18 additions & 2 deletions cli/src/output.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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;
}
Expand Down
19 changes: 15 additions & 4 deletions cli/src/repl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<RLHelper>::new();
editor.set_helper(Some(RLHelper {
highlighter: LineHighlighter {},
Expand Down Expand Up @@ -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,
Expand All @@ -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);
Expand Down Expand Up @@ -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),
}
}

Expand Down

0 comments on commit 1ff6813

Please sign in to comment.