Skip to content

Commit

Permalink
feat: async micro task initialization error handling (#1710)
Browse files Browse the repository at this point in the history
  • Loading branch information
sxyazi authored Oct 1, 2024
1 parent 40211df commit e1773b1
Show file tree
Hide file tree
Showing 21 changed files with 243 additions and 233 deletions.
119 changes: 64 additions & 55 deletions Cargo.lock

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@ crossterm = { version = "0.28.1", features = [ "event-stream" ] }
dirs = "5.0.1"
futures = "0.3.30"
globset = "0.4.15"
libc = "0.2.158"
libc = "0.2.159"
md-5 = "0.10.6"
mlua = { version = "0.9.9", features = [ "lua54", "serialize", "macros", "async" ] }
parking_lot = "0.12.3"
ratatui = { version = "0.28.1", features = [ "unstable-rendered-line-info" ] }
regex = "1.10.6"
regex = "1.11.0"
scopeguard = "1.2.0"
serde = { version = "1.0.210", features = [ "derive" ] }
serde_json = "1.0.128"
Expand Down
2 changes: 1 addition & 1 deletion snap/snapcraft.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ description: |
It aims to provide an efficient, user-friendly, and customizable file management experience.
license: MIT
grade: stable
confinement: strict
confinement: classic

platforms:
amd64:
Expand Down
2 changes: 1 addition & 1 deletion yazi-cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ crossterm = { workspace = true }
md-5 = { workspace = true }
serde_json = { workspace = true }
tokio = { workspace = true }
toml_edit = "0.22.21"
toml_edit = "0.22.22"

[build-dependencies]
# External build dependencies
Expand Down
18 changes: 5 additions & 13 deletions yazi-core/src/manager/commands/bulk_rename.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use tokio::{fs::{self, OpenOptions}, io::{AsyncReadExt, AsyncWriteExt, stdin}};
use yazi_config::{OPEN, PREVIEW};
use yazi_dds::Pubsub;
use yazi_proxy::{AppProxy, HIDER, TasksProxy, WATCHER};
use yazi_shared::{fs::{File, Url, max_common_root, maybe_exists, paths_to_same_file}, terminal_clear};
use yazi_shared::{fs::{File, FilesOp, Url, max_common_root, maybe_exists, paths_to_same_file}, terminal_clear};

use crate::manager::Manager;

Expand All @@ -16,7 +16,6 @@ impl Manager {
return AppProxy::notify_warn("Bulk rename", "No text opener found");
};

let cwd = self.cwd().to_owned();
let old: Vec<_> = self.selected_or_hovered(true).collect();

let root = max_common_root(&old);
Expand All @@ -42,16 +41,11 @@ impl Manager {
AppProxy::stop().await;

let new: Vec<_> = fs::read_to_string(&tmp).await?.lines().map(PathBuf::from).collect();
Self::bulk_rename_do(cwd, root, old, new).await
Self::bulk_rename_do(root, old, new).await
});
}

async fn bulk_rename_do(
cwd: Url,
root: PathBuf,
old: Vec<PathBuf>,
new: Vec<PathBuf>,
) -> Result<()> {
async fn bulk_rename_do(root: PathBuf, old: Vec<PathBuf>, new: Vec<PathBuf>) -> Result<()> {
terminal_clear(&mut stderr())?;
if old.len() != new.len() {
eprintln!("Number of old and new differ, press ENTER to exit");
Expand Down Expand Up @@ -95,11 +89,9 @@ impl Manager {
}
}

// FIXME: consider old and new in the different directories
if !succeeded.is_empty() {
Pubsub::pub_from_bulk(succeeded.iter().map(|(u, f)| (u, &f.url)).collect());
// FIXME 0
// FilesOp::Upserting(cwd, succeeded).emit();
Pubsub::pub_from_bulk(succeeded.iter().map(|(o, n)| (o, &n.url)).collect());
FilesOp::rename(succeeded);
}
drop(permit);

Expand Down
2 changes: 1 addition & 1 deletion yazi-core/src/manager/commands/create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ impl Manager {
fs::create_dir_all(&new).await?;
} else if let Some(real) = realname(&new).await {
ok_or_not_found(fs::remove_file(&new).await)?;
FilesOp::Deleting(parent.clone(), HashSet::from_iter([UrnBuf::_from(real)])).emit();
FilesOp::Deleting(parent.clone(), HashSet::from_iter([UrnBuf::from(real)])).emit();
fs::File::create(&new).await?;
} else {
fs::create_dir_all(&parent).await.ok();
Expand Down
2 changes: 1 addition & 1 deletion yazi-core/src/manager/commands/rename.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ impl Manager {

if let Some(o) = overwritten {
ok_or_not_found(fs::rename(p_new.join(&o), &new).await)?;
FilesOp::Deleting(p_new.clone(), HashSet::from_iter([UrnBuf::_from(o)])).emit();
FilesOp::Deleting(p_new.clone(), HashSet::from_iter([UrnBuf::from(o)])).emit();
}
Pubsub::pub_from_rename(tab, &old, &new);

Expand Down
6 changes: 3 additions & 3 deletions yazi-core/src/manager/commands/update_files.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ impl Manager {

if let Some(f) = tab.parent.as_mut() {
render!(f.update(op.into_owned()));
render!(f.hover(urn._deref()));
render!(f.hover(urn.as_urn()));
}

if leave {
Expand All @@ -81,7 +81,7 @@ impl Manager {
return;
}

tab.current.repos(hovered.as_ref().map(|u| u._deref()));
tab.current.repos(hovered.as_ref().map(|u| u.as_urn()));
if foreign {
return;
}
Expand Down Expand Up @@ -115,7 +115,7 @@ impl Manager {
let folder = tab.history.entry(op.cwd().clone()).or_insert_with(|| Folder::from(op.cwd()));
let hovered = folder.hovered().filter(|_| folder.tracing).map(|h| h.urn_owned());
if folder.update(op.into_owned()) {
folder.repos(hovered.as_ref().map(|u| u._deref()));
folder.repos(hovered.as_ref().map(|u| u.as_urn()));
}

if leave {
Expand Down
2 changes: 1 addition & 1 deletion yazi-core/src/manager/watcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ impl Watcher {

let u = &file.url;
let eq = (!file.is_link() && fs::canonicalize(u).await.is_ok_and(|p| p == ***u))
|| realname_unchecked(u, &mut cached).await.is_ok_and(|s| s == urn._deref()._as_path());
|| realname_unchecked(u, &mut cached).await.is_ok_and(|s| urn.as_urn() == s);

if !eq {
FilesOp::Deleting(parent, HashSet::from_iter([urn])).emit();
Expand Down
4 changes: 2 additions & 2 deletions yazi-core/src/tab/commands/filter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@ impl Tab {
return;
}

self.current.repos(hovered.as_ref().map(|u| u._deref()));
if self.current.hovered().map(|f| f.urn()) != hovered.as_ref().map(|u| u._deref()) {
self.current.repos(hovered.as_ref().map(|u| u.as_urn()));
if self.current.hovered().map(|f| f.urn()) != hovered.as_ref().map(|u| u.as_urn()) {
ManagerProxy::hover(None, self.idx);
}

Expand Down
2 changes: 1 addition & 1 deletion yazi-core/src/tab/tab.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ impl Tab {
f.files.set_sorter(self.conf.sorter());

render!(f.files.catchup_revision());
render!(f.repos(hovered.as_ref().map(|u| u._deref())));
render!(f.repos(hovered.as_ref().map(|u| u.as_urn())));
};

apply(&mut self.current);
Expand Down
6 changes: 3 additions & 3 deletions yazi-fs/src/files.rs
Original file line number Diff line number Diff line change
Expand Up @@ -214,13 +214,13 @@ impl Files {

let (mut hidden, mut items) = if let Some(filter) = &self.filter {
urns.into_iter().partition(|u| {
(!self.show_hidden && u._deref().is_hidden())
|| !u._deref().name().is_some_and(|s| filter.matches(s))
(!self.show_hidden && u.as_urn().is_hidden())
|| !u.as_urn().name().is_some_and(|s| filter.matches(s))
})
} else if self.show_hidden {
(HashSet::new(), urns)
} else {
urns.into_iter().partition(|u| u._deref().is_hidden())
urns.into_iter().partition(|u| u.as_urn().is_hidden())
};

if !items.is_empty() {
Expand Down
24 changes: 0 additions & 24 deletions yazi-plugin/preset/plugins/mime.lua
Original file line number Diff line number Diff line change
Expand Up @@ -59,28 +59,4 @@ function M:fetch()
return j == #urls and 3 or 2
end

-- TODO: remove this after v0.3 release
local notified = ya.sync(function (state)
if state.notified then
return true
else
state.notified = true
return false
end
end)
function M:preload()
if notified() then
return 1
end
ya.notify {
title = "Error",
content = [[In Yazi v0.3, the `mime` plugin has been re-classified as a fetcher. Please remove it from the `preloaders` of your yazi.toml
See https://github.com/sxyazi/yazi/issues/1046 for details.]],
timeout = 20,
level = "error",
}
return 1
end

return M
7 changes: 4 additions & 3 deletions yazi-plugin/src/fs/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,10 @@ pub fn install(lua: &Lua) -> mlua::Result<()> {
(
"unique_name",
lua.create_async_function(|lua, url: UrlRef| async move {
// FIXME: should return a `std::io::Result` to handle errors such as
// permission denied
Url::cast(lua, yazi_shared::fs::unique_name(url.clone()).await)
match yazi_shared::fs::unique_name(url.clone()).await {
Ok(u) => (Url::cast(lua, u)?, Value::Nil).into_lua_multi(lua),
Err(e) => (Value::Nil, e.raw_os_error()).into_lua_multi(lua),
}
})?,
),
])?,
Expand Down
7 changes: 5 additions & 2 deletions yazi-scheduler/src/prework/prework.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,11 @@ impl Prework {
}

let parent = buf[0].0.parent_url().unwrap();
// FIXME 0
// FilesOp::Size(parent, HashMap::from_iter(buf)).emit();
FilesOp::Size(
parent,
HashMap::from_iter(buf.into_iter().map(|(u, s)| (u.urn_owned(), s))),
)
.emit();
});
self.prog.send(TaskProg::Adv(task.id, 1, 0))?;
}
Expand Down
Loading

0 comments on commit e1773b1

Please sign in to comment.