mirror of
https://github.com/nivekuil/rip
synced 2025-02-18 13:08:27 +00:00
Refactoring
This commit is contained in:
parent
d601ffd137
commit
6b7337d26d
1 changed files with 6 additions and 7 deletions
13
src/main.rs
13
src/main.rs
|
@ -65,8 +65,8 @@ Send files to the graveyard (/tmp/.graveyard) instead of unlinking them.")
|
||||||
}
|
}
|
||||||
|
|
||||||
if matches.is_present("resurrect") {
|
if matches.is_present("resurrect") {
|
||||||
let histfile: PathBuf = graveyard.join(HISTFILE);
|
let histfile: &Path = &graveyard.join(HISTFILE);
|
||||||
if let Ok(s) = read_last_line(&histfile) {
|
if let Ok(s) = read_last_line(histfile) {
|
||||||
let mut tokens = StrExt::split(s.as_str(), "\t");
|
let mut tokens = StrExt::split(s.as_str(), "\t");
|
||||||
let dest = tokens.next().expect("Bad histfile format: column A");
|
let dest = tokens.next().expect("Bad histfile format: column A");
|
||||||
let source = tokens.next().expect("Bad histfile format: column B");
|
let source = tokens.next().expect("Bad histfile format: column B");
|
||||||
|
@ -74,12 +74,11 @@ Send files to the graveyard (/tmp/.graveyard) instead of unlinking them.")
|
||||||
println!("ERROR: {}: {}", e, source);
|
println!("ERROR: {}: {}", e, source);
|
||||||
println!("Maybe the file was removed from the graveyard.");
|
println!("Maybe the file was removed from the graveyard.");
|
||||||
if prompt_yes("Remove it from the history?") {
|
if prompt_yes("Remove it from the history?") {
|
||||||
delete_last_line(&histfile).unwrap();
|
delete_last_line(histfile).unwrap();
|
||||||
}
|
}
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
println!("Returned {} to {}", source, dest);
|
println!("Returned {} to {}", source, dest);
|
||||||
delete_last_line(&histfile).expect("Failed to remove history");
|
delete_last_line(histfile).expect("Failed to remove history");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
|
@ -253,7 +252,7 @@ fn prompt_yes(prompt: &str) -> bool {
|
||||||
false
|
false
|
||||||
}
|
}
|
||||||
|
|
||||||
fn read_last_line(path: &PathBuf) -> std::io::Result<String> {
|
fn read_last_line(path: &Path) -> std::io::Result<String> {
|
||||||
match fs::File::open(path) {
|
match fs::File::open(path) {
|
||||||
Ok(f) => BufReader::new(f).lines().last().expect("Empty histfile"),
|
Ok(f) => BufReader::new(f).lines().last().expect("Empty histfile"),
|
||||||
Err(e) => Err(e)
|
Err(e) => Err(e)
|
||||||
|
@ -262,7 +261,7 @@ fn read_last_line(path: &PathBuf) -> std::io::Result<String> {
|
||||||
|
|
||||||
/// Set the length of the file to the difference between the size of the file
|
/// Set the length of the file to the difference between the size of the file
|
||||||
/// and the size of last line of the file.
|
/// and the size of last line of the file.
|
||||||
fn delete_last_line(path: &PathBuf) -> std::io::Result<()> {
|
fn delete_last_line(path: &Path) -> std::io::Result<()> {
|
||||||
match fs::OpenOptions::new().write(true).open(path) {
|
match fs::OpenOptions::new().write(true).open(path) {
|
||||||
Ok(f) => {
|
Ok(f) => {
|
||||||
let total: u64 = f.metadata().expect("Failed to stat file").len();
|
let total: u64 = f.metadata().expect("Failed to stat file").len();
|
||||||
|
|
Loading…
Add table
Reference in a new issue