<refactor> 使用Spectre.Console组件

This commit is contained in:
nsnail 2022-12-07 17:54:45 +08:00
parent d0ffeb34a9
commit 62fef5b30b
4 changed files with 57 additions and 52 deletions

View File

@ -28,7 +28,7 @@
<value>输入文本为空</value>
</data>
<data name="SearchingFile" xml:space="preserve">
<value>查找文件...</value>
<value>查找文件</value>
</data>
<data name="PathNotFound" xml:space="preserve">
<value>指定的路径“{0}”不存在</value>
@ -182,4 +182,18 @@
<data name="FindGitReps" xml:space="preserve">
<value>查找 "{0}" 下所有git仓库目录... </value>
</data>
<data name="ExerciseMode" xml:space="preserve">
<value>演习模式, 不会真实修改文件!</value>
</data>
<data name="Read" xml:space="preserve">
<value>读取</value>
</data>
<data name="Write" xml:space="preserve">
<value>写入</value>
</data>
<data name="Break" xml:space="preserve">
<value>跳过</value>
</data>
</root>

View File

@ -1,6 +1,7 @@
using System.Reflection;
using System.Text;
using Dot;
using Spectre.Console;
Type[] LoadVerbs()
{
@ -18,9 +19,8 @@ async Task Run(object args)
var tool = ToolsFactory.Create(option);
await tool.Run();
if (option!.KeepSession) {
Console.WriteLine();
Console.WriteLine(Str.PressAnyKey);
Console.ReadKey();
AnsiConsole.MarkupLine(Str.PressAnyKey);
AnsiConsole.Console.Input.ReadKey(true);
}
}
@ -33,7 +33,7 @@ try {
await Parser.Default.ParseArguments(args, types).WithParsedAsync(Run);
}
catch (ArgumentException ex) {
Console.Error.WriteLine(ex.Message);
AnsiConsole.MarkupLine($"[red]{ex.Message}[/]");
return -1;
}

View File

@ -1,34 +1,25 @@
using System.Diagnostics.CodeAnalysis;
using Spectre.Console;
namespace Dot.ToLf;
public sealed class Main : ToolBase<Option>, IDisposable
public sealed class Main : ToolBase<Option>
{
private int _breakCnt;
private bool _disposed;
private ProgressTask _fileTask;
private static readonly object _lockObj = new();
private int _procedCnt;
private long _procedCnt;
private int _replaceCnt;
private ChildProgressBar _step2Bar;
private int _totalCnt;
public Main(Option opt) : base(opt) { }
~Main()
public Main(Option opt) : base(opt)
{
Dispose(false);
}
private void Dispose(bool disposing)
{
if (_disposed) return;
if (disposing) _step2Bar?.Dispose();
_disposed = true;
if (!Directory.Exists(Opt.Path))
throw new ArgumentException(nameof(Opt.Path), string.Format(Str.PathNotFound, Opt.Path));
}
private async ValueTask FileHandle(string file, CancellationToken _)
{
_step2Bar.Tick();
ShowMessage(1, 0, 0);
var tmpFile = $"{file}.tmp";
@ -76,7 +67,6 @@ public sealed class Main : ToolBase<Option>, IDisposable
if (isReplaced && !isBin) {
MoveFile(tmpFile, file);
ShowMessage(0, 1, 0);
}
else {
@ -92,36 +82,36 @@ public sealed class Main : ToolBase<Option>, IDisposable
_procedCnt += procedCnt;
_replaceCnt += replaceCnt;
_breakCnt += breakCnt;
_step2Bar.Message = string.Format(Str.ShowMessageTemp, _procedCnt, _totalCnt, _replaceCnt, _breakCnt);
if (procedCnt > 0) _fileTask.Increment(1);
_fileTask.Description
= $"{Str.Read}: [green]{_procedCnt}[/]/{_totalCnt}, {Str.Write}: [red]{_replaceCnt}[/], {Str.Break}: [gray]{_breakCnt}[/]";
}
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
[SuppressMessage("ReSharper", "PossibleMultipleEnumeration")]
public override async Task Run()
{
if (!Directory.Exists(Opt.Path))
throw new ArgumentException(nameof(Opt.Path), string.Format(Str.PathNotFound, Opt.Path));
using var step1Bar = new IndeterminateProgressBar(Str.SearchingFile, DefaultProgressBarOptions);
var fileList = EnumerateFiles(Opt.Path, Opt.Filter);
if (Opt.ReadOnly) AnsiConsole.MarkupLine("[gray]{0}[/]", Str.ExerciseMode);
IEnumerable<string> fileList;
await AnsiConsole.Progress()
.Columns(new ProgressBarColumn() //
, new ElapsedTimeColumn() //
, new PercentageColumn() //
, new SpinnerColumn() //
, new TaskDescriptionColumn { Alignment = Justify.Left } //
)
.StartAsync(async ctx => {
var taskSearchfile = ctx.AddTask(Str.SearchingFile).IsIndeterminate();
_fileTask = ctx.AddTask("-/-", false);
fileList = EnumerateFiles(Opt.Path, Opt.Filter);
_totalCnt = fileList.Count();
taskSearchfile.IsIndeterminate(false);
taskSearchfile.Increment(100);
step1Bar.Message = string.Format(Str.SearchingFileOK, _totalCnt);
step1Bar.Finished();
if (_totalCnt == 0) return;
_step2Bar = step1Bar.Spawn(_totalCnt, string.Empty, DefaultProgressBarOptions);
_fileTask.MaxValue = _totalCnt;
_fileTask.StartTask();
await Parallel.ForEachAsync(fileList, FileHandle);
});
}
}

View File

@ -27,6 +27,7 @@
<PackageReference Include="CommandLineParser" Version="2.9.1"/>
<PackageReference Include="NSExt" Version="1.0.6"/>
<PackageReference Include="ShellProgressBar" Version="5.2.0"/>
<PackageReference Include="Spectre.Console" Version="0.45.1-preview.0.46"/>
<PackageReference Include="TextCopy" Version="6.2.0"/>
</ItemGroup>