mirror of
https://github.com/nsnail/dot.git
synced 2025-06-17 21:13:21 +08:00
<feat> + tolf: 写入文件信息统计
This commit is contained in:
parent
62fef5b30b
commit
a77ba1966b
@ -8,4 +8,5 @@ var path = Directory.GetFiles(@".idea", "workspace.xml", SearchOption.AllDirecto
|
|||||||
""";
|
""";
|
||||||
var content = File.ReadAllText(path);
|
var content = File.ReadAllText(path);
|
||||||
content = content.Replace(findStr, replaceStr);
|
content = content.Replace(findStr, replaceStr);
|
||||||
|
Console.WriteLine(content);
|
||||||
File.WriteAllText(path, content);
|
File.WriteAllText(path, content);
|
@ -1,3 +1,3 @@
|
|||||||
dot rm-bom
|
dot rm-bom -w
|
||||||
dot rm-blank
|
dot rm-blank -w
|
||||||
dot tolf
|
dot tolf -w
|
@ -9,6 +9,6 @@ public class DirOption : OptionBase
|
|||||||
public string Path { get; set; }
|
public string Path { get; set; }
|
||||||
|
|
||||||
|
|
||||||
[Option('r', "readonly", HelpText = nameof(Str.ReadOnly), Default = false, ResourceType = typeof(Str))]
|
[Option('w', "write", HelpText = nameof(Str.WriteMode), Default = false, ResourceType = typeof(Str))]
|
||||||
public bool ReadOnly { get; set; }
|
public bool WriteMode { get; set; }
|
||||||
}
|
}
|
@ -1,4 +1,4 @@
|
|||||||
<?xml version="1.0" encoding="utf-8"?>
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
<root>
|
<root>
|
||||||
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata"
|
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata"
|
||||||
id="root" xmlns="">
|
id="root" xmlns="">
|
||||||
@ -74,9 +74,6 @@
|
|||||||
<data name="PressAnyKey" xml:space="preserve">
|
<data name="PressAnyKey" xml:space="preserve">
|
||||||
<value>Press any key to continue...</value>
|
<value>Press any key to continue...</value>
|
||||||
</data>
|
</data>
|
||||||
<data name="ReadOnly" xml:space="preserve">
|
|
||||||
<value>Read-only mode (only for testing, no actual modification)</value>
|
|
||||||
</data>
|
|
||||||
<data name="NoFileToBeProcessed" xml:space="preserve">
|
<data name="NoFileToBeProcessed" xml:space="preserve">
|
||||||
<value>No documents to be processed</value>
|
<value>No documents to be processed</value>
|
||||||
</data>
|
</data>
|
||||||
@ -173,4 +170,22 @@
|
|||||||
<data name="JsonToString" xml:space="preserve">
|
<data name="JsonToString" xml:space="preserve">
|
||||||
<value>Json text escaped into a string</value>
|
<value>Json text escaped into a string</value>
|
||||||
</data>
|
</data>
|
||||||
</root>
|
<data name="WriteMode" xml:space="preserve">
|
||||||
|
<value>Enable write mode</value>
|
||||||
|
</data>
|
||||||
|
<data name="ExerciseMode" xml:space="preserve">
|
||||||
|
<value>Read-only mode, the file will not be modified in real time!</value>
|
||||||
|
</data>
|
||||||
|
<data name="Read" xml:space="preserve">
|
||||||
|
<value>read</value>
|
||||||
|
</data>
|
||||||
|
<data name="Write" xml:space="preserve">
|
||||||
|
<value>write</value>
|
||||||
|
</data>
|
||||||
|
<data name="Break" xml:space="preserve">
|
||||||
|
<value>skip</value>
|
||||||
|
</data>
|
||||||
|
<data name="WriteFileStats" xml:space="preserve">
|
||||||
|
<value>Write statistics</value>
|
||||||
|
</data>
|
||||||
|
</root>
|
||||||
|
@ -137,8 +137,8 @@
|
|||||||
<data name="PressAnyKey" xml:space="preserve">
|
<data name="PressAnyKey" xml:space="preserve">
|
||||||
<value>按下任意键继续...</value>
|
<value>按下任意键继续...</value>
|
||||||
</data>
|
</data>
|
||||||
<data name="ReadOnly" xml:space="preserve">
|
<data name="WriteMode" xml:space="preserve">
|
||||||
<value>只读模式(仅做测试,不实际修改)</value>
|
<value>启用写入模式</value>
|
||||||
</data>
|
</data>
|
||||||
<data name="NoFileToBeProcessed" xml:space="preserve">
|
<data name="NoFileToBeProcessed" xml:space="preserve">
|
||||||
<value>没有需要处理的文件</value>
|
<value>没有需要处理的文件</value>
|
||||||
@ -183,7 +183,7 @@
|
|||||||
<value>查找 "{0}" 下所有git仓库目录... </value>
|
<value>查找 "{0}" 下所有git仓库目录... </value>
|
||||||
</data>
|
</data>
|
||||||
<data name="ExerciseMode" xml:space="preserve">
|
<data name="ExerciseMode" xml:space="preserve">
|
||||||
<value>演习模式, 不会真实修改文件!</value>
|
<value>只读模式, 不会真实修改文件!</value>
|
||||||
</data>
|
</data>
|
||||||
<data name="Read" xml:space="preserve">
|
<data name="Read" xml:space="preserve">
|
||||||
<value>读取</value>
|
<value>读取</value>
|
||||||
@ -196,4 +196,7 @@
|
|||||||
<data name="Break" xml:space="preserve">
|
<data name="Break" xml:space="preserve">
|
||||||
<value>跳过</value>
|
<value>跳过</value>
|
||||||
</data>
|
</data>
|
||||||
|
<data name="WriteFileStats" xml:space="preserve">
|
||||||
|
<value>写入统计</value>
|
||||||
|
</data>
|
||||||
</root>
|
</root>
|
@ -35,7 +35,7 @@ public sealed class Main : ToolBase<Option>, IDisposable
|
|||||||
|
|
||||||
await using var fsrw = OpenFileStream(file, FileMode.Open, FileAccess.ReadWrite);
|
await using var fsrw = OpenFileStream(file, FileMode.Open, FileAccess.ReadWrite);
|
||||||
|
|
||||||
if (Opt.ReadOnly) { //测试,只读模式
|
if (!Opt.WriteMode) { //测试,只读模式
|
||||||
ShowMessage(0, 1, 0);
|
ShowMessage(0, 1, 0);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -46,7 +46,7 @@ public sealed class Main : ToolBase<Option>, IDisposable
|
|||||||
var tmpFile = $"{file}.tmp";
|
var tmpFile = $"{file}.tmp";
|
||||||
bool isReplaced;
|
bool isReplaced;
|
||||||
await using (var fsr = OpenFileStream(file, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) {
|
await using (var fsr = OpenFileStream(file, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) {
|
||||||
if (Opt.ReadOnly) { //测试,只读模式
|
if (!Opt.WriteMode) { //测试,只读模式
|
||||||
ShowMessage(0, 1, 0);
|
ShowMessage(0, 1, 0);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -1,16 +1,19 @@
|
|||||||
|
using System.Collections.Concurrent;
|
||||||
using System.Diagnostics.CodeAnalysis;
|
using System.Diagnostics.CodeAnalysis;
|
||||||
using Spectre.Console;
|
using Spectre.Console;
|
||||||
|
using Panel = Spectre.Console.Panel;
|
||||||
|
|
||||||
namespace Dot.ToLf;
|
namespace Dot.ToLf;
|
||||||
|
|
||||||
public sealed class Main : ToolBase<Option>
|
public sealed class Main : ToolBase<Option>
|
||||||
{
|
{
|
||||||
private int _breakCnt;
|
private int _breakCnt;
|
||||||
private ProgressTask _fileTask;
|
private ProgressTask _fileTask;
|
||||||
private static readonly object _lockObj = new();
|
private static readonly object _lockObj = new();
|
||||||
private long _procedCnt;
|
private long _procedCnt;
|
||||||
private int _replaceCnt;
|
private int _replaceCnt;
|
||||||
private int _totalCnt;
|
private readonly ConcurrentDictionary<string, int> _replacedStats = new();
|
||||||
|
private int _totalCnt;
|
||||||
|
|
||||||
public Main(Option opt) : base(opt)
|
public Main(Option opt) : base(opt)
|
||||||
{
|
{
|
||||||
@ -28,11 +31,6 @@ public sealed class Main : ToolBase<Option>
|
|||||||
int data;
|
int data;
|
||||||
|
|
||||||
await using (var fsr = OpenFileStream(file, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) {
|
await using (var fsr = OpenFileStream(file, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) {
|
||||||
if (Opt.ReadOnly) { //测试,只读模式
|
|
||||||
ShowMessage(0, 1, 0);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (fsr is null) {
|
if (fsr is null) {
|
||||||
ShowMessage(0, 0, 1);
|
ShowMessage(0, 0, 1);
|
||||||
return;
|
return;
|
||||||
@ -66,13 +64,16 @@ public sealed class Main : ToolBase<Option>
|
|||||||
|
|
||||||
|
|
||||||
if (isReplaced && !isBin) {
|
if (isReplaced && !isBin) {
|
||||||
MoveFile(tmpFile, file);
|
if (Opt.WriteMode) //测试,只读模式
|
||||||
|
MoveFile(tmpFile, file);
|
||||||
ShowMessage(0, 1, 0);
|
ShowMessage(0, 1, 0);
|
||||||
|
_replacedStats.AddOrUpdate(Path.GetExtension(file), 1, (_, oldValue) => oldValue + 1);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
File.Delete(tmpFile);
|
|
||||||
ShowMessage(0, 0, 1);
|
ShowMessage(0, 0, 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
File.Delete(tmpFile);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -92,7 +93,7 @@ public sealed class Main : ToolBase<Option>
|
|||||||
[SuppressMessage("ReSharper", "PossibleMultipleEnumeration")]
|
[SuppressMessage("ReSharper", "PossibleMultipleEnumeration")]
|
||||||
public override async Task Run()
|
public override async Task Run()
|
||||||
{
|
{
|
||||||
if (Opt.ReadOnly) AnsiConsole.MarkupLine("[gray]{0}[/]", Str.ExerciseMode);
|
if (!Opt.WriteMode) AnsiConsole.MarkupLine("[gray]{0}[/]", Str.ExerciseMode);
|
||||||
IEnumerable<string> fileList;
|
IEnumerable<string> fileList;
|
||||||
await AnsiConsole.Progress()
|
await AnsiConsole.Progress()
|
||||||
.Columns(new ProgressBarColumn() //
|
.Columns(new ProgressBarColumn() //
|
||||||
@ -113,5 +114,12 @@ public sealed class Main : ToolBase<Option>
|
|||||||
_fileTask.StartTask();
|
_fileTask.StartTask();
|
||||||
await Parallel.ForEachAsync(fileList, FileHandle);
|
await Parallel.ForEachAsync(fileList, FileHandle);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
var grid = new Grid().AddColumn(new GridColumn().NoWrap().PadRight(16))
|
||||||
|
.AddColumn(new GridColumn().Alignment(Justify.Right));
|
||||||
|
|
||||||
|
foreach (var kv in _replacedStats) grid.AddRow(kv.Key, kv.Value.ToString());
|
||||||
|
|
||||||
|
AnsiConsole.Write(new Panel(grid).Header(Str.WriteFileStats));
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
x
Reference in New Issue
Block a user