mirror of
https://github.com/sxyazi/yazi.git
synced 2026-07-24 08:11:04 +00:00
30 lines
618 B
Rust
30 lines
618 B
Rust
use std::time::{SystemTime, UNIX_EPOCH};
|
|
|
|
use mlua::{ExternalError, Lua, Table};
|
|
|
|
use super::Utils;
|
|
|
|
impl Utils {
|
|
pub(super) fn time(lua: &Lua, ya: &Table) -> mlua::Result<()> {
|
|
ya.raw_set(
|
|
"time",
|
|
lua.create_function(|_, ()| {
|
|
Ok(SystemTime::now().duration_since(UNIX_EPOCH).map(|d| d.as_secs_f64()).ok())
|
|
})?,
|
|
)?;
|
|
|
|
ya.raw_set(
|
|
"sleep",
|
|
lua.create_async_function(|_, secs: f64| async move {
|
|
if secs < 0.0 {
|
|
return Err("negative sleep duration".into_lua_err());
|
|
}
|
|
|
|
tokio::time::sleep(tokio::time::Duration::from_secs_f64(secs)).await;
|
|
Ok(())
|
|
})?,
|
|
)?;
|
|
|
|
Ok(())
|
|
}
|
|
}
|