1
0
mirror of https://github.com/oliverbooth/fdup.git synced 2024-12-05 02:28:47 +00:00

feat: add visual feedback of hash process

This commit is contained in:
Oliver Booth 2024-04-16 20:59:43 +01:00
parent 9f7e605d38
commit a6a8f85ce9
Signed by: oliverbooth
GPG Key ID: E60B570D1B7557B5

View File

@ -23,7 +23,9 @@ internal sealed class ListCommand : AsyncCommand<ListSettings>
AnsiConsole.MarkupLineInterpolated($"Searching [cyan]{inputDirectory.FullName}[/]");
AnsiConsole.MarkupLine($"Recursive mode is {(settings.Recursive ? "[green]ON" : "[red]OFF")}[/]");
await SearchAsync(inputDirectory, settings);
await AnsiConsole.Status()
.StartAsync("Waiting to hash files...", DoHashWaitAsync)
.ConfigureAwait(false);
AnsiConsole.WriteLine();
@ -51,11 +53,35 @@ internal sealed class ListCommand : AsyncCommand<ListSettings>
AnsiConsole.MarkupLineInterpolated($"[yellow]Found [cyan]{duplicates}[/] duplicates![/]");
return 0;
async Task DoHashWaitAsync(StatusContext ctx)
{
await WaitForHashCompletionAsync(settings, inputDirectory, ctx);
}
}
private async Task SearchAsync(DirectoryInfo inputDirectory, ListSettings settings)
private async Task WaitForHashCompletionAsync(ListSettings settings,
DirectoryInfo inputDirectory,
StatusContext ctx)
{
var tasks = new List<Task>();
SearchDuplicates(inputDirectory, settings, tasks);
await Task.Run(() =>
{
int incompleteTasks;
do
{
incompleteTasks = tasks.Count(t => !t.IsCompleted);
ctx.Status($"Waiting to hash {incompleteTasks} {(incompleteTasks == 1 ? "file" : "files")}...");
ctx.Refresh();
} while (tasks.Count > 0 && incompleteTasks > 0);
ctx.Status("Hash complete");
}).ConfigureAwait(false);
}
private void SearchDuplicates(DirectoryInfo inputDirectory, ListSettings settings, ICollection<Task> tasks)
{
var directoryStack = new Stack<DirectoryInfo>([inputDirectory]);
while (directoryStack.Count > 0)
{
@ -77,8 +103,6 @@ internal sealed class ListCommand : AsyncCommand<ListSettings>
tasks.Add(Task.Run(() => ProcessFile(file)));
}
}
await Task.WhenAll(tasks);
}
private void ProcessFile(FileInfo file)