Skip to content

Commit

Permalink
Added assert + async fixes and added a note.
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel Kongsgaard authored and Daniel Kongsgaard committed Sep 24, 2023
1 parent 4e12e0e commit d511d1e
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 8 deletions.
6 changes: 4 additions & 2 deletions docs/config/lua/wezterm/canonical_path.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,15 @@ Due to limitations in the lua bindings, all of the paths
must be able to be represented as UTF-8 or this function will generate an
error.

Note: This function is similar to the shell commands realpath and readlink.

The function can for example be used get the correct absolute path for a path
in a different format.
```lua
local wezterm = require 'wezterm'
local canonical_path = wezterm.canonical_path

wezterm.log_error(
assert(
wezterm.home_dir == canonical_path(wezterm.home_dir .. '/.')
)
```
Expand All @@ -34,7 +36,7 @@ local wezterm = require 'wezterm'
local canonical_path = wezterm.canonical_path
local home_dir = wezterm.home_dir

wezterm.log_error(
assert(
home_dir .. '/Library/CloudStorage/Dropbox'
== canonical_path(home_dir .. '/Dropbox')
)
Expand Down
2 changes: 1 addition & 1 deletion docs/config/lua/wezterm/dirname.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ local wezterm = require 'wezterm'
local basename = wezterm.basename
local dirname = wezterm.dirname

wezterm.log_error('bar' == basename(dirname '/foo/bar/baz.txt'))
assert('bar' == basename(dirname '/foo/bar/baz.txt'))
```

See also [basename](basename.md).
9 changes: 4 additions & 5 deletions lua-api-crates/filesystem/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ use std::path::Path;
pub fn register(lua: &Lua) -> anyhow::Result<()> {
let wezterm_mod = get_or_create_module(lua, "wezterm")?;
wezterm_mod.set("read_dir", lua.create_async_function(read_dir)?)?;
wezterm_mod.set("basename", lua.create_async_function(basename)?)?;
wezterm_mod.set("dirname", lua.create_async_function(dirname)?)?;
wezterm_mod.set("basename", lua.create_function(basename)?)?;
wezterm_mod.set("dirname", lua.create_function(dirname)?)?;
wezterm_mod.set("canonical_path", lua.create_async_function(canonical_path)?)?;
wezterm_mod.set("glob", lua.create_async_function(glob)?)?;
Ok(())
Expand Down Expand Up @@ -53,7 +53,7 @@ fn basename<'lua>(_: &'lua Lua, path: String) -> mlua::Result<String> {

// return the path without its final component if there is one
// similar to the shell command dirname
async fn dirname<'lua>(_: &'lua Lua, path: String) -> mlua::Result<String> {
fn dirname<'lua>(_: &'lua Lua, path: String) -> mlua::Result<String> {
let path_rs = Path::new(&path);
if let Some(parent_path) = path_rs.parent() {
if let Some(utf8) = parent_path.to_str() {
Expand All @@ -79,8 +79,7 @@ async fn canonical_path<'lua>(_: &'lua Lua, path: String) -> mlua::Result<String
Ok(utf8.to_string())
} else {
return Err(mlua::Error::external(anyhow!(
"path entry {} is not representable as utf8",
&path
"path entry {path} is not representable as utf8"
)));
}
}
Expand Down

0 comments on commit d511d1e

Please sign in to comment.