<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> <value>输入文本为空</value>
</data> </data>
<data name="SearchingFile" xml:space="preserve"> <data name="SearchingFile" xml:space="preserve">
<value>查找文件...</value> <value>查找文件</value>
</data> </data>
<data name="PathNotFound" xml:space="preserve"> <data name="PathNotFound" xml:space="preserve">
<value>指定的路径“{0}”不存在</value> <value>指定的路径“{0}”不存在</value>
@ -182,4 +182,18 @@
<data name="FindGitReps" xml:space="preserve"> <data name="FindGitReps" xml:space="preserve">
<value>查找 "{0}" 下所有git仓库目录... </value> <value>查找 "{0}" 下所有git仓库目录... </value>
</data> </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> </root>

View File

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

View File

@ -1,34 +1,25 @@
using System.Diagnostics.CodeAnalysis; using System.Diagnostics.CodeAnalysis;
using Spectre.Console;
namespace Dot.ToLf; namespace Dot.ToLf;
public sealed class Main : ToolBase<Option>, IDisposable public sealed class Main : ToolBase<Option>
{ {
private int _breakCnt; private int _breakCnt;
private bool _disposed; private ProgressTask _fileTask;
private static readonly object _lockObj = new(); private static readonly object _lockObj = new();
private int _procedCnt; private long _procedCnt;
private int _replaceCnt; private int _replaceCnt;
private ChildProgressBar _step2Bar; private int _totalCnt;
private int _totalCnt;
public Main(Option opt) : base(opt) { }
public Main(Option opt) : base(opt)
~Main()
{ {
Dispose(false); if (!Directory.Exists(Opt.Path))
} throw new ArgumentException(nameof(Opt.Path), string.Format(Str.PathNotFound, Opt.Path));
private void Dispose(bool disposing)
{
if (_disposed) return;
if (disposing) _step2Bar?.Dispose();
_disposed = true;
} }
private async ValueTask FileHandle(string file, CancellationToken _) private async ValueTask FileHandle(string file, CancellationToken _)
{ {
_step2Bar.Tick();
ShowMessage(1, 0, 0); ShowMessage(1, 0, 0);
var tmpFile = $"{file}.tmp"; var tmpFile = $"{file}.tmp";
@ -76,7 +67,6 @@ public sealed class Main : ToolBase<Option>, IDisposable
if (isReplaced && !isBin) { if (isReplaced && !isBin) {
MoveFile(tmpFile, file); MoveFile(tmpFile, file);
ShowMessage(0, 1, 0); ShowMessage(0, 1, 0);
} }
else { else {
@ -89,39 +79,39 @@ public sealed class Main : ToolBase<Option>, IDisposable
private void ShowMessage(int procedCnt, int replaceCnt, int breakCnt) private void ShowMessage(int procedCnt, int replaceCnt, int breakCnt)
{ {
lock (_lockObj) { lock (_lockObj) {
_procedCnt += procedCnt; _procedCnt += procedCnt;
_replaceCnt += replaceCnt; _replaceCnt += replaceCnt;
_breakCnt += breakCnt; _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")] [SuppressMessage("ReSharper", "PossibleMultipleEnumeration")]
public override async Task Run() public override async Task Run()
{ {
if (!Directory.Exists(Opt.Path)) if (Opt.ReadOnly) AnsiConsole.MarkupLine("[gray]{0}[/]", Str.ExerciseMode);
throw new ArgumentException(nameof(Opt.Path), string.Format(Str.PathNotFound, Opt.Path)); 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);
_fileTask.MaxValue = _totalCnt;
using var step1Bar = new IndeterminateProgressBar(Str.SearchingFile, DefaultProgressBarOptions); _fileTask.StartTask();
await Parallel.ForEachAsync(fileList, FileHandle);
});
var fileList = EnumerateFiles(Opt.Path, Opt.Filter);
_totalCnt = fileList.Count();
step1Bar.Message = string.Format(Str.SearchingFileOK, _totalCnt);
step1Bar.Finished();
if (_totalCnt == 0) return;
_step2Bar = step1Bar.Spawn(_totalCnt, string.Empty, DefaultProgressBarOptions);
await Parallel.ForEachAsync(fileList, FileHandle);
} }
} }

View File

@ -27,6 +27,7 @@
<PackageReference Include="CommandLineParser" Version="2.9.1"/> <PackageReference Include="CommandLineParser" Version="2.9.1"/>
<PackageReference Include="NSExt" Version="1.0.6"/> <PackageReference Include="NSExt" Version="1.0.6"/>
<PackageReference Include="ShellProgressBar" Version="5.2.0"/> <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"/> <PackageReference Include="TextCopy" Version="6.2.0"/>
</ItemGroup> </ItemGroup>