This commit is contained in:
parent
20aeda31e8
commit
75a0c21c01
1 changed files with 28 additions and 28 deletions
56
src/main.rs
56
src/main.rs
|
@ -2,20 +2,20 @@ use std::env;
|
||||||
use std::fs;
|
use std::fs;
|
||||||
|
|
||||||
enum Mode {
|
enum Mode {
|
||||||
Info,
|
Info,
|
||||||
Add,
|
Add,
|
||||||
Remove,
|
Remove,
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let mut mode = Mode::Info;
|
let mut mode = Mode::Info;
|
||||||
|
|
||||||
for arg in env::args().skip(1) {
|
for arg in env::args().skip(1) {
|
||||||
match arg.as_str() {
|
match arg.as_str() {
|
||||||
"--add" => { mode = Mode::Add; }
|
"--add" => { mode = Mode::Add; }
|
||||||
"--remove" => { mode = Mode::Remove; }
|
"--remove" => { mode = Mode::Remove; }
|
||||||
_ => { process_file(&arg, &mode); }
|
_ => { process_file(&arg, &mode); }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -23,27 +23,27 @@ fn process_file(path: &str, mode: &Mode) {
|
||||||
let mut content = fs::read_to_string(path).expect("could not read file");
|
let mut content = fs::read_to_string(path).expect("could not read file");
|
||||||
|
|
||||||
if ends_with_newline(&content) {
|
if ends_with_newline(&content) {
|
||||||
match mode {
|
match mode {
|
||||||
Mode::Remove => {
|
Mode::Remove => {
|
||||||
content.pop();
|
content.pop();
|
||||||
fs::write(path, content).expect("could not write file");
|
fs::write(path, content).expect("could not write file");
|
||||||
println!("\x1B[1m{}\x1B[0m: \x1B[31mno final newline [removed]\x1B[0m", path);
|
println!("\x1B[1m{}\x1B[0m: \x1B[31mno final newline [removed]\x1B[0m", path);
|
||||||
}
|
}
|
||||||
_ => {
|
_ => {
|
||||||
println!("\x1B[1m{}\x1B[0m: \x1B[32mfinal newline\x1B[0m", path);
|
println!("\x1B[1m{}\x1B[0m: \x1B[32mfinal newline\x1B[0m", path);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
match mode {
|
match mode {
|
||||||
Mode::Add => {
|
Mode::Add => {
|
||||||
content.push('\n');
|
content.push('\n');
|
||||||
fs::write(path, content).expect("could not write file");
|
fs::write(path, content).expect("could not write file");
|
||||||
println!("\x1B[1m{}\x1B[0m: \x1B[32mfinal newline [added]\x1B[0m", path);
|
println!("\x1B[1m{}\x1B[0m: \x1B[32mfinal newline [added]\x1B[0m", path);
|
||||||
}
|
}
|
||||||
_ => {
|
_ => {
|
||||||
println!("\x1B[1m{}\x1B[0m: \x1B[31mno final newline\x1B[0m", path);
|
println!("\x1B[1m{}\x1B[0m: \x1B[31mno final newline\x1B[0m", path);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue