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

Fixes #24 #155

Merged
merged 1 commit into from
Nov 2, 2024
Merged
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
66 changes: 35 additions & 31 deletions twinTrim/flags.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.")
Expand All @@ -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))
Expand All @@ -47,61 +53,59 @@ 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:
duplicates_dict[original].append(duplicate)

# 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 = [
f"{idx + 1}) {duplicate} (Size: {os.path.getsize(duplicate)} bytes)" for idx, duplicate in enumerate(duplicates_list)
]

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.")
click.echo(click.style(f"Time taken: {time_taken:.2f} seconds.", fg=SUCCESS_COLOR))
logging.info(f"Total time taken: {time_taken:.2f} seconds.")
Loading