<feat> + 目录检索设置

This commit is contained in:
nsnail 2022-12-08 10:49:19 +08:00
parent 8b01483d85
commit f3d250ae87
6 changed files with 113 additions and 99 deletions

View File

@ -2,9 +2,17 @@ namespace Dot;
public class DirOption : OptionBase
{
[Option('f', "filter", HelpText = nameof(Str.FileSearchPattern), Default = "*.*", ResourceType = typeof(Str))]
[Option('e', "exclude", HelpText = nameof(Str.ExcludePathRegexes), ResourceType = typeof(Str))]
public IEnumerable<string> ExcludeRegexes { get; set; }
[Option('f', "filter", HelpText = nameof(Str.FileSearchPattern), Default = "*", ResourceType = typeof(Str))]
public string Filter { get; set; }
[Option('d', "max-depth", HelpText = nameof(Str.MaxRecursionDepth), Default = int.MaxValue
, ResourceType = typeof(Str))]
public int MaxRecursionDepth { get; set; }
[Value(0, HelpText = nameof(Str.FolderPath), Default = ".", ResourceType = typeof(Str))]
public string Path { get; set; }

View File

@ -1,4 +1,5 @@
using System.Collections.Concurrent;
using System.Text.RegularExpressions;
using Panel = Spectre.Console.Panel;
namespace Dot;
@ -7,6 +8,7 @@ public abstract class FilesTool<TOption> : ToolBase<TOption> where TOption : Dir
{
private int _breakCnt; //跳过文件数
private ProgressTask _childTask; //子任务进度
private int _excludeCnt; //排除文件数
private static readonly object _lock = new(); //线程锁
private int _readCnt; //读取文件数
private int _totalCnt; //总文件数
@ -19,33 +21,31 @@ public abstract class FilesTool<TOption> : ToolBase<TOption> where TOption : Dir
throw new ArgumentException(nameof(Opt.Path), string.Format(Str.PathNotFound, Opt.Path));
}
private static string[] EnumerateFiles(string path, string searchPattern)
private string[] EnumerateFiles(string path, string searchPattern, out int excludeCnt)
{
var fileList = Directory
.EnumerateFiles(path, searchPattern
, new EnumerationOptions {
RecurseSubdirectories = true
, AttributesToSkip = FileAttributes.ReparsePoint
})
.Where(x => !new[] { ".git", "node_modules" }.Any(
y => x.Contains(y, StringComparison.OrdinalIgnoreCase)))
.ToArray();
var exCnt = 0;
if (!Opt.ExcludeRegexes.Any()) //默认排除.git 、 node_modules 目录
Opt.ExcludeRegexes = new[] { @"\.git", "node_modules" };
var excludeRegexes = Opt.ExcludeRegexes.Select(x => new Regex(x));
var fileList = Directory.EnumerateFiles(path, searchPattern
, new EnumerationOptions {
RecurseSubdirectories = true
, AttributesToSkip
= FileAttributes.ReparsePoint
, IgnoreInaccessible = true
, MaxRecursionDepth = Opt.MaxRecursionDepth
})
.Where(x => {
if (!excludeRegexes.Any(y => y.IsMatch(x))) return true;
++exCnt;
return false;
})
.ToArray();
excludeCnt = exCnt;
return fileList;
}
protected static void CopyFile(string source, string dest)
{
try {
File.Copy(source, dest, true);
}
catch (UnauthorizedAccessException) {
File.SetAttributes(dest, new FileInfo(dest).Attributes & ~FileAttributes.ReadOnly);
File.Copy(source, dest, true);
}
}
protected FileStream CreateTempFile(out string file)
{
file = Path.Combine(Path.GetTempPath(), $"{System.Guid.NewGuid()}.tmp");
@ -85,10 +85,11 @@ public abstract class FilesTool<TOption> : ToolBase<TOption> where TOption : Dir
_breakCnt += breakCnt;
if (readCnt > 0) _childTask.Increment(1);
_childTask.Description
= $"{Str.Read}: [green]{_readCnt}[/]/{_totalCnt}, {Str.Write}: [red]{_writeCnt}[/], {Str.Break}: [gray]{_breakCnt}[/]";
= $"{Str.Read}: [green]{_readCnt}[/]/{_totalCnt}, {Str.Write}: [red]{_writeCnt}[/], {Str.Break}: [gray]{_breakCnt}[/], {Str.Exclude}: [yellow]{_excludeCnt}[/]";
}
}
protected void UpdateStats(string key)
{
_writeStats.AddOrUpdate(key, 1, (_, oldValue) => oldValue + 1);
@ -108,7 +109,7 @@ public abstract class FilesTool<TOption> : ToolBase<TOption> where TOption : Dir
.StartAsync(async ctx => {
var taskSearchfile = ctx.AddTask(Str.SearchingFile).IsIndeterminate();
_childTask = ctx.AddTask("-/-", false);
fileList = EnumerateFiles(Opt.Path, Opt.Filter);
fileList = EnumerateFiles(Opt.Path, Opt.Filter, out _excludeCnt);
_totalCnt = fileList.Count();
taskSearchfile.IsIndeterminate(false);
taskSearchfile.Increment(100);

View File

@ -1,191 +1,190 @@
<?xml version="1.0" encoding="utf-8"?>
<root>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata"
id="root" xmlns="">
<xsd:element name="root" msdata:IsDataSet="true"></xsd:element>
</xsd:schema>
<resheader name="resmimetype">
<value>text/microsoft-resx</value>
</resheader>
<resheader name="version">
<value>1.3</value>
</resheader>
<resheader name="reader">
<value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=2.0.0.0, Culture=neutral,
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" id="root" xmlns="">
<xsd:element name="root" msdata:IsDataSet="true"></xsd:element>
</xsd:schema>
<resheader name="resmimetype">
<value>text/microsoft-resx</value>
</resheader>
<resheader name="version">
<value>1.3</value>
</resheader>
<resheader name="reader">
<value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=2.0.0.0, Culture=neutral,
PublicKeyToken=b77a5c561934e089
</value>
</resheader>
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=2.0.0.0, Culture=neutral,
</resheader>
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=2.0.0.0, Culture=neutral,
PublicKeyToken=b77a5c561934e089
</value>
</resheader>
<data name="InputTextIsEmpty" xml:space="preserve">
</resheader>
<data name="InputTextIsEmpty" xml:space="preserve">
<value>The input text is empty</value>
</data>
<data name="SearchingFile" xml:space="preserve">
<data name="SearchingFile" xml:space="preserve">
<value>Find files...</value>
</data>
<data name="PathNotFound" xml:space="preserve">
<data name="PathNotFound" xml:space="preserve">
<value>The specified path "{0}" does not exist</value>
</data>
<data name="SearchingFileOK" xml:space="preserve">
<data name="SearchingFileOK" xml:space="preserve">
<value>{0} files</value>
</data>
<data name="ShowMessageTemp" xml:space="preserve">
<data name="ShowMessageTemp" xml:space="preserve">
<value>Read: {0}/{1}, processed: {2}, skipped: {3}</value>
</data>
<data name="Copied" xml:space="preserve">
<data name="Copied" xml:space="preserve">
<value>{0}(copied to clipboard)</value>
</data>
<data name="FileSearchPattern" xml:space="preserve">
<data name="FileSearchPattern" xml:space="preserve">
<value>File wildcards</value>
</data>
<data name="FolderPath" xml:space="preserve">
<data name="FolderPath" xml:space="preserve">
<value>Directory path to be processed</value>
</data>
<data name="ConvertEndOfLineToLF" xml:space="preserve">
<data name="ConvertEndOfLineToLF" xml:space="preserve">
<value>Convert newline characters to LF</value>
</data>
<data name="GuidTool" xml:space="preserve">
<data name="GuidTool" xml:space="preserve">
<value>GUID tool</value>
</data>
<data name="UseUppercase" xml:space="preserve">
<data name="UseUppercase" xml:space="preserve">
<value>Use uppercase output</value>
</data>
<data name="RandomPasswordGenerator" xml:space="preserve">
<data name="RandomPasswordGenerator" xml:space="preserve">
<value>Random password generator</value>
</data>
<data name="PwdLength" xml:space="preserve">
<data name="PwdLength" xml:space="preserve">
<value>Password length</value>
</data>
<data name="PwdGenerateTypes" xml:space="preserve">
<data name="PwdGenerateTypes" xml:space="preserve">
<value>BitSet 1[0-9]2[a-z]4[A-Z]8[ascii.0x21-0x2F]</value>
</data>
<data name="RemoveTrailingWhiteSpaces" xml:space="preserve">
<data name="RemoveTrailingWhiteSpaces" xml:space="preserve">
<value>Remove line breaks and spaces at the end of the file</value>
</data>
<data name="TrimUtf8Bom" xml:space="preserve">
<data name="TrimUtf8Bom" xml:space="preserve">
<value>Remove the uf8 bom of the file</value>
</data>
<data name="TextTobeProcessed" xml:space="preserve">
<data name="TextTobeProcessed" xml:space="preserve">
<value>Text to be processed (clipboard value is taken by default)</value>
</data>
<data name="PressAnyKey" xml:space="preserve">
<data name="PressAnyKey" xml:space="preserve">
<value>Press any key to continue...</value>
</data>
<data name="NoFileToBeProcessed" xml:space="preserve">
<data name="NoFileToBeProcessed" xml:space="preserve">
<value>No documents to be processed</value>
</data>
<data name="TimeoutMillSecs" xml:space="preserve">
<data name="TimeoutMillSecs" xml:space="preserve">
<value>Timeout for connecting to the NTP server (milliseconds)</value>
</data>
<data name="SyncToLocalTime" xml:space="preserve">
<data name="SyncToLocalTime" xml:space="preserve">
<value>Synchronize local time</value>
</data>
<data name="NtpReceiveDone" xml:space="preserve">
<data name="NtpReceiveDone" xml:space="preserve">
<value>Success {0}/{1}, the average value of the clock offset of the machine:{2}ms</value>
</data>
<data name="NtpServerCount" xml:space="preserve">
<data name="NtpServerCount" xml:space="preserve">
<value>{0}/{1} NTP servers</value>
</data>
<data name="NtpCalling" xml:space="preserve">
<data name="NtpCalling" xml:space="preserve">
<value>{0} In communication...</value>
</data>
<data name="LocalTimeOffset" xml:space="preserve">
<data name="LocalTimeOffset" xml:space="preserve">
<value>{0}, local clock offset: {1} ms</value>
</data>
<data name="LocalTimeSyncDone" xml:space="preserve">
<data name="LocalTimeSyncDone" xml:space="preserve">
<value>Local time has been synchronized</value>
</data>
<data name="Server" xml:space="preserve">
<data name="Server" xml:space="preserve">
<value>Server</value>
</data>
<data name="Status" xml:space="preserve">
<data name="Status" xml:space="preserve">
<value>Status</value>
</data>
<data name="LocalClockOffset" xml:space="preserve">
<data name="LocalClockOffset" xml:space="preserve">
<value>Local clock offset</value>
</data>
<data name="TimeTool" xml:space="preserve">
<data name="TimeTool" xml:space="preserve">
<value>Time synchronization tool</value>
</data>
<data name="TextTool" xml:space="preserve">
<data name="TextTool" xml:space="preserve">
<value>Text encoding tool</value>
</data>
<data name="ScreenPixelTool" xml:space="preserve">
<data name="ScreenPixelTool" xml:space="preserve">
<value>Screen coordinate color selection tool</value>
</data>
<data name="ClickCopyColor" xml:space="preserve">
<data name="ClickCopyColor" xml:space="preserve">
<value>Click the left mouse button to copy the colors and coordinates to the clipboard</value>
</data>
<data name="PublicIP" xml:space="preserve">
<data name="PublicIP" xml:space="preserve">
<value>Public network ip: </value>
</data>
<data name="ServerTime" xml:space="preserve">
<data name="ServerTime" xml:space="preserve">
<value>Synchronize local time</value>
</data>
<data name="Ip" xml:space="preserve">
<data name="Ip" xml:space="preserve">
<value>IP tools</value>
</data>
<data name="KeepSession" xml:space="preserve">
<data name="KeepSession" xml:space="preserve">
<value>Keep the session after executing the command</value>
</data>
<data name="NtpServerTime" xml:space="preserve">
<data name="NtpServerTime" xml:space="preserve">
<value>NTP server standard clock: {0}</value>
</data>
<data name="GitTool" xml:space="preserve">
<data name="GitTool" xml:space="preserve">
<value>Git batch operation tool</value>
</data>
<data name="GitArgs" xml:space="preserve">
<data name="GitArgs" xml:space="preserve">
<value>Parameters passed to Git</value>
</data>
<data name="Ok" xml:space="preserve">
<data name="Ok" xml:space="preserve">
<value>OK</value>
</data>
<data name="FindGitReps" xml:space="preserve">
<data name="FindGitReps" xml:space="preserve">
<value>Find all git repository directories under "{0}"...</value>
</data>
<data name="GitOutputEncoding" xml:space="preserve">
<data name="GitOutputEncoding" xml:space="preserve">
<value>Git output encoding</value>
</data>
<data name="MaxRecursionDepth" xml:space="preserve">
<data name="MaxRecursionDepth" xml:space="preserve">
<value>Directory search depth</value>
</data>
<data name="InvalidJsonString" xml:space="preserve">
<data name="InvalidJsonString" xml:space="preserve">
<value>Clipboard does not contain correct Json string</value>
</data>
<data name="Json" xml:space="preserve">
<data name="Json" xml:space="preserve">
<value>JsonTools</value>
</data>
<data name="CompressJson" xml:space="preserve">
<data name="CompressJson" xml:space="preserve">
<value>Compress Json text</value>
</data>
<data name="FormatJson" xml:space="preserve">
<data name="FormatJson" xml:space="preserve">
<value>Format JSON text</value>
</data>
<data name="GeneratorClass" xml:space="preserve">
<data name="GeneratorClass" xml:space="preserve">
<value>generate entity classes</value>
</data>
<data name="JsonToString" xml:space="preserve">
<data name="JsonToString" xml:space="preserve">
<value>Json text escaped into a string</value>
</data>
<data name="WriteMode" xml:space="preserve">
<data name="WriteMode" xml:space="preserve">
<value>Enable write mode</value>
</data>
<data name="ExerciseMode" xml:space="preserve">
<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">
<data name="Read" xml:space="preserve">
<value>read</value>
</data>
<data name="Write" xml:space="preserve">
<data name="Write" xml:space="preserve">
<value>write</value>
</data>
<data name="Break" xml:space="preserve">
<data name="Break" xml:space="preserve">
<value>skip</value>
</data>
<data name="WriteFileStats" xml:space="preserve">
<data name="WriteFileStats" xml:space="preserve">
<value>Write statistics</value>
</data>
</root>

View File

@ -143,6 +143,12 @@
<data name="NoFileToBeProcessed" xml:space="preserve">
<value>没有需要处理的文件</value>
</data>
<data name="ExcludePathRegexes" xml:space="preserve">
<value>排除路径的正则表达式</value>
</data>
<data name="Exclude" xml:space="preserve">
<value>排除</value>
</data>
<data name="NtpReceiveDone" xml:space="preserve">
<value>成功 {0}/{1} , 本机时钟偏移平均值: {2} ms</value>
</data>

View File

@ -33,7 +33,7 @@ public sealed class Main : FilesTool<Option>
}
if (CloneFileWithoutBom(fsr, ref tmpFile)) {
if (Opt.WriteMode) CopyFile(tmpFile, file);
if (Opt.WriteMode) File.Copy(tmpFile, file, true);
ShowMessage(0, 1, 0);
UpdateStats(Path.GetExtension(file));
}

View File

@ -47,7 +47,7 @@ public sealed class Main : FilesTool<Option>
if (hasWrote && !isBin) {
if (Opt.WriteMode) CopyFile(tmpFile, file);
if (Opt.WriteMode) File.Copy(tmpFile, file, true);
ShowMessage(0, 1, 0);
UpdateStats(Path.GetExtension(file));
}