Skip to content

Commit

Permalink
Add with function
Browse files Browse the repository at this point in the history
  • Loading branch information
MartinJM committed Feb 24, 2022
1 parent f60d7d1 commit a4e2095
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 1 deletion.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ All notable changes to eww will be listed here, starting at changes since versio
- Add support for `:hover` css selectors for eventbox (By: druskus20)
- Add `eww get` subcommand (By: druskus20)
- Add circular progress widget (By: druskus20)
- Add `search` and `captures` functions for expressions (By: MartinJM)
- Add `search`, `captures`, and `with` functions for expressions (By: MartinJM)

### Notable Internal changes
- Rework state management completely, now making local state and dynamic widget hierarchy changes possible.
Expand Down
15 changes: 15 additions & 0 deletions crates/simplexpr/src/eval.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::parse_string;
use itertools::Itertools;

use crate::{
Expand Down Expand Up @@ -27,6 +28,9 @@ pub enum EvalError {
#[error("Unknown function {0}")]
UnknownFunction(String),

#[error("Invalid expression for with: {0}")]
InvalidWithExpression(String),

#[error("Unable to index into value {0}")]
CannotIndex(String),

Expand Down Expand Up @@ -330,6 +334,17 @@ fn call_expr_function(name: &str, args: Vec<DynVal>) -> Result<DynVal, EvalError
}
_ => Err(EvalError::WrongArgCount(name.to_string())),
},
"with" => match args.as_slice() {
[varname, value, function] => {
let mut valuemap = HashMap::new();
valuemap.insert(VarName(varname.to_string()), value.clone());
match parse_string(0, 0, &function.as_string()?) {
Ok(p) => p.eval(&valuemap),
Err(_) => Err(EvalError::InvalidWithExpression(function.as_string()?)),
}
}
_ => Err(EvalError::WrongArgCount(name.to_string())),
},
_ => Err(EvalError::UnknownFunction(name.to_string())),
}
}
1 change: 1 addition & 0 deletions docs/src/expression_language.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,5 @@ Supported currently are the following features:
- `replace(string, regex, replacement)`: Replace matches of a given regex in a string
- `search(string, regex)`: Search for a given regex in a string (returns json)
- `captures(string, regex)`: Get the captures of a given regex in a string (returns json)
- `with(name, value, expression)`: Evaluate the simple expression (which must be a string), where the variable `name` has value `value`

0 comments on commit a4e2095

Please sign in to comment.