103 lines
3.8 KiB
C#
103 lines
3.8 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.ComponentModel;
|
|
using System.Diagnostics.CodeAnalysis;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Text.Json;
|
|
using System.Text.Json.Serialization;
|
|
using System.Threading.Tasks;
|
|
using Newtonsoft.Json;
|
|
using RedundancyFinder;
|
|
using Spectre.Console;
|
|
using Spectre.Console.Cli;
|
|
namespace RedundancyFinderCLI
|
|
{
|
|
internal sealed class AnalyzeCommand : Command<AnalyzeCommand.Settings>
|
|
{
|
|
public sealed class Settings : CommandSettings
|
|
{
|
|
[Description("Path to analyze.")]
|
|
[DefaultValue("redundancies.json")]
|
|
[CommandArgument(0, "[path]")]
|
|
public string? Path { get; init; }
|
|
|
|
[Description("File extensions to search for. Comma separated.")]
|
|
[CommandOption("-e|--extensions")]
|
|
[DefaultValue(".jpg,.webp,.raw,.pdf,.xsl,.xslx,.doc,.docx,.txt,.jpeg,.mov,.mp4,.mp3,.wav,.bmp,.gif,.png,.cu,.mid,.msb,.mov,.avi,.wmv,.flv,.m4v,.bak ,.cpr,.xml,.psd")]
|
|
public string? Extensions { get; init; }
|
|
|
|
[Description("Show all information.")]
|
|
[CommandOption("-v|--verbose")]
|
|
[DefaultValue(false)]
|
|
public bool Verbose { get; init; }
|
|
|
|
}
|
|
public override int Execute([NotNull] CommandContext context, [NotNull] Settings settings)
|
|
{
|
|
|
|
Dictionary<string, Redundancy> redundancies = null;
|
|
|
|
|
|
AnsiConsole.Status()
|
|
.Start($"[yellow]Analyzing [/]'{settings.Path}'", ctx =>
|
|
{
|
|
ctx.Spinner(Spinner.Known.Clock);
|
|
ctx.SpinnerStyle = Style.Parse("yellow bold");
|
|
Thread.Sleep(1000);
|
|
redundancies = JsonConvert.DeserializeObject<Dictionary<string, Redundancy>>(File.ReadAllText(settings.Path));
|
|
|
|
});
|
|
|
|
|
|
|
|
if (redundancies == null)
|
|
{
|
|
|
|
}
|
|
|
|
var groups = redundancies
|
|
.GroupBy(x => Path.GetExtension(x.Value.Paths[0]), x => x.Value)
|
|
.ToDictionary(x => x.Key, x => new
|
|
{
|
|
FileSize = x.Sum(y => y.FileSize),
|
|
RedundancySize = x.Sum(y => y.FileSize * (y.Paths.Count - 1)),
|
|
RedundancyCount = x.Sum(y => y.Paths.Count) - 1,
|
|
RedundantFiles = x.Count(y => y.Paths.Count > 1)
|
|
});
|
|
// x => new { FileSize = x, RedundancySize = x.Sum()*(x.Count()-1)}
|
|
var extensions = settings.Extensions.Split(",", StringSplitOptions.RemoveEmptyEntries);
|
|
var table = new Table();
|
|
|
|
table.AddColumn("Extension");
|
|
table.AddColumn(new TableColumn("Files").RightAligned());
|
|
table.AddColumn(new TableColumn("Size").RightAligned());
|
|
table.AddColumn(new TableColumn("Redundancies").RightAligned());
|
|
table.AddColumn(new TableColumn("Redundancies size").RightAligned());
|
|
|
|
foreach (var extension in extensions.OrderBy(x => x))
|
|
{
|
|
if (groups.ContainsKey(extension))
|
|
{
|
|
|
|
var size = groups[extension].FileSize;
|
|
var sizeFormat = Global.GetSizeFormat((ulong)size);
|
|
|
|
var redundancySize = groups[extension].RedundancySize;
|
|
var redundancySizeFormat = Global.GetSizeFormat((ulong)redundancySize);
|
|
table.AddRow(
|
|
new Text(extension),
|
|
new Markup($"[darkgreen]{groups[extension].RedundantFiles}[/]"),
|
|
new Markup($"[green]{sizeFormat}[/]"),
|
|
new Markup($"[cyan]{groups[extension].RedundancyCount}[/]"),
|
|
new Markup($"[yellow]{redundancySizeFormat}[/]"));
|
|
}
|
|
}
|
|
AnsiConsole.Write(table);
|
|
return 0;
|
|
}
|
|
|
|
|
|
}
|
|
}
|