1
0
mirror of https://github.com/oliverbooth/fdup.git synced 2024-12-04 19:08:49 +00:00

feat: enable verbose output to list hash of every file

This commit is contained in:
Oliver Booth 2024-04-17 14:17:56 +01:00
parent d2ca59e174
commit 435318cd20
Signed by: oliverbooth
GPG Key ID: E60B570D1B7557B5
2 changed files with 9 additions and 5 deletions

View File

@ -1,6 +1,4 @@
using System.Collections.Concurrent;
using System.Diagnostics;
using System.Security;
using System.Security.Cryptography;
using System.Text;
using Spectre.Console;
@ -110,7 +108,7 @@ internal sealed class ListCommand : AsyncCommand<ListSettings>
{
string relativeFilePath = Path.GetRelativePath(inputDirectory.FullName, file.FullName);
AnsiConsole.MarkupLineInterpolated($"Checking hash for [cyan]{relativeFilePath}[/]");
tasks.Add(Task.Run(() => ProcessFile(file)));
tasks.Add(Task.Run(() => ProcessFile(file, settings)));
}
}
catch (Exception ex)
@ -120,7 +118,7 @@ internal sealed class ListCommand : AsyncCommand<ListSettings>
}
}
private void ProcessFile(FileInfo file)
private void ProcessFile(FileInfo file, ListSettings settings)
{
Span<byte> buffer = stackalloc byte[64];
try
@ -129,7 +127,8 @@ internal sealed class ListCommand : AsyncCommand<ListSettings>
using BufferedStream bufferedStream = new BufferedStream(stream, 1048576 /* 1MB */);
SHA512.HashData(bufferedStream, buffer);
string hash = ByteSpanToString(buffer);
Trace.WriteLine($"{file.FullName}: {hash}");
if (settings.Verbose)
AnsiConsole.WriteLine($"{file.FullName} ->\n {hash}");
if (!_fileHashMap.TryGetValue(hash, out List<FileInfo>? cache))
_fileHashMap[hash] = cache = new List<FileInfo>();

View File

@ -13,4 +13,9 @@ internal sealed class ListSettings : CommandSettings
[Description("When this flag is set, the directory will be scanned recursively. This may take longer.")]
[DefaultValue(false)]
public bool Recursive { get; set; } = false;
[CommandOption("--verbose")]
[Description("Enable verbose output.")]
[DefaultValue(false)]
public bool Verbose { get; set; } = false;
}