Skip to content

Commit

Permalink
WIP: Add '--hard' option to 'link' command
Browse files Browse the repository at this point in the history
WIP because no support for directories yet.
  • Loading branch information
Ape committed Jul 6, 2024
1 parent 11547ee commit 415443f
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 16 deletions.
7 changes: 5 additions & 2 deletions yazi-core/src/manager/commands/link.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,14 @@ use crate::{manager::Manager, tasks::Tasks};

pub struct Opt {
relative: bool,
hard: bool,
force: bool,
}

impl From<Cmd> for Opt {
fn from(c: Cmd) -> Self { Self { relative: c.bool("relative"), force: c.bool("force") } }
fn from(c: Cmd) -> Self {
Self { relative: c.bool("relative"), hard: c.bool("hard"), force: c.bool("force") }
}
}

impl Manager {
Expand All @@ -18,6 +21,6 @@ impl Manager {
}

let opt = opt.into() as Opt;
tasks.file_link(&self.yanked, self.cwd(), opt.relative, opt.force);
tasks.file_link(&self.yanked, self.cwd(), opt.relative, opt.hard, opt.force);
}
}
4 changes: 2 additions & 2 deletions yazi-core/src/tasks/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,13 @@ impl Tasks {
}
}

pub fn file_link(&self, src: &HashSet<Url>, dest: &Url, relative: bool, force: bool) {
pub fn file_link(&self, src: &HashSet<Url>, dest: &Url, relative: bool, hard: bool, force: bool) {
for u in src {
let to = dest.join(u.file_name().unwrap());
if force && *u == to {
debug!("file_link: same file, skipping {:?}", to);
} else {
self.scheduler.file_link(u.clone(), to, relative, force);
self.scheduler.file_link(u.clone(), to, relative, hard, force);
}
}
}
Expand Down
25 changes: 15 additions & 10 deletions yazi-scheduler/src/file/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,16 +81,21 @@ impl File {
};

ok_or_not_found(fs::remove_file(&task.to).await)?;
#[cfg(unix)]
{
fs::symlink(src, &task.to).await?
}
#[cfg(windows)]
{
if meta.is_dir() {
fs::symlink_dir(src, &task.to).await?
} else {
fs::symlink_file(src, &task.to).await?

if task.hard {
fs::hard_link(src, &task.to).await?
} else {
#[cfg(unix)]
{
fs::symlink(src, &task.to).await?
}
#[cfg(windows)]
{
if meta.is_dir() {
fs::symlink_dir(src, &task.to).await?
} else {
fs::symlink_file(src, &task.to).await?
}
}
}

Expand Down
2 changes: 2 additions & 0 deletions yazi-scheduler/src/file/op.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ pub struct FileOpLink {
pub meta: Option<Metadata>,
pub resolve: bool,
pub relative: bool,
pub hard: bool,
pub delete: bool,
}

Expand All @@ -66,6 +67,7 @@ impl From<FileOpPaste> for FileOpLink {
meta: value.meta,
resolve: true,
relative: false,
hard: false,
delete: value.cut,
}
}
Expand Down
13 changes: 11 additions & 2 deletions yazi-scheduler/src/scheduler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ impl Scheduler {
);
}

pub fn file_link(&self, from: Url, mut to: Url, relative: bool, force: bool) {
pub fn file_link(&self, from: Url, mut to: Url, relative: bool, hard: bool, force: bool) {
let name = format!("Link {from:?} to {to:?}");
let id = self.ongoing.lock().add(TaskKind::User, name);

Expand All @@ -145,7 +145,16 @@ impl Scheduler {
to = unique_path(to).await;
}
file
.link(FileOpLink { id, from, to, meta: None, resolve: false, relative, delete: false })
.link(FileOpLink {
id,
from,
to,
meta: None,
resolve: false,
relative,
hard,
delete: false,
})
.await
.ok();
}
Expand Down

0 comments on commit 415443f

Please sign in to comment.