Skip to content

Commit

Permalink
Finished updating based on feedback.
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 0274c41 commit bca589c
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 16 deletions.
2 changes: 1 addition & 1 deletion docs/config/lua/wezterm/basename.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ from Rust's [`std::path::PathBuf`](https://doc.rust-lang.org/nightly/std/path/st
local wezterm = require 'wezterm'
local basename = wezterm.basename

wezterm.log_info('baz.txt = ' .. basename '/foo/bar/baz.txt')
wezterm.log_info('baz.txt' == basename '/foo/bar/baz.txt')
```

See also [dirname](dirname.md).
8 changes: 3 additions & 5 deletions docs/config/lua/wezterm/canonical_path.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ local wezterm = require 'wezterm'
local canonical_path = wezterm.canonical_path

wezterm.log_error(
wezterm.home_dir .. ' = ' .. canonical_path(wezterm.home_dir .. '/.')
wezterm.home_dir == canonical_path(wezterm.home_dir .. '/.')
)
```

Expand All @@ -35,10 +35,8 @@ local canonical_path = wezterm.canonical_path
local home_dir = wezterm.home_dir

wezterm.log_error(
home_dir
.. '/Library/CloudStorage/Dropbox'
.. ' = '
.. canonical_path(home_dir .. '/Dropbox')
home_dir .. '/Library/CloudStorage/Dropbox'
== canonical_path(home_dir .. '/Dropbox')
)
```

Expand Down
4 changes: 2 additions & 2 deletions docs/config/lua/wezterm/dirname.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ behave slightly different in some edge case.
local wezterm = require 'wezterm'
local dirname = wezterm.dirname

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

If you want only the directory name and not the full path, you can use
Expand All @@ -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'))
wezterm.log_error('bar' == basename(dirname '/foo/bar/baz.txt'))
```

See also [basename](basename.md).
16 changes: 8 additions & 8 deletions lua-api-crates/filesystem/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,34 +55,34 @@ async 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> {
let path = Path::new(&path);
if let Some(parent_path) = path.parent() {
let path_rs = Path::new(&path);
if let Some(parent_path) = path_rs.parent() {
if let Some(utf8) = parent_path.to_str() {
Ok(utf8.to_string())
} else {
return Err(mlua::Error::external(anyhow!(
"path entry {} is not representable as utf8",
path.display()
path_rs.display()
)));
}
} else {
// parent returns None if the path terminates in a root or prefix
Ok("".to_string())
Ok(path)
}
}

// if path exists return the canonical form of the path with all
// intermediate components normalized and symbolic links resolved
async fn canonical_path<'lua>(_: &'lua Lua, path: String) -> mlua::Result<String> {
let path = smol::fs::canonicalize(&path)
let path_rs = smol::fs::canonicalize(&path)
.await
.map_err(|err| format!(mlua::Error::external("canonical_path('{path}'): {err:#}")))?;
if let Some(utf8) = &path.to_str() {
.map_err(|err| mlua::Error::external(format!("canonical_path('{path}'): {err:#}")))?;
if let Some(utf8) = &path_rs.to_str() {
Ok(utf8.to_string())
} else {
return Err(mlua::Error::external(anyhow!(
"path entry {} is not representable as utf8",
path.display()
path_rs.display()
)));
}
}
Expand Down

0 comments on commit bca589c

Please sign in to comment.