From 4d63f6deec7fd0cc1fe34907df1848ac2b94e90f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Eren=20=C3=96nen?= Date: Fri, 9 Jun 2023 18:17:41 +0300 Subject: [PATCH] add `EWW_TIME` magic variable (#792) --- CHANGELOG.md | 1 + Cargo.lock | 336 ++++++++++++++++++++++++-- crates/eww/Cargo.toml | 1 + crates/eww/src/config/inbuilt.rs | 19 +- crates/eww/src/config/system_stats.rs | 4 + crates/simplexpr/Cargo.toml | 2 + crates/simplexpr/src/dynval.rs | 4 + crates/simplexpr/src/eval.rs | 23 ++ docs/src/expression_language.md | 8 + docs/src/magic-vars.md | 2 +- 10 files changed, 366 insertions(+), 34 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ef91c8f..8f3bbcb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -16,6 +16,7 @@ All notable changes to eww will be listed here, starting at changes since versio - Add Vanilla CSS support (By: Ezequiel Ramis) - Add `jq` function, offering jq-style json processing - Add `justify` property to the label widget, allowing text justification (By: n3oney) +- Add `EWW_TIME` magic variable (By: Erenoit) ## [0.4.0] (04.09.2022) diff --git a/Cargo.lock b/Cargo.lock index fb11492..ee71ec8 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -31,6 +31,21 @@ dependencies = [ "memchr", ] +[[package]] +name = "android-tzdata" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e999941b234f3131b00bc13c22d06e8c5ff726d1b6318ac7eb276997bbb4fef0" + +[[package]] +name = "android_system_properties" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "819e7219dbd41043ac279b19830f2efc897156490d7fd6ea916720117ee66311" +dependencies = [ + "libc", +] + [[package]] name = "anyhow" version = "1.0.70" @@ -134,6 +149,12 @@ version = "1.3.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" +[[package]] +name = "bumpalo" +version = "3.13.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a3e2c3daef883ecc1b5d58c15adae93470a91d425f3532ba1695849656af3fc1" + [[package]] name = "bytes" version = "1.2.1" @@ -204,9 +225,9 @@ dependencies = [ [[package]] name = "cc" -version = "1.0.77" +version = "1.0.79" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e9f73505338f7d905b19d18738976aae232eb46b8efc15554ffc56deb5d9ebe4" +checksum = "50d30906286121d95be3d479533b458f87493b30a4b5f79a607db8f5d11aa91f" [[package]] name = "cfg-expr" @@ -223,6 +244,43 @@ version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" +[[package]] +name = "chrono" +version = "0.4.26" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ec837a71355b28f6556dbd569b37b3f363091c0bd4b2e735674521b4c5fd9bc5" +dependencies = [ + "android-tzdata", + "iana-time-zone", + "js-sys", + "num-traits", + "time", + "wasm-bindgen", + "winapi", +] + +[[package]] +name = "chrono-tz" +version = "0.8.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cf9cc2b23599e6d7479755f3594285efb3f74a1bdca7a7374948bc831e23a552" +dependencies = [ + "chrono", + "chrono-tz-build", + "phf 0.11.1", +] + +[[package]] +name = "chrono-tz-build" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d9998fb9f7e9b2111641485bf8beb32f92945f97f92a3d061f744cfef335f751" +dependencies = [ + "parse-zoneinfo", + "phf 0.11.1", + "phf_codegen", +] + [[package]] name = "chumsky" version = "0.8.0" @@ -534,6 +592,7 @@ dependencies = [ "bincode", "cairo-rs", "cairo-sys-rs", + "chrono", "clap", "codespan-reporting", "derive_more", @@ -822,7 +881,7 @@ checksum = "c05aeb6a22b8f62540c194aac980f2115af067bfe15a0734d7277a768d396b31" dependencies = [ "cfg-if", "libc", - "wasi", + "wasi 0.11.0+wasi-snapshot-preview1", ] [[package]] @@ -930,7 +989,7 @@ dependencies = [ "indexmap", "lasso", "once_cell", - "phf", + "phf 0.10.1", ] [[package]] @@ -1067,6 +1126,29 @@ dependencies = [ "quick-error", ] +[[package]] +name = "iana-time-zone" +version = "0.1.56" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0722cd7114b7de04316e7ea5456a0bbb20e4adb46fd27a3697adb812cff0f37c" +dependencies = [ + "android_system_properties", + "core-foundation-sys", + "iana-time-zone-haiku", + "js-sys", + "wasm-bindgen", + "windows", +] + +[[package]] +name = "iana-time-zone-haiku" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f31827a206f56af32e590ba56d5d2d085f558508192593743f16b2306495269f" +dependencies = [ + "cc", +] + [[package]] name = "ident_case" version = "1.0.1" @@ -1198,6 +1280,15 @@ dependencies = [ "jaq-parse", ] +[[package]] +name = "js-sys" +version = "0.3.63" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2f37a4a5928311ac501dee68b3c7613a1037d0edb30c8e5427bd832d55d1b790" +dependencies = [ + "wasm-bindgen", +] + [[package]] name = "kqueue" version = "1.0.7" @@ -1340,7 +1431,7 @@ checksum = "e5d732bc30207a6423068df043e3d02e0735b155ad7ce1a6f76fe2baa5b158de" dependencies = [ "libc", "log", - "wasi", + "wasi 0.11.0+wasi-snapshot-preview1", "windows-sys 0.42.0", ] @@ -1404,6 +1495,15 @@ dependencies = [ "winapi", ] +[[package]] +name = "num-traits" +version = "0.2.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "578ede34cf02f8924ab9447f50c28075b4d3e5b269972345e7e0372b38c6cdcd" +dependencies = [ + "autocfg", +] + [[package]] name = "num_cpus" version = "1.13.1" @@ -1483,6 +1583,15 @@ dependencies = [ "windows-sys 0.42.0", ] +[[package]] +name = "parse-zoneinfo" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c705f256449c60da65e11ff6626e0c16a0a0b96aaa348de61376b249bc340f41" +dependencies = [ + "regex", +] + [[package]] name = "pest" version = "2.4.0" @@ -1510,17 +1619,46 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "fabbf1ead8a5bcbc20f5f8b939ee3f5b0f6f281b6ad3468b84656b658b455259" dependencies = [ "phf_macros", - "phf_shared", + "phf_shared 0.10.0", "proc-macro-hack", ] +[[package]] +name = "phf" +version = "0.11.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "928c6535de93548188ef63bb7c4036bd415cd8f36ad25af44b9789b2ee72a48c" +dependencies = [ + "phf_shared 0.11.1", +] + +[[package]] +name = "phf_codegen" +version = "0.11.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a56ac890c5e3ca598bbdeaa99964edb5b0258a583a9eb6ef4e89fc85d9224770" +dependencies = [ + "phf_generator 0.11.1", + "phf_shared 0.11.1", +] + [[package]] name = "phf_generator" version = "0.10.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5d5285893bb5eb82e6aaf5d59ee909a06a16737a8970984dd7746ba9283498d6" dependencies = [ - "phf_shared", + "phf_shared 0.10.0", + "rand", +] + +[[package]] +name = "phf_generator" +version = "0.11.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b1181c94580fa345f50f19d738aaa39c0ed30a600d95cb2d3e23f94266f14fbf" +dependencies = [ + "phf_shared 0.11.1", "rand", ] @@ -1530,8 +1668,8 @@ version = "0.10.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "58fdf3184dd560f160dd73922bea2d5cd6e8f064bf4b13110abd81b03697b4e0" dependencies = [ - "phf_generator", - "phf_shared", + "phf_generator 0.10.0", + "phf_shared 0.10.0", "proc-macro-hack", "proc-macro2", "quote", @@ -1547,6 +1685,15 @@ dependencies = [ "siphasher", ] +[[package]] +name = "phf_shared" +version = "0.11.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e1fb5f6f826b772a8d4c0394209441e7d37cbbb967ae9c7e0e8134365c9ee676" +dependencies = [ + "siphasher", +] + [[package]] name = "pico-args" version = "0.4.2" @@ -1925,6 +2072,8 @@ name = "simplexpr" version = "0.1.0" dependencies = [ "cached", + "chrono", + "chrono-tz", "eww_shared_util", "insta", "itertools", @@ -1999,7 +2148,7 @@ dependencies = [ "new_debug_unreachable", "once_cell", "parking_lot", - "phf_shared", + "phf_shared 0.10.0", "precomputed-hash", ] @@ -2131,6 +2280,17 @@ dependencies = [ "syn 1.0.103", ] +[[package]] +name = "time" +version = "0.1.45" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1b797afad3f312d1c66a56d11d0316f916356d11bd158fbc6ca6389ff6bf805a" +dependencies = [ + "libc", + "wasi 0.10.0+wasi-snapshot-preview1", + "winapi", +] + [[package]] name = "tiny-keccak" version = "2.0.2" @@ -2255,12 +2415,72 @@ dependencies = [ "winapi-util", ] +[[package]] +name = "wasi" +version = "0.10.0+wasi-snapshot-preview1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1a143597ca7c7793eff794def352d41792a93c481eb1042423ff7ff72ba2c31f" + [[package]] name = "wasi" version = "0.11.0+wasi-snapshot-preview1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423" +[[package]] +name = "wasm-bindgen" +version = "0.2.86" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5bba0e8cb82ba49ff4e229459ff22a191bbe9a1cb3a341610c9c33efc27ddf73" +dependencies = [ + "cfg-if", + "wasm-bindgen-macro", +] + +[[package]] +name = "wasm-bindgen-backend" +version = "0.2.86" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "19b04bc93f9d6bdee709f6bd2118f57dd6679cf1176a1af464fca3ab0d66d8fb" +dependencies = [ + "bumpalo", + "log", + "once_cell", + "proc-macro2", + "quote", + "syn 2.0.10", + "wasm-bindgen-shared", +] + +[[package]] +name = "wasm-bindgen-macro" +version = "0.2.86" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "14d6b024f1a526bb0234f52840389927257beb670610081360e5a03c5df9c258" +dependencies = [ + "quote", + "wasm-bindgen-macro-support", +] + +[[package]] +name = "wasm-bindgen-macro-support" +version = "0.2.86" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e128beba882dd1eb6200e1dc92ae6c5dbaa4311aa7bb211ca035779e5efc39f8" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.10", + "wasm-bindgen-backend", + "wasm-bindgen-shared", +] + +[[package]] +name = "wasm-bindgen-shared" +version = "0.2.86" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ed9d5b4305409d1fc9482fee2d7f9bcbf24b3972bf59817ef757e23982242a93" + [[package]] name = "winapi" version = "0.3.9" @@ -2301,19 +2521,28 @@ version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" +[[package]] +name = "windows" +version = "0.48.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e686886bc078bc1b0b600cac0147aadb815089b6e4da64016cbd754b6342700f" +dependencies = [ + "windows-targets 0.48.0", +] + [[package]] name = "windows-sys" version = "0.42.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5a3e1820f08b8513f676f7ab6c1f99ff312fb97b553d30ff4dd86f9f15728aa7" dependencies = [ - "windows_aarch64_gnullvm", - "windows_aarch64_msvc", - "windows_i686_gnu", - "windows_i686_msvc", - "windows_x86_64_gnu", - "windows_x86_64_gnullvm", - "windows_x86_64_msvc", + "windows_aarch64_gnullvm 0.42.2", + "windows_aarch64_msvc 0.42.2", + "windows_i686_gnu 0.42.2", + "windows_i686_msvc 0.42.2", + "windows_x86_64_gnu 0.42.2", + "windows_x86_64_gnullvm 0.42.2", + "windows_x86_64_msvc 0.42.2", ] [[package]] @@ -2322,7 +2551,7 @@ version = "0.45.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "75283be5efb2831d37ea142365f009c02ec203cd29a3ebecbc093d52315b66d0" dependencies = [ - "windows-targets", + "windows-targets 0.42.2", ] [[package]] @@ -2331,13 +2560,28 @@ version = "0.42.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8e5180c00cd44c9b1c88adb3693291f1cd93605ded80c250a75d472756b4d071" dependencies = [ - "windows_aarch64_gnullvm", - "windows_aarch64_msvc", - "windows_i686_gnu", - "windows_i686_msvc", - "windows_x86_64_gnu", - "windows_x86_64_gnullvm", - "windows_x86_64_msvc", + "windows_aarch64_gnullvm 0.42.2", + "windows_aarch64_msvc 0.42.2", + "windows_i686_gnu 0.42.2", + "windows_i686_msvc 0.42.2", + "windows_x86_64_gnu 0.42.2", + "windows_x86_64_gnullvm 0.42.2", + "windows_x86_64_msvc 0.42.2", +] + +[[package]] +name = "windows-targets" +version = "0.48.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7b1eb6f0cd7c80c79759c929114ef071b87354ce476d9d94271031c0497adfd5" +dependencies = [ + "windows_aarch64_gnullvm 0.48.0", + "windows_aarch64_msvc 0.48.0", + "windows_i686_gnu 0.48.0", + "windows_i686_msvc 0.48.0", + "windows_x86_64_gnu 0.48.0", + "windows_x86_64_gnullvm 0.48.0", + "windows_x86_64_msvc 0.48.0", ] [[package]] @@ -2346,42 +2590,84 @@ version = "0.42.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "597a5118570b68bc08d8d59125332c54f1ba9d9adeedeef5b99b02ba2b0698f8" +[[package]] +name = "windows_aarch64_gnullvm" +version = "0.48.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "91ae572e1b79dba883e0d315474df7305d12f569b400fcf90581b06062f7e1bc" + [[package]] name = "windows_aarch64_msvc" version = "0.42.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e08e8864a60f06ef0d0ff4ba04124db8b0fb3be5776a5cd47641e942e58c4d43" +[[package]] +name = "windows_aarch64_msvc" +version = "0.48.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b2ef27e0d7bdfcfc7b868b317c1d32c641a6fe4629c171b8928c7b08d98d7cf3" + [[package]] name = "windows_i686_gnu" version = "0.42.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c61d927d8da41da96a81f029489353e68739737d3beca43145c8afec9a31a84f" +[[package]] +name = "windows_i686_gnu" +version = "0.48.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "622a1962a7db830d6fd0a69683c80a18fda201879f0f447f065a3b7467daa241" + [[package]] name = "windows_i686_msvc" version = "0.42.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "44d840b6ec649f480a41c8d80f9c65108b92d89345dd94027bfe06ac444d1060" +[[package]] +name = "windows_i686_msvc" +version = "0.48.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4542c6e364ce21bf45d69fdd2a8e455fa38d316158cfd43b3ac1c5b1b19f8e00" + [[package]] name = "windows_x86_64_gnu" version = "0.42.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8de912b8b8feb55c064867cf047dda097f92d51efad5b491dfb98f6bbb70cb36" +[[package]] +name = "windows_x86_64_gnu" +version = "0.48.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ca2b8a661f7628cbd23440e50b05d705db3686f894fc9580820623656af974b1" + [[package]] name = "windows_x86_64_gnullvm" version = "0.42.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "26d41b46a36d453748aedef1486d5c7a85db22e56aff34643984ea85514e94a3" +[[package]] +name = "windows_x86_64_gnullvm" +version = "0.48.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7896dbc1f41e08872e9d5e8f8baa8fdd2677f29468c4e156210174edc7f7b953" + [[package]] name = "windows_x86_64_msvc" version = "0.42.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9aec5da331524158c6d1a4ac0ab1541149c0b9505fde06423b02f5ef0106b9f0" +[[package]] +name = "windows_x86_64_msvc" +version = "0.48.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1a515f5799fe4961cb532f983ce2b23082366b898e52ffbce459c86f67c8378a" + [[package]] name = "x11" version = "2.20.0" diff --git a/crates/eww/Cargo.toml b/crates/eww/Cargo.toml index 25d26f3..35e2ce6 100644 --- a/crates/eww/Cargo.toml +++ b/crates/eww/Cargo.toml @@ -54,6 +54,7 @@ futures-util = "0.3.27" tokio-util = "0.7.7" sysinfo = "0.28.4" +chrono = "0.4.26" wait-timeout = "0.2" diff --git a/crates/eww/src/config/inbuilt.rs b/crates/eww/src/config/inbuilt.rs index fba822d..bca0f22 100644 --- a/crates/eww/src/config/inbuilt.rs +++ b/crates/eww/src/config/inbuilt.rs @@ -10,7 +10,7 @@ use crate::{config::system_stats::*, paths::EwwPaths}; use eww_shared_util::VarName; macro_rules! define_builtin_vars { - ($interval:expr, $($name:literal => $fun:expr),*$(,)?) => { + ($($name:literal => $fun:expr => $interval:expr),*$(,)?) => { pub static INBUILT_VAR_NAMES: &[&'static str] = &[$($name),*]; pub fn get_inbuilt_vars() -> HashMap { maplit::hashmap! { @@ -29,18 +29,18 @@ macro_rules! define_builtin_vars { } } -define_builtin_vars! { Duration::new(2, 0), +define_builtin_vars! { // @desc EWW_TEMPS - Heat of the components in Celcius // @prop { : temperature } - "EWW_TEMPS" => || Ok(DynVal::from(get_temperatures())), + "EWW_TEMPS" => || Ok(DynVal::from(get_temperatures())) => Duration::new(2, 0), // @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())), + "EWW_RAM" => || Ok(DynVal::from(get_ram())) => Duration::new(2, 0), // @desc EWW_DISK - Information on on all mounted partitions (Might report inaccurately on some filesystems, like btrfs and zfs) Example: `{EWW_DISK["/"]}` // @prop { : { name, total, free, used, used_perc } } - "EWW_DISK" => || Ok(DynVal::from(get_disks())), + "EWW_DISK" => || Ok(DynVal::from(get_disks())) => Duration::new(2, 0), // @desc EWW_BATTERY - Battery capacity in procent of the main battery // @prop { : { capacity, status } } @@ -52,15 +52,18 @@ define_builtin_vars! { Duration::new(2, 0), } 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())), + "EWW_CPU" => || Ok(DynVal::from(get_cpus())) => Duration::new(2, 0), // @desc EWW_NET - Bytes up/down on all interfaces // @prop { : { up, down } } - "EWW_NET" => || Ok(DynVal::from(net())), + "EWW_NET" => || Ok(DynVal::from(net())) => Duration::new(2, 0), + + // @desc EWW_TIME - Information on current time in UNIX timestamp + "EWW_TIME" => || Ok(DynVal::from(get_time())) => Duration::new(1, 0), } macro_rules! define_magic_constants { diff --git a/crates/eww/src/config/system_stats.rs b/crates/eww/src/config/system_stats.rs index 3ba426d..37e56cf 100644 --- a/crates/eww/src/config/system_stats.rs +++ b/crates/eww/src/config/system_stats.rs @@ -177,3 +177,7 @@ pub fn net() -> String { ); interfaces } + +pub fn get_time() -> String { + format!("{}", chrono::offset::Utc::now().format("%s")) +} diff --git a/crates/simplexpr/Cargo.toml b/crates/simplexpr/Cargo.toml index edcb5ce..5b8ea39 100644 --- a/crates/simplexpr/Cargo.toml +++ b/crates/simplexpr/Cargo.toml @@ -25,6 +25,8 @@ jaq-core = "0.9.0" jaq-std = {version = "0.9.0", features = ["bincode"]} static_assertions = "1.1.0" cached = "0.42.0" +chrono = "0.4.26" +chrono-tz = "0.8.2" strum = { version = "0.24", features = ["derive"] } diff --git a/crates/simplexpr/src/dynval.rs b/crates/simplexpr/src/dynval.rs index ebfe672..2bdc312 100644 --- a/crates/simplexpr/src/dynval.rs +++ b/crates/simplexpr/src/dynval.rs @@ -168,6 +168,10 @@ impl DynVal { self.0.parse().map_err(|e| ConversionError::new(self.clone(), "i32", e)) } + pub fn as_i64(&self) -> Result { + self.0.parse().map_err(|e| ConversionError::new(self.clone(), "i64", e)) + } + pub fn as_bool(&self) -> Result { self.0.parse().map_err(|e| ConversionError::new(self.clone(), "bool", e)) } diff --git a/crates/simplexpr/src/eval.rs b/crates/simplexpr/src/eval.rs index 76f811d..490119d 100644 --- a/crates/simplexpr/src/eval.rs +++ b/crates/simplexpr/src/eval.rs @@ -1,4 +1,5 @@ use cached::proc_macro::cached; +use chrono::{Local, LocalResult, TimeZone}; use itertools::Itertools; use crate::{ @@ -9,6 +10,7 @@ use eww_shared_util::{Span, Spanned, VarName}; use std::{ collections::HashMap, convert::{TryFrom, TryInto}, + str::FromStr, sync::Arc, }; @@ -57,6 +59,9 @@ pub enum EvalError { #[error("{1}")] Spanned(Span, Box), + + #[error("Error parsing date: {0}")] + ChronoError(String), } static_assertions::assert_impl_all!(EvalError: Send, Sync); @@ -376,6 +381,24 @@ fn call_expr_function(name: &str, args: Vec) -> Result Err(EvalError::WrongArgCount(name.to_string())), }, + "formattime" => match args.as_slice() { + [timestamp, timezone, format] => { + let timezone = match chrono_tz::Tz::from_str(&timezone.as_string()?) { + Ok(x) => x, + Err(_) => return Err(EvalError::ChronoError("Invalid timezone".to_string())), + }; + + Ok(DynVal::from(match timezone.timestamp_opt(timestamp.as_i64()?, 0) { + LocalResult::Single(t) | LocalResult::Ambiguous(t, _) => t.format(&format.as_string()?).to_string(), + LocalResult::None => return Err(EvalError::ChronoError("Invalid UNIX timestamp".to_string())), + })) + } + [timestamp, format] => Ok(DynVal::from(match Local.timestamp_opt(timestamp.as_i64()?, 0) { + LocalResult::Single(t) | LocalResult::Ambiguous(t, _) => t.format(&format.as_string()?).to_string(), + LocalResult::None => return Err(EvalError::ChronoError("Invalid UNIX timestamp".to_string())), + })), + _ => Err(EvalError::WrongArgCount(name.to_string())), + }, _ => Err(EvalError::UnknownFunction(name.to_string())), } diff --git a/docs/src/expression_language.md b/docs/src/expression_language.md index f6a4ecd..d804563 100644 --- a/docs/src/expression_language.md +++ b/docs/src/expression_language.md @@ -47,3 +47,11 @@ 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. + 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. + - `formattime(unix_timestamp, format_str)`: Gets the time in a given format from UNIX timestamp. + Same as other `formattime`, but does not accept timezone. Instead, it uses system's local timezone. + Check [chrono's documentation](https://docs.rs/chrono/latest/chrono/format/strftime/index.html) for more + information about format string. diff --git a/docs/src/magic-vars.md b/docs/src/magic-vars.md index 331642c..aed7637 100644 --- a/docs/src/magic-vars.md +++ b/docs/src/magic-vars.md @@ -2,5 +2,5 @@ These are variables that are always there, without you having to import them. -The delay between the updating variables is always 2s. +The delay between all the updating variables except `EWW_TIME` is 2s, for `EWW_TIME` it is 1s.