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:
Pedro Burgos 2021-09-11 13:50:45 +02:00 committed by GitHub
parent 089f617393
commit df1168d6d0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 25 additions and 10 deletions

View file

@ -274,7 +274,6 @@ impl App {
// refresh script-var poll stuff // refresh script-var poll stuff
self.script_var_handler.stop_all(); self.script_var_handler.stop_all();
log::trace!("loading config: {:#?}", config); log::trace!("loading config: {:#?}", config);
self.eww_config = config; self.eww_config = config;

View file

@ -13,6 +13,7 @@ macro_rules! builtin_vars {
VarName::from($name) => ScriptVarDefinition::Poll(PollScriptVar { VarName::from($name) => ScriptVarDefinition::Poll(PollScriptVar {
name: VarName::from($name), name: VarName::from($name),
command: VarSource::Function($fun), command: VarSource::Function($fun),
initial_value: None,
interval: $interval, interval: $interval,
name_span: eww_shared_util::span::Span::DUMMY, name_span: eww_shared_util::span::Span::DUMMY,
}) })

View file

@ -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> { pub fn initial_value(var: &ScriptVarDefinition) -> Result<DynVal> {
match var { match var {
ScriptVarDefinition::Poll(x) => match &x.command { ScriptVarDefinition::Poll(x) => match &x.initial_value {
VarSource::Function(f) => { Some(value) => Ok(value.clone()),
f().map_err(|err| anyhow!(err)).with_context(|| format!("Failed to compute initial value for {}", &var.name())) 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) => { VarSource::Shell(span, command) => {
run_command(command).map_err(|e| anyhow!(create_script_var_failed_warn(*span, var.name(), &e.to_string()))) 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()), ScriptVarDefinition::Listen(var) => Ok(var.initial_value.clone()),
} }
} }

View file

@ -56,6 +56,7 @@ pub enum VarSource {
pub struct PollScriptVar { pub struct PollScriptVar {
pub name: VarName, pub name: VarName,
pub command: VarSource, pub command: VarSource,
pub initial_value: Option<DynVal>,
pub interval: std::time::Duration, pub interval: std::time::Duration,
pub name_span: Span, pub name_span: Span,
} }
@ -67,6 +68,7 @@ impl FromAstElementContent for PollScriptVar {
let result: AstResult<_> = try { let result: AstResult<_> = try {
let (name_span, name) = iter.expect_symbol()?; let (name_span, name) = iter.expect_symbol()?;
let mut attrs = iter.expect_key_values()?; 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 interval = attrs.primitive_required::<DynVal, _>("interval")?.as_duration()?;
let timeout = attrs let timeout = attrs
.primitive_optional::<DynVal, _>("timeout")? .primitive_optional::<DynVal, _>("timeout")?
@ -75,7 +77,13 @@ impl FromAstElementContent for PollScriptVar {
.unwrap_or_else(|| std::time::Duration::from_millis(200)); .unwrap_or_else(|| std::time::Duration::from_millis(200));
let (script_span, script) = iter.expect_literal()?; let (script_span, script) = iter.expect_literal()?;
iter.expect_done()?; 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'")`"#) result.note(r#"Expected format: `(defpoll name :interval "10s" "echo 'a shell script'")`"#)
} }

View file

@ -1,7 +1,6 @@
# Writing your eww configuration # Writing your eww configuration
(For a list of all built in widgets (i.e. `box`, `label`, `button`) see [Widget Documentation](widgets.md)) (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`. 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, 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. 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 ```lisp
(defpoll time :interval "1s" (defpoll time :interval "1s"
:timeout "0.1s" ; setting timeout is optional :timeout "0.1s" ; setting timeout is optional
:initial "initial-value" ; setting timeout is optional
`date +%H:%M:%S`) `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. 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. 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`)** **Listening variables (`deflisten`)**
```lisp ```lisp