1
1
mirror of https://github.com/wez/wezterm.git synced 2024-11-27 02:25:28 +03:00

lua: add wezterm.strftime

refs: https://github.com/wez/wezterm/issues/500
This commit is contained in:
Wez Furlong 2021-03-02 09:51:56 -08:00
parent 99a0b044a0
commit ff8bda29e3
4 changed files with 30 additions and 0 deletions

8
Cargo.lock generated
View File

@ -555,6 +555,7 @@ dependencies = [
"libc", "libc",
"num-integer", "num-integer",
"num-traits 0.2.14", "num-traits 0.2.14",
"pure-rust-locales",
"time", "time",
"winapi 0.3.9", "winapi 0.3.9",
] ]
@ -674,6 +675,7 @@ dependencies = [
"anyhow", "anyhow",
"bitflags", "bitflags",
"bstr 0.2.15", "bstr 0.2.15",
"chrono",
"dirs-next", "dirs-next",
"filenamegen", "filenamegen",
"hostname", "hostname",
@ -2869,6 +2871,12 @@ dependencies = [
"unicase", "unicase",
] ]
[[package]]
name = "pure-rust-locales"
version = "0.5.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4f72f6d09f6d70d4e704070c8d6d77e759f9b7f7820609ac0fe7776bbc7ce09d"
[[package]] [[package]]
name = "quick-error" name = "quick-error"
version = "1.2.3" version = "1.2.3"

View File

@ -17,6 +17,7 @@ pretty_env_logger = "0.4"
anyhow = "1.0" anyhow = "1.0"
bitflags = "1.0" bitflags = "1.0"
bstr = "0.2" bstr = "0.2"
chrono = {version="0.4", features=["unstable-locales"]}
dirs-next = "2.0" dirs-next = "2.0"
filenamegen = "0.2" filenamegen = "0.2"
hostname = "0.3" hostname = "0.3"

View File

@ -133,6 +133,7 @@ pub fn make_lua_context(config_dir: &Path) -> anyhow::Result<Lua> {
wezterm_mod.set("emit", lua.create_async_function(emit_event)?)?; wezterm_mod.set("emit", lua.create_async_function(emit_event)?)?;
wezterm_mod.set("sleep_ms", lua.create_async_function(sleep_ms)?)?; wezterm_mod.set("sleep_ms", lua.create_async_function(sleep_ms)?)?;
wezterm_mod.set("format", lua.create_function(format)?)?; wezterm_mod.set("format", lua.create_function(format)?)?;
wezterm_mod.set("strftime", lua.create_function(strftime)?)?;
package.set("path", path_array.join(";"))?; 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<String> {
use chrono::prelude::*;
let local: DateTime<Local> = Local::now();
Ok(local.format(&format).to_string())
}
fn format<'lua>(_: &'lua Lua, items: Vec<FormatItem>) -> mlua::Result<String> { fn format<'lua>(_: &'lua Lua, items: Vec<FormatItem>) -> mlua::Result<String> {
let mut changes: Vec<Change> = items.into_iter().map(Into::into).collect(); let mut changes: Vec<Change> = items.into_iter().map(Into::into).collect();
changes.push(Change::AllAttributes(CellAttributes::default()).into()); changes.push(Change::AllAttributes(CellAttributes::default()).into());

View File

@ -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);
```