feat: add raw-output
for jq
(#1199)
* feat: add raw-output arg to jq Co-authored-by: Roman Hargrave <roman@hargrave.info> * test: add some test cases for jq raw output * refactor: improve run_jaq_function * docs: document changes --------- Co-authored-by: Roman Hargrave <roman@hargrave.info>
This commit is contained in:
parent
81398d6d28
commit
6ee166707f
3 changed files with 31 additions and 17 deletions
|
@ -19,6 +19,7 @@ All notable changes to eww will be listed here, starting at changes since versio
|
||||||
|
|
||||||
### Features
|
### Features
|
||||||
- Add OnDemand support for focusable on wayland (By: GallowsDove)
|
- Add OnDemand support for focusable on wayland (By: GallowsDove)
|
||||||
|
- Add jq `raw-output` support (By: RomanHargrave)
|
||||||
- Update rust toolchain to 1.81.0 (By: w-lfchen)
|
- Update rust toolchain to 1.81.0 (By: w-lfchen)
|
||||||
- Add `:fill-svg` and `:preserve-aspect-ratio` properties to images (By: hypernova7, w-lfchen)
|
- Add `:fill-svg` and `:preserve-aspect-ratio` properties to images (By: hypernova7, w-lfchen)
|
||||||
- Add `:truncate` property to labels, disabled by default (except in cases where truncation would be enabled in version `0.5.0` and before) (By: Rayzeq).
|
- Add `:truncate` property to labels, disabled by default (except in cases where truncation would be enabled in version `0.5.0` and before) (By: Rayzeq).
|
||||||
|
|
|
@ -472,7 +472,9 @@ fn call_expr_function(name: &str, args: Vec<DynVal>) -> Result<DynVal, EvalError
|
||||||
_ => Err(EvalError::WrongArgCount(name.to_string())),
|
_ => Err(EvalError::WrongArgCount(name.to_string())),
|
||||||
},
|
},
|
||||||
"jq" => match args.as_slice() {
|
"jq" => match args.as_slice() {
|
||||||
[json, code] => run_jaq_function(json.as_json_value()?, code.as_string()?)
|
[json, code] => run_jaq_function(json.as_json_value()?, code.as_string()?, "")
|
||||||
|
.map_err(|e| EvalError::Spanned(code.span(), Box::new(e))),
|
||||||
|
[json, code, args] => run_jaq_function(json.as_json_value()?, code.as_string()?, &args.as_string()?)
|
||||||
.map_err(|e| EvalError::Spanned(code.span(), Box::new(e))),
|
.map_err(|e| EvalError::Spanned(code.span(), Box::new(e))),
|
||||||
_ => Err(EvalError::WrongArgCount(name.to_string())),
|
_ => Err(EvalError::WrongArgCount(name.to_string())),
|
||||||
},
|
},
|
||||||
|
@ -522,16 +524,20 @@ fn prepare_jaq_filter(code: String) -> Result<Arc<jaq_interpret::Filter>, EvalEr
|
||||||
Ok(Arc::new(filter))
|
Ok(Arc::new(filter))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn run_jaq_function(json: serde_json::Value, code: String) -> Result<DynVal, EvalError> {
|
fn run_jaq_function(json: serde_json::Value, code: String, args: &str) -> Result<DynVal, EvalError> {
|
||||||
let filter: Arc<jaq_interpret::Filter> = prepare_jaq_filter(code)?;
|
use jaq_interpret::{Ctx, RcIter, Val};
|
||||||
let inputs = jaq_interpret::RcIter::new(std::iter::empty());
|
prepare_jaq_filter(code)?
|
||||||
let out = filter
|
.run((Ctx::new([], &RcIter::new(std::iter::empty())), Val::from(json)))
|
||||||
.run((jaq_interpret::Ctx::new([], &inputs), jaq_interpret::Val::from(json)))
|
.map(|r| r.map(Into::<serde_json::Value>::into))
|
||||||
.map(|x| x.map(Into::<serde_json::Value>::into))
|
.map(|x| {
|
||||||
.map(|x| x.map(|x| DynVal::from_string(serde_json::to_string(&x).unwrap())))
|
x.map(|val| match (args, val) {
|
||||||
|
("r", serde_json::Value::String(s)) => DynVal::from_string(s),
|
||||||
|
// invalid arguments are silently ignored
|
||||||
|
(_, v) => DynVal::from_string(serde_json::to_string(&v).unwrap()),
|
||||||
|
})
|
||||||
|
})
|
||||||
.collect::<Result<_, _>>()
|
.collect::<Result<_, _>>()
|
||||||
.map_err(|e| EvalError::JaqError(e.to_string()))?;
|
.map_err(|e| EvalError::JaqError(e.to_string()))
|
||||||
Ok(out)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
|
@ -592,5 +598,9 @@ mod tests {
|
||||||
lazy_evaluation_or(r#"true || "null".test"#) => Ok(DynVal::from(true)),
|
lazy_evaluation_or(r#"true || "null".test"#) => Ok(DynVal::from(true)),
|
||||||
lazy_evaluation_elvis(r#""test"?: "null".test"#) => Ok(DynVal::from("test")),
|
lazy_evaluation_elvis(r#""test"?: "null".test"#) => Ok(DynVal::from("test")),
|
||||||
jq_basic_index(r#"jq("[7,8,9]", ".[0]")"#) => Ok(DynVal::from(7)),
|
jq_basic_index(r#"jq("[7,8,9]", ".[0]")"#) => Ok(DynVal::from(7)),
|
||||||
|
jq_raw_arg(r#"jq("[ \"foo\" ]", ".[0]", "r")"#) => Ok(DynVal::from("foo")),
|
||||||
|
jq_empty_arg(r#"jq("[ \"foo\" ]", ".[0]", "")"#) => Ok(DynVal::from(r#""foo""#)),
|
||||||
|
jq_invalid_arg(r#"jq("[ \"foo\" ]", ".[0]", "hello")"#) => Ok(DynVal::from(r#""foo""#)),
|
||||||
|
jq_no_arg(r#"jq("[ \"foo\" ]", ".[0]")"#) => Ok(DynVal::from(r#""foo""#)),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -54,7 +54,10 @@ Supported currently are the following features:
|
||||||
- `substring(string, start, length)`: Return a substring of given length starting at the given index
|
- `substring(string, start, length)`: Return a substring of given length starting at the given index
|
||||||
- `arraylength(value)`: Gets the length of the array
|
- `arraylength(value)`: Gets the length of the array
|
||||||
- `objectlength(value)`: Gets the amount of entries in the object
|
- `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).
|
- `jq(value, jq_filter_string)`: run a [jq](https://jqlang.github.io/jq/manual/) style command on a json value. (Uses [jaq](https://crates.io/crates/jaq) internally).
|
||||||
|
- `jq(value, jq_filter_string, args)`: Emulate command line flags for jq, see [the docs](https://jqlang.github.io/jq/manual/#invoking-jq) on invoking jq for details. Invalid flags are silently ignored.
|
||||||
|
Currently supported flags:
|
||||||
|
- `"r"`: If the result is a string, it won't be formatted as a JSON string. The equivalent jq flag is `--raw-output`.
|
||||||
- `get_env(string)`: Gets the specified enviroment variable
|
- `get_env(string)`: Gets the specified enviroment variable
|
||||||
- `formattime(unix_timestamp, format_str, timezone)`: Gets the time in a given format from UNIX timestamp.
|
- `formattime(unix_timestamp, format_str, timezone)`: Gets the time in a given format from UNIX timestamp.
|
||||||
Check [chrono's documentation](https://docs.rs/chrono/latest/chrono/format/strftime/index.html) for more
|
Check [chrono's documentation](https://docs.rs/chrono/latest/chrono/format/strftime/index.html) for more
|
||||||
|
|
Loading…
Add table
Reference in a new issue