feat(bulk_rename): work in progress

This commit is contained in:
TD-Sky 2023-08-12 20:20:53 +08:00 committed by sxyazi
parent ebb06789fc
commit 2090da2480
No known key found for this signature in database
5 changed files with 137 additions and 0 deletions

37
Cargo.lock generated
View file

@ -1315,6 +1315,12 @@ dependencies = [
"miniz_oxide",
]
[[package]]
name = "ppv-lite86"
version = "0.2.17"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5b40af805b3121feab8a3c29f04d8ad262fa8e0561883e7653e024ae4479e6de"
[[package]]
name = "proc-macro2"
version = "1.0.66"
@ -1351,6 +1357,36 @@ dependencies = [
"proc-macro2",
]
[[package]]
name = "rand"
version = "0.8.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "34af8d1a0e25924bc5b7c43c079c942339d8f0a8b57c39049bef581b46327404"
dependencies = [
"libc",
"rand_chacha",
"rand_core",
]
[[package]]
name = "rand_chacha"
version = "0.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e6c10a63a0fa32252be49d21e7709d4d4baf8d231c2dbce1eaa8141b9b127d88"
dependencies = [
"ppv-lite86",
"rand_core",
]
[[package]]
name = "rand_core"
version = "0.6.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c"
dependencies = [
"getrandom",
]
[[package]]
name = "ratatui"
version = "0.22.0"
@ -1529,6 +1565,7 @@ dependencies = [
"crossterm 0.27.0",
"libc",
"parking_lot",
"rand",
"ratatui",
"tokio",
]

46
core/src/external/editor.rs vendored Normal file
View file

@ -0,0 +1,46 @@
use std::{env, ffi::{OsStr, OsString}, path::Path, str::FromStr};
use anyhow::{Context, Result};
use once_cell::sync::OnceCell;
use tokio::process::Command;
#[derive(Debug)]
struct Editor {
name: String,
options: Vec<String>,
}
static EDITOR: OnceCell<Editor> = OnceCell::new();
fn try_init() -> Result<&'static Editor> {
EDITOR.get_or_try_init(|| {
env::var_os("EDITOR")
.context("environment variable `EDITOR` is undefined")?
.to_string_lossy()
.parse()
})
}
pub async fn edit(file: impl AsRef<Path>) -> Result<()> {
let editor = try_init()?;
// TODO: 编辑并返回是否成功
// let output = Command::new(&editor.name)
// .args(&editor.options)
// .arg(file.as_ref())
// .kill_on_drop(true)
// .output()
// .await?;
todo!()
}
impl FromStr for Editor {
type Err = anyhow::Error;
fn from_str(s: &str) -> std::result::Result<Self, Self::Err> {
let mut args = s.split(' ');
Ok(Self {
name: args.next().context("environment variable `EDITOR` is empty")?.to_owned(),
options: args.map(str::to_owned).collect(),
})
}
}

View file

@ -476,3 +476,19 @@ impl Manager {
self.active().mode.is_visual() || self.current().has_selected()
}
}
fn parse_new_names(text: &str, count: usize) -> anyhow::Result<IndexSet<&str>> {
// NOTE: call `size_hint` on `str::Split` always returns 0
let new_names: Vec<_> = text.split('\n').collect();
if new_names.len() != count {
anyhow::bail!("the number of new names doesn't match the number of old names");
}
let mut names = IndexSet::with_capacity(count);
for name in new_names {
if !names.insert(name) {
anyhow::bail!("there are more than one new entries named {name:?}");
}
}
Ok(names)
}

View file

@ -4,6 +4,7 @@ mod defer;
mod fns;
mod fs;
mod mime;
mod temp_path;
mod term;
mod throttle;
mod tty;
@ -14,6 +15,7 @@ pub use defer::*;
pub use fns::*;
pub use fs::*;
pub use mime::*;
pub use temp_path::*;
pub use term::*;
pub use throttle::*;
pub use tty::*;

36
shared/src/temp_path.rs Normal file
View file

@ -0,0 +1,36 @@
use std::path::PathBuf;
use rand::{distributions::Alphanumeric, Rng};
const FILE_PREFIX: &str = "yazi-";
const ID_LEN: usize = 10;
/// generate a path under the system temporary directory
///
/// `ext`: an ASCII string
pub fn temp_path(ext: Option<&str>) -> PathBuf {
let (ext, suffix_len) = ext.map(|ext| (ext, ext.len())).unwrap_or_default();
let mut name = String::with_capacity(FILE_PREFIX.len() + ID_LEN + suffix_len);
name.push_str(FILE_PREFIX);
rand::thread_rng().sample_iter(&Alphanumeric).take(ID_LEN).for_each(|c| name.push(c as char));
if !ext.is_empty() {
name.push('.');
name.push_str(ext);
}
let tmp = std::env::temp_dir();
tmp.join(&name)
}
#[cfg(test)]
mod tests {
use super::temp_path;
#[test]
fn test_temp_path() {
let p = temp_path(Some("txt"));
println!("{p:?}");
}
}