Intermediate commit. Was trying out a progress bar

This commit is contained in:
Adam Hathcock
2017-05-29 10:19:09 +01:00
parent e7720ccc4e
commit 9d24c53cfd
6 changed files with 118 additions and 32 deletions

View File

@@ -0,0 +1,32 @@
using System;
using System.Collections.Generic;
using System.IO;
using CommandLine;
namespace SharpCompress
{
public class BaseOptions
{
[Value(0, Min = 1)]
public IEnumerable<string> Path { get; set; }
protected IEnumerable<FileInfo> GetFilesFromPath()
{
foreach (var s in Path)
{
var fileInfo = new FileInfo(s);
if (fileInfo.Exists)
{
yield return fileInfo;
}
else
{
using (ConsoleHelper.PushError())
{
Console.WriteLine($"{s} does not exist");
}
}
}
}
}
}

View File

@@ -0,0 +1,32 @@
using System;
namespace SharpCompress
{
public static class ConsoleHelper
{
private class ConsoleTextPush : IDisposable
{
private readonly ConsoleColor _restoreColor;
public ConsoleTextPush(ConsoleColor displayColor)
{
_restoreColor = Console.ForegroundColor;
Console.ForegroundColor = displayColor;
}
public void Dispose()
{
Console.ForegroundColor = _restoreColor;
}
}
public static IDisposable PushForeground(ConsoleColor color)
{
return new ConsoleTextPush(color);
}
public static IDisposable PushError()
{
return PushForeground(ConsoleColor.Red);
}
}
}

View File

@@ -1,10 +1,37 @@
using System;
using CommandLine;
using SharpCompress.Readers;
namespace SharpCompress
{
[Verb("x", HelpText = "Extract an archive")]
public class ExtractOptions
public class ExtractOptions : BaseOptions
{
[Option('p', HelpText = "Path to extract to")]
public string ExtractionPath { get; set; } = AppContext.BaseDirectory;
public int Process()
{
foreach (var fileInfo in GetFilesFromPath())
{
Console.WriteLine($"Extracting archive {fileInfo.FullName} to path: {ExtractionPath}");
using (var reader = ReaderFactory.Open(fileInfo.OpenRead()))
{
while (reader.MoveToNextEntry())
{
var progress = new ProgressBar();
reader.EntryExtractionProgress += (sender, args) =>
{
progress.Report(args.ReaderProgress.PercentageReadExact);
};
Console.Write($"Extracting entry {reader.Entry.Key}: ");
reader.WriteEntryToDirectory(ExtractionPath);
Console.WriteLine();
}
}
}
return 1;
}
}
}

View File

@@ -1,54 +1,53 @@
using System;
using System.Collections.Generic;
using System.IO;
using CommandLine;
using SharpCompress.Archives;
using SharpCompress.Common;
namespace SharpCompress
{
[Verb("i", HelpText = "Information about an archive")]
public class InfoOptions
public class InfoOptions : BaseOptions
{
[Value(0, Min = 1)]
public IEnumerable<string> Path { get; set; }
[Option('e', HelpText = "Show Archive Entry Information")]
public bool ShowEntries { get; set; }
public int Process()
{
foreach (var s in Path)
foreach (var fileInfo in GetFilesFromPath())
{
if (File.Exists(s))
Console.WriteLine($"=== Archive: {fileInfo}");
try
{
Console.WriteLine($"=== Archive: {s}");
try
using (var archive = ArchiveFactory.Open(fileInfo.OpenRead()))
{
using (var archive = ArchiveFactory.Open(File.OpenRead(s)))
Console.WriteLine($"Archive Type: {archive.Type}");
Console.WriteLine($"Size: {archive.TotalSize}");
Console.WriteLine($"Uncompressed Size: {archive.TotalUncompressSize}");
if (ShowEntries)
{
Console.WriteLine($"Archive Type: {archive.Type}");
Console.WriteLine($"Size: {archive.TotalSize}");
Console.WriteLine($"Uncompressed Size: {archive.TotalUncompressSize}");
if (ShowEntries)
foreach (var archiveEntry in archive.Entries)
{
foreach (var archiveEntry in archive.Entries)
{
Console.WriteLine($"\tEntry: {archiveEntry.Key}");
}
Console.WriteLine($"\tEntry: {archiveEntry.Key}");
}
}
}
catch (Exception)
}
catch (InvalidFormatException)
{
using (ConsoleHelper.PushError())
{
Console.WriteLine("Archive Type is unknown.");
}
}
else
catch (Exception e)
{
Console.WriteLine($"{s} does not exist");
return 1;
using (ConsoleHelper.PushError())
{
Console.WriteLine($"Unhandled Error: {e}");
return 1;
}
}
}
return 0;

View File

@@ -9,13 +9,8 @@ namespace SharpCompress
return Parser.Default.ParseArguments<InfoOptions, ExtractOptions>(args)
.MapResult(
(InfoOptions opts) => opts.Process(),
(ExtractOptions opts) => Extract(opts),
(ExtractOptions opts) => opts.Process(),
errs => 1);
}
public static int Extract(ExtractOptions options)
{
return 0;
}
}
}

View File

@@ -14,5 +14,6 @@
<PackageReference Include="CommandLineParser">
<Version>2.1.1-beta</Version>
</PackageReference>
<PackageReference Include="goblinfactory.konsole" Version="2.0.2" />
</ItemGroup>
</Project>