Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: show IO errors for files in the UI #1311

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions yazi-core/src/folder/files.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,11 @@ impl Deref for Files {

impl Files {
pub async fn from_dir(url: &Url) -> std::io::Result<UnboundedReceiver<File>> {
let mut it = fs::read_dir(url).await?;
let mut it = fs::read_dir(url).await.map_err(|e| {
Ape marked this conversation as resolved.
Show resolved Hide resolved
FilesOp::IOErr(url.clone(), e.kind(), e.to_string()).emit();
e
})?;

let (tx, rx) = mpsc::unbounded_channel();

tokio::spawn(async move {
Expand Down Expand Up @@ -100,13 +104,18 @@ impl Files {
match fs::metadata(url).await {
Ok(m) if !m.is_dir() => {
// FIXME: use `ErrorKind::NotADirectory` instead once it gets stabilized
FilesOp::IOErr(url.clone(), std::io::ErrorKind::AlreadyExists).emit();
FilesOp::IOErr(
url.clone(),
std::io::ErrorKind::AlreadyExists,
"Not a directory".to_string(),
)
.emit();
}
Ok(m) if mtime == m.modified().ok() => {}
Ok(m) => return Some(m),
Err(e) => {
if maybe_exists(url).await {
FilesOp::IOErr(url.clone(), e.kind()).emit();
FilesOp::IOErr(url.clone(), e.kind(), e.to_string()).emit();
} else if let Some(p) = url.parent_url() {
FilesOp::Deleting(p, vec![url.clone()]).emit();
}
Expand Down
8 changes: 4 additions & 4 deletions yazi-core/src/folder/folder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ impl From<&Url> for Folder {

impl Folder {
pub fn update(&mut self, op: FilesOp) -> bool {
let (stage, revision) = (self.stage, self.files.revision);
let (stage, revision) = (self.stage.clone(), self.files.revision);
match op {
FilesOp::Full(_, _, mtime) => {
(self.mtime, self.stage) = (mtime, FolderStage::Loaded);
Expand All @@ -39,8 +39,8 @@ impl Folder {
FilesOp::Done(_, mtime, ticket) if ticket == self.files.ticket() => {
(self.mtime, self.stage) = (mtime, FolderStage::Loaded);
}
FilesOp::IOErr(_, kind) => {
(self.mtime, self.stage) = (None, FolderStage::Failed(kind));
FilesOp::IOErr(_, kind, ref msg) => {
(self.mtime, self.stage) = (None, FolderStage::Failed(kind, msg.clone()));
}
_ => {}
}
Expand All @@ -59,7 +59,7 @@ impl Folder {
}

self.arrow(0);
(stage, revision) != (self.stage, self.files.revision)
(stage, revision) != (self.stage.clone(), self.files.revision)
}

pub fn arrow(&mut self, step: impl Into<Step>) -> bool {
Expand Down
4 changes: 2 additions & 2 deletions yazi-core/src/folder/stage.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
#[derive(Clone, Debug, Default, Eq, PartialEq)]
pub enum FolderStage {
#[default]
Loading,
Loaded,
Failed(std::io::ErrorKind),
Failed(std::io::ErrorKind, String),
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why don't we generate error messages through std::io::ErrorKind instead of saving them as strings?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There doesn't seem to be any way to do that.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about using the Display trait to do this?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That doesn't give the pretty human readable messages, just the name of the kind.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The error messages for the two cases NotFound and PermissionDenied are "entity not found" and "permission denied", which I think are quite readable. What does the current implementation look like?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The error strings in the current commit are the classic C / POSIX error messages with the os error code, e.g. "No such file or directory (os error 2)" or "Permission denied (os error 13)". I wanted to use these since they seem familiar and are easy to search details for especially when you encounter a rarer type of error that you might not understand.

However, I guess the ErrorKind strings are actually good enough and it makes the implementation simpler.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

However, I guess the ErrorKind strings are actually good enough and it makes the implementation simpler.

Nice, please change it! Let's implement it in the simplest way for now, if someone reports that it's not clear enough, we can create our own messages by ErrorKind.

}
15 changes: 13 additions & 2 deletions yazi-fm/src/lives/folder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ impl Folder {
lua.register_userdata_type::<Self>(|reg| {
reg.add_field_method_get("cwd", |lua, me| Url::cast(lua, me.cwd.clone()));
reg.add_field_method_get("files", |_, me| Files::make(0..me.files.len(), me, me.tab()));
reg.add_field_method_get("stage", |lua, me| lua.create_any_userdata(me.stage));
reg.add_field_method_get("stage", |lua, me| lua.create_any_userdata(me.stage.clone()));
reg.add_field_method_get("window", |_, me| Files::make(me.window.clone(), me, me.tab()));

reg.add_field_method_get("offset", |_, me| Ok(me.offset));
Expand All @@ -53,12 +53,23 @@ impl Folder {
lua.register_userdata_type::<yazi_core::folder::FolderStage>(|reg| {
reg.add_meta_method(MetaMethod::ToString, |lua, me, ()| {
use yazi_core::folder::FolderStage::{Failed, Loaded, Loading};

lua.create_string(match me {
Loading => "loading",
Loaded => "loaded",
Failed(_) => "failed",
Failed(..) => "failed",
})
});

reg.add_field_method_get("error", |lua, me| {
use yazi_core::folder::FolderStage::{Failed, Loaded, Loading};

match me {
Loading => Ok(None),
Loaded => Ok(None),
Failed(_, msg) => Some(lua.create_string(msg)).transpose(),
}
});
})?;

Ok(())
Expand Down
6 changes: 5 additions & 1 deletion yazi-plugin/preset/components/current.lua
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,12 @@ function Current:empty()
local line
if self._folder.files.filter then
line = ui.Line("No filter results")
elseif tostring(self._folder.stage) == "loading" then
Ape marked this conversation as resolved.
Show resolved Hide resolved
line = ui.Line("Loading...")
elseif tostring(self._folder.stage) == "failed" then
line = ui.Line(self._folder.stage.error)
else
line = ui.Line(self._folder.stage == "loading" and "Loading..." or "No items")
line = ui.Line("No items")
end

return {
Expand Down
12 changes: 10 additions & 2 deletions yazi-plugin/preset/plugins/folder.lua
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,17 @@ function M:peek()
end

if #folder.files == 0 then
local line
if tostring(folder.stage) == "loading" then
line = ui.Line("Loading...")
elseif tostring(folder.stage) == "failed" then
line = ui.Line(folder.stage.error)
else
line = ui.Line("No items")
end

return ya.preview_widgets(self, {
ui.Paragraph(self.area, { ui.Line(folder.stage == "loading" and "Loading..." or "No items") })
:align(ui.Paragraph.CENTER),
ui.Paragraph(self.area, { line }):align(ui.Paragraph.CENTER),
})
end

Expand Down
6 changes: 3 additions & 3 deletions yazi-shared/src/fs/op.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ pub enum FilesOp {
Part(Url, Vec<File>, u64),
Done(Url, Option<SystemTime>, u64),
Size(Url, HashMap<Url, u64>),
IOErr(Url, std::io::ErrorKind),
IOErr(Url, std::io::ErrorKind, String),

Creating(Url, Vec<File>),
Deleting(Url, Vec<Url>),
Expand All @@ -27,7 +27,7 @@ impl FilesOp {
Self::Part(url, ..) => url,
Self::Done(url, ..) => url,
Self::Size(url, _) => url,
Self::IOErr(url, _) => url,
Self::IOErr(url, ..) => url,

Self::Creating(url, _) => url,
Self::Deleting(url, _) => url,
Expand Down Expand Up @@ -83,7 +83,7 @@ impl FilesOp {
Self::Part(_, files, ticket) => Self::Part(u, files!(files), *ticket),
Self::Done(_, mtime, ticket) => Self::Done(u, *mtime, *ticket),
Self::Size(_, map) => Self::Size(u, map.iter().map(|(k, v)| (new!(k), *v)).collect()),
Self::IOErr(_, err) => Self::IOErr(u, *err),
Self::IOErr(_, err, msg) => Self::IOErr(u, *err, msg.clone()),

Self::Creating(_, files) => Self::Creating(u, files!(files)),
Self::Deleting(_, urls) => Self::Deleting(u, urls.iter().map(|u| new!(u)).collect()),
Expand Down