cli: Add --raw flag

This commit is contained in:
PaddiM8 2024-10-03 20:09:55 +02:00
parent 676f637bda
commit 18d682c5b8
4 changed files with 20 additions and 10 deletions

4
Cargo.lock generated
View File

@ -163,7 +163,7 @@ dependencies = [
[[package]] [[package]]
name = "kalk" name = "kalk"
version = "3.2.0" version = "3.2.1"
dependencies = [ dependencies = [
"lazy_static", "lazy_static",
"regex", "regex",
@ -175,7 +175,7 @@ dependencies = [
[[package]] [[package]]
name = "kalker" name = "kalker"
version = "2.2.0" version = "2.2.1"
dependencies = [ dependencies = [
"ansi_term", "ansi_term",
"atty", "atty",

View File

@ -41,6 +41,11 @@ fn main() {
.flag( .flag(
Flag::new("no-leading-eq", FlagType::Bool) Flag::new("no-leading-eq", FlagType::Bool)
.description("Don't include an equal sign at the start of results") .description("Don't include an equal sign at the start of results")
)
.flag(
Flag::new("raw", FlagType::Bool)
.description("Only print a single number as the result, nothing else.")
.alias("r")
); );
app.run(args); app.run(args);
@ -91,7 +96,8 @@ fn default_action(context: &Context) {
&mut parser_context, &mut parser_context,
precision, precision,
format, format,
context.bool_flag("no-leading-eq") context.bool_flag("no-leading-eq"),
context.bool_flag("raw")
); );
} else { } else {
// Direct output // Direct output
@ -101,7 +107,8 @@ fn default_action(context: &Context) {
precision, precision,
10u8, 10u8,
format, format,
context.bool_flag("no-leading-eq") context.bool_flag("no-leading-eq"),
context.bool_flag("raw")
); );
} }
} }

View File

@ -9,7 +9,8 @@ pub fn eval(
precision: u32, precision: u32,
base: u8, base: u8,
format: ScientificNotationFormat, format: ScientificNotationFormat,
no_leading_equal: bool no_leading_equal: bool,
raw: bool
) { ) {
match parser::eval(parser, input, precision) { match parser::eval(parser, input, precision) {
Ok(Some(mut result)) => { Ok(Some(mut result)) => {
@ -19,7 +20,7 @@ pub fn eval(
return; return;
} }
if precision == DEFAULT_PRECISION { if precision == DEFAULT_PRECISION && !raw {
let mut result_str = result.to_string_pretty_format(format); let mut result_str = result.to_string_pretty_format(format);
if no_leading_equal { if no_leading_equal {
result_str = result_str result_str = result_str

View File

@ -31,7 +31,8 @@ pub fn start(
parser: &mut parser::Context, parser: &mut parser::Context,
precision: u32, precision: u32,
format: ScientificNotationFormat, format: ScientificNotationFormat,
no_leading_equal: bool no_leading_equal: bool,
raw: bool
) { ) {
let mut editor = Editor::<RLHelper>::new(); let mut editor = Editor::<RLHelper>::new();
editor.set_helper(Some(RLHelper { editor.set_helper(Some(RLHelper {
@ -78,7 +79,7 @@ pub fn start(
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, no_leading_equal); eval_repl(&mut repl, parser, &input, precision, no_leading_equal, raw);
} }
Err(ReadlineError::Interrupted) => break, Err(ReadlineError::Interrupted) => break,
_ => break, _ => break,
@ -95,7 +96,8 @@ fn eval_repl(
parser: &mut parser::Context, parser: &mut parser::Context,
input: &str, input: &str,
precision: u32, precision: u32,
no_leading_equal: bool no_leading_equal: bool,
raw: 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) {
@ -144,7 +146,7 @@ fn eval_repl(
"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, no_leading_equal), _ => output::eval(parser, input, precision, repl.base, repl.mode, no_leading_equal, raw),
} }
} }