diff --git a/CHANGELOG.md b/CHANGELOG.md index 7744ad6..d2edca8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -18,6 +18,7 @@ All notable changes to eww will be listed here, starting at changes since versio - Add `justify` property to the label widget, allowing text justification (By: n3oney) - Add `EWW_TIME` magic variable (By: Erenoit) - Add trigonometric functions (`sin`, `cos`, `tan`, `cot`) and degree/radian conversions (`degtorad`, `radtodeg`) (By: end-4) +- Add `substring` function to simplexpr ## [0.4.0] (04.09.2022) diff --git a/crates/simplexpr/src/eval.rs b/crates/simplexpr/src/eval.rs index c890ce8..a77b703 100644 --- a/crates/simplexpr/src/eval.rs +++ b/crates/simplexpr/src/eval.rs @@ -379,6 +379,18 @@ fn call_expr_function(name: &str, args: Vec) -> Result Err(EvalError::WrongArgCount(name.to_string())), }, + "substring" => match args.as_slice() { + [string, start, len] => { + let result: String = string + .as_string()? + .chars() + .skip(start.as_i32()?.max(0) as usize) + .take(len.as_i32()?.max(0) as usize) + .collect(); + Ok(DynVal::from(result)) + } + _ => Err(EvalError::WrongArgCount(name.to_string())), + }, "search" => match args.as_slice() { [string, pattern] => { use serde_json::Value; diff --git a/docs/src/expression_language.md b/docs/src/expression_language.md index 7d54811..574e8e5 100644 --- a/docs/src/expression_language.md +++ b/docs/src/expression_language.md @@ -47,6 +47,7 @@ Supported currently are the following features: - `matches(string, regex)`: check if a given string matches a given regex (returns bool) - `captures(string, regex)`: Get the captures of a given regex in a string (returns array) - `strlength(value)`: Gets the length of the string + - `substring(string, start, length)`: Return a substring of given length starting at the given index - `arraylength(value)`: Gets the length of the array - `objectlength(value)`: Gets the amount of entries in the object - `jq(value, jq_filter_string)`: run a [jq](https://stedolan.github.io/jq/manual/) style command on a json value. (Uses [jaq](https://crates.io/crates/jaq) internally).