Cleanup EWW_TIME API and code

This commit is contained in:
elkowar 2023-06-09 01:28:59 +02:00
parent 4d63f6deec
commit b31e397e97
No known key found for this signature in database
GPG Key ID: 50E76B4711E4C3E4
4 changed files with 18 additions and 18 deletions

View File

@ -1,4 +1,4 @@
use std::{collections::HashMap, time::Duration};
use std::collections::HashMap;
use simplexpr::{dynval::DynVal, SimplExpr};
use yuck::config::{
@ -10,7 +10,7 @@ use crate::{config::system_stats::*, paths::EwwPaths};
use eww_shared_util::VarName;
macro_rules! define_builtin_vars {
($($name:literal => $fun:expr => $interval:expr),*$(,)?) => {
($($name:literal [$interval:literal] => $fun:expr),*$(,)?) => {
pub static INBUILT_VAR_NAMES: &[&'static str] = &[$($name),*];
pub fn get_inbuilt_vars() -> HashMap<VarName, ScriptVarDefinition> {
maplit::hashmap! {
@ -20,7 +20,7 @@ macro_rules! define_builtin_vars {
run_while_expr: SimplExpr::Literal(DynVal::from(true)),
command: VarSource::Function($fun),
initial_value: None,
interval: $interval,
interval: std::time::Duration::from_secs($interval),
name_span: eww_shared_util::span::Span::DUMMY,
})
),*
@ -32,19 +32,19 @@ macro_rules! define_builtin_vars {
define_builtin_vars! {
// @desc EWW_TEMPS - Heat of the components in Celcius
// @prop { <name>: temperature }
"EWW_TEMPS" => || Ok(DynVal::from(get_temperatures())) => Duration::new(2, 0),
"EWW_TEMPS" [2] => || Ok(DynVal::from(get_temperatures())),
// @desc EWW_RAM - Information on ram and swap usage in kB.
// @prop { total_mem, free_mem, total_swap, free_swap, available_mem, used_mem, used_mem_perc }
"EWW_RAM" => || Ok(DynVal::from(get_ram())) => Duration::new(2, 0),
"EWW_RAM" [2] => || Ok(DynVal::from(get_ram())),
// @desc EWW_DISK - Information on on all mounted partitions (Might report inaccurately on some filesystems, like btrfs and zfs) Example: `{EWW_DISK["/"]}`
// @prop { <mount_point>: { name, total, free, used, used_perc } }
"EWW_DISK" => || Ok(DynVal::from(get_disks())) => Duration::new(2, 0),
"EWW_DISK" [2] => || Ok(DynVal::from(get_disks())),
// @desc EWW_BATTERY - Battery capacity in procent of the main battery
// @prop { <name>: { capacity, status } }
"EWW_BATTERY" => || Ok(DynVal::from(
"EWW_BATTERY" [2] => || Ok(DynVal::from(
match get_battery_capacity() {
Err(e) => {
log::error!("Couldn't get the battery capacity: {:?}", e);
@ -52,18 +52,18 @@ define_builtin_vars! {
}
Ok(o) => o,
}
)) => Duration::new(2, 0),
)),
// @desc EWW_CPU - Information on the CPU cores: frequency and usage (No MacOS support)
// @prop { cores: [{ core, freq, usage }], avg }
"EWW_CPU" => || Ok(DynVal::from(get_cpus())) => Duration::new(2, 0),
"EWW_CPU" [2] => || Ok(DynVal::from(get_cpus())) ,
// @desc EWW_NET - Bytes up/down on all interfaces
// @prop { <name>: { up, down } }
"EWW_NET" => || Ok(DynVal::from(net())) => Duration::new(2, 0),
"EWW_NET" [2] => || Ok(DynVal::from(net())) ,
// @desc EWW_TIME - Information on current time in UNIX timestamp
"EWW_TIME" => || Ok(DynVal::from(get_time())) => Duration::new(1, 0),
// @desc EWW_TIME - the current UNIX timestamp
"EWW_TIME" [1] => || Ok(DynVal::from(get_time())) ,
}
macro_rules! define_magic_constants {

View File

@ -179,5 +179,5 @@ pub fn net() -> String {
}
pub fn get_time() -> String {
format!("{}", chrono::offset::Utc::now().format("%s"))
chrono::offset::Utc::now().timestamp().to_string()
}

View File

@ -57,11 +57,11 @@ pub enum EvalError {
#[error(transparent)]
JaqParseError(JaqParseError),
#[error("{1}")]
Spanned(Span, Box<EvalError>),
#[error("Error parsing date: {0}")]
ChronoError(String),
#[error("{1}")]
Spanned(Span, Box<EvalError>),
}
static_assertions::assert_impl_all!(EvalError: Send, Sync);
@ -382,7 +382,7 @@ fn call_expr_function(name: &str, args: Vec<DynVal>) -> Result<DynVal, EvalError
_ => Err(EvalError::WrongArgCount(name.to_string())),
},
"formattime" => match args.as_slice() {
[timestamp, timezone, format] => {
[timestamp, format, timezone] => {
let timezone = match chrono_tz::Tz::from_str(&timezone.as_string()?) {
Ok(x) => x,
Err(_) => return Err(EvalError::ChronoError("Invalid timezone".to_string())),

View File

@ -47,7 +47,7 @@ Supported currently are the following features:
- `arraylength(value)`: Gets the length of the array
- `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).
- `formattime(unix_timestamp, timezone, format_str)`: 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
information about format string and [chrono-tz's documentation](https://docs.rs/chrono-tz/latest/chrono_tz/enum.Tz.html)
for available time zones.