mirror of
https://github.com/PaddiM8/kalker.git
synced 2025-03-04 08:11:11 +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(
|
||||||
Flag::new("max-recursion-depth", FlagType::Int)
|
Flag::new("max-recursion-depth", FlagType::Int)
|
||||||
.description("The maximum allowed recursion depth. This is used to avoid crashes."),
|
.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);
|
app.run(args);
|
||||||
@ -83,7 +87,12 @@ fn default_action(context: &Context) {
|
|||||||
|
|
||||||
if context.args.is_empty() {
|
if context.args.is_empty() {
|
||||||
// REPL
|
// REPL
|
||||||
repl::start(&mut parser_context, precision, format);
|
repl::start(
|
||||||
|
&mut parser_context,
|
||||||
|
precision,
|
||||||
|
format,
|
||||||
|
context.bool_flag("no-leading-eq")
|
||||||
|
);
|
||||||
} else {
|
} else {
|
||||||
// Direct output
|
// Direct output
|
||||||
output::eval(
|
output::eval(
|
||||||
@ -92,6 +101,7 @@ fn default_action(context: &Context) {
|
|||||||
precision,
|
precision,
|
||||||
10u8,
|
10u8,
|
||||||
format,
|
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(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) {
|
match parser::eval(parser, input, precision) {
|
||||||
Ok(Some(mut result)) => {
|
Ok(Some(mut result)) => {
|
||||||
if base != 10 && !result.set_radix(base) {
|
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 {
|
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;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -27,7 +27,12 @@ struct Context {
|
|||||||
mode: ScientificNotationFormat,
|
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();
|
let mut editor = Editor::<RLHelper>::new();
|
||||||
editor.set_helper(Some(RLHelper {
|
editor.set_helper(Some(RLHelper {
|
||||||
highlighter: LineHighlighter {},
|
highlighter: LineHighlighter {},
|
||||||
@ -73,7 +78,7 @@ pub fn start(parser: &mut parser::Context, precision: u32, format: ScientificNot
|
|||||||
match readline {
|
match readline {
|
||||||
Ok(input) => {
|
Ok(input) => {
|
||||||
editor.add_history_entry(input.as_str());
|
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,
|
Err(ReadlineError::Interrupted) => break,
|
||||||
_ => 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_name) = input.strip_prefix("load ") {
|
||||||
if let Some(file_path) = crate::get_input_file_by_name(file_name) {
|
if let Some(file_path) = crate::get_input_file_by_name(file_name) {
|
||||||
crate::load_input_file(&file_path, precision, parser);
|
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"),
|
"clear" => print!("\x1B[2J"),
|
||||||
"exit" => process::exit(0),
|
"exit" => process::exit(0),
|
||||||
"help" => print_cli_help(),
|
"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