mirror of
https://github.com/sxyazi/yazi.git
synced 2026-07-25 08:41:05 +00:00
Simplify the code
This commit is contained in:
parent
b17d130c12
commit
70811f9501
5 changed files with 55 additions and 61 deletions
|
|
@ -1,5 +1,5 @@
|
||||||
use anyhow::Result;
|
use anyhow::Result;
|
||||||
use yazi_macro::succ;
|
use yazi_macro::{act, succ};
|
||||||
use yazi_parser::mgr::SpotOpt;
|
use yazi_parser::mgr::SpotOpt;
|
||||||
use yazi_shared::{data::Data, pool::InternStr};
|
use yazi_shared::{data::Data, pool::InternStr};
|
||||||
|
|
||||||
|
|
@ -13,12 +13,13 @@ impl Actor for Spot {
|
||||||
const NAME: &str = "spot";
|
const NAME: &str = "spot";
|
||||||
|
|
||||||
fn act(cx: &mut Ctx, opt: Self::Options) -> Result<Data> {
|
fn act(cx: &mut Ctx, opt: Self::Options) -> Result<Data> {
|
||||||
|
act!(mgr:escape_visual, cx)?;
|
||||||
let Some(hovered) = cx.hovered().cloned() else { succ!() };
|
let Some(hovered) = cx.hovered().cloned() else { succ!() };
|
||||||
|
|
||||||
let (mime, urls) = if cx.tab().selected.len() >= 2 {
|
let mime = if cx.tab().selected.is_empty() {
|
||||||
("multi/selected".intern(), Some(cx.tab().selected.values().cloned().collect()))
|
cx.mgr.mimetype.owned(&hovered.url).unwrap_or_default()
|
||||||
} else {
|
} else {
|
||||||
(cx.mgr.mimetype.owned(&hovered.url).unwrap_or_default(), None)
|
"multi/unknown".intern()
|
||||||
};
|
};
|
||||||
|
|
||||||
// if !self.active().spot.same_file(&hovered, &mime) {
|
// if !self.active().spot.same_file(&hovered, &mime) {
|
||||||
|
|
@ -31,7 +32,7 @@ impl Actor for Spot {
|
||||||
cx.tab_mut().spot.skip = 0;
|
cx.tab_mut().spot.skip = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
cx.tab_mut().spot.go(hovered, mime, urls);
|
cx.tab_mut().spot.go(hovered, mime);
|
||||||
succ!();
|
succ!();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -105,7 +105,9 @@ fetchers = [
|
||||||
{ id = "mime", url = "remote://*", run = "mime.remote", prio = "high" },
|
{ id = "mime", url = "remote://*", run = "mime.remote", prio = "high" },
|
||||||
]
|
]
|
||||||
spotters = [
|
spotters = [
|
||||||
|
# Multi-file
|
||||||
{ mime = "multi/*", run = "multi" },
|
{ mime = "multi/*", run = "multi" },
|
||||||
|
# Folder
|
||||||
{ url = "*/", run = "folder" },
|
{ url = "*/", run = "folder" },
|
||||||
# Code
|
# Code
|
||||||
{ mime = "text/*", run = "code" },
|
{ mime = "text/*", run = "code" },
|
||||||
|
|
|
||||||
|
|
@ -15,10 +15,10 @@ pub struct Spot {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Spot {
|
impl Spot {
|
||||||
pub fn go(&mut self, file: File, mime: Symbol<str>, urls: Option<Vec<UrlBuf>>) {
|
pub fn go(&mut self, file: File, mime: Symbol<str>) {
|
||||||
if mime.is_empty() {
|
if mime.is_empty() {
|
||||||
return; // Wait till mimetype is resolved to avoid flickering
|
return; // Wait till mimetype is resolved to avoid flickering
|
||||||
} else if urls.is_none() && self.same_lock(&file, &mime) {
|
} else if self.same_lock(&file, &mime) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -27,7 +27,7 @@ impl Spot {
|
||||||
};
|
};
|
||||||
|
|
||||||
self.abort();
|
self.abort();
|
||||||
self.ct = Some(isolate::spot(&spotter.run, file, mime, self.skip, urls));
|
self.ct = Some(isolate::spot(&spotter.run, file, mime, self.skip));
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn visible(&self) -> bool { self.lock.is_some() }
|
pub fn visible(&self) -> bool { self.lock.is_some() }
|
||||||
|
|
|
||||||
|
|
@ -1,61 +1,46 @@
|
||||||
|
--- @sync peek
|
||||||
|
|
||||||
local M = {}
|
local M = {}
|
||||||
|
|
||||||
|
local selected = ya.sync(function()
|
||||||
|
local urls = {}
|
||||||
|
for _, u in pairs(cx.active.selected) do
|
||||||
|
urls[#urls + 1] = u
|
||||||
|
end
|
||||||
|
return urls
|
||||||
|
end)
|
||||||
|
|
||||||
function M:spot(job)
|
function M:spot(job)
|
||||||
self.size = 0
|
self.sum, self.sizes, self.last, self.selected = 0, {}, 0, selected()
|
||||||
self.done = 0
|
self:spot_multi(job, false)
|
||||||
self.total = #job.files
|
|
||||||
self.last = 0
|
for _, u in ipairs(self.selected) do
|
||||||
self:spot_render(job, false)
|
local it, size = fs.calc_size(u), 0
|
||||||
local sizes = {}
|
while true do
|
||||||
for _, url in ipairs(job.files) do
|
local next = it:recv()
|
||||||
local cha = fs.cha(url)
|
if next then
|
||||||
if cha and not cha.is_dir then
|
size, self.sum = size + next, self.sum + next
|
||||||
self.size = self.size + cha.len
|
self:spot_multi(job, false)
|
||||||
self.done = self.done + 1
|
else
|
||||||
self:spot_render(job, false)
|
self.sizes[u], size = size, 0
|
||||||
else
|
break
|
||||||
local it = fs.calc_size(url)
|
|
||||||
local sub = 0
|
|
||||||
while true do
|
|
||||||
local next = it:recv()
|
|
||||||
if next then
|
|
||||||
sub = sub + next
|
|
||||||
self.size = self.size + next
|
|
||||||
self:spot_render(job, false)
|
|
||||||
else
|
|
||||||
break
|
|
||||||
end
|
|
||||||
end
|
|
||||||
self.done = self.done + 1
|
|
||||||
if cha and cha.is_dir then
|
|
||||||
sizes[url.urn] = sub
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
if next(sizes) then
|
self:spot_multi(job, true)
|
||||||
local first = job.files[1]
|
|
||||||
local parent = first.parent
|
|
||||||
if parent then
|
|
||||||
local op = fs.op("size", { url = parent, sizes = sizes })
|
|
||||||
ya.emit("update_files", { op = op })
|
|
||||||
end
|
|
||||||
end
|
|
||||||
self:spot_render(job, true)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
function M:spot_render(job, comp)
|
function M:spot_multi(job, comp)
|
||||||
local now = ya.time()
|
local now = ya.time()
|
||||||
if not comp and now < self.last + 0.1 then
|
if not comp and now < self.last + 0.1 then
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
local progress = string.format("%d/%d", self.done, self.total)
|
|
||||||
local rows = {
|
local rows = {
|
||||||
ui.Row({ "Selected" }):style(ui.Style():fg("green")),
|
ui.Row({ "Multi" }):style(ui.Style():fg("green")),
|
||||||
ui.Row { " Count:", tostring(self.total) },
|
ui.Row { " Count:", string.format("%d items", #self.selected) },
|
||||||
ui.Row { " Size:", ya.readable_size(self.size) .. (comp and "" or " (?)") },
|
ui.Row { " Size:", ya.readable_size(self.sum) .. (comp and "" or " (?)") },
|
||||||
ui.Row { " Progress:", comp and "Done" or progress },
|
|
||||||
}
|
}
|
||||||
|
|
||||||
ya.spot_table(
|
ya.spot_table(
|
||||||
|
|
@ -68,7 +53,21 @@ function M:spot_render(job, comp)
|
||||||
:cell_style(th.spot.tbl_cell)
|
:cell_style(th.spot.tbl_cell)
|
||||||
:widths { ui.Constraint.Length(14), ui.Constraint.Fill(1) }
|
:widths { ui.Constraint.Length(14), ui.Constraint.Fill(1) }
|
||||||
)
|
)
|
||||||
|
|
||||||
|
self:update_sizes()
|
||||||
self.last = now
|
self.last = now
|
||||||
end
|
end
|
||||||
|
|
||||||
|
function M:update_sizes()
|
||||||
|
local parents = {}
|
||||||
|
for url, size in pairs(self.sizes) do
|
||||||
|
local p = url.parent
|
||||||
|
parents[p] = parents[p] or {}
|
||||||
|
parents[p][url.urn] = size
|
||||||
|
end
|
||||||
|
for parent, sizes in pairs(parents) do
|
||||||
|
ya.emit("update_files", { op = fs.op("size", { url = parent, sizes = sizes }) })
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
return M
|
return M
|
||||||
|
|
|
||||||
|
|
@ -2,9 +2,9 @@ use mlua::{ExternalError, ExternalResult, HookTriggers, IntoLua, ObjectLike, VmS
|
||||||
use tokio::{runtime::Handle, select};
|
use tokio::{runtime::Handle, select};
|
||||||
use tokio_util::sync::CancellationToken;
|
use tokio_util::sync::CancellationToken;
|
||||||
use tracing::error;
|
use tracing::error;
|
||||||
use yazi_binding::{File, Id, Url};
|
use yazi_binding::{File, Id};
|
||||||
use yazi_dds::Sendable;
|
use yazi_dds::Sendable;
|
||||||
use yazi_shared::{Ids, event::Action, pool::Symbol, url::UrlBuf};
|
use yazi_shared::{Ids, event::Action, pool::Symbol};
|
||||||
|
|
||||||
use super::slim_lua;
|
use super::slim_lua;
|
||||||
use crate::loader::LOADER;
|
use crate::loader::LOADER;
|
||||||
|
|
@ -16,7 +16,6 @@ pub fn spot(
|
||||||
file: yazi_fs::File,
|
file: yazi_fs::File,
|
||||||
mime: Symbol<str>,
|
mime: Symbol<str>,
|
||||||
skip: usize,
|
skip: usize,
|
||||||
urls: Option<Vec<UrlBuf>>,
|
|
||||||
) -> CancellationToken {
|
) -> CancellationToken {
|
||||||
let ct = CancellationToken::new();
|
let ct = CancellationToken::new();
|
||||||
let (ct1, ct2) = (ct.clone(), ct.clone());
|
let (ct1, ct2) = (ct.clone(), ct.clone());
|
||||||
|
|
@ -45,13 +44,6 @@ pub fn spot(
|
||||||
("mime", mime.into_lua(&lua)?),
|
("mime", mime.into_lua(&lua)?),
|
||||||
("skip", skip.into_lua(&lua)?),
|
("skip", skip.into_lua(&lua)?),
|
||||||
])?;
|
])?;
|
||||||
if let Some(urls) = urls {
|
|
||||||
let files = lua.create_table_with_capacity(urls.len(), 0)?;
|
|
||||||
for (i, url) in urls.into_iter().enumerate() {
|
|
||||||
files.raw_set(i + 1, Url::new(url))?;
|
|
||||||
}
|
|
||||||
job.raw_set("files", files)?;
|
|
||||||
}
|
|
||||||
|
|
||||||
if ct2.is_cancelled() { Ok(()) } else { plugin.call_async_method("spot", job).await }
|
if ct2.is_cancelled() { Ok(()) } else { plugin.call_async_method("spot", job).await }
|
||||||
};
|
};
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue