-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 8b9bb65
Showing
5 changed files
with
284 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/target |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
[package] | ||
name = "punc2en" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
|
||
[dependencies] | ||
colored = "2.0.4" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
use colored::Colorize; | ||
|
||
pub struct Config { | ||
pub path: String, | ||
pub is_dir: bool, | ||
} | ||
|
||
impl Config { | ||
pub fn new(args: &[String]) -> Result<Config, &'static str> { | ||
if args.len() < 2 { | ||
return Err("not enough arguments"); | ||
} | ||
let path = args[1].clone(); | ||
let is_dir = args.len() == 3 && args[2] == "-d"; | ||
Ok(Config { path, is_dir }) | ||
} | ||
} | ||
|
||
pub fn run(path: String) { | ||
let content: String = match std::fs::read_to_string(&path) { | ||
Err(e) => { | ||
println!( | ||
"{} {}", | ||
"Something went wrong reading the file: ".red().bold(), | ||
e.to_string().red().bold() | ||
); | ||
std::process::exit(1); | ||
} | ||
Ok(content) => content, | ||
}; | ||
let mut res: Vec<String> = vec![]; | ||
content.lines().for_each(|line| { | ||
let mut muted = line.replace(",", ","); | ||
muted = muted.replace("。", "."); | ||
muted = muted.replace("、", "\\"); | ||
muted = muted.replace(";", ";"); | ||
muted = muted.replace("‘", "\'"); | ||
muted = muted.replace("’", "\'"); | ||
muted = muted.replace("【", "["); | ||
muted = muted.replace("】", "]"); | ||
muted = muted.replace("·", "`"); | ||
muted = muted.replace("《", "<"); | ||
muted = muted.replace("》", ">"); | ||
muted = muted.replace("?", "?"); | ||
muted = muted.replace(":", ":"); | ||
muted = muted.replace("“", "\""); | ||
muted = muted.replace("”", "\""); | ||
muted = muted.replace("!", "!"); | ||
muted = muted.replace("¥", "$"); | ||
muted = muted.replace("……", "..."); | ||
muted = muted.replace("(", "("); | ||
muted = muted.replace(")", ")"); | ||
muted = muted.replace("——", "_"); | ||
res.push(muted); | ||
}); | ||
match std::fs::write(&path, res.join("\n")) { | ||
Err(e) => { | ||
println!( | ||
"{} {}", | ||
"Something went wrong writing the file: ".red().bold(), | ||
e.to_string().red().bold() | ||
); | ||
std::process::exit(1); | ||
} | ||
Ok(_) => { | ||
println!("{}", "Done!".green().bold()); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
use colored::Colorize; | ||
|
||
fn main() { | ||
let args: Vec<String> = std::env::args().collect(); | ||
let config = match punc2en::Config::new(&args) { | ||
Err(e) => { | ||
println!("Problem parsing arguments: {}", e); | ||
std::process::exit(1); | ||
} | ||
Ok(config) => config, | ||
}; | ||
if !config.is_dir { | ||
return punc2en::run(config.path); | ||
} | ||
let read_dir = match std::fs::read_dir(config.path) { | ||
Ok(files) => files, | ||
Err(e) => { | ||
println!( | ||
"{} {}", | ||
"Problem reading directory: ".red().bold(), | ||
e.to_string().red().bold() | ||
); | ||
std::process::exit(1); | ||
} | ||
}; | ||
read_dir.for_each(|f| { | ||
let file = match f { | ||
Err(e) => { | ||
println!( | ||
"{} {}", | ||
"Problem reading file: ".red().bold(), | ||
e.to_string().red().bold() | ||
); | ||
std::process::exit(1); | ||
} | ||
Ok(f) => f, | ||
}; | ||
let is_file = match file.file_type() { | ||
Ok(t) => t.is_file(), | ||
Err(e) => { | ||
println!( | ||
"{} {}", | ||
"Problem reading file type: ".red().bold(), | ||
e.to_string().red().bold() | ||
); | ||
std::process::exit(1); | ||
} | ||
}; | ||
if is_file { | ||
let file_path = file.path().to_str().unwrap().to_string(); | ||
punc2en::run(file_path); | ||
} | ||
}) | ||
} |