mirror of
https://github.com/PaddiM8/kalker.git
synced 2024-11-07 08:24:33 +01:00
cli: Add --no-leading-eq flag, closes #150
This commit is contained in:
parent
504cf11106
commit
1ff6813951
@ -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")
|
||||
);
|
||||
}
|
||||
}
|
||||
|
@ -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;
|
||||
}
|
||||
|
@ -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 {},
|
||||
@ -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),
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user