Skip to content

Commit

Permalink
Merge pull request #171 from adityakalburgi/update-dry-mode
Browse files Browse the repository at this point in the history
updated dry run feature
  • Loading branch information
techy4shri authored Nov 9, 2024
2 parents ae1a7ad + 573f2f2 commit 9999d53
Showing 1 changed file with 38 additions and 27 deletions.
65 changes: 38 additions & 27 deletions twinTrim/flags.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@
@click.option("--exclude", multiple=True, help="Files to exclude by name.")
@click.option("--label-color", default="yellow", type=str, help="Color of the label of progress bar.")
@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):
@click.option("--dry-run", is_flag=True, help="Simulate the deletion without actually removing any files.")
def cli(directory, all, min_size, max_size, file_type, exclude, label_color, bar_color, dry_run):
"""Find and manage duplicate files in the specified DIRECTORY."""

# Initialize the FileFilter object
Expand All @@ -43,7 +44,7 @@ def cli(directory, all, min_size, max_size, file_type, exclude, label_color, bar

if all:
logging.info("Deleting all duplicate files without asking.")
handleAllFlag(directory, file_filter, label_color, bar_color)
handleAllFlag(directory, file_filter, label_color, bar_color, dry_run)
return

start_time = time.time()
Expand All @@ -57,12 +58,12 @@ def cli(directory, all, min_size, max_size, file_type, exclude, label_color, bar
return

if not duplicates:
click.echo(click.style("No duplicate files found.", fg=SUCCESS_COLOR))
click.echo(click.style("No duplicate files found.", fg='green'))
logging.info("No duplicate files found.")
return

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")
logging.info(f"Found {len(duplicates)} set of duplicate files")

duplicates_dict = defaultdict(list)
for original, duplicate in duplicates:
Expand All @@ -73,37 +74,47 @@ def cli(directory, all, min_size, max_size, file_type, exclude, label_color, bar
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=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
)
]
)

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)
if dry_run:
# Log and display which files would be deleted in dry-run mode
click.echo(click.style("Dry Run Mode: These files would be deleted:", fg='yellow'))
for option in file_options:
click.echo(click.style(option, fg='red'))
logging.info(f"[Dry Run] Would delete files: {file_options}")
else:
click.echo(click.style("File deletion canceled.", fg=WARNING_COLOR))
# If not dry run, prompt for deletion
answers = inquirer.prompt(
[
inquirer.Checkbox(
'files',
message="Select files to delete (Use space to select, enter to confirm, or ctrl+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=WARNING_COLOR))

end_time = time.time()
time_taken = end_time - start_time
Expand Down

0 comments on commit 9999d53

Please sign in to comment.