add pattern-edit
This commit is contained in:
parent
d1e9c0a431
commit
ce6254df7e
3 changed files with 86 additions and 4 deletions
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