From df1168d6d08233f2e8ae841dec9fd922566c2846 Mon Sep 17 00:00:00 2001 From: Pedro Burgos <43417195+druskus20@users.noreply.github.com> Date: Sat, 11 Sep 2021 13:50:45 +0200 Subject: [PATCH] Implement optional initial value for pollvars (#272) * Implement optional initial value for pollvars * Updated docs to include the atribute initial for pollvars --- crates/eww/src/app.rs | 1 - crates/eww/src/config/inbuilt.rs | 1 + crates/eww/src/config/script_var.rs | 18 +++++++++++------- .../yuck/src/config/script_var_definition.rs | 10 +++++++++- docs/src/configuration.md | 5 ++++- 5 files changed, 25 insertions(+), 10 deletions(-) diff --git a/crates/eww/src/app.rs b/crates/eww/src/app.rs index 9069b6e..aea4157 100644 --- a/crates/eww/src/app.rs +++ b/crates/eww/src/app.rs @@ -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; diff --git a/crates/eww/src/config/inbuilt.rs b/crates/eww/src/config/inbuilt.rs index 7f0a668..f09af9d 100644 --- a/crates/eww/src/config/inbuilt.rs +++ b/crates/eww/src/config/inbuilt.rs @@ -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, }) diff --git a/crates/eww/src/config/script_var.rs b/crates/eww/src/config/script_var.rs index da44b43..9e4c424 100644 --- a/crates/eww/src/config/script_var.rs +++ b/crates/eww/src/config/script_var.rs @@ -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 { 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()), } } diff --git a/crates/yuck/src/config/script_var_definition.rs b/crates/yuck/src/config/script_var_definition.rs index 79b4fb8..a67afbb 100644 --- a/crates/yuck/src/config/script_var_definition.rs +++ b/crates/yuck/src/config/script_var_definition.rs @@ -56,6 +56,7 @@ pub enum VarSource { pub struct PollScriptVar { pub name: VarName, pub command: VarSource, + pub initial_value: Option, 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::("interval")?.as_duration()?; let timeout = attrs .primitive_optional::("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'")`"#) } diff --git a/docs/src/configuration.md b/docs/src/configuration.md index bdd2e71..3b8bcb9 100644 --- a/docs/src/configuration.md +++ b/docs/src/configuration.md @@ -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