mirror of
https://github.com/sxyazi/yazi.git
synced 2026-07-25 08:41:05 +00:00
feat: new force option for creating and renaming (#208)
This commit is contained in:
parent
a5aa9cb817
commit
b2d3e39e96
2 changed files with 39 additions and 20 deletions
|
|
@ -124,8 +124,8 @@ impl Executor {
|
||||||
let permanently = exec.named.contains_key("permanently");
|
let permanently = exec.named.contains_key("permanently");
|
||||||
cx.tasks.file_remove(targets, force, permanently)
|
cx.tasks.file_remove(targets, force, permanently)
|
||||||
}
|
}
|
||||||
"create" => cx.manager.create(),
|
"create" => cx.manager.create(exec.named.contains_key("force")),
|
||||||
"rename" => cx.manager.rename(),
|
"rename" => cx.manager.rename(exec.named.contains_key("force")),
|
||||||
"copy" => cx.manager.active().copy(exec.args.get(0).map(|s| s.as_str()).unwrap_or("")),
|
"copy" => cx.manager.active().copy(exec.args.get(0).map(|s| s.as_str()).unwrap_or("")),
|
||||||
"shell" => cx.manager.active().shell(
|
"shell" => cx.manager.active().shell(
|
||||||
exec.args.get(0).map(|e| e.as_str()).unwrap_or(""),
|
exec.args.get(0).map(|e| e.as_str()).unwrap_or(""),
|
||||||
|
|
|
||||||
|
|
@ -177,33 +177,40 @@ impl Manager {
|
||||||
false
|
false
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn create(&self) -> bool {
|
pub fn create(&self, force: bool) -> bool {
|
||||||
let cwd = self.cwd().to_owned();
|
let cwd = self.cwd().to_owned();
|
||||||
tokio::spawn(async move {
|
tokio::spawn(async move {
|
||||||
let mut result = emit!(Input(InputOpt::top("Create:")));
|
let mut result = emit!(Input(InputOpt::top("Create:")));
|
||||||
|
let Some(Ok(name)) = result.recv().await else {
|
||||||
|
return Ok(());
|
||||||
|
};
|
||||||
|
|
||||||
if let Some(Ok(name)) = result.recv().await {
|
let path = cwd.join(&name);
|
||||||
let path = cwd.join(&name);
|
if !force && fs::symlink_metadata(&path).await.is_ok() {
|
||||||
let hovered = path.components().take(cwd.components().count() + 1).collect::<PathBuf>();
|
match emit!(Input(InputOpt::top("Overwrite an existing file? (y/N)"))).recv().await {
|
||||||
|
Some(Ok(c)) if c == "y" || c == "Y" => (),
|
||||||
if name.ends_with('/') {
|
_ => return Ok(()),
|
||||||
fs::create_dir_all(path).await?;
|
|
||||||
} else {
|
|
||||||
fs::create_dir_all(path.parent().unwrap()).await.ok();
|
|
||||||
fs::File::create(path).await?;
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if let Ok(file) = File::from(Url::from(hovered)).await {
|
if name.ends_with('/') {
|
||||||
emit!(Hover(file));
|
fs::create_dir_all(&path).await?;
|
||||||
emit!(Refresh);
|
} else {
|
||||||
}
|
fs::create_dir_all(&path.parent().unwrap()).await.ok();
|
||||||
|
fs::File::create(&path).await?;
|
||||||
|
}
|
||||||
|
|
||||||
|
let child = path.components().take(cwd.components().count() + 1).collect::<PathBuf>();
|
||||||
|
if let Ok(file) = File::from(Url::from(child)).await {
|
||||||
|
emit!(Hover(file));
|
||||||
|
emit!(Refresh);
|
||||||
}
|
}
|
||||||
Ok::<(), Error>(())
|
Ok::<(), Error>(())
|
||||||
});
|
});
|
||||||
false
|
false
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn rename(&self) -> bool {
|
pub fn rename(&self, force: bool) -> bool {
|
||||||
if self.active().in_selecting() {
|
if self.active().in_selecting() {
|
||||||
return self.bulk_rename();
|
return self.bulk_rename();
|
||||||
}
|
}
|
||||||
|
|
@ -217,10 +224,22 @@ impl Manager {
|
||||||
InputOpt::hovered("Rename:").with_value(hovered.file_name().unwrap().to_string_lossy())
|
InputOpt::hovered("Rename:").with_value(hovered.file_name().unwrap().to_string_lossy())
|
||||||
));
|
));
|
||||||
|
|
||||||
if let Some(Ok(new)) = result.recv().await {
|
let Some(Ok(name)) = result.recv().await else {
|
||||||
let to = hovered.parent().unwrap().join(new);
|
return;
|
||||||
fs::rename(&hovered, to).await.ok();
|
};
|
||||||
|
|
||||||
|
let new = hovered.parent().unwrap().join(name);
|
||||||
|
if force || fs::symlink_metadata(&new).await.is_err() {
|
||||||
|
fs::rename(&hovered, new).await.ok();
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let mut result = emit!(Input(InputOpt::hovered("Overwrite an existing file? (y/N)")));
|
||||||
|
if let Some(Ok(choice)) = result.recv().await {
|
||||||
|
if choice == "y" || choice == "Y" {
|
||||||
|
fs::rename(&hovered, new).await.ok();
|
||||||
|
}
|
||||||
|
};
|
||||||
});
|
});
|
||||||
false
|
false
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue