add pattern-edit
This commit is contained in:
parent
d1e9c0a431
commit
ce6254df7e
3 changed files with 86 additions and 4 deletions
60
Cargo.lock
generated
60
Cargo.lock
generated
|
|
@ -2,6 +2,15 @@
|
|||
# It is not intended for manual editing.
|
||||
version = 4
|
||||
|
||||
[[package]]
|
||||
name = "aho-corasick"
|
||||
version = "1.1.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "8e60d3430d3a69478ad0993f19238d2df97c507009a52b3c10addcd7f6bcb916"
|
||||
dependencies = [
|
||||
"memchr",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "anstream"
|
||||
version = "0.6.18"
|
||||
|
|
@ -154,7 +163,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
|
|||
checksum = "cea14ef9355e3beab063703aa9dab15afd25f0667c341310c1e5274bb1d0da18"
|
||||
dependencies = [
|
||||
"libc",
|
||||
"windows-sys 0.52.0",
|
||||
"windows-sys 0.59.0",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
|
|
@ -205,6 +214,12 @@ version = "0.9.4"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "cd945864f07fe9f5371a27ad7b52a172b4b499999f1d97574c9fa68373937e12"
|
||||
|
||||
[[package]]
|
||||
name = "memchr"
|
||||
version = "2.7.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "32a282da65faaf38286cf3be983213fcf1d2e2a58700e808f83f4ea9a4804bc0"
|
||||
|
||||
[[package]]
|
||||
name = "mvw"
|
||||
version = "0.1.6"
|
||||
|
|
@ -212,6 +227,7 @@ dependencies = [
|
|||
"clap",
|
||||
"dialoguer",
|
||||
"rand",
|
||||
"sedregex",
|
||||
"tempfile",
|
||||
]
|
||||
|
||||
|
|
@ -283,6 +299,35 @@ dependencies = [
|
|||
"getrandom",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "regex"
|
||||
version = "1.11.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "b544ef1b4eac5dc2db33ea63606ae9ffcfac26c1416a2806ae0bf5f56b201191"
|
||||
dependencies = [
|
||||
"aho-corasick",
|
||||
"memchr",
|
||||
"regex-automata",
|
||||
"regex-syntax",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "regex-automata"
|
||||
version = "0.4.9"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "809e8dc61f6de73b46c85f4c96486310fe304c434cfa43669d7b40f711150908"
|
||||
dependencies = [
|
||||
"aho-corasick",
|
||||
"memchr",
|
||||
"regex-syntax",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "regex-syntax"
|
||||
version = "0.8.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "2b15c43186be67a4fd63bee50d0303afffcef381492ebe2c5d87f324e1b8815c"
|
||||
|
||||
[[package]]
|
||||
name = "rustix"
|
||||
version = "1.0.7"
|
||||
|
|
@ -293,7 +338,16 @@ dependencies = [
|
|||
"errno",
|
||||
"libc",
|
||||
"linux-raw-sys",
|
||||
"windows-sys 0.52.0",
|
||||
"windows-sys 0.59.0",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "sedregex"
|
||||
version = "0.2.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "19411e23596093f03bbd11dc45603b6329bb4bfec77b9fd13e2b9fc9b02efe3e"
|
||||
dependencies = [
|
||||
"regex",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
|
|
@ -329,7 +383,7 @@ dependencies = [
|
|||
"getrandom",
|
||||
"once_cell",
|
||||
"rustix",
|
||||
"windows-sys 0.52.0",
|
||||
"windows-sys 0.59.0",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
|
|
|
|||
|
|
@ -5,9 +5,11 @@ edition = "2021"
|
|||
|
||||
[profile.release]
|
||||
lto = "thin"
|
||||
strip = true
|
||||
|
||||
[dependencies]
|
||||
tempfile = "3.20.0"
|
||||
dialoguer = "0.11.0"
|
||||
rand = "0.9.0"
|
||||
clap = { version = "4.5.38", features = [ "derive" ]}
|
||||
sedregex = "0.2.5"
|
||||
|
|
|
|||
28
src/main.rs
28
src/main.rs
|
|
@ -4,12 +4,14 @@ pub use crate::dirlist::*;
|
|||
use std::env;
|
||||
use std::fs;
|
||||
use std::fs::File;
|
||||
use std::io::{BufRead, BufReader};
|
||||
use std::path::Path;
|
||||
use std::process;
|
||||
use std::process::Command;
|
||||
use tempfile::NamedTempFile;
|
||||
use dialoguer::Confirm;
|
||||
use clap::Parser;
|
||||
use sedregex::find_and_replace;
|
||||
|
||||
const LOCK_FILE: &str = ".mvwrap";
|
||||
const SUFFIX_TMPFILE: &str = ".mvwrap";
|
||||
|
|
@ -41,6 +43,10 @@ struct Cli {
|
|||
|
||||
#[arg(required = false)]
|
||||
files: Vec<std::path::PathBuf>,
|
||||
|
||||
/// Use sed pattern to batch rename
|
||||
#[arg(short, long)]
|
||||
pattern: Vec<String>,
|
||||
}
|
||||
|
||||
|
||||
|
|
@ -83,6 +89,22 @@ fn edit_temp_file(tmpfile: &String, editor_cli: &Option<String>) {
|
|||
.expect("failed to execute editor process");
|
||||
}
|
||||
|
||||
fn pattern_edit_temp_file(tmpfile: &String, patterns: &Vec<String>) {
|
||||
let file = File::open(&tmpfile).expect("no such file");
|
||||
let buf = BufReader::new(file);
|
||||
let mut lines: Vec<String> = buf.lines()
|
||||
.map(|l| l.expect("Could not parse line"))
|
||||
.collect();
|
||||
|
||||
// Modify the lines (example: convert to uppercase)
|
||||
for line in &mut lines {
|
||||
*line = find_and_replace(line, patterns).unwrap().to_string();
|
||||
}
|
||||
|
||||
// Write the modified content back to the file
|
||||
let _ = fs::write(&tmpfile, lines.join("\n"));
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let mut args = Cli::parse();
|
||||
|
||||
|
|
@ -117,7 +139,11 @@ fn main() {
|
|||
if args.noop { source_list.set_noop(); source_list.set_verbose(); }
|
||||
if args.verbose { source_list.set_verbose(); }
|
||||
|
||||
edit_temp_file(&temp_file, &args.editor);
|
||||
if args.pattern.is_empty() {
|
||||
edit_temp_file(&temp_file, &args.editor);
|
||||
} else {
|
||||
pattern_edit_temp_file(&temp_file, &args.pattern);
|
||||
}
|
||||
|
||||
let mut target_list = DirList::from_file(&temp_file);
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue