Implement optional initial value for pollvars (#272)
* Implement optional initial value for pollvars * Updated docs to include the atribute initial for pollvars
This commit is contained in:
parent
089f617393
commit
df1168d6d0
5 changed files with 25 additions and 10 deletions
|
@ -274,7 +274,6 @@ impl App {
|
|||
// refresh script-var poll stuff
|
||||
self.script_var_handler.stop_all();
|
||||
|
||||
|
||||
log::trace!("loading config: {:#?}", config);
|
||||
|
||||
self.eww_config = config;
|
||||
|
|
|
@ -13,6 +13,7 @@ macro_rules! builtin_vars {
|
|||
VarName::from($name) => ScriptVarDefinition::Poll(PollScriptVar {
|
||||
name: VarName::from($name),
|
||||
command: VarSource::Function($fun),
|
||||
initial_value: None,
|
||||
interval: $interval,
|
||||
name_span: eww_shared_util::span::Span::DUMMY,
|
||||
})
|
||||
|
|
|
@ -22,14 +22,18 @@ pub fn create_script_var_failed_warn(span: Span, var_name: &VarName, error_outpu
|
|||
|
||||
pub fn initial_value(var: &ScriptVarDefinition) -> Result<DynVal> {
|
||||
match var {
|
||||
ScriptVarDefinition::Poll(x) => match &x.command {
|
||||
VarSource::Function(f) => {
|
||||
f().map_err(|err| anyhow!(err)).with_context(|| format!("Failed to compute initial value for {}", &var.name()))
|
||||
}
|
||||
VarSource::Shell(span, command) => {
|
||||
run_command(command).map_err(|e| anyhow!(create_script_var_failed_warn(*span, var.name(), &e.to_string())))
|
||||
}
|
||||
ScriptVarDefinition::Poll(x) => match &x.initial_value {
|
||||
Some(value) => Ok(value.clone()),
|
||||
None => match &x.command {
|
||||
VarSource::Function(f) => f()
|
||||
.map_err(|err| anyhow!(err))
|
||||
.with_context(|| format!("Failed to compute initial value for {}", &var.name())),
|
||||
VarSource::Shell(span, command) => {
|
||||
run_command(command).map_err(|e| anyhow!(create_script_var_failed_warn(*span, var.name(), &e.to_string())))
|
||||
}
|
||||
},
|
||||
},
|
||||
|
||||
ScriptVarDefinition::Listen(var) => Ok(var.initial_value.clone()),
|
||||
}
|
||||
}
|
||||
|
|
|
@ -56,6 +56,7 @@ pub enum VarSource {
|
|||
pub struct PollScriptVar {
|
||||
pub name: VarName,
|
||||
pub command: VarSource,
|
||||
pub initial_value: Option<DynVal>,
|
||||
pub interval: std::time::Duration,
|
||||
pub name_span: Span,
|
||||
}
|
||||
|
@ -67,6 +68,7 @@ impl FromAstElementContent for PollScriptVar {
|
|||
let result: AstResult<_> = try {
|
||||
let (name_span, name) = iter.expect_symbol()?;
|
||||
let mut attrs = iter.expect_key_values()?;
|
||||
let initial_value = Some(attrs.primitive_optional("initial")?.unwrap_or_else(|| DynVal::from_string(String::new())));
|
||||
let interval = attrs.primitive_required::<DynVal, _>("interval")?.as_duration()?;
|
||||
let timeout = attrs
|
||||
.primitive_optional::<DynVal, _>("timeout")?
|
||||
|
@ -75,7 +77,13 @@ impl FromAstElementContent for PollScriptVar {
|
|||
.unwrap_or_else(|| std::time::Duration::from_millis(200));
|
||||
let (script_span, script) = iter.expect_literal()?;
|
||||
iter.expect_done()?;
|
||||
Self { name_span, name: VarName(name), command: VarSource::Shell(script_span, script.to_string()), interval }
|
||||
Self {
|
||||
name_span,
|
||||
name: VarName(name),
|
||||
command: VarSource::Shell(script_span, script.to_string()),
|
||||
initial_value,
|
||||
interval,
|
||||
}
|
||||
};
|
||||
result.note(r#"Expected format: `(defpoll name :interval "10s" "echo 'a shell script'")`"#)
|
||||
}
|
||||
|
|
|
@ -1,7 +1,6 @@
|
|||
# Writing your eww configuration
|
||||
|
||||
(For a list of all built in widgets (i.e. `box`, `label`, `button`) see [Widget Documentation](widgets.md))
|
||||
|
||||
Eww is configured using its own language called `yuck`.
|
||||
using yuck, you declare the structure and content of your widgets, the geometry, position and behavior of any windows,
|
||||
as well as any state and data that will be used in your widgets.
|
||||
|
@ -157,6 +156,7 @@ They may also be useful to have buttons within eww change what is shown within y
|
|||
```lisp
|
||||
(defpoll time :interval "1s"
|
||||
:timeout "0.1s" ; setting timeout is optional
|
||||
:initial "initial-value" ; setting timeout is optional
|
||||
`date +%H:%M:%S`)
|
||||
```
|
||||
|
||||
|
@ -169,6 +169,9 @@ and thus are the perfect choice for showing your time, date, as well as other bi
|
|||
Optionally, you can specify a timeout, after which the provided script will be aborted.
|
||||
This helps to avoid accidentally launching thousands of never-ending processes on your system.
|
||||
|
||||
You can also specify an initial-value, this should prevent eww from waiting for the result of a give command during startup, thus,
|
||||
making the startup time faster.
|
||||
|
||||
**Listening variables (`deflisten`)**
|
||||
|
||||
```lisp
|
||||
|
|
Loading…
Add table
Reference in a new issue