Allow calculator to be customized as a parameter
This commit is contained in:
parent
cf5bf59333
commit
bcd5ee2e60
@ -3,13 +3,25 @@ use kakplugin::{get_selections, set_selections, KakError};
|
|||||||
use std::borrow::Cow;
|
use std::borrow::Cow;
|
||||||
|
|
||||||
#[derive(clap::Args, Debug)]
|
#[derive(clap::Args, Debug)]
|
||||||
pub struct Options;
|
pub struct Options {
|
||||||
pub fn math_eval(_options: &Options) -> Result<String, KakError> {
|
/// Additional arguments to pass to the math evaluator
|
||||||
|
///
|
||||||
|
/// For example, you can run `kakutils-rs bc + 5` to add 5 to all selections
|
||||||
|
extra_math_args: Option<Vec<String>>,
|
||||||
|
}
|
||||||
|
pub fn math_eval(options: &Options) -> Result<String, KakError> {
|
||||||
let mut err_count: usize = 0;
|
let mut err_count: usize = 0;
|
||||||
|
|
||||||
let selections = get_selections(None)?;
|
let selections = get_selections(None)?;
|
||||||
|
|
||||||
set_selections(selections.iter().map(|s| match eval(s) {
|
let extra = options.extra_math_args.as_ref().map(|v| v.join(" "));
|
||||||
|
|
||||||
|
set_selections(selections.iter().map(|s| {
|
||||||
|
match eval(&if let Some(e) = &extra {
|
||||||
|
Cow::Owned(format!("{s} {e}"))
|
||||||
|
} else {
|
||||||
|
Cow::Borrowed(s)
|
||||||
|
}) {
|
||||||
Ok(Value::Float(f)) => Cow::Owned(f.to_string()),
|
Ok(Value::Float(f)) => Cow::Owned(f.to_string()),
|
||||||
Ok(Value::Int(f)) => Cow::Owned(f.to_string()),
|
Ok(Value::Int(f)) => Cow::Owned(f.to_string()),
|
||||||
Ok(_) => Cow::Borrowed(""),
|
Ok(_) => Cow::Borrowed(""),
|
||||||
@ -19,6 +31,7 @@ pub fn math_eval(_options: &Options) -> Result<String, KakError> {
|
|||||||
// Set the selection to empty
|
// Set the selection to empty
|
||||||
Cow::Borrowed("")
|
Cow::Borrowed("")
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}))?;
|
}))?;
|
||||||
|
|
||||||
Ok(if err_count == 0 {
|
Ok(if err_count == 0 {
|
||||||
|
Loading…
Reference in New Issue
Block a user