From 7f261e33f7b6f4a023e579f42f9966a5b0225635 Mon Sep 17 00:00:00 2001 From: RhiyaBhat Date: Sun, 27 Oct 2024 21:10:56 +0530 Subject: [PATCH] Fixes #24 --- twinTrim/flags.py | 66 +++++++++++++++++++++++++---------------------- 1 file changed, 35 insertions(+), 31 deletions(-) diff --git a/twinTrim/flags.py b/twinTrim/flags.py index 30d7f61..032f871 100644 --- a/twinTrim/flags.py +++ b/twinTrim/flags.py @@ -9,12 +9,18 @@ from twinTrim.dataStructures.fileFilter import FileFilter # Setting up logging configuration -logging.basicConfig ( - filename='duplicate_file_manager.log', - level = logging.INFO, - format = '%(asctime)s - %(levelname)s - %(message)s' +logging.basicConfig( + filename='duplicate_file_manager.log', + level=logging.INFO, + format='%(asctime)s - %(levelname)s - %(message)s' ) +# Define color constants for consistency +INFO_COLOR = 'blue' +WARNING_COLOR = 'yellow' +SUCCESS_COLOR = 'green' +ERROR_COLOR = 'red' + @click.command() @click.argument("directory", type=click.Path(exists=True)) @click.option("--all", is_flag=True, help="Delete duplicates automatically without asking.") @@ -26,7 +32,7 @@ @click.option("--bar-color", default='#aaaaaa', type=str, help="Color of the progress bar.") def cli(directory, all, min_size, max_size, file_type, exclude, label_color, bar_color): """Find and manage duplicate files in the specified DIRECTORY.""" - + # Initialize the FileFilter object file_filter = FileFilter() file_filter.setMinFileSize(parse_size(min_size)) @@ -47,16 +53,16 @@ def cli(directory, all, min_size, max_size, file_type, exclude, label_color, bar duplicates = find_duplicates(directory, file_filter, label_color, bar_color) except Exception as e: logging.error(f"Error finding duplicates: {str(e)}") - click.echo(click.style("Error while finding duplicates. Check the log for details.", fg='red')) + click.echo(click.style("Error while finding duplicates. Check the log for details.", fg=ERROR_COLOR)) return if not duplicates: - click.echo(click.style("No duplicate files found.", fg='green')) + click.echo(click.style("No duplicate files found.", fg=SUCCESS_COLOR)) logging.info("No duplicate files found.") return - click.echo(click.style(f"Found {len(duplicates)} sets of duplicate files:", fg='yellow')) - logging.info(f"Found {len(duplicates)} set of duplicate files") + click.echo(click.style(f"Found {len(duplicates)} sets of duplicate files:", fg=WARNING_COLOR)) + logging.info(f"Found {len(duplicates)} sets of duplicate files") duplicates_dict = defaultdict(list) for original, duplicate in duplicates: @@ -64,11 +70,10 @@ def cli(directory, all, min_size, max_size, file_type, exclude, label_color, bar # Process each set of duplicates for original, duplicates_list in duplicates_dict.items(): - click.echo(click.style(f"Original file: \"{original}\"", fg='cyan')) - click.echo(click.style(f"Number of duplicate files found: {len(duplicates_list)}", fg='cyan')) + click.echo(click.style(f"Original file: \"{original}\"", fg=INFO_COLOR)) + click.echo(click.style(f"Number of duplicate files found: {len(duplicates_list)}", fg=INFO_COLOR)) logging.info(f"Original file: \"{original}\" with {len(duplicates_list)} duplicates") - - click.echo(click.style("They are:", fg='cyan')) + click.echo(click.style("They are:", fg=INFO_COLOR)) # Create file options with additional information file_options = [ @@ -76,32 +81,31 @@ def cli(directory, all, min_size, max_size, file_type, exclude, label_color, bar ] answers = inquirer.prompt( - [ - inquirer.Checkbox( - 'files', - message="Select files to delete (Use space to select, enter to confirm, or ctr + c to cancel, arrow key to navigate.)", - choices=file_options, - validate=lambda answer, current: len(answer) > 0 or "You must choose at least one file.", - ), - inquirer.Confirm( - 'confirm', - message="Are you sure you want to delete the selected files?", - default=True - ) - ]) - + [ + inquirer.Checkbox( + 'files', + message="Select files to delete (Use space to select, enter to confirm, or ctr + c to cancel, arrow key to navigate.)", + choices=file_options, + validate=lambda answer, current: len(answer) > 0 or "You must choose at least one file.", + ), + inquirer.Confirm( + 'confirm', + message="Are you sure you want to delete the selected files?", + default=True + ) + ] + ) if answers and answers['confirm']: selected_files = answers['files'] # Convert the selected options back to the original file paths files_to_delete = [duplicates_list[int(option.split(")")[0]) - 1] for option in selected_files] - for file_path in files_to_delete: handle_and_remove(file_path) else: - click.echo(click.style("File deletion canceled.", fg='yellow')) + click.echo(click.style("File deletion canceled.", fg=WARNING_COLOR)) end_time = time.time() time_taken = end_time - start_time - click.echo(click.style(f"Time taken: {time_taken:.2f} seconds.", fg='green')) - logging.info(f"Total time taken: {time_taken:.2f} seconds.") \ No newline at end of file + click.echo(click.style(f"Time taken: {time_taken:.2f} seconds.", fg=SUCCESS_COLOR)) + logging.info(f"Total time taken: {time_taken:.2f} seconds.")