refactor: ensure that each Url contains the full location information (#1659)

This commit is contained in:
三咲雅 · Misaki Masa 2024-09-20 22:21:32 +08:00 committed by GitHub
parent 7222e178a3
commit e858d11ce0
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
48 changed files with 301 additions and 289 deletions

View file

@ -7,9 +7,9 @@
keymap = [ keymap = [
{ on = "<Esc>", run = "escape", desc = "Exit visual mode, clear selected, or cancel search" }, { on = "<Esc>", run = "escape", desc = "Exit visual mode, clear selected, or cancel search" },
{ on = "<C-[>", run = "escape", desc = "Exit visual mode, clear selected, or cancel search" }, { on = "<C-[>", run = "escape", desc = "Exit visual mode, clear selected, or cancel search" },
{ on = "q", run = "quit", desc = "Exit the process" }, { on = "q", run = "quit", desc = "Quit the process" },
{ on = "Q", run = "quit --no-cwd-file", desc = "Exit the process without writing cwd-file" }, { on = "Q", run = "quit --no-cwd-file", desc = "Quit the process without outputting cwd-file" },
{ on = "<C-c>", run = "close", desc = "Close the current tab, or quit if it is last tab" }, { on = "<C-c>", run = "close", desc = "Close the current tab, or quit if it's last" },
{ on = "<C-z>", run = "suspend", desc = "Suspend the process" }, { on = "<C-z>", run = "suspend", desc = "Suspend the process" },
# Hopping # Hopping
@ -74,19 +74,19 @@ keymap = [
{ on = ";", run = "shell --interactive", desc = "Run a shell command" }, { on = ";", run = "shell --interactive", desc = "Run a shell command" },
{ on = ":", run = "shell --block --interactive", desc = "Run a shell command (block until finishes)" }, { on = ":", run = "shell --block --interactive", desc = "Run a shell command (block until finishes)" },
{ on = ".", run = "hidden toggle", desc = "Toggle the visibility of hidden files" }, { on = ".", run = "hidden toggle", desc = "Toggle the visibility of hidden files" },
{ on = "s", run = "search fd", desc = "Search files by name using fd" }, { on = "s", run = "search fd", desc = "Search files by name via fd" },
{ on = "S", run = "search rg", desc = "Search files by content using ripgrep" }, { on = "S", run = "search rg", desc = "Search files by content via ripgrep" },
{ on = "<C-s>", run = "escape --search", desc = "Cancel the ongoing search" }, { on = "<C-s>", run = "escape --search", desc = "Cancel the ongoing search" },
{ on = "z", run = "plugin zoxide", desc = "Jump to a directory using zoxide" }, { on = "z", run = "plugin zoxide", desc = "Jump to a directory via zoxide" },
{ on = "Z", run = "plugin fzf", desc = "Jump to a directory or reveal a file using fzf" }, { on = "Z", run = "plugin fzf", desc = "Jump to a file/directory via fzf" },
# Linemode # Linemode
{ on = [ "m", "s" ], run = "linemode size", desc = "Set linemode to size" }, { on = [ "m", "s" ], run = "linemode size", desc = "Linemode: size" },
{ on = [ "m", "p" ], run = "linemode permissions", desc = "Set linemode to permissions" }, { on = [ "m", "p" ], run = "linemode permissions", desc = "Linemode: permissions" },
{ on = [ "m", "c" ], run = "linemode ctime", desc = "Set linemode to ctime" }, { on = [ "m", "c" ], run = "linemode ctime", desc = "Linemode: ctime" },
{ on = [ "m", "m" ], run = "linemode mtime", desc = "Set linemode to mtime" }, { on = [ "m", "m" ], run = "linemode mtime", desc = "Linemode: mtime" },
{ on = [ "m", "o" ], run = "linemode owner", desc = "Set linemode to owner" }, { on = [ "m", "o" ], run = "linemode owner", desc = "Linemode: owner" },
{ on = [ "m", "n" ], run = "linemode none", desc = "Set linemode to none" }, { on = [ "m", "n" ], run = "linemode none", desc = "Linemode: none" },
# Copy # Copy
{ on = [ "c", "c" ], run = "copy path", desc = "Copy the file path" }, { on = [ "c", "c" ], run = "copy path", desc = "Copy the file path" },
@ -119,10 +119,10 @@ keymap = [
{ on = [ ",", "r" ], run = "sort random --reverse=no", desc = "Sort randomly" }, { on = [ ",", "r" ], run = "sort random --reverse=no", desc = "Sort randomly" },
# Goto # Goto
{ on = [ "g", "h" ], run = "cd ~", desc = "Go to the home directory" }, { on = [ "g", "h" ], run = "cd ~", desc = "Go home" },
{ on = [ "g", "c" ], run = "cd ~/.config", desc = "Go to the config directory" }, { on = [ "g", "c" ], run = "cd ~/.config", desc = "Go to ~/.config" },
{ on = [ "g", "d" ], run = "cd ~/Downloads", desc = "Go to the downloads directory" }, { on = [ "g", "d" ], run = "cd ~/Downloads", desc = "Go to ~/Downloads" },
{ on = [ "g", "<Space>" ], run = "cd --interactive", desc = "Go to a directory interactively" }, { on = [ "g", "<Space>" ], run = "cd --interactive", desc = "Jump interactively" },
# Tabs # Tabs
{ on = "t", run = "tab_create --current", desc = "Create a new tab with CWD" }, { on = "t", run = "tab_create --current", desc = "Create a new tab with CWD" },
@ -309,7 +309,6 @@ keymap = [
keymap = [ keymap = [
{ on = "<Esc>", run = "escape", desc = "Clear the filter, or hide the help" }, { on = "<Esc>", run = "escape", desc = "Clear the filter, or hide the help" },
{ on = "<C-[>", run = "escape", desc = "Clear the filter, or hide the help" }, { on = "<C-[>", run = "escape", desc = "Clear the filter, or hide the help" },
{ on = "q", run = "close", desc = "Exit the process" },
{ on = "<C-c>", run = "close", desc = "Hide the help" }, { on = "<C-c>", run = "close", desc = "Hide the help" },
# Navigation # Navigation

View file

@ -18,7 +18,7 @@ impl Filetype {
} }
self.mime.as_ref().is_some_and(|p| p.match_mime(mime)) self.mime.as_ref().is_some_and(|p| p.match_mime(mime))
|| self.name.as_ref().is_some_and(|n| n.match_path(file.url(), file.is_dir())) || self.name.as_ref().is_some_and(|n| n.match_path(&file.url, file.is_dir()))
} }
} }

View file

@ -43,7 +43,7 @@ impl Icons {
#[inline] #[inline]
fn match_by_glob(&self, file: &File) -> Option<&Icon> { fn match_by_glob(&self, file: &File) -> Option<&Icon> {
self.globs.iter().find(|(p, _)| p.match_path(file.url(), file.is_dir())).map(|(_, i)| i) self.globs.iter().find(|(p, _)| p.match_path(&file.url, file.is_dir())).map(|(_, i)| i)
} }
#[inline] #[inline]
@ -62,7 +62,7 @@ impl Icons {
#[inline] #[inline]
fn match_by_ext(&self, file: &File) -> Option<&Icon> { fn match_by_ext(&self, file: &File) -> Option<&Icon> {
let ext = file.url().extension()?.to_str()?; let ext = file.url.extension()?.to_str()?;
self.exts.get(ext).or_else(|| self.exts.get(&ext.to_ascii_lowercase())) self.exts.get(ext).or_else(|| self.exts.get(&ext.to_ascii_lowercase()))
} }
} }

View file

@ -16,7 +16,7 @@ impl Manager {
return AppProxy::notify_warn("Bulk rename", "No text opener found"); return AppProxy::notify_warn("Bulk rename", "No text opener found");
}; };
let cwd = self.cwd().url_owned(); let cwd = self.cwd().to_owned();
let old: Vec<_> = self.selected_or_hovered(true).collect(); let old: Vec<_> = self.selected_or_hovered(true).collect();
let root = max_common_root(&old); let root = max_common_root(&old);
@ -97,7 +97,7 @@ impl Manager {
// FIXME: consider old and new in the different directories // FIXME: consider old and new in the different directories
if !succeeded.is_empty() { if !succeeded.is_empty() {
Pubsub::pub_from_bulk(succeeded.iter().map(|(u, f)| (u, f.url())).collect()); Pubsub::pub_from_bulk(succeeded.iter().map(|(u, f)| (u, &f.url)).collect());
// FIXME 0 // FIXME 0
// FilesOp::Upserting(cwd, succeeded).emit(); // FilesOp::Upserting(cwd, succeeded).emit();
} }

View file

@ -37,28 +37,28 @@ impl Manager {
// Refresh watcher // Refresh watcher
let mut to_watch = HashSet::with_capacity(3 * self.tabs.len()); let mut to_watch = HashSet::with_capacity(3 * self.tabs.len());
for tab in self.tabs.iter() { for tab in self.tabs.iter() {
to_watch.insert(tab.cwd().url()); to_watch.insert(tab.cwd());
if let Some(ref p) = tab.parent { if let Some(ref p) = tab.parent {
to_watch.insert(&p.loc); to_watch.insert(&p.url);
} }
if let Some(h) = tab.current.hovered().filter(|&h| h.is_dir()) { if let Some(h) = tab.current.hovered().filter(|&h| h.is_dir()) {
to_watch.insert(h.url()); to_watch.insert(&h.url);
} }
} }
self.watcher.watch(to_watch); self.watcher.watch(to_watch);
// Publish through DDS // Publish through DDS
Pubsub::pub_from_hover(self.active().idx, self.hovered().map(|h| h.url())); Pubsub::pub_from_hover(self.active().idx, self.hovered().map(|h| &h.url));
} }
fn hover_do(&mut self, url: Url, tab: Option<usize>) { fn hover_do(&mut self, url: Url, tab: Option<usize>) {
// Hover on the file // Hover on the file
if let Some(p) = url.strip_prefix(&self.current_or(tab).loc).map(PathBuf::from) { if let Ok(p) = url.strip_prefix(&self.current_or(tab).url).map(PathBuf::from) {
render!(self.current_or_mut(tab).repos(Some(Urn::new(&p)))); render!(self.current_or_mut(tab).repos(Some(Urn::new(&p))));
} }
// Turn on tracing // Turn on tracing
if self.current_or(tab).hovered().is_some_and(|f| url == *f.url()) { if self.current_or(tab).hovered().is_some_and(|f| url == f.url) {
// `hover(Some)` occurs after user actions, such as create, rename, reveal, etc. // `hover(Some)` occurs after user actions, such as create, rename, reveal, etc.
// At this point, it's intuitive to track the location of this file regardless. // At this point, it's intuitive to track the location of this file regardless.
self.current_or_mut(tab).tracing = true; self.current_or_mut(tab).tracing = true;

View file

@ -110,9 +110,8 @@ impl Manager {
}; };
let find = |folder: Option<&Folder>| { let find = |folder: Option<&Folder>| {
folder.filter(|&f| p == *f.loc).is_some_and(|folder| { folder.is_some_and(|folder| {
let loc = url.to_loc(&folder.loc); p == folder.url && folder.files.iter().any(|f| f.is_dir() && f.urn() == url.urn())
folder.files.iter().any(|f| f.is_dir() && f.urn() == loc.urn())
}) })
}; };

View file

@ -31,13 +31,13 @@ impl Manager {
}; };
let folder = self.active().hovered_folder().map(|f| (f.offset, f.cha)); let folder = self.active().hovered_folder().map(|f| (f.offset, f.cha));
if !self.active().preview.same_url(hovered.url()) { if !self.active().preview.same_url(&hovered.url) {
self.active_mut().preview.skip = folder.map(|f| f.0).unwrap_or_default(); self.active_mut().preview.skip = folder.map(|f| f.0).unwrap_or_default();
render!(self.active_mut().preview.reset()); render!(self.active_mut().preview.reset());
} }
let opt = opt.into() as Opt; let opt = opt.into() as Opt;
if matches!(opt.only_if, Some(ref u) if u != hovered.url()) { if matches!(opt.only_if, Some(u) if u != hovered.url) {
return; return;
} }
@ -55,7 +55,7 @@ impl Manager {
return; return;
} }
let mime = self.mimetype.get_owned(hovered.url()).unwrap_or_default(); let mime = self.mimetype.get_owned(&hovered.url).unwrap_or_default();
if !mime.is_empty() { if !mime.is_empty() {
// Wait till mimetype is resolved to avoid flickering // Wait till mimetype is resolved to avoid flickering
self.active_mut().preview.go(hovered, &mime, opt.force); self.active_mut().preview.go(hovered, &mime, opt.force);

View file

@ -9,7 +9,7 @@ use crate::{manager::Manager, tasks::Tasks};
impl Manager { impl Manager {
pub fn refresh(&mut self, _: Cmd, tasks: &Tasks) { pub fn refresh(&mut self, _: Cmd, tasks: &Tasks) {
env::set_current_dir(self.cwd()).ok(); env::set_current_dir(self.cwd()).ok();
env::set_var("PWD", self.cwd().url()); env::set_var("PWD", self.cwd());
if !MANAGER.title_format.is_empty() { if !MANAGER.title_format.is_empty() {
execute!(std::io::stderr(), SetTitle(self.title())).ok(); execute!(std::io::stderr(), SetTitle(self.title())).ok();
@ -31,7 +31,7 @@ impl Manager {
fn title(&self) -> String { fn title(&self) -> String {
let home = dirs::home_dir().unwrap_or_default(); let home = dirs::home_dir().unwrap_or_default();
let cwd = if let Some(p) = self.cwd().strip_prefix(home) { let cwd = if let Ok(p) = self.cwd().strip_prefix(home) {
format!("~{}{}", MAIN_SEPARATOR, p.display()) format!("~{}{}", MAIN_SEPARATOR, p.display())
} else { } else {
format!("{}", self.cwd().display()) format!("{}", self.cwd().display())

View file

@ -27,7 +27,7 @@ impl Manager {
if !self.active_mut().try_escape_visual() { if !self.active_mut().try_escape_visual() {
return; return;
} }
let Some(hovered) = self.hovered().map(|h| h.url()) else { let Some(hovered) = self.hovered().map(|h| &h.url) else {
return; return;
}; };

View file

@ -21,13 +21,13 @@ impl Manager {
let mime = if hovered.is_dir() { let mime = if hovered.is_dir() {
MIME_DIR MIME_DIR
} else if let Some(s) = self.mimetype.get(hovered.url()) { } else if let Some(s) = self.mimetype.get(&hovered.url) {
s s
} else { } else {
return render!(self.active_mut().preview.reset()); return render!(self.active_mut().preview.reset());
}; };
let Some(previewer) = PLUGIN.previewer(hovered.url(), mime) else { let Some(previewer) = PLUGIN.previewer(&hovered.url, mime) else {
return render!(self.active_mut().preview.reset()); return render!(self.active_mut().preview.reset());
}; };

View file

@ -46,7 +46,7 @@ impl Tabs {
} else { } else {
tab.conf = self.active().conf.clone(); tab.conf = self.active().conf.clone();
tab.apply_files_attrs(); tab.apply_files_attrs();
tab.cd(self.active().cwd().url_owned()); tab.cd(self.active().cwd().to_owned());
} }
self.items.insert(self.cursor + 1, tab); self.items.insert(self.cursor + 1, tab);

View file

@ -47,11 +47,11 @@ impl Manager {
let url = op.cwd(); let url = op.cwd();
tab.selected.apply_op(&op); tab.selected.apply_op(&op);
if url == tab.cwd().url() { if url == tab.cwd() {
Self::update_current(tab, op, tasks); Self::update_current(tab, op, tasks);
} else if matches!(&tab.parent, Some(p) if url == &*p.loc) { } else if matches!(&tab.parent, Some(p) if *url == p.url) {
Self::update_parent(tab, op); Self::update_parent(tab, op);
} else if matches!(tab.current.hovered(), Some(h) if url == h.url()) { } else if matches!(tab.current.hovered(), Some(h) if *url == h.url) {
Self::update_hovered(tab, op); Self::update_hovered(tab, op);
} else { } else {
Self::update_history(tab, op); Self::update_history(tab, op);
@ -108,7 +108,7 @@ impl Manager {
} }
fn update_history(tab: &mut Tab, op: Cow<FilesOp>) { fn update_history(tab: &mut Tab, op: Cow<FilesOp>) {
let leave = tab.parent.as_ref().and_then(|f| f.loc.parent_url().map(|p| (p, f.loc.urn()))).is_some_and( let leave = tab.parent.as_ref().and_then(|f| f.url.parent_url().map(|p| (p, f.url.urn()))).is_some_and(
|(p, n)| matches!(*op, FilesOp::Deleting(ref parent, ref urns) if *parent == p && urns.contains(n)), |(p, n)| matches!(*op, FilesOp::Deleting(ref parent, ref urns) if *parent == p && urns.contains(n)),
); );

View file

@ -46,11 +46,11 @@ impl Manager {
.current() .current()
.paginate(self.current().page) .paginate(self.current().page)
.iter() .iter()
.filter(|&f| updates.contains_key(f.url())) .filter(|&f| updates.contains_key(&f.url))
.cloned() .cloned()
.collect(); .collect();
let repeek = self.hovered().is_some_and(|f| updates.contains_key(f.url())); let repeek = self.hovered().is_some_and(|f| updates.contains_key(&f.url));
self.mimetype.extend(updates); self.mimetype.extend(updates);
if repeek { if repeek {

View file

@ -27,7 +27,7 @@ impl Manager {
return; return;
}; };
if opt.only_if.is_some_and(|u| u != *self.cwd().url()) { if opt.only_if.is_some_and(|u| u != *self.cwd()) {
return; return;
} }

View file

@ -2,7 +2,7 @@ use ratatui::layout::Rect;
use yazi_adapter::Dimension; use yazi_adapter::Dimension;
use yazi_config::popup::{Origin, Position}; use yazi_config::popup::{Origin, Position};
use yazi_fs::Folder; use yazi_fs::Folder;
use yazi_shared::fs::{File, Loc, Url}; use yazi_shared::fs::{File, Url};
use super::{Mimetype, Tabs, Watcher, Yanked}; use super::{Mimetype, Tabs, Watcher, Yanked};
use crate::tab::Tab; use crate::tab::Tab;
@ -39,7 +39,7 @@ impl Manager {
impl Manager { impl Manager {
#[inline] #[inline]
pub fn cwd(&self) -> &Loc { self.active().cwd() } pub fn cwd(&self) -> &Url { self.active().cwd() }
#[inline] #[inline]
pub fn active(&self) -> &Tab { self.tabs.active() } pub fn active(&self) -> &Tab { self.tabs.active() }

View file

@ -61,7 +61,7 @@ impl Watcher {
pub(super) fn trigger_dirs(&self, folders: &[&Folder]) { pub(super) fn trigger_dirs(&self, folders: &[&Folder]) {
let todo: Vec<_> = let todo: Vec<_> =
folders.iter().filter(|&f| f.loc.is_regular()).map(|&f| (f.loc.url_owned(), f.cha)).collect(); folders.iter().filter(|&f| f.url.is_regular()).map(|&f| (f.url.to_owned(), f.cha)).collect();
if todo.is_empty() { if todo.is_empty() {
return; return;
} }
@ -131,8 +131,8 @@ impl Watcher {
continue; continue;
}; };
let u = file.url(); let u = &file.url;
let eq = (!file.is_link() && fs::canonicalize(u).await.is_ok_and(|p| p == **u)) 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| s == urn._deref()._as_path());
if !eq { if !eq {
@ -168,7 +168,7 @@ impl Watcher {
for from in todo { for from in todo {
let Ok(to) = fs::canonicalize(&from).await else { continue }; let Ok(to) = fs::canonicalize(&from).await else { continue };
if to != *from && WATCHED.read().contains(&from) { if to != **from && WATCHED.read().contains(&from) {
LINKED.write().insert(from, Url::from(to)); LINKED.write().insert(from, Url::from(to));
} }
} }

View file

@ -18,7 +18,7 @@ impl From<Cmd> for Opt {
fn from(mut c: Cmd) -> Self { fn from(mut c: Cmd) -> Self {
let mut target = c.take_first().and_then(Data::into_url).unwrap_or_default(); let mut target = c.take_first().and_then(Data::into_url).unwrap_or_default();
if target.is_regular() { if target.is_regular() {
target.set_path(expand_path(&target)) target = Url::from(expand_path(&target));
} }
Self { target, interactive: c.bool("interactive") } Self { target, interactive: c.bool("interactive") }
@ -39,20 +39,20 @@ impl Tab {
return self.cd_interactive(); return self.cd_interactive();
} }
if opt.target == *self.cwd().url() { if opt.target == *self.cwd() {
return; return;
} }
// Take parent to history // Take parent to history
if let Some(rep) = self.parent.take() { if let Some(rep) = self.parent.take() {
self.history.insert(rep.loc.url_owned(), rep); self.history.insert(rep.url.to_owned(), rep);
} }
// Current // Current
let rep = self.history.remove_or(&opt.target); let rep = self.history.remove_or(&opt.target);
let rep = mem::replace(&mut self.current, rep); let rep = mem::replace(&mut self.current, rep);
if rep.loc.is_regular() { if rep.url.is_regular() {
self.history.insert(rep.loc.url_owned(), rep); self.history.insert(rep.url.to_owned(), rep);
} }
// Parent // Parent

View file

@ -106,7 +106,7 @@ impl Tab {
render!(); render!();
let urls: Vec<_> = let urls: Vec<_> =
indices.into_iter().filter_map(|i| self.current.files.get(i)).map(|f| f.url()).collect(); indices.into_iter().filter_map(|i| self.current.files.get(i)).map(|f| &f.url).collect();
let same = !self.cwd().is_search(); let same = !self.cwd().is_search();
if !select { if !select {

View file

@ -14,7 +14,7 @@ impl Tab {
let hovered = self.current.hovered().map(|f| f.url_owned()); let hovered = self.current.hovered().map(|f| f.url_owned());
self.apply_files_attrs(); self.apply_files_attrs();
if hovered.as_ref() != self.current.hovered().map(|f| f.url()) { if hovered.as_ref() != self.current.hovered().map(|f| &f.url) {
ManagerProxy::hover(hovered, self.idx); ManagerProxy::hover(hovered, self.idx);
} else if self.current.hovered().is_some_and(|f| f.is_dir()) { } else if self.current.hovered().is_some_and(|f| f.is_dir()) {
ManagerProxy::peek(true); ManagerProxy::peek(true);

View file

@ -16,7 +16,7 @@ impl Tab {
.current .current
.hovered() .hovered()
.and_then(|h| h.parent()) .and_then(|h| h.parent())
.filter(|u| u != self.cwd().url()) .filter(|u| u != self.cwd())
.or_else(|| self.cwd().parent_url()) .or_else(|| self.cwd().parent_url())
.map(|u| self.cd(u)); .map(|u| self.cd(u));
} }

View file

@ -17,7 +17,7 @@ impl TryFrom<Cmd> for Opt {
impl Tab { impl Tab {
pub fn preview(&mut self, opt: impl TryInto<Opt>) { pub fn preview(&mut self, opt: impl TryInto<Opt>) {
let Some(hovered) = self.current.hovered().map(|h| h.url()) else { let Some(hovered) = self.current.hovered().map(|h| &h.url) else {
return render!(self.preview.reset()); return render!(self.preview.reset());
}; };

View file

@ -11,7 +11,7 @@ impl From<Cmd> for Opt {
fn from(mut c: Cmd) -> Self { fn from(mut c: Cmd) -> Self {
let mut target = c.take_first().and_then(Data::into_url).unwrap_or_default(); let mut target = c.take_first().and_then(Data::into_url).unwrap_or_default();
if target.is_regular() { if target.is_regular() {
target.set_path(expand_path(&target)) target = Url::from(expand_path(&target));
} }
Self { target } Self { target }

View file

@ -26,7 +26,7 @@ impl<'a> From<Cmd> for Opt<'a> {
impl<'a> Tab { impl<'a> Tab {
pub fn select(&mut self, opt: impl Into<Opt<'a>>) { pub fn select(&mut self, opt: impl Into<Opt<'a>>) {
let opt = opt.into() as Opt; let opt = opt.into() as Opt;
let Some(url) = opt.url.or_else(|| self.current.hovered().map(|h| Cow::Borrowed(h.url()))) let Some(url) = opt.url.or_else(|| self.current.hovered().map(|h| Cow::Borrowed(&h.url)))
else { else {
return; return;
}; };

View file

@ -24,7 +24,7 @@ impl From<Option<bool>> for Opt {
impl Tab { impl Tab {
pub fn select_all(&mut self, opt: impl Into<Opt>) { pub fn select_all(&mut self, opt: impl Into<Opt>) {
let iter = self.current.files.iter().map(|f| f.url()); let iter = self.current.files.iter().map(|f| &f.url);
let (removal, addition): (Vec<_>, Vec<_>) = match opt.into().state { let (removal, addition): (Vec<_>, Vec<_>) = match opt.into().state {
Some(true) => (vec![], iter.collect()), Some(true) => (vec![], iter.collect()),
Some(false) => (iter.collect(), vec![]), Some(false) => (iter.collect(), vec![]),

View file

@ -20,11 +20,11 @@ pub struct Preview {
impl Preview { impl Preview {
pub fn go(&mut self, file: File, mime: &str, force: bool) { pub fn go(&mut self, file: File, mime: &str, force: bool) {
if !force && self.content_unchanged(file.url(), file.cha) { if !force && self.content_unchanged(&file.url, file.cha) {
return; return;
} }
let Some(previewer) = PLUGIN.previewer(file.url(), mime) else { let Some(previewer) = PLUGIN.previewer(&file.url, mime) else {
self.reset(); self.reset();
return; return;
}; };

View file

@ -6,7 +6,7 @@ use tokio::task::JoinHandle;
use yazi_adapter::Dimension; use yazi_adapter::Dimension;
use yazi_config::{popup::{Origin, Position}, LAYOUT}; use yazi_config::{popup::{Origin, Position}, LAYOUT};
use yazi_fs::{Folder, FolderStage}; use yazi_fs::{Folder, FolderStage};
use yazi_shared::{fs::{Loc, Url}, render}; use yazi_shared::{fs::Url, render};
use super::{Backstack, Config, Finder, History, Mode, Preview}; use super::{Backstack, Config, Finder, History, Mode, Preview};
use crate::tab::Selected; use crate::tab::Selected;
@ -39,7 +39,7 @@ impl Tab {
impl Tab { impl Tab {
// --- Current // --- Current
#[inline] #[inline]
pub fn cwd(&self) -> &Loc { &self.current.loc } pub fn cwd(&self) -> &Url { &self.current.url }
pub fn hovered_rect(&self) -> Option<Rect> { pub fn hovered_rect(&self) -> Option<Rect> {
let y = self.current.files.position(self.current.hovered()?.urn())? - self.current.offset; let y = self.current.files.position(self.current.hovered()?.urn())? - self.current.offset;
@ -61,7 +61,7 @@ impl Tab {
pub fn selected_or_hovered(&self, reorder: bool) -> Box<dyn Iterator<Item = &Url> + '_> { pub fn selected_or_hovered(&self, reorder: bool) -> Box<dyn Iterator<Item = &Url> + '_> {
if self.selected.is_empty() { if self.selected.is_empty() {
Box::new(self.current.hovered().map(|h| vec![h.url()]).unwrap_or_default().into_iter()) Box::new(self.current.hovered().map(|h| vec![&h.url]).unwrap_or_default().into_iter())
} else if !reorder { } else if !reorder {
Box::new(self.selected.keys()) Box::new(self.selected.keys())
} else { } else {
@ -75,20 +75,20 @@ impl Tab {
let Some(h) = self.current.hovered() else { return Box::new(iter::empty()) }; let Some(h) = self.current.hovered() else { return Box::new(iter::empty()) };
if self.selected.is_empty() { if self.selected.is_empty() {
Box::new([h.url(), h.url()].into_iter()) Box::new([&h.url, &h.url].into_iter())
} else if !reorder { } else if !reorder {
Box::new([h.url()].into_iter().chain(self.selected.keys())) Box::new([&h.url].into_iter().chain(self.selected.keys()))
} else { } else {
let mut vec: Vec<_> = self.selected.iter().collect(); let mut vec: Vec<_> = self.selected.iter().collect();
vec.sort_unstable_by(|a, b| a.1.cmp(b.1)); vec.sort_unstable_by(|a, b| a.1.cmp(b.1));
Box::new([h.url()].into_iter().chain(vec.into_iter().map(|(k, _)| k))) Box::new([&h.url].into_iter().chain(vec.into_iter().map(|(k, _)| k)))
} }
} }
// --- History // --- History
#[inline] #[inline]
pub fn hovered_folder(&self) -> Option<&Folder> { pub fn hovered_folder(&self) -> Option<&Folder> {
self.current.hovered().filter(|&h| h.is_dir()).and_then(|h| self.history.get(h.url())) self.current.hovered().filter(|&h| h.is_dir()).and_then(|h| self.history.get(&h.url))
} }
pub fn apply_files_attrs(&mut self) { pub fn apply_files_attrs(&mut self) {
@ -111,15 +111,15 @@ impl Tab {
apply(parent); apply(parent);
// The parent should always track the CWD // The parent should always track the CWD
parent.hover(self.current.loc.urn()); parent.hover(self.current.url.urn());
parent.tracing = parent.hovered().map(|h| h.url()) == Some(&self.current.loc); parent.tracing = parent.hovered().map(|h| &h.url) == Some(&self.current.url);
} }
self self
.current .current
.hovered() .hovered()
.filter(|h| h.is_dir()) .filter(|h| h.is_dir())
.and_then(|h| self.history.get_mut(h.url())) .and_then(|h| self.history.get_mut(&h.url))
.map(apply); .map(apply);
} }
} }

View file

@ -7,7 +7,7 @@ impl Tasks {
if let Ok(opt) = opt.try_into() { if let Ok(opt) = opt.try_into() {
self.process_from_opener( self.process_from_opener(
opt.opener, opt.opener,
opt.targets.into_iter().map(|u| u.into_os_string()).collect(), opt.targets.into_iter().map(|u| u.into_path().into_os_string()).collect(),
); );
} }
} }

View file

@ -10,14 +10,14 @@ impl Tasks {
let mut loaded = self.scheduler.prework.loaded.lock(); let mut loaded = self.scheduler.prework.loaded.lock();
let mut tasks: [Vec<_>; MAX_PREWORKERS as usize] = Default::default(); let mut tasks: [Vec<_>; MAX_PREWORKERS as usize] = Default::default();
for f in paged { for f in paged {
let mime = if f.is_dir() { MIME_DIR } else { mimetype.get(f.url()).unwrap_or_default() }; let mime = if f.is_dir() { MIME_DIR } else { mimetype.get(&f.url).unwrap_or_default() };
let factors = |s: &str| match s { let factors = |s: &str| match s {
"mime" => !mime.is_empty(), "mime" => !mime.is_empty(),
_ => false, _ => false,
}; };
for p in PLUGIN.fetchers(f.url(), mime, factors) { for p in PLUGIN.fetchers(&f.url, mime, factors) {
match loaded.get_mut(f.url()) { match loaded.get_mut(&f.url) {
Some(n) if *n & (1 << p.idx) != 0 => continue, Some(n) if *n & (1 << p.idx) != 0 => continue,
Some(n) => *n |= 1 << p.idx, Some(n) => *n |= 1 << p.idx,
None => _ = loaded.insert(f.url_owned(), 1 << p.idx), None => _ = loaded.insert(f.url_owned(), 1 << p.idx),
@ -37,9 +37,9 @@ impl Tasks {
pub fn preload_paged(&self, paged: &[File], mimetype: &Mimetype) { pub fn preload_paged(&self, paged: &[File], mimetype: &Mimetype) {
let mut loaded = self.scheduler.prework.loaded.lock(); let mut loaded = self.scheduler.prework.loaded.lock();
for f in paged { for f in paged {
let mime = if f.is_dir() { MIME_DIR } else { mimetype.get(f.url()).unwrap_or_default() }; let mime = if f.is_dir() { MIME_DIR } else { mimetype.get(&f.url).unwrap_or_default() };
for p in PLUGIN.preloaders(f.url(), mime) { for p in PLUGIN.preloaders(&f.url, mime) {
match loaded.get_mut(f.url()) { match loaded.get_mut(&f.url) {
Some(n) if *n & (1 << p.idx) != 0 => continue, Some(n) if *n & (1 << p.idx) != 0 => continue,
Some(n) => *n |= 1 << p.idx, Some(n) => *n |= 1 << p.idx,
None => _ = loaded.insert(f.url_owned(), 1 << p.idx), None => _ = loaded.insert(f.url_owned(), 1 << p.idx),
@ -54,7 +54,7 @@ impl Tasks {
{ {
let mut loaded = self.scheduler.prework.loaded.lock(); let mut loaded = self.scheduler.prework.loaded.lock();
for f in affected { for f in affected {
loaded.get_mut(f.url()).map(|n| *n &= mask); loaded.get_mut(&f.url).map(|n| *n &= mask);
} }
} }
@ -71,10 +71,8 @@ impl Tasks {
let loading = self.scheduler.prework.size_loading.read(); let loading = self.scheduler.prework.size_loading.read();
targets targets
.iter() .iter()
.filter(|f| { .filter(|f| f.is_dir() && !targets.sizes.contains_key(f.urn()) && !loading.contains(&f.url))
f.is_dir() && !targets.sizes.contains_key(f.urn()) && !loading.contains(f.url()) .map(|f| &f.url)
})
.map(|f| f.url())
.collect() .collect()
}; };
if targets.is_empty() { if targets.is_empty() {

View file

@ -16,7 +16,7 @@ impl Tasks {
for (opener, args) in openers { for (opener, args) in openers {
self.process_from_opener( self.process_from_opener(
Cow::Borrowed(opener), Cow::Borrowed(opener),
args.into_iter().map(|u| u.into_os_string()).collect(), args.into_iter().map(|u| u.into_path().into_os_string()).collect(),
); );
} }
} }

View file

@ -43,31 +43,28 @@ impl File {
}); });
reg.add_method("mime", |lua, me, ()| { reg.add_method("mime", |lua, me, ()| {
let cx = lua.named_registry_value::<CtxRef>("cx")?; let cx = lua.named_registry_value::<CtxRef>("cx")?;
Ok(cx.manager.mimetype.get_owned(me.url())) Ok(cx.manager.mimetype.get_owned(&me.url))
}); });
reg.add_method("prefix", |lua, me, ()| { reg.add_method("prefix", |lua, me, ()| {
if !me.folder().loc.is_search() { if !me.folder().url.is_search() {
return Ok(None); return Ok(None);
} }
let mut p = me.url().strip_prefix(&me.folder().loc).unwrap_or(me.url()).components(); let mut p = me.url.strip_prefix(&me.folder().url).unwrap_or(&me.url).components();
p.next_back(); p.next_back();
Some(lua.create_string(p.as_path().as_os_str().as_encoded_bytes())).transpose() Some(lua.create_string(p.as_path().as_os_str().as_encoded_bytes())).transpose()
}); });
reg.add_method("style", |lua, me, ()| { reg.add_method("style", |lua, me, ()| {
let cx = lua.named_registry_value::<CtxRef>("cx")?; let cx = lua.named_registry_value::<CtxRef>("cx")?;
let mime = if me.is_dir() { let mime =
MIME_DIR if me.is_dir() { MIME_DIR } else { cx.manager.mimetype.get(&me.url).unwrap_or_default() };
} else {
cx.manager.mimetype.get(me.url()).unwrap_or_default()
};
Ok(THEME.filetypes.iter().find(|&x| x.matches(me, mime)).map(|x| Style::from(x.style))) Ok(THEME.filetypes.iter().find(|&x| x.matches(me, mime)).map(|x| Style::from(x.style)))
}); });
reg.add_method("is_hovered", |_, me, ()| Ok(me.idx == me.folder().cursor)); reg.add_method("is_hovered", |_, me, ()| Ok(me.idx == me.folder().cursor));
reg.add_method("is_yanked", |lua, me, ()| { reg.add_method("is_yanked", |lua, me, ()| {
let cx = lua.named_registry_value::<CtxRef>("cx")?; let cx = lua.named_registry_value::<CtxRef>("cx")?;
Ok(if !cx.manager.yanked.contains(me.url()) { Ok(if !cx.manager.yanked.contains(&me.url) {
0u8 0u8
} else if cx.manager.yanked.cut { } else if cx.manager.yanked.cut {
2u8 2u8
@ -77,7 +74,7 @@ impl File {
}); });
reg.add_method("is_marked", |_, me, ()| { reg.add_method("is_marked", |_, me, ()| {
use yazi_core::tab::Mode::*; use yazi_core::tab::Mode::*;
if !me.tab().mode.is_visual() || me.folder().loc != me.tab().current.loc { if !me.tab().mode.is_visual() || me.folder().url != me.tab().current.url {
return Ok(0u8); return Ok(0u8);
} }
@ -87,13 +84,13 @@ impl File {
_ => 0u8, _ => 0u8,
}) })
}); });
reg.add_method("is_selected", |_, me, ()| Ok(me.tab().selected.contains_key(me.url()))); reg.add_method("is_selected", |_, me, ()| Ok(me.tab().selected.contains_key(&me.url)));
reg.add_method("in_parent", |_, me, ()| { reg.add_method("in_parent", |_, me, ()| {
Ok(me.tab().parent.as_ref().is_some_and(|f| me.folder().loc == f.loc)) Ok(me.tab().parent.as_ref().is_some_and(|f| me.folder().url == f.url))
}); });
reg.add_method("in_current", |_, me, ()| Ok(me.folder().loc == me.tab().current.loc)); reg.add_method("in_current", |_, me, ()| Ok(me.folder().url == me.tab().current.url));
reg.add_method("in_preview", |_, me, ()| { reg.add_method("in_preview", |_, me, ()| {
Ok(me.tab().current.hovered().is_some_and(|f| f.url() == &*me.folder().loc)) Ok(me.tab().current.hovered().is_some_and(|f| f.url == me.folder().url))
}); });
reg.add_method("found", |lua, me, ()| { reg.add_method("found", |lua, me, ()| {
let cx = lua.named_registry_value::<CtxRef>("cx")?; let cx = lua.named_registry_value::<CtxRef>("cx")?;
@ -101,7 +98,7 @@ impl File {
return Ok(None); return Ok(None);
}; };
let Some(idx) = finder.matched_idx(me.url()) else { let Some(idx) = finder.matched_idx(&me.url) else {
return Ok(None); return Ok(None);
}; };
@ -113,7 +110,7 @@ impl File {
let Some(finder) = &cx.manager.active().finder else { let Some(finder) = &cx.manager.active().finder else {
return Ok(None); return Ok(None);
}; };
if me.folder().loc != me.tab().current.loc { if me.folder().url != me.tab().current.url {
return Ok(None); return Ok(None);
} }
let Some(h) = finder.filter.highlighted(me.name()) else { let Some(h) = finder.filter.highlighted(me.name()) else {

View file

@ -38,7 +38,7 @@ impl Folder {
pub(super) fn register(lua: &Lua) -> mlua::Result<()> { pub(super) fn register(lua: &Lua) -> mlua::Result<()> {
lua.register_userdata_type::<Self>(|reg| { lua.register_userdata_type::<Self>(|reg| {
reg.add_field_method_get("cwd", |lua, me| Url::cast(lua, me.loc.url_owned())); reg.add_field_method_get("cwd", |lua, me| Url::cast(lua, me.url.to_owned()));
reg.add_field_method_get("files", |_, me| Files::make(0..me.files.len(), me, me.tab())); 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));
reg.add_field_method_get("window", |_, me| Files::make(me.window.clone(), me, me.tab())); reg.add_field_method_get("window", |_, me| Files::make(me.window.clone(), me, me.tab()));

View file

@ -24,7 +24,7 @@ impl Tab {
pub(super) fn register(lua: &Lua) -> mlua::Result<()> { pub(super) fn register(lua: &Lua) -> mlua::Result<()> {
lua.register_userdata_type::<Self>(|reg| { lua.register_userdata_type::<Self>(|reg| {
reg.add_method("name", |lua, me, ()| { reg.add_method("name", |lua, me, ()| {
lua.create_string(me.current.loc.name().as_encoded_bytes()) lua.create_string(me.current.url.name().as_encoded_bytes())
}); });
reg.add_field_method_get("mode", |_, me| Mode::make(&me.mode)); reg.add_field_method_get("mode", |_, me| Mode::make(&me.mode));

View file

@ -176,7 +176,8 @@ impl Files {
($dist:expr, $src:expr, $inc:literal) => { ($dist:expr, $src:expr, $inc:literal) => {
let mut todo: HashMap<_, _> = $src.into_iter().map(|f| (f.url_owned(), f)).collect(); let mut todo: HashMap<_, _> = $src.into_iter().map(|f| (f.url_owned(), f)).collect();
for f in &$dist { for f in &$dist {
if todo.remove(f.url()).is_some() && todo.is_empty() { // TODO: use urn instead
if todo.remove(&f.url).is_some() && todo.is_empty() {
break; break;
} }
} }

View file

@ -2,14 +2,14 @@ use std::mem;
use yazi_config::{LAYOUT, MANAGER}; use yazi_config::{LAYOUT, MANAGER};
use yazi_proxy::ManagerProxy; use yazi_proxy::ManagerProxy;
use yazi_shared::fs::{Cha, File, FilesOp, Loc, Url, Urn}; use yazi_shared::fs::{Cha, File, FilesOp, Url, Urn};
use super::FolderStage; use super::FolderStage;
use crate::{Files, Step}; use crate::{Files, Step};
#[derive(Default)] #[derive(Default)]
pub struct Folder { pub struct Folder {
pub loc: Loc, pub url: Url,
pub cha: Cha, pub cha: Cha,
pub files: Files, pub files: Files,
pub stage: FolderStage, pub stage: FolderStage,
@ -22,7 +22,7 @@ pub struct Folder {
} }
impl From<&Url> for Folder { impl From<&Url> for Folder {
fn from(cwd: &Url) -> Self { Self { loc: Loc::from(cwd.clone()), ..Default::default() } } fn from(url: &Url) -> Self { Self { url: url.clone(), ..Default::default() } }
} }
impl Folder { impl Folder {
@ -101,7 +101,7 @@ impl Folder {
let new = self.cursor / limit; let new = self.cursor / limit;
if mem::replace(&mut self.page, new) != new || force { if mem::replace(&mut self.page, new) != new || force {
ManagerProxy::update_paged_by(new, &self.loc); ManagerProxy::update_paged_by(new, &self.url);
} }
} }

View file

@ -38,11 +38,11 @@ impl FilesSorter {
}), }),
SortBy::Extension => items.sort_unstable_by(|a, b| { SortBy::Extension => items.sort_unstable_by(|a, b| {
let ord = if self.sensitive { let ord = if self.sensitive {
self.cmp(a.url().extension(), b.url().extension(), self.promote(a, b)) self.cmp(a.url.extension(), b.url.extension(), self.promote(a, b))
} else { } else {
self.cmp( self.cmp(
a.url().extension().map(|s| s.to_ascii_lowercase()), a.url.extension().map(|s| s.to_ascii_lowercase()),
b.url().extension().map(|s| s.to_ascii_lowercase()), b.url.extension().map(|s| s.to_ascii_lowercase()),
self.promote(a, b), self.promote(a, b),
) )
}; };

View file

@ -62,6 +62,7 @@ impl Padding {
} }
pub fn register(lua: &Lua) -> mlua::Result<()> { pub fn register(lua: &Lua) -> mlua::Result<()> {
// TODO: remove this
lua.register_userdata_type::<ratatui::widgets::Padding>(|reg| { lua.register_userdata_type::<ratatui::widgets::Padding>(|reg| {
reg.add_field_method_get("left", |_, me| Ok(me.left)); reg.add_field_method_get("left", |_, me| Ok(me.left));
reg.add_field_method_get("right", |_, me| Ok(me.right)); reg.add_field_method_get("right", |_, me| Ok(me.right));

View file

@ -24,6 +24,7 @@ impl Position {
} }
pub fn register(lua: &Lua) -> mlua::Result<()> { pub fn register(lua: &Lua) -> mlua::Result<()> {
// TODO: remove this
lua.register_userdata_type::<ratatui::layout::Position>(|reg| { lua.register_userdata_type::<ratatui::layout::Position>(|reg| {
reg.add_field_method_get("x", |_, me| Ok(me.x)); reg.add_field_method_get("x", |_, me| Ok(me.x));
reg.add_field_method_get("y", |_, me| Ok(me.y)); reg.add_field_method_get("y", |_, me| Ok(me.y));

View file

@ -27,6 +27,7 @@ impl Rect {
} }
pub fn register(lua: &Lua) -> mlua::Result<()> { pub fn register(lua: &Lua) -> mlua::Result<()> {
// TODO: remove this
lua.register_userdata_type::<ratatui::layout::Rect>(|reg| { lua.register_userdata_type::<ratatui::layout::Rect>(|reg| {
reg.add_field_method_get("x", |_, me| Ok(me.x)); reg.add_field_method_get("x", |_, me| Ok(me.x));
reg.add_field_method_get("y", |_, me| Ok(me.y)); reg.add_field_method_get("y", |_, me| Ok(me.y));

View file

@ -29,7 +29,7 @@ pub fn fd(opt: FdOpt) -> Result<UnboundedReceiver<File>> {
tokio::spawn(async move { tokio::spawn(async move {
while let Ok(Some(line)) = it.next_line().await { while let Ok(Some(line)) = it.next_line().await {
if let Ok(file) = File::from_search_item(&opt.cwd, opt.cwd.join(line)).await { if let Ok(file) = File::from(opt.cwd.join(line)).await {
tx.send(file).ok(); tx.send(file).ok();
} }
} }

View file

@ -28,7 +28,7 @@ pub fn rg(opt: RgOpt) -> Result<UnboundedReceiver<File>> {
tokio::spawn(async move { tokio::spawn(async move {
while let Ok(Some(line)) = it.next_line().await { while let Ok(Some(line)) = it.next_line().await {
if let Ok(file) = File::from_search_item(&opt.cwd, opt.cwd.join(line)).await { if let Ok(file) = File::from(opt.cwd.join(line)).await {
tx.send(file).ok(); tx.send(file).ok();
} }
} }

View file

@ -1,6 +1,5 @@
use mlua::{AnyUserData, Lua, Table, UserDataFields, UserDataMethods, UserDataRef, UserDataRegistry}; use mlua::{AnyUserData, Lua, Table, UserDataFields, UserDataMethods, UserDataRef, UserDataRegistry};
use yazi_config::THEME; use yazi_config::THEME;
use yazi_shared::fs::Loc;
use crate::{bindings::{Cast, Icon}, cha::Cha, url::Url}; use crate::{bindings::{Cast, Icon}, cha::Cha, url::Url};
@ -52,7 +51,7 @@ impl File {
"File", "File",
lua.create_function(|lua, t: Table| { lua.create_function(|lua, t: Table| {
Self::cast(lua, yazi_shared::fs::File { Self::cast(lua, yazi_shared::fs::File {
loc: Loc::from(t.raw_get::<_, AnyUserData>("url")?.take()?), url: t.raw_get::<_, AnyUserData>("url")?.take()?,
cha: t.raw_get::<_, AnyUserData>("cha")?.take()?, cha: t.raw_get::<_, AnyUserData>("cha")?.take()?,
..Default::default() ..Default::default()
}) })

View file

@ -55,7 +55,7 @@ impl Url {
Value::UserData(ud) => me.strip_prefix(&*ud.borrow::<yazi_shared::fs::Url>()?), Value::UserData(ud) => me.strip_prefix(&*ud.borrow::<yazi_shared::fs::Url>()?),
_ => Err("must be a string or a Url".into_lua_err())?, _ => Err("must be a string or a Url".into_lua_err())?,
}; };
path.map(|p| Self::cast(lua, yazi_shared::fs::Url::from(p))).transpose() path.ok().map(|p| Self::cast(lua, yazi_shared::fs::Url::from(p))).transpose()
}); });
reg.add_meta_method(MetaMethod::Eq, |_, me, other: UrlRef| Ok(me == &*other)); reg.add_meta_method(MetaMethod::Eq, |_, me, other: UrlRef| Ok(me == &*other));

View file

@ -11,12 +11,12 @@ impl Utils {
"file_cache", "file_cache",
lua.create_function(|lua, t: Table| { lua.create_function(|lua, t: Table| {
let file: FileRef = t.raw_get("file")?; let file: FileRef = t.raw_get("file")?;
if file.url().parent() == Some(&PREVIEW.cache_dir) { if file.url.parent() == Some(&PREVIEW.cache_dir) {
return Ok(None); return Ok(None);
} }
let hex = { let hex = {
let mut digest = Md5::new_with_prefix(file.url().as_os_str().as_encoded_bytes()); let mut digest = Md5::new_with_prefix(file.url.as_os_str().as_encoded_bytes());
digest.update(format!("//{:?}//{}", file.cha.mtime, t.raw_get("skip").unwrap_or(0))); digest.update(format!("//{:?}//{}", file.cha.mtime, t.raw_get("skip").unwrap_or(0)));
format!("{:x}", digest.finalize()) format!("{:x}", digest.finalize())
}; };

View file

@ -3,12 +3,12 @@ use std::{cell::Cell, ffi::OsStr, fs::{FileType, Metadata}, ops::Deref};
use anyhow::Result; use anyhow::Result;
use tokio::fs; use tokio::fs;
use super::{Loc, Urn, UrnBuf}; use super::{Urn, UrnBuf};
use crate::{fs::{Cha, ChaKind, Url}, theme::IconCache}; use crate::{fs::{Cha, ChaKind, Url}, theme::IconCache};
#[derive(Clone, Debug, Default)] #[derive(Clone, Debug, Default)]
pub struct File { pub struct File {
pub loc: Loc, pub url: Url,
pub cha: Cha, pub cha: Cha,
pub link_to: Option<Url>, pub link_to: Option<Url>,
pub icon: Cell<IconCache>, pub icon: Cell<IconCache>,
@ -34,44 +34,13 @@ impl File {
} }
#[inline] #[inline]
pub async fn from_search_item(cwd: &Url, url: Url) -> Result<Self> { pub async fn from_meta(url: Url, mut meta: Metadata) -> Self {
let loc = Loc::from_search_item(cwd, url);
let meta = fs::symlink_metadata(loc.url()).await?;
Ok(Self::from_loc(loc, meta).await)
}
#[inline]
pub async fn from_meta(url: Url, meta: Metadata) -> Self {
Self::from_loc(Loc::from(url), meta).await
}
#[inline]
pub fn from_dummy(url: Url, ft: Option<FileType>) -> Self {
Self {
loc: Loc::from(url),
cha: ft.map_or_else(Cha::dummy, Cha::from),
link_to: None,
icon: Default::default(),
}
}
#[inline]
pub fn rebase(&self, parent: &Url) -> Self {
Self {
loc: self.loc.rebase(parent),
cha: self.cha,
link_to: self.link_to.clone(),
icon: Default::default(),
}
}
async fn from_loc(loc: Loc, mut meta: Metadata) -> Self {
let mut ck = ChaKind::empty(); let mut ck = ChaKind::empty();
let (is_link, mut link_to) = (meta.is_symlink(), None); let (is_link, mut link_to) = (meta.is_symlink(), None);
if is_link { if is_link {
meta = fs::metadata(loc.url()).await.unwrap_or(meta); meta = fs::metadata(&url).await.unwrap_or(meta);
link_to = fs::read_link(loc.url()).await.map(Url::from).ok(); link_to = fs::read_link(&url).await.map(Url::from).ok();
} }
if is_link && meta.is_symlink() { if is_link && meta.is_symlink() {
@ -81,7 +50,7 @@ impl File {
} }
#[cfg(unix)] #[cfg(unix)]
if loc.urn().is_hidden() { if url.is_hidden() {
ck |= ChaKind::HIDDEN; ck |= ChaKind::HIDDEN;
} }
#[cfg(windows)] #[cfg(windows)]
@ -92,30 +61,47 @@ impl File {
} }
} }
Self { loc, cha: Cha::from(meta).with_kind(ck), link_to, icon: Default::default() } Self { url, cha: Cha::from(meta).with_kind(ck), link_to, icon: Default::default() }
}
#[inline]
pub fn from_dummy(url: Url, ft: Option<FileType>) -> Self {
Self {
url,
cha: ft.map_or_else(Cha::dummy, Cha::from),
link_to: None,
icon: Default::default(),
}
}
#[inline]
pub fn rebase(&self, parent: &Url) -> Self {
Self {
url: self.url.rebase(parent),
cha: self.cha,
link_to: self.link_to.clone(),
icon: Default::default(),
}
} }
} }
impl File { impl File {
// --- Loc // --- Url
#[inline] #[inline]
pub fn url(&self) -> &Url { self.loc.url() } pub fn url_owned(&self) -> Url { self.url.to_owned() }
#[inline] #[inline]
pub fn url_owned(&self) -> Url { self.loc.url_owned() } pub fn urn(&self) -> &Urn { self.url.urn() }
#[inline] #[inline]
pub fn urn(&self) -> &Urn { self.loc.urn() } pub fn urn_owned(&self) -> UrnBuf { self.url.urn_owned() }
#[inline] #[inline]
pub fn urn_owned(&self) -> UrnBuf { self.loc.urn_owned() } pub fn name(&self) -> &OsStr { self.url.name() }
#[inline] #[inline]
pub fn name(&self) -> &OsStr { self.loc.name() } pub fn stem(&self) -> Option<&OsStr> { self.url.file_stem() }
#[inline] #[inline]
pub fn stem(&self) -> Option<&OsStr> { self.url().file_stem() } pub fn parent(&self) -> Option<Url> { self.url.parent_url() }
#[inline]
pub fn parent(&self) -> Option<Url> { self.url().parent_url() }
} }

View file

@ -1,50 +1,60 @@
use std::{ffi::OsStr, fmt::{self, Debug, Formatter}, ops::Deref, path::Path}; use std::{cmp, ffi::OsStr, fmt::{self, Debug, Formatter}, hash::{Hash, Hasher}, ops::Deref, path::{Path, PathBuf}};
use super::{Url, Urn, UrnBuf}; use super::{Urn, UrnBuf};
pub struct Loc { pub struct Loc {
url: Url, path: PathBuf,
urn: *const OsStr, urn: *const OsStr,
name: *const OsStr, name: *const OsStr,
} }
unsafe impl Send for Loc {} unsafe impl Send for Loc {}
unsafe impl Sync for Loc {}
impl Default for Loc { impl Default for Loc {
fn default() -> Self { Self { url: Url::default(), urn: OsStr::new(""), name: OsStr::new("") } } fn default() -> Self {
} Self { path: PathBuf::default(), urn: OsStr::new(""), name: OsStr::new("") }
impl Deref for Loc {
type Target = Url;
fn deref(&self) -> &Self::Target { &self.url }
}
impl AsRef<Path> for Loc {
fn as_ref(&self) -> &Path { self.url() }
}
impl Eq for Loc {}
impl PartialEq for Loc {
fn eq(&self, other: &Self) -> bool {
self.url == other.url && self.urn() == other.urn() && self.name() == other.name()
} }
} }
impl Clone for Loc { impl Clone for Loc {
fn clone(&self) -> Self { fn clone(&self) -> Self {
let url = self.url.clone(); let path = self.path.clone();
let name = url.file_name().unwrap_or(OsStr::new("")) as *const OsStr; let name = path.file_name().unwrap_or(OsStr::new("")) as *const OsStr;
let urn = if url.is_search_item() { self.twin_urn(&url) } else { name }; let urn = if self.urn()._as_path() == self.name() { name } else { self.twin_urn(&path) };
Self { url, urn, name } Self { path, urn, name }
} }
} }
impl Deref for Loc {
type Target = PathBuf;
fn deref(&self) -> &Self::Target { &self.path }
}
impl PartialEq for Loc {
fn eq(&self, other: &Self) -> bool { self.path == other.path }
}
impl Eq for Loc {}
impl Ord for Loc {
fn cmp(&self, other: &Self) -> cmp::Ordering { self.path.cmp(&other.path) }
}
impl PartialOrd for Loc {
fn partial_cmp(&self, other: &Self) -> Option<cmp::Ordering> { Some(self.cmp(other)) }
}
impl Hash for Loc {
fn hash<H: Hasher>(&self, state: &mut H) { self.path.hash(state) }
}
impl Debug for Loc { impl Debug for Loc {
fn fmt(&self, f: &mut Formatter) -> fmt::Result { fn fmt(&self, f: &mut Formatter) -> fmt::Result {
f.debug_struct("Loc") f.debug_struct("Loc")
.field("url", &self.url) .field("path", &self.path)
.field("urn", &self.urn()) .field("urn", &self.urn())
.field("name", &self.name()) .field("name", &self.name())
.finish() .finish()
@ -52,26 +62,35 @@ impl Debug for Loc {
} }
impl Loc { impl Loc {
pub fn from(url: Url) -> Self { pub fn new(path: PathBuf) -> Self {
let urn = url.file_name().unwrap_or(OsStr::new("")) as *const OsStr; let urn = path.file_name().unwrap_or(OsStr::new("")) as *const OsStr;
Self { url, urn, name: urn } Self { path, urn, name: urn }
} }
pub fn from_search_item(cwd: &Url, url: Url) -> Self { pub fn from(base: &Path, path: PathBuf) -> Self {
let urn = url.strip_prefix(cwd).unwrap_or(&url).as_os_str() as *const OsStr; let urn = path.strip_prefix(base).unwrap_or(&path).as_os_str() as *const OsStr;
let name = url.file_name().unwrap_or(OsStr::new("")) as *const OsStr; let name = path.file_name().unwrap_or(OsStr::new("")) as *const OsStr;
Self { url, urn, name } Self { path, urn, name }
} }
pub fn rebase(&self, parent: &Url) -> Self { pub fn base(&self) -> &Path {
let url = parent.join(self.name()); let mut it = self.path.components();
let name = url.file_name().unwrap_or(OsStr::new("")) as *const OsStr; for _ in 0..self.urn()._as_path().components().count() {
let urn = if url.is_search_item() { self.twin_urn(&url) } else { name }; it.next_back().unwrap();
Self { url, urn, name } }
it.as_path()
}
pub fn rebase(&self, parent: &Path) -> Self {
debug_assert!(self.urn()._as_path() == self.name());
let path = parent.join(self.name());
let name = path.file_name().unwrap_or(OsStr::new("")) as *const OsStr;
Self { path, urn: name, name }
} }
#[inline] #[inline]
fn twin_urn<'a>(&self, new: &'a Url) -> &'a OsStr { fn twin_urn<'a>(&self, new: &'a Path) -> &'a OsStr {
let total = new.components().count(); let total = new.components().count();
let take = self.urn()._as_path().components().count(); let take = self.urn()._as_path().components().count();
@ -85,12 +104,6 @@ impl Loc {
} }
impl Loc { impl Loc {
#[inline]
pub fn url(&self) -> &Url { &self.url }
#[inline]
pub fn url_owned(&self) -> Url { self.url.to_owned() }
#[inline] #[inline]
pub fn urn(&self) -> &Urn { Urn::new(unsafe { &*self.urn }) } pub fn urn(&self) -> &Urn { Urn::new(unsafe { &*self.urn }) }
@ -99,4 +112,7 @@ impl Loc {
#[inline] #[inline]
pub fn name(&self) -> &OsStr { unsafe { &*self.name } } pub fn name(&self) -> &OsStr { unsafe { &*self.name } }
#[inline]
pub fn into_path(self) -> PathBuf { self.path }
} }

View file

@ -57,9 +57,9 @@ impl FilesOp {
let n = new.clone(); let n = new.clone();
match self { match self {
Self::Full(_, files, mtime) => Self::Full(n, files!(files), *mtime), Self::Full(_, files, cha) => Self::Full(n, files!(files), *cha),
Self::Part(_, files, ticket) => Self::Part(n, files!(files), *ticket), Self::Part(_, files, ticket) => Self::Part(n, files!(files), *ticket),
Self::Done(_, mtime, ticket) => Self::Done(n, *mtime, *ticket), Self::Done(_, cha, ticket) => Self::Done(n, *cha, *ticket),
Self::Size(_, map) => Self::Size(n, map.iter().map(|(u, &s)| (u.clone(), s)).collect()), Self::Size(_, map) => Self::Size(n, map.iter().map(|(u, &s)| (u.clone(), s)).collect()),
Self::IOErr(_, err) => Self::IOErr(n, *err), Self::IOErr(_, err) => Self::IOErr(n, *err),

View file

@ -88,17 +88,19 @@ pub async fn unique_name(mut u: Url) -> Url {
.unwrap_or_default(); .unwrap_or_default();
let mut i = 1u64; let mut i = 1u64;
while maybe_exists(&u).await { let mut p = u.into_path();
while maybe_exists(&p).await {
let mut name = OsString::with_capacity(stem.len() + ext.len() + 5); let mut name = OsString::with_capacity(stem.len() + ext.len() + 5);
name.push(&stem); name.push(&stem);
name.push("_"); name.push("_");
name.push(i.to_string()); name.push(i.to_string());
name.push(&ext); name.push(&ext);
u.set_file_name(name); p.set_file_name(name);
i += 1; i += 1;
} }
u // FIXME 3: handle base path
Url::from(p)
} }
// Parameters // Parameters

View file

@ -1,4 +1,4 @@
use std::{ffi::{OsStr, OsString}, fmt::{Debug, Display, Formatter}, ops::{Deref, DerefMut}, path::{Path, PathBuf}}; use std::{ffi::OsStr, fmt::{Debug, Display, Formatter}, ops::Deref, path::{Path, PathBuf}};
use percent_encoding::{percent_decode_str, percent_encode, AsciiSet, CONTROLS}; use percent_encoding::{percent_decode_str, percent_encode, AsciiSet, CONTROLS};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
@ -9,8 +9,8 @@ const ENCODE_SET: &AsciiSet = &CONTROLS.add(b'#');
#[derive(Clone, Default, Eq, Hash, Ord, PartialEq, PartialOrd)] #[derive(Clone, Default, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub struct Url { pub struct Url {
loc: Loc,
scheme: UrlScheme, scheme: UrlScheme,
path: PathBuf,
frag: String, frag: String,
} }
@ -24,37 +24,36 @@ pub enum UrlScheme {
} }
impl Deref for Url { impl Deref for Url {
type Target = PathBuf; type Target = Loc;
fn deref(&self) -> &Self::Target { &self.path } fn deref(&self) -> &Self::Target { &self.loc }
}
impl DerefMut for Url {
fn deref_mut(&mut self) -> &mut Self::Target { &mut self.path }
} }
impl Debug for Url { impl Debug for Url {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { write!(f, "{}", self.path.display()) } fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
match self.scheme {
UrlScheme::Regular => write!(f, "Regular({:?})", self.loc),
UrlScheme::Search => write!(f, "Search({:?}, {})", self.loc, self.frag),
UrlScheme::SearchItem => write!(f, "SearchItem({:?})", self.loc),
UrlScheme::Archive => write!(f, "Archive({:?})", self.loc),
}
}
}
impl From<Loc> for Url {
fn from(loc: Loc) -> Self { Self { loc, ..Default::default() } }
} }
impl From<PathBuf> for Url { impl From<PathBuf> for Url {
fn from(path: PathBuf) -> Self { Self { path, ..Default::default() } } fn from(path: PathBuf) -> Self { Loc::new(path).into() }
} }
impl From<&PathBuf> for Url { impl From<&PathBuf> for Url {
fn from(path: &PathBuf) -> Self { Self::from(path.clone()) } fn from(path: &PathBuf) -> Self { path.to_owned().into() }
} }
impl From<&Path> for Url { impl From<&Path> for Url {
fn from(path: &Path) -> Self { Self::from(path.to_path_buf()) } fn from(path: &Path) -> Self { path.to_owned().into() }
}
impl From<String> for Url {
fn from(path: String) -> Self { Self::from(path.as_str()) }
}
impl From<&String> for Url {
fn from(path: &String) -> Self { Self::from(path.as_str()) }
} }
impl From<&str> for Url { impl From<&str> for Url {
@ -62,11 +61,11 @@ impl From<&str> for Url {
let mut url = Url::default(); let mut url = Url::default();
match path.split_once("://").map(|(a, b)| (UrlScheme::from(a), b)) { match path.split_once("://").map(|(a, b)| (UrlScheme::from(a), b)) {
None => { None => {
url.path = PathBuf::from(path); url.loc = Loc::new(PathBuf::from(path));
return url; return url;
} }
Some((UrlScheme::Regular, b)) => { Some((UrlScheme::Regular, b)) => {
url.path = PathBuf::from(b); url.loc = Loc::new(PathBuf::from(b));
return url; return url;
} }
Some((a, b)) => { Some((a, b)) => {
@ -76,10 +75,12 @@ impl From<&str> for Url {
} }
match path.split_once('#') { match path.split_once('#') {
None => { None => {
url.path = percent_decode_str(path).decode_utf8_lossy().into_owned().into(); // FIXME: use `Loc::from(base, path)` instead
url.loc = Loc::new(percent_decode_str(path).decode_utf8_lossy().into_owned().into());
} }
Some((a, b)) => { Some((a, b)) => {
url.path = percent_decode_str(a).decode_utf8_lossy().into_owned().into(); // FIXME: use `Loc::from(base, path)` instead
url.loc = Loc::new(percent_decode_str(a).decode_utf8_lossy().into_owned().into());
url.frag = b.to_string(); url.frag = b.to_string();
} }
} }
@ -87,22 +88,30 @@ impl From<&str> for Url {
} }
} }
impl From<String> for Url {
fn from(path: String) -> Self { path.as_str().into() }
}
impl From<&String> for Url {
fn from(path: &String) -> Self { path.as_str().into() }
}
impl AsRef<Url> for Url { impl AsRef<Url> for Url {
fn as_ref(&self) -> &Url { self } fn as_ref(&self) -> &Url { self }
} }
impl AsRef<Path> for Url { impl AsRef<Path> for Url {
fn as_ref(&self) -> &Path { &self.path } fn as_ref(&self) -> &Path { &self.loc }
} }
impl AsRef<OsStr> for Url { impl AsRef<OsStr> for Url {
fn as_ref(&self) -> &OsStr { self.path.as_os_str() } fn as_ref(&self) -> &OsStr { self.loc.as_os_str() }
} }
impl Display for Url { impl Display for Url {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
if matches!(self.scheme, UrlScheme::Regular | UrlScheme::SearchItem) { if matches!(self.scheme, UrlScheme::Regular | UrlScheme::SearchItem) {
return f.write_str(&self.path.to_string_lossy()); return f.write_str(&self.loc.to_string_lossy());
} }
let scheme = match self.scheme { let scheme = match self.scheme {
@ -110,7 +119,7 @@ impl Display for Url {
UrlScheme::Search => "search://", UrlScheme::Search => "search://",
UrlScheme::Archive => "archive://", UrlScheme::Archive => "archive://",
}; };
let path = percent_encode(self.path.as_os_str().as_encoded_bytes(), ENCODE_SET); let path = percent_encode(self.loc.as_os_str().as_encoded_bytes(), ENCODE_SET);
write!(f, "{scheme}{path}")?; write!(f, "{scheme}{path}")?;
if !self.frag.is_empty() { if !self.frag.is_empty() {
@ -128,48 +137,47 @@ impl From<&Url> for String {
impl Url { impl Url {
#[inline] #[inline]
pub fn join(&self, path: impl AsRef<Path>) -> Self { pub fn join(&self, path: impl AsRef<Path>) -> Self {
let url = Self::from(self.path.join(path));
match self.scheme { match self.scheme {
UrlScheme::Regular => url, UrlScheme::Regular => Self::from(self.loc.join(path)),
UrlScheme::Search => url.into_search_item(), UrlScheme::Search => {
UrlScheme::SearchItem => url, let loc = Loc::from(&self.loc, self.loc.join(path));
UrlScheme::Archive => url.into_archive(), Self::from(loc).into_search_item()
}
UrlScheme::SearchItem => {
let loc = Loc::from(self.loc.base(), self.loc.join(path));
Self::from(loc).into_search_item()
}
UrlScheme::Archive => Self::from(self.loc.join(path)).into_archive(),
} }
} }
#[inline] #[inline]
pub fn parent_url(&self) -> Option<Url> { pub fn parent_url(&self) -> Option<Url> {
self.path.parent().map(|p| { let p = self.loc.parent()?;
let url = Self::from(p); Some(match self.scheme {
match self.scheme { UrlScheme::Regular | UrlScheme::Search => Self::from(p),
UrlScheme::Regular => url, UrlScheme::SearchItem => {
UrlScheme::Search => url, if p == self.loc.base() {
UrlScheme::SearchItem => url, Self::from(p).into_search("")
UrlScheme::Archive => url, } else {
Self::from(p).into_search_item()
} }
}
UrlScheme::Archive => Self::from(p),
}) })
} }
#[inline] #[inline]
pub fn pair(&self) -> Option<(Self, UrnBuf)> { pub fn pair(&self) -> Option<(Self, UrnBuf)> { Some((self.parent_url()?, self.loc.urn_owned())) }
let urn = UrnBuf::_from(self.path.file_name()?);
Some((self.parent_url()?, urn)) #[cfg(unix)]
} #[inline]
pub fn is_hidden(&self) -> bool { self.loc.urn().is_hidden() }
#[inline] #[inline]
pub fn strip_prefix(&self, base: impl AsRef<Path>) -> Option<&Path> { pub fn rebase(&self, parent: &Path) -> Self {
self.path.strip_prefix(base).ok() debug_assert!(self.is_regular());
} self.loc.rebase(parent).into()
#[inline]
pub fn into_os_string(self) -> OsString { self.path.into_os_string() }
#[inline]
pub fn to_loc(&self, cwd: &Url) -> Loc { self.clone().into_loc(cwd) }
#[inline]
pub fn into_loc(self, cwd: &Url) -> Loc {
if self.is_search_item() { Loc::from_search_item(cwd, self) } else { Loc::from(self) }
} }
} }
@ -180,7 +188,7 @@ impl Url {
#[inline] #[inline]
pub fn to_regular(&self) -> Self { pub fn to_regular(&self) -> Self {
Self { scheme: UrlScheme::Regular, path: self.path.clone(), frag: String::new() } Self { loc: self.loc.clone(), scheme: UrlScheme::Regular, frag: String::new() }
} }
#[inline] #[inline]
@ -196,7 +204,14 @@ impl Url {
#[inline] #[inline]
pub fn to_search(&self, frag: &str) -> Self { pub fn to_search(&self, frag: &str) -> Self {
Self { scheme: UrlScheme::Search, path: self.path.clone(), frag: frag.to_owned() } Self { loc: self.loc.clone(), scheme: UrlScheme::Search, frag: frag.to_owned() }
}
#[inline]
pub fn into_search(mut self, frag: &str) -> Self {
self.scheme = UrlScheme::Search;
self.frag = frag.to_owned();
self
} }
#[inline] #[inline]
@ -214,7 +229,7 @@ impl Url {
#[inline] #[inline]
pub fn to_archive(&self) -> Self { pub fn to_archive(&self) -> Self {
Self { scheme: UrlScheme::Archive, path: self.path.clone(), frag: String::new() } Self { loc: self.loc.clone(), scheme: UrlScheme::Archive, frag: String::new() }
} }
#[inline] #[inline]
@ -224,17 +239,14 @@ impl Url {
self self
} }
// --- Path
#[inline]
pub fn into_path(self) -> PathBuf { self.loc.into_path() }
// --- Scheme // --- Scheme
#[inline] #[inline]
pub fn scheme(&self) -> UrlScheme { self.scheme } pub fn scheme(&self) -> UrlScheme { self.scheme }
// --- Path
#[inline]
pub fn set_path(&mut self, path: PathBuf) { self.path = path; }
#[inline]
pub fn into_path(self) -> PathBuf { self.path }
// --- Frag // --- Frag
#[inline] #[inline]
pub fn frag(&self) -> &str { &self.frag } pub fn frag(&self) -> &str { &self.frag }