Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Code Optimizations #21

Merged
merged 2 commits into from
Jan 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions src/app.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
use crate::database;
use crate::output;
use crate::params::Params;
use crate::scanner;
use anyhow::Result;

pub struct App;

impl App {
pub fn init(app_args: &Params) -> Result<()> {
let connection = database::get_connection(app_args)?;
let duplicates = scanner::duplicates(app_args, &connection)?;
match app_args.interactive {
true => output::interactive(duplicates, app_args),
false => output::print(duplicates, app_args),
}

Ok(())
}
}
28 changes: 0 additions & 28 deletions src/app/event_handler.rs

This file was deleted.

4 changes: 0 additions & 4 deletions src/app/events.rs

This file was deleted.

Empty file removed src/app/formatter.rs
Empty file.
89 changes: 0 additions & 89 deletions src/app/mod.rs

This file was deleted.

54 changes: 0 additions & 54 deletions src/app/ui.rs

This file was deleted.

9 changes: 1 addition & 8 deletions src/database.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,7 @@
use std::env::temp_dir;

use anyhow::Result;

use crate::params::Params;

#[derive(Debug, Clone)]
pub struct File {
pub path: String,
pub hash: String,
}
use crate::file_manager::File;

fn db_connection_url(args: &Params) -> String {
match args.nocache {
Expand Down
7 changes: 6 additions & 1 deletion src/app/file_manager.rs → src/file_manager.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
use crate::database::File;
use anyhow::Result;
use colored::Colorize;

#[derive(Debug, Clone)]
pub struct File {
pub path: String,
pub hash: String,
}

pub fn delete_files(files: Vec<File>) -> Result<()> {
files.into_iter().for_each(|file| {
match std::fs::remove_file(file.path.clone()) {
Expand Down
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#![allow(unused)] // TODO: remove this once TUI is implemented
mod app;
mod database;
mod file_manager;
mod output;
mod params;
mod scanner;
Expand Down
64 changes: 35 additions & 29 deletions src/output.rs
Original file line number Diff line number Diff line change
@@ -1,22 +1,18 @@
use std::{collections::HashMap, fs, io};
use std::io::Write;

use std::{collections::HashMap, fs, io};
use anyhow::Result;
use chrono::offset::Utc;
use chrono::DateTime;
use colored::Colorize;
use humansize::{format_size, DECIMAL};
use itertools::Itertools;

use crate::app::file_manager;
use crate::database::File;
use crate::file_manager::{self, File};
use crate::params::Params;
use prettytable::{format, row, Cell, Row, Table};
use prettytable::{format, row, Table};
use unicode_segmentation::UnicodeSegmentation;

fn format_path(path: &str, opts: &Params) -> Result<String> {
let display_path = path.replace(&opts.get_directory()?, "");

let display_range = if display_path.chars().count() > 32 {
display_path
.graphemes(true)
Expand Down Expand Up @@ -82,7 +78,7 @@ fn scan_group_confirmation() -> Result<bool> {

match user_input.trim() {
"Y" | "y" => Ok(true),
_ => Ok(false)
_ => Ok(false),
}
}

Expand All @@ -108,42 +104,52 @@ fn process_group_action(duplicates: &Vec<File>, dup_index: usize, dup_size: usiz

print!("{esc}[2J{esc}[1;1H", esc = 27 as char);

if parsed_file_indices.is_empty() { return }
if parsed_file_indices.is_empty() {
return;
}

let files_to_delete = parsed_file_indices
.into_iter()
.map(|index| duplicates[index].clone());

println!("\n{}", "The following files will be deleted:".red());
files_to_delete.clone().enumerate().for_each(|(index, file)| {
println!("{}: {}", index.to_string().blue(), file.path);
});

files_to_delete
.clone()
.enumerate()
.for_each(|(index, file)| {
println!("{}: {}", index.to_string().blue(), file.path);
});

match scan_group_confirmation().unwrap() {
true => { file_manager::delete_files(files_to_delete.collect_vec()); },
false => println!("{}", "\nCancelled Delete Operation.".red())
true => {
file_manager::delete_files(files_to_delete.collect_vec());
}
false => println!("{}", "\nCancelled Delete Operation.".red()),
}
}

pub fn interactive(duplicates: Vec<File>, opts: &Params) {
print_meta_info(&duplicates, opts);
let grouped_duplicates = group_duplicates(duplicates);

grouped_duplicates.iter().enumerate().for_each(|(gindex, (hash, group))| {
let mut itable = Table::new();
itable.set_format(*format::consts::FORMAT_NO_BORDER_LINE_SEPARATOR);
itable.set_titles(row!["index", "filename", "size", "updated_at"]);
group.iter().enumerate().for_each(|(index, file)| {
itable.add_row(row![
index,
format_path(&file.path, opts).unwrap_or_default().blue(),
file_size(&file.path).unwrap_or_default().red(),
modified_time(&file.path).unwrap_or_default().yellow()
]);
grouped_duplicates
.iter()
.enumerate()
.for_each(|(gindex, (hash, group))| {
let mut itable = Table::new();
itable.set_format(*format::consts::FORMAT_NO_BORDER_LINE_SEPARATOR);
itable.set_titles(row!["index", "filename", "size", "updated_at"]);
group.iter().enumerate().for_each(|(index, file)| {
itable.add_row(row![
index,
format_path(&file.path, opts).unwrap_or_default().blue(),
file_size(&file.path).unwrap_or_default().red(),
modified_time(&file.path).unwrap_or_default().yellow()
]);
});

process_group_action(group, gindex, grouped_duplicates.len(), itable);
});

process_group_action(group, gindex, grouped_duplicates.len(), itable);
});
}

pub fn print(duplicates: Vec<File>, opts: &Params) {
Expand Down
3 changes: 1 addition & 2 deletions src/params.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use std::{fs, path::PathBuf};

use anyhow::{anyhow, Result};
use clap::Parser;

Expand All @@ -17,7 +16,7 @@ pub struct Params {
pub nocache: bool,
/// Delete files interactively
#[arg(long, short)]
pub interactive: bool
pub interactive: bool,
}

impl Params {
Expand Down
Loading