Skip to content

Commit

Permalink
v1
Browse files Browse the repository at this point in the history
  • Loading branch information
1uciuszzz committed Oct 13, 2023
0 parents commit 8b9bb65
Show file tree
Hide file tree
Showing 5 changed files with 284 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/target
151 changes: 151 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions Cargo.toml
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"
69 changes: 69 additions & 0 deletions src/lib.rs
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());
}
}
}
54 changes: 54 additions & 0 deletions src/main.rs
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);
}
})
}

0 comments on commit 8b9bb65

Please sign in to comment.