mirror of
https://github.com/nsnail/dot.git
synced 2025-04-14 09:32:49 +08:00
parent
de264e58a0
commit
6c7102af61
@ -1,113 +0,0 @@
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
|
||||
namespace Dot.Convert2Lf;
|
||||
|
||||
public sealed class Main : Tool<Option>, IDisposable
|
||||
{
|
||||
private int _breakCnt;
|
||||
private bool _disposed;
|
||||
private static readonly object _lockObj = new();
|
||||
private int _procedCnt;
|
||||
private int _replaceCnt;
|
||||
private ChildProgressBar _step2Bar;
|
||||
private int _totalCnt;
|
||||
public Main(Option opt) : base(opt) { }
|
||||
|
||||
|
||||
~Main()
|
||||
{
|
||||
Dispose(false);
|
||||
}
|
||||
|
||||
private void Dispose(bool disposing)
|
||||
{
|
||||
if (_disposed) return;
|
||||
if (disposing) _step2Bar?.Dispose();
|
||||
_disposed = true;
|
||||
}
|
||||
|
||||
private void FileHandle(string file)
|
||||
{
|
||||
_step2Bar.Tick();
|
||||
ShowMessage(1, 0, 0);
|
||||
|
||||
var tmpFile = $"{file}.tmp";
|
||||
var isReplaced = false;
|
||||
var isBin = false;
|
||||
int data;
|
||||
|
||||
using (var fsr = new FileStream(file, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) {
|
||||
using var fsw = new FileStream(tmpFile, FileMode.OpenOrCreate, FileAccess.Write);
|
||||
|
||||
while ((data = fsr.ReadByte()) != -1) {
|
||||
switch (data) {
|
||||
case 0x0d when fsr.ReadByte() == 0x0a: // crlf windows
|
||||
fsw.WriteByte(0x0a);
|
||||
isReplaced = true;
|
||||
continue;
|
||||
case 0x0d: //cr macos
|
||||
fsw.WriteByte(0x0a);
|
||||
fsr.Seek(-1, SeekOrigin.Current);
|
||||
isReplaced = true;
|
||||
continue;
|
||||
case 0x00 or 0xff: //非文本文件
|
||||
isBin = true;
|
||||
break;
|
||||
default:
|
||||
fsw.WriteByte((byte)data);
|
||||
continue;
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if (isReplaced && !isBin) {
|
||||
MoveFile(tmpFile, file);
|
||||
|
||||
ShowMessage(0, 1, 0);
|
||||
}
|
||||
else {
|
||||
File.Delete(tmpFile);
|
||||
ShowMessage(0, 0, 1);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private void ShowMessage(int procedCnt, int replaceCnt, int breakCnt)
|
||||
{
|
||||
lock (_lockObj) {
|
||||
_procedCnt += procedCnt;
|
||||
_replaceCnt += replaceCnt;
|
||||
_breakCnt += breakCnt;
|
||||
_step2Bar.Message = $"已处理:{_procedCnt}/{_totalCnt},替换:{_replaceCnt},跳过:{_breakCnt}";
|
||||
}
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
Dispose(true);
|
||||
GC.SuppressFinalize(this);
|
||||
}
|
||||
|
||||
[SuppressMessage("ReSharper", "PossibleMultipleEnumeration")]
|
||||
public override void Run()
|
||||
{
|
||||
if (!Directory.Exists(Opt.Path)) throw new ArgumentException(nameof(Opt.Path), $"指定的路径“{Opt.Path}”不存在");
|
||||
|
||||
|
||||
using var step1Bar = new IndeterminateProgressBar("查找文件...", DefaultProgressBarOptions);
|
||||
|
||||
|
||||
var fileList = EnumerateFiles(Opt.Path, Opt.Filter);
|
||||
_totalCnt = fileList.Count();
|
||||
|
||||
step1Bar.Message = "查找文件...OK";
|
||||
step1Bar.Finished();
|
||||
|
||||
_step2Bar = step1Bar.Spawn(_totalCnt, string.Empty, DefaultProgressBarOptions);
|
||||
|
||||
Parallel.ForEach(fileList, FileHandle);
|
||||
}
|
||||
}
|
@ -1,11 +0,0 @@
|
||||
namespace Dot.Convert2Lf;
|
||||
|
||||
[Verb("convert-lf", HelpText = "换行符转换为lf")]
|
||||
public class Option : IOption
|
||||
{
|
||||
[Option('f', "filter", Required = false, HelpText = "文件通配符", Default = "*.*")]
|
||||
public string Filter { get; set; } //normal options here
|
||||
|
||||
[Option('p', "path", Required = false, HelpText = "要处理的目录路径", Default = ".")]
|
||||
public string Path { get; set; }
|
||||
}
|
@ -1,103 +0,0 @@
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using NSExt.Extensions;
|
||||
|
||||
namespace Dot.RemoveTrailingWhiteSpace;
|
||||
|
||||
public sealed class Main : Tool<Option>, IDisposable
|
||||
{
|
||||
private int _breakCnt;
|
||||
private bool _disposed;
|
||||
private static readonly object _lockObj = new();
|
||||
private int _procedCnt;
|
||||
private int _replaceCnt;
|
||||
private ChildProgressBar _step2Bar;
|
||||
private int _totalCnt;
|
||||
public Main(Option opt) : base(opt) { }
|
||||
|
||||
|
||||
~Main()
|
||||
{
|
||||
Dispose(false);
|
||||
}
|
||||
|
||||
private void Dispose(bool disposing)
|
||||
{
|
||||
if (_disposed) return;
|
||||
if (disposing) _step2Bar?.Dispose();
|
||||
_disposed = true;
|
||||
}
|
||||
|
||||
private void FileHandle(string file)
|
||||
{
|
||||
_step2Bar.Tick();
|
||||
ShowMessage(1, 0, 0);
|
||||
var spacesCnt = 0;
|
||||
|
||||
using var fsr = OpenFileToWrite(file);
|
||||
if (fsr.Length == 0 || (spacesCnt = GetSpacesCnt(fsr)) == 0) {
|
||||
ShowMessage(0, 0, 1);
|
||||
return;
|
||||
}
|
||||
|
||||
fsr.Seek(0, SeekOrigin.Begin);
|
||||
if (!fsr.IsTextStream()) return;
|
||||
ShowMessage(0, 1, 0);
|
||||
fsr.SetLength(fsr.Length - spacesCnt);
|
||||
}
|
||||
|
||||
private static int GetSpacesCnt(Stream fsr)
|
||||
{
|
||||
var trimLen = 0;
|
||||
fsr.Seek(-1, SeekOrigin.End);
|
||||
int data;
|
||||
while ((data = fsr.ReadByte()) != -1)
|
||||
if (new[] { 0x20, 0x0d, 0x0a }.Contains(data)) {
|
||||
++trimLen;
|
||||
if (fsr.Position - 2 < 0) break;
|
||||
fsr.Seek(-2, SeekOrigin.Current);
|
||||
}
|
||||
else {
|
||||
break;
|
||||
}
|
||||
|
||||
return trimLen;
|
||||
}
|
||||
|
||||
|
||||
private void ShowMessage(int procedCnt, int removeCnt, int breakCnt)
|
||||
{
|
||||
lock (_lockObj) {
|
||||
_procedCnt += procedCnt;
|
||||
_replaceCnt += removeCnt;
|
||||
_breakCnt += breakCnt;
|
||||
_step2Bar.Message = $"已处理:{_procedCnt}/{_totalCnt},替换:{_replaceCnt},跳过:{_breakCnt}";
|
||||
}
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
Dispose(true);
|
||||
GC.SuppressFinalize(this);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
[SuppressMessage("ReSharper", "PossibleMultipleEnumeration")]
|
||||
public override void Run()
|
||||
{
|
||||
if (!Directory.Exists(Opt.Path)) throw new ArgumentException(nameof(Opt.Path), $"指定的路径“{Opt.Path}”不存在");
|
||||
|
||||
|
||||
using var step1Bar = new IndeterminateProgressBar("查找文件...", DefaultProgressBarOptions);
|
||||
|
||||
|
||||
var fileList = EnumerateFiles(Opt.Path, Opt.Filter);
|
||||
_totalCnt = fileList.Count();
|
||||
|
||||
step1Bar.Message = "查找文件...OK";
|
||||
step1Bar.Finished();
|
||||
|
||||
_step2Bar = step1Bar.Spawn(_totalCnt, string.Empty, DefaultProgressBarOptions);
|
||||
|
||||
Parallel.ForEach(fileList, FileHandle);
|
||||
}
|
||||
}
|
@ -1,11 +0,0 @@
|
||||
namespace Dot.RemoveTrailingWhiteSpace;
|
||||
|
||||
[Verb("remove-whitespace", HelpText = "移除文件尾部换行和空格")]
|
||||
public class Option : IOption
|
||||
{
|
||||
[Option('f', "filter", Required = false, HelpText = "文件通配符", Default = "*.*")]
|
||||
public string Filter { get; set; } //normal options here
|
||||
|
||||
[Option('p', "path", Required = false, HelpText = "要处理的目录路径", Default = ".")]
|
||||
public string Path { get; set; }
|
||||
}
|
@ -1,90 +0,0 @@
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
|
||||
namespace Dot.TrimUtf8Bom;
|
||||
|
||||
public sealed class Main : Tool<Option>, IDisposable
|
||||
{
|
||||
private int _breakCnt;
|
||||
private bool _disposed;
|
||||
private static readonly object _lockObj = new();
|
||||
private int _procedCnt;
|
||||
private ChildProgressBar _step2Bar;
|
||||
private int _totalCnt;
|
||||
private int _trimCnt;
|
||||
public Main(Option opt) : base(opt) { }
|
||||
|
||||
|
||||
~Main()
|
||||
{
|
||||
Dispose(false);
|
||||
}
|
||||
|
||||
private void Dispose(bool disposing)
|
||||
{
|
||||
if (_disposed) return;
|
||||
if (disposing) _step2Bar?.Dispose();
|
||||
_disposed = true;
|
||||
}
|
||||
|
||||
|
||||
private void ShowMessage(int procedCnt, int replaceCnt, int breakCnt)
|
||||
{
|
||||
lock (_lockObj) {
|
||||
_procedCnt += procedCnt;
|
||||
_trimCnt += replaceCnt;
|
||||
_breakCnt += breakCnt;
|
||||
_step2Bar.Message = $"已处理:{_procedCnt}/{_totalCnt},移除BOM:{_trimCnt},跳过:{_breakCnt}";
|
||||
}
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
Dispose(true);
|
||||
GC.SuppressFinalize(this);
|
||||
}
|
||||
|
||||
[SuppressMessage("ReSharper", "PossibleMultipleEnumeration")]
|
||||
public override void Run()
|
||||
{
|
||||
if (!Directory.Exists(Opt.Path)) throw new ArgumentException(nameof(Opt.Path), $"指定的路径“{Opt.Path}”不存在");
|
||||
|
||||
|
||||
var utf8Bom = new byte[] { 0xef, 0xbb, 0xbf };
|
||||
using var step1Bar = new IndeterminateProgressBar("查找文件...", DefaultProgressBarOptions);
|
||||
|
||||
|
||||
var fileList = EnumerateFiles(Opt.Path, Opt.Filter);
|
||||
_totalCnt = fileList.Count();
|
||||
|
||||
step1Bar.Message = "查找文件...OK";
|
||||
step1Bar.Finished();
|
||||
|
||||
_step2Bar = step1Bar.Spawn(_totalCnt, string.Empty, DefaultProgressBarOptions);
|
||||
|
||||
Parallel.ForEach(fileList, file => {
|
||||
_step2Bar.Tick();
|
||||
ShowMessage(1, 0, 0);
|
||||
|
||||
var tmpFile = $"{file}.tmp";
|
||||
var isReplaced = false;
|
||||
using (var fsr = new FileStream(file, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) {
|
||||
Span<byte> buffer = stackalloc byte[utf8Bom.Length];
|
||||
var readLen = fsr.Read(buffer);
|
||||
if (readLen == utf8Bom.Length && buffer.SequenceEqual(utf8Bom)) {
|
||||
using var fsw = new FileStream(tmpFile, FileMode.OpenOrCreate, FileAccess.Write);
|
||||
int data;
|
||||
while ((data = fsr.ReadByte()) != -1) fsw.WriteByte((byte)data);
|
||||
isReplaced = true;
|
||||
}
|
||||
}
|
||||
|
||||
if (isReplaced) {
|
||||
MoveFile(tmpFile, file);
|
||||
ShowMessage(0, 1, 0);
|
||||
}
|
||||
else {
|
||||
ShowMessage(0, 0, 1);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
@ -1,11 +0,0 @@
|
||||
namespace Dot.TrimUtf8Bom;
|
||||
|
||||
[Verb("trim-utf8-bom", HelpText = "移除文件的uf8 bom")]
|
||||
public class Option : IOption
|
||||
{
|
||||
[Option('f', "filter", Required = false, HelpText = "文件通配符", Default = "*.*")]
|
||||
public string Filter { get; set; } //normal options here
|
||||
|
||||
[Option('p', "path", Required = false, HelpText = "要处理的目录路径", Default = ".")]
|
||||
public string Path { get; set; }
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user