cli: Add mode switching command

This commit is contained in:
PaddiM8 2024-03-23 02:06:50 +01:00
parent 416b2c571f
commit fee73e6b26
2 changed files with 28 additions and 5 deletions

View File

@ -27,7 +27,7 @@ fn main() {
) )
.flag( .flag(
Flag::new("eng", FlagType::Bool) Flag::new("eng", FlagType::Bool)
.description("Engineering mode") .description("Engineering mode. Modes can also be switched between by typing `mode [normal|eng]`.")
) )
.flag( .flag(
Flag::new("angle-unit", FlagType::String) Flag::new("angle-unit", FlagType::String)

View File

@ -1,4 +1,5 @@
use crate::output; use crate::output;
use crate::output::print_err;
use ansi_term::Colour::{self, Cyan}; use ansi_term::Colour::{self, Cyan};
use kalk::kalk_value::ScientificNotationFormat; use kalk::kalk_value::ScientificNotationFormat;
use kalk::parser; use kalk::parser;
@ -23,6 +24,7 @@ use std::process;
struct Context { struct Context {
base: u8, base: u8,
mode: ScientificNotationFormat,
} }
pub fn start(parser: &mut parser::Context, precision: u32, format: ScientificNotationFormat) { pub fn start(parser: &mut parser::Context, precision: u32, format: ScientificNotationFormat) {
@ -55,7 +57,11 @@ pub fn start(parser: &mut parser::Context, precision: u32, format: ScientificNot
); );
} }
let mut repl = Context { base: 10u8 }; let mut repl = Context {
base: 10u8,
mode: format,
};
loop { loop {
let prompt = if cfg!(windows) { let prompt = if cfg!(windows) {
String::from(">> ") String::from(">> ")
@ -67,7 +73,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, format); eval_repl(&mut repl, parser, &input, precision);
} }
Err(ReadlineError::Interrupted) => break, Err(ReadlineError::Interrupted) => break,
_ => break, _ => break,
@ -79,7 +85,7 @@ 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, format: ScientificNotationFormat) { fn eval_repl(repl: &mut self::Context, parser: &mut parser::Context, input: &str, precision: u32) {
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);
@ -105,12 +111,29 @@ fn eval_repl(repl: &mut self::Context, parser: &mut parser::Context, input: &str
} }
} }
if input.starts_with("mode") {
let mut parts = input.split(' ');
let mode = match parts.nth(1) {
Some("normal") => ScientificNotationFormat::Normal,
Some("eng") => ScientificNotationFormat::Engineering,
_ => {
print_err("Invalid mode name. Available modes: normal, eng");
return;
},
};
repl.mode = mode;
return;
}
match input { match input {
"" => eprint!(""), "" => eprint!(""),
"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, format), _ => output::eval(parser, input, precision, repl.base, repl.mode),
} }
} }