<refactor> * 去除ShellProgressBar组件

This commit is contained in:
2022-12-08 09:42:31 +08:00
parent a77ba1966b
commit de2e156fa5
11 changed files with 211 additions and 324 deletions

View File

@ -1,110 +1,47 @@
using System.Diagnostics.CodeAnalysis;
namespace Dot.RmBom;
public sealed class Main : ToolBase<Option>, IDisposable
public sealed class Main : FilesTool<Option>
{
private int _breakCnt;
private bool _disposed;
private static readonly object _lockObj = new();
private int _procedCnt;
private ChildProgressBar _step2Bar;
private int _totalCnt;
private int _trimCnt;
private readonly byte[] _utf8Bom = { 0xef, 0xbb, 0xbf };
private readonly byte[] _utf8Bom = { 0xef, 0xbb, 0xbf };
public Main(Option opt) : base(opt) { }
~Main()
{
Dispose(false);
}
private bool CreateTempFile(Stream fsr, string tmpFile)
private bool CloneFileWithoutBom(Stream fsr, ref string tempFile)
{
Span<byte> buffer = stackalloc byte[_utf8Bom.Length];
var readLen = fsr.Read(buffer);
if (readLen != _utf8Bom.Length || !buffer.SequenceEqual(_utf8Bom)) return false;
using var fsw = OpenFileStream(tmpFile, FileMode.OpenOrCreate, FileAccess.Write);
using var fsw = CreateTempFile(out tempFile);
int data;
while ((data = fsr.ReadByte()) != -1) fsw.WriteByte((byte)data);
return true;
}
private void Dispose(bool disposing)
{
if (_disposed) return;
if (disposing) _step2Bar?.Dispose();
_disposed = true;
}
private async ValueTask FileHandle(string file, CancellationToken _)
protected override async ValueTask FileHandle(string file, CancellationToken _)
{
_step2Bar.Tick();
ShowMessage(1, 0, 0);
var tmpFile = $"{file}.tmp";
bool isReplaced;
string tmpFile = default;
await using (var fsr = OpenFileStream(file, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) {
if (!Opt.WriteMode) { //测试,只读模式
ShowMessage(0, 1, 0);
return;
}
if (fsr is null) {
ShowMessage(0, 0, 1);
return;
}
isReplaced = CreateTempFile(fsr, tmpFile);
if (CloneFileWithoutBom(fsr, ref tmpFile)) {
if (Opt.WriteMode) CopyFile(tmpFile, file);
ShowMessage(0, 1, 0);
UpdateStats(Path.GetExtension(file));
}
else {
ShowMessage(0, 0, 1);
}
}
if (isReplaced) {
MoveFile(tmpFile, file);
ShowMessage(0, 1, 0);
}
else {
ShowMessage(0, 0, 1);
}
}
private void ShowMessage(int procedCnt, int replaceCnt, int breakCnt)
{
lock (_lockObj) {
_procedCnt += procedCnt;
_trimCnt += replaceCnt;
_breakCnt += breakCnt;
_step2Bar.Message = string.Format(Str.ShowMessageTemp, _procedCnt, _totalCnt, _trimCnt, _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);
_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);
if (tmpFile != default) File.Delete(tmpFile);
}
}