diff --git a/Cargo.lock b/Cargo.lock index 30f1735e2..3ade1c645 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -555,6 +555,7 @@ dependencies = [ "libc", "num-integer", "num-traits 0.2.14", + "pure-rust-locales", "time", "winapi 0.3.9", ] @@ -674,6 +675,7 @@ dependencies = [ "anyhow", "bitflags", "bstr 0.2.15", + "chrono", "dirs-next", "filenamegen", "hostname", @@ -2869,6 +2871,12 @@ dependencies = [ "unicase", ] +[[package]] +name = "pure-rust-locales" +version = "0.5.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4f72f6d09f6d70d4e704070c8d6d77e759f9b7f7820609ac0fe7776bbc7ce09d" + [[package]] name = "quick-error" version = "1.2.3" diff --git a/config/Cargo.toml b/config/Cargo.toml index 5a3553cdf..317a60b1c 100644 --- a/config/Cargo.toml +++ b/config/Cargo.toml @@ -17,6 +17,7 @@ pretty_env_logger = "0.4" anyhow = "1.0" bitflags = "1.0" bstr = "0.2" +chrono = {version="0.4", features=["unstable-locales"]} dirs-next = "2.0" filenamegen = "0.2" hostname = "0.3" diff --git a/config/src/lua.rs b/config/src/lua.rs index 66eda6a73..06240b73b 100644 --- a/config/src/lua.rs +++ b/config/src/lua.rs @@ -133,6 +133,7 @@ pub fn make_lua_context(config_dir: &Path) -> anyhow::Result { wezterm_mod.set("emit", lua.create_async_function(emit_event)?)?; wezterm_mod.set("sleep_ms", lua.create_async_function(sleep_ms)?)?; wezterm_mod.set("format", lua.create_function(format)?)?; + wezterm_mod.set("strftime", lua.create_function(strftime)?)?; package.set("path", path_array.join(";"))?; @@ -236,6 +237,12 @@ impl termwiz::render::RenderTty for FormatTarget { } } +fn strftime<'lua>(_: &'lua Lua, format: String) -> mlua::Result { + use chrono::prelude::*; + let local: DateTime = Local::now(); + Ok(local.format(&format).to_string()) +} + fn format<'lua>(_: &'lua Lua, items: Vec) -> mlua::Result { let mut changes: Vec = items.into_iter().map(Into::into).collect(); changes.push(Change::AllAttributes(CellAttributes::default()).into()); diff --git a/docs/config/lua/wezterm/strftime.md b/docs/config/lua/wezterm/strftime.md new file mode 100644 index 000000000..03d14d3b6 --- /dev/null +++ b/docs/config/lua/wezterm/strftime.md @@ -0,0 +1,14 @@ +# `wezterm.strftime(format)` + +*Since: nightly* + +Formats the current local date/time into a string using [the Rust chrono +strftime syntax](https://docs.rs/chrono/0.4.19/chrono/format/strftime/index.html). + +```lua +local wezterm = require 'wezterm'; + +local date_and_time = wezterm.strftime("%Y-%m-%d %H:%M:%S"); +wezterm.log_info(date_and_time); +``` +