Skip to content

Commit

Permalink
Merge pull request #16 from AmineZouitine/refacto
Browse files Browse the repository at this point in the history
Refacto
  • Loading branch information
AmineZouitine authored Oct 12, 2022
2 parents ed9fa2c + b59d502 commit 3b819f4
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 38 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ description = "remove files or directories and save them in trash."
documentation = "https://github.com/AmineZouitine/rmt.rs"
homepage = "https://github.com/AmineZouitine/rmt.rs"
repository = "https://github.com/AmineZouitine/rmt.rs"
version = "0.1.2"
version = "0.1.4"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
Expand Down
Empty file added restored_item
Empty file.
24 changes: 24 additions & 0 deletions src/args.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
use clap::Parser;


#[derive(Parser,Default,Debug)]
pub struct Arguments {
pub files: Vec<String>,
#[clap(short, long)]
pub is_force: Option<>, // -f --force
// pub confirmation: Confirmation,
// pub is_recursive: Option, // -r -R --recursive
// #[clap(short, long)]
// pub is_empty_dir: Option, // -d --dir
// #[clap(short, long)]
// pub is_verbose: OPTION, // -v --verbose
// // pub is_help: Option, // -h --help
// #[clap(short, long)]
// pub is_trash_display: Option, // rmt trash_display or rmt td
// #[clap(short, long)]
// pub is_trash_info: Option, // rmt trash_info or rmt ti
// #[clap(short, long)]
// pub is_trash_flush: Option, // rmt trash_flush or rmt tf
// #[clap(short, long)]
// pub is_destroy: Option,
}
8 changes: 8 additions & 0 deletions src/display_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,3 +155,11 @@ fn get_color_display_element(is_restore: bool, is_delete: bool, text: &str) -> C
text.white()
}
}

pub fn get_user_validation(message: &str) -> bool {
let mut user_input = String::new();
println!("{} {}", message, "[y/n]".green().bold());
std::io::stdin().read_line(&mut user_input).unwrap();
user_input.pop();
user_input == "y" || user_input == "yes"
}
12 changes: 4 additions & 8 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,11 @@ fn main() {
if arguments_manager.is_trash_display {
input_manager::start_display(&connection, is_test);
} else if arguments_manager.is_trash_flush {
let mut user_input = String::new();
println!(
"Are you sure to {} all the elements of your trash ? {}",
"flush".green().bold(),
"[y/n]".green().bold()
let message = format!(
"Are you sure to {} all the elements of your trash ?",
"flush".green().bold()
);
std::io::stdin().read_line(&mut user_input).unwrap();
user_input.pop();
if user_input == "y" || user_input == "yes" {
if display_manager::get_user_validation(&message) {
data_manager::delete_all_trash_item(&connection, is_test);
}
} else if arguments_manager.is_trash_info {
Expand Down
41 changes: 13 additions & 28 deletions src/trash_manager.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::arguments_manager::ArgumentsManager;
use crate::display_manager;
use crate::structure_manager::{self, get_home_directory_path};
use crate::{
config::Config, data_manager, structure_manager::get_trash_directory_path,
Expand Down Expand Up @@ -119,35 +120,19 @@ pub fn add_all_elements_to_trash(
is_test: bool,
arguments_manager: &ArgumentsManager,
) {
if arguments_manager.confirmation_once && element_name.len() > 3 {
let message = format!(
"Sure you want to delete all {} files ?",
element_name.len().to_string().bold().green()
);
if !display_manager::get_user_validation(&message) {
return;
}
}
for path in element_name {
let mut user_input = String::new();
if arguments_manager.confirmation_always {
println!(
"Are you sure to delete {} ? {}",
path.bold().green(),
"[y/n]".green().bold()
);
std::io::stdin().read_line(&mut user_input).unwrap();
user_input.pop();
if user_input == "y" || user_input == "yes" {
add_element_to_trash(connection, config, path, is_test, arguments_manager);
}
} else if arguments_manager.confirmation_once {
if element_name.len() > 3 {
println!(
"Sure you want to delete all {} files ? {}",
element_name.len().to_string().bold().green(),
"[y/n]".green().bold()
);
std::io::stdin().read_line(&mut user_input).unwrap();
user_input.pop();
if user_input == "y" || user_input == "yes" {
add_element_to_trash(connection, config, path, is_test, arguments_manager);
}
} else {
add_element_to_trash(connection, config, path, is_test, arguments_manager);
}
} else {
let message = format!("Are you sure to delete {} ?", path.bold().green());
if !arguments_manager.confirmation_always || display_manager::get_user_validation(&message)
{
add_element_to_trash(connection, config, path, is_test, arguments_manager);
}
}
Expand Down

0 comments on commit 3b819f4

Please sign in to comment.