mirror of
https://github.com/nsnail/dot.git
synced 2025-06-18 05:23:21 +08:00
<feat> + 时间同步工具
This commit is contained in:
parent
4ab2af5ca3
commit
f4175480e5
@ -2,14 +2,13 @@ namespace Dot;
|
|||||||
|
|
||||||
public class DirOption : IOption
|
public class DirOption : IOption
|
||||||
{
|
{
|
||||||
[Option('f', "filter", HelpText = nameof(Strings.FileSearchPattern), Default = "*.*"
|
[Option('f', "filter", HelpText = nameof(Str.FileSearchPattern), Default = "*.*", ResourceType = typeof(Str))]
|
||||||
, ResourceType = typeof(Strings))]
|
|
||||||
public string Filter { get; set; }
|
public string Filter { get; set; }
|
||||||
|
|
||||||
[Value(0, HelpText = nameof(Strings.FolderPath), Default = ".", ResourceType = typeof(Strings))]
|
[Value(0, HelpText = nameof(Str.FolderPath), Default = ".", ResourceType = typeof(Str))]
|
||||||
public string Path { get; set; }
|
public string Path { get; set; }
|
||||||
|
|
||||||
|
|
||||||
[Option('r', "readonly", HelpText = nameof(Strings.ReadOnly), Default = false, ResourceType = typeof(Strings))]
|
[Option('r', "readonly", HelpText = nameof(Str.ReadOnly), Default = false, ResourceType = typeof(Str))]
|
||||||
public bool ReadOnly { get; set; }
|
public bool ReadOnly { get; set; }
|
||||||
}
|
}
|
@ -7,11 +7,12 @@ public sealed class Main : Tool<Option>
|
|||||||
public Main(Option opt) : base(opt) { }
|
public Main(Option opt) : base(opt) { }
|
||||||
|
|
||||||
|
|
||||||
public override void Run()
|
public override Task Run()
|
||||||
{
|
{
|
||||||
var guid = System.Guid.NewGuid().ToString();
|
var guid = System.Guid.NewGuid().ToString();
|
||||||
if (Opt.Upper) guid = guid.ToUpper();
|
if (Opt.Upper) guid = guid.ToUpper();
|
||||||
ClipboardService.SetText(guid);
|
ClipboardService.SetText(guid);
|
||||||
Console.WriteLine(Strings.Copied, guid);
|
Console.WriteLine(Str.Copied, guid);
|
||||||
|
return Task.CompletedTask;
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -1,8 +1,8 @@
|
|||||||
namespace Dot.Guid;
|
namespace Dot.Guid;
|
||||||
|
|
||||||
[Verb("guid", HelpText = nameof(Strings.GuidTool), ResourceType = typeof(Strings))]
|
[Verb("guid", HelpText = nameof(Str.GuidTool), ResourceType = typeof(Str))]
|
||||||
public class Option : IOption
|
public class Option : IOption
|
||||||
{
|
{
|
||||||
[Option('u', "upper", HelpText = nameof(Strings.UseUppercase), Default = false, ResourceType = typeof(Strings))]
|
[Option('u', "upper", HelpText = nameof(Str.UseUppercase), Default = false, ResourceType = typeof(Str))]
|
||||||
public bool Upper { get; set; } //normal options here
|
public bool Upper { get; set; } //normal options here
|
||||||
}
|
}
|
@ -2,5 +2,5 @@ namespace Dot;
|
|||||||
|
|
||||||
public interface ITool
|
public interface ITool
|
||||||
{
|
{
|
||||||
void Run();
|
Task Run();
|
||||||
}
|
}
|
@ -21,14 +21,14 @@ namespace Dot.Lang {
|
|||||||
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "4.0.0.0")]
|
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "4.0.0.0")]
|
||||||
[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
|
[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
|
||||||
[global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()]
|
[global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()]
|
||||||
public class Strings {
|
public class Str {
|
||||||
|
|
||||||
private static global::System.Resources.ResourceManager resourceMan;
|
private static global::System.Resources.ResourceManager resourceMan;
|
||||||
|
|
||||||
private static global::System.Globalization.CultureInfo resourceCulture;
|
private static global::System.Globalization.CultureInfo resourceCulture;
|
||||||
|
|
||||||
[global::System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
|
[global::System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
|
||||||
internal Strings() {
|
internal Str() {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@ -38,7 +38,7 @@ namespace Dot.Lang {
|
|||||||
public static global::System.Resources.ResourceManager ResourceManager {
|
public static global::System.Resources.ResourceManager ResourceManager {
|
||||||
get {
|
get {
|
||||||
if (object.ReferenceEquals(resourceMan, null)) {
|
if (object.ReferenceEquals(resourceMan, null)) {
|
||||||
global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("Dot.Lang.Strings", typeof(Strings).Assembly);
|
global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("Dot.Lang.Str", typeof(Str).Assembly);
|
||||||
resourceMan = temp;
|
resourceMan = temp;
|
||||||
}
|
}
|
||||||
return resourceMan;
|
return resourceMan;
|
||||||
@ -122,6 +122,33 @@ namespace Dot.Lang {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Looks up a localized string similar to Local clock offset.
|
||||||
|
/// </summary>
|
||||||
|
public static string LocalClockOffset {
|
||||||
|
get {
|
||||||
|
return ResourceManager.GetString("LocalClockOffset", resourceCulture);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Looks up a localized string similar to {0}, 本机时钟偏移: {1} ms.
|
||||||
|
/// </summary>
|
||||||
|
public static string LocalTimeOffset {
|
||||||
|
get {
|
||||||
|
return ResourceManager.GetString("LocalTimeOffset", resourceCulture);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Looks up a localized string similar to 本机时间已同步.
|
||||||
|
/// </summary>
|
||||||
|
public static string LocalTimeSyncDone {
|
||||||
|
get {
|
||||||
|
return ResourceManager.GetString("LocalTimeSyncDone", resourceCulture);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Looks up a localized string similar to 没有需要处理的文件.
|
/// Looks up a localized string similar to 没有需要处理的文件.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@ -131,6 +158,33 @@ namespace Dot.Lang {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Looks up a localized string similar to {0} 通信中....
|
||||||
|
/// </summary>
|
||||||
|
public static string NtpCalling {
|
||||||
|
get {
|
||||||
|
return ResourceManager.GetString("NtpCalling", resourceCulture);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Looks up a localized string similar to 成功 {0}/{1} , 本机时钟偏移平均值: {2} ms.
|
||||||
|
/// </summary>
|
||||||
|
public static string NtpReceiveDone {
|
||||||
|
get {
|
||||||
|
return ResourceManager.GetString("NtpReceiveDone", resourceCulture);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Looks up a localized string similar to {0}/{1} 个 NTP 服务器.
|
||||||
|
/// </summary>
|
||||||
|
public static string NtpServerCount {
|
||||||
|
get {
|
||||||
|
return ResourceManager.GetString("NtpServerCount", resourceCulture);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Looks up a localized string similar to 指定的路径“{0}”不存在.
|
/// Looks up a localized string similar to 指定的路径“{0}”不存在.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@ -212,6 +266,15 @@ namespace Dot.Lang {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Looks up a localized string similar to Server.
|
||||||
|
/// </summary>
|
||||||
|
public static string Server {
|
||||||
|
get {
|
||||||
|
return ResourceManager.GetString("Server", resourceCulture);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Looks up a localized string similar to 已读取:{0}/{1},处理:{2},跳过:{3}.
|
/// Looks up a localized string similar to 已读取:{0}/{1},处理:{2},跳过:{3}.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@ -221,6 +284,24 @@ namespace Dot.Lang {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Looks up a localized string similar to Status.
|
||||||
|
/// </summary>
|
||||||
|
public static string Status {
|
||||||
|
get {
|
||||||
|
return ResourceManager.GetString("Status", resourceCulture);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Looks up a localized string similar to 同步本机时间.
|
||||||
|
/// </summary>
|
||||||
|
public static string SyncToLocalTime {
|
||||||
|
get {
|
||||||
|
return ResourceManager.GetString("SyncToLocalTime", resourceCulture);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Looks up a localized string similar to 要处理的文本(默认取取剪贴板值).
|
/// Looks up a localized string similar to 要处理的文本(默认取取剪贴板值).
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@ -230,6 +311,15 @@ namespace Dot.Lang {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Looks up a localized string similar to 连接NTP服务器超时时间 (毫秒).
|
||||||
|
/// </summary>
|
||||||
|
public static string TimeoutMillSecs {
|
||||||
|
get {
|
||||||
|
return ResourceManager.GetString("TimeoutMillSecs", resourceCulture);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Looks up a localized string similar to 移除文件的uf8 bom.
|
/// Looks up a localized string similar to 移除文件的uf8 bom.
|
||||||
/// </summary>
|
/// </summary>
|
@ -83,4 +83,34 @@
|
|||||||
<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>
|
||||||
|
<data name="TimeoutMillSecs" xml:space="preserve">
|
||||||
|
<value>Timeout for connecting to the NTP server (milliseconds)</value>
|
||||||
|
</data>
|
||||||
|
<data name="SyncToLocalTime" xml:space="preserve">
|
||||||
|
<value>Synchronize local time</value>
|
||||||
|
</data>
|
||||||
|
<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">
|
||||||
|
<value>{0}/{1} NTP servers</value>
|
||||||
|
</data>
|
||||||
|
<data name="NtpCalling" xml:space="preserve">
|
||||||
|
<value>{0} In communication...</value>
|
||||||
|
</data>
|
||||||
|
<data name="LocalTimeOffset" xml:space="preserve">
|
||||||
|
<value>{0}, local clock offset: {1} ms</value>
|
||||||
|
</data>
|
||||||
|
<data name="LocalTimeSyncDone" xml:space="preserve">
|
||||||
|
<value>Local time has been synchronized</value>
|
||||||
|
</data>
|
||||||
|
<data name="Server" xml:space="preserve">
|
||||||
|
<value>Server</value>
|
||||||
|
</data>
|
||||||
|
<data name="Status" xml:space="preserve">
|
||||||
|
<value>Status</value>
|
||||||
|
</data>
|
||||||
|
<data name="LocalClockOffset" xml:space="preserve">
|
||||||
|
<value>Local clock offset</value>
|
||||||
|
</data>
|
||||||
</root>
|
</root>
|
@ -51,6 +51,13 @@
|
|||||||
<data name="FileSearchPattern" xml:space="preserve">
|
<data name="FileSearchPattern" xml:space="preserve">
|
||||||
<value>文件通配符</value>
|
<value>文件通配符</value>
|
||||||
</data>
|
</data>
|
||||||
|
<data name="TimeoutMillSecs" xml:space="preserve">
|
||||||
|
<value>连接NTP服务器超时时间 (毫秒)</value>
|
||||||
|
</data>
|
||||||
|
<data name="SyncToLocalTime" xml:space="preserve">
|
||||||
|
<value>同步本机时间</value>
|
||||||
|
</data>
|
||||||
|
|
||||||
<data name="FolderPath" xml:space="preserve">
|
<data name="FolderPath" xml:space="preserve">
|
||||||
<value>要处理的目录路径</value>
|
<value>要处理的目录路径</value>
|
||||||
</data>
|
</data>
|
||||||
@ -91,4 +98,28 @@
|
|||||||
<data name="NoFileToBeProcessed" xml:space="preserve">
|
<data name="NoFileToBeProcessed" xml:space="preserve">
|
||||||
<value>没有需要处理的文件</value>
|
<value>没有需要处理的文件</value>
|
||||||
</data>
|
</data>
|
||||||
|
<data name="NtpReceiveDone" xml:space="preserve">
|
||||||
|
<value>成功 {0}/{1} , 本机时钟偏移平均值: {2} ms</value>
|
||||||
|
</data>
|
||||||
|
<data name="NtpServerCount" xml:space="preserve">
|
||||||
|
<value>{0}/{1} 个 NTP 服务器</value>
|
||||||
|
</data>
|
||||||
|
<data name="NtpCalling" xml:space="preserve">
|
||||||
|
<value>{0} 通信中...</value>
|
||||||
|
</data>
|
||||||
|
<data name="LocalTimeOffset" xml:space="preserve">
|
||||||
|
<value>{0}, 本机时钟偏移: {1} ms</value>
|
||||||
|
</data>
|
||||||
|
<data name="LocalTimeSyncDone" xml:space="preserve">
|
||||||
|
<value>本机时间已同步</value>
|
||||||
|
</data>
|
||||||
|
<data name="Server" xml:space="preserve">
|
||||||
|
<value>Server</value>
|
||||||
|
</data>
|
||||||
|
<data name="Status" xml:space="preserve">
|
||||||
|
<value>Status</value>
|
||||||
|
</data>
|
||||||
|
<data name="LocalClockOffset" xml:space="preserve">
|
||||||
|
<value>Local clock offset</value>
|
||||||
|
</data>
|
||||||
</root>
|
</root>
|
@ -10,10 +10,10 @@ Type[] LoadVerbs()
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void Run(object args)
|
async Task Run(object args)
|
||||||
{
|
{
|
||||||
var tool = ToolsFactory.Create(args as IOption);
|
var tool = ToolsFactory.Create(args as IOption);
|
||||||
tool.Run();
|
await tool.Run();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -22,7 +22,7 @@ void Run(object args)
|
|||||||
var types = LoadVerbs();
|
var types = LoadVerbs();
|
||||||
|
|
||||||
try {
|
try {
|
||||||
Parser.Default.ParseArguments(args, types).WithParsed(Run);
|
await Parser.Default.ParseArguments(args, types).WithParsedAsync(Run);
|
||||||
}
|
}
|
||||||
catch (ArgumentException ex) {
|
catch (ArgumentException ex) {
|
||||||
Console.Error.WriteLine(ex.Message);
|
Console.Error.WriteLine(ex.Message);
|
||||||
|
@ -15,7 +15,7 @@ public sealed class Main : Tool<Option>
|
|||||||
|
|
||||||
public Main(Option opt) : base(opt) { }
|
public Main(Option opt) : base(opt) { }
|
||||||
|
|
||||||
public override void Run()
|
public override Task Run()
|
||||||
{
|
{
|
||||||
unsafe {
|
unsafe {
|
||||||
var pSource = stackalloc char[_charTable.Sum(x => x.Length)];
|
var pSource = stackalloc char[_charTable.Sum(x => x.Length)];
|
||||||
@ -47,7 +47,9 @@ public sealed class Main : Tool<Option>
|
|||||||
|
|
||||||
var result = new string(pDest, 0, Opt.Length);
|
var result = new string(pDest, 0, Opt.Length);
|
||||||
ClipboardService.SetText(result);
|
ClipboardService.SetText(result);
|
||||||
Console.WriteLine(Strings.Copied, result);
|
Console.WriteLine(Str.Copied, result);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return Task.CompletedTask;
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -1,6 +1,6 @@
|
|||||||
namespace Dot.Pwd;
|
namespace Dot.Pwd;
|
||||||
|
|
||||||
[Verb("pwd", HelpText = nameof(Strings.RandomPasswordGenerator), ResourceType = typeof(Strings))]
|
[Verb("pwd", HelpText = nameof(Str.RandomPasswordGenerator), ResourceType = typeof(Str))]
|
||||||
public class Option : IOption
|
public class Option : IOption
|
||||||
{
|
{
|
||||||
[Flags]
|
[Flags]
|
||||||
@ -12,10 +12,10 @@ public class Option : IOption
|
|||||||
, SpecialCharacter = 8
|
, SpecialCharacter = 8
|
||||||
}
|
}
|
||||||
|
|
||||||
[Value(1, Required = true, HelpText = nameof(Strings.PwdLength), ResourceType = typeof(Strings))]
|
[Value(1, Required = true, HelpText = nameof(Str.PwdLength), ResourceType = typeof(Str))]
|
||||||
public int Length { get; set; }
|
public int Length { get; set; }
|
||||||
|
|
||||||
|
|
||||||
[Value(0, Required = true, HelpText = nameof(Strings.PwdGenerateTypes), ResourceType = typeof(Strings))]
|
[Value(0, Required = true, HelpText = nameof(Str.PwdGenerateTypes), ResourceType = typeof(Str))]
|
||||||
public GenerateTypes Type { get; set; }
|
public GenerateTypes Type { get; set; }
|
||||||
}
|
}
|
@ -1,66 +0,0 @@
|
|||||||
using System.Text.RegularExpressions;
|
|
||||||
using NSExt.Extensions;
|
|
||||||
using TextCopy;
|
|
||||||
|
|
||||||
namespace Dot.Random;
|
|
||||||
|
|
||||||
public sealed partial class Main : Tool<Option>
|
|
||||||
{
|
|
||||||
private readonly char[][] _charTable = {
|
|
||||||
"0123456789".ToCharArray() //
|
|
||||||
, "abcdefghijklmnopqrstuvwxyz".ToCharArray()
|
|
||||||
, "ABCDEFGHIJKLMNOPQRSTUVWXYZ".ToCharArray()
|
|
||||||
, "!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~".ToCharArray()
|
|
||||||
};
|
|
||||||
|
|
||||||
|
|
||||||
public Main(Option opt) : base(opt) { }
|
|
||||||
|
|
||||||
[GeneratedRegex("[a-z]")]
|
|
||||||
private static partial Regex RegexLowerCaseLetter();
|
|
||||||
|
|
||||||
[GeneratedRegex("\\d")]
|
|
||||||
private static partial Regex RegexNumber();
|
|
||||||
|
|
||||||
[GeneratedRegex("[^\\da-zA-Z]")]
|
|
||||||
private static partial Regex RegexSpecialCharacter();
|
|
||||||
|
|
||||||
[GeneratedRegex("[A-Z]")]
|
|
||||||
private static partial Regex RegexUpperCaseLetter();
|
|
||||||
|
|
||||||
public override void Run()
|
|
||||||
{
|
|
||||||
unsafe {
|
|
||||||
var pSource = stackalloc char[_charTable.Sum(x => x.Length)];
|
|
||||||
var pDest = stackalloc char[Opt.Length];
|
|
||||||
var sourceLen = 0;
|
|
||||||
|
|
||||||
if (Opt.Type.HasFlag(Option.GenerateTypes.Number))
|
|
||||||
foreach (var c in _charTable[0])
|
|
||||||
*(pSource + sourceLen++) = c;
|
|
||||||
|
|
||||||
|
|
||||||
if (Opt.Type.HasFlag(Option.GenerateTypes.LowerCaseLetter))
|
|
||||||
foreach (var c in _charTable[1])
|
|
||||||
*(pSource + sourceLen++) = c;
|
|
||||||
|
|
||||||
if (Opt.Type.HasFlag(Option.GenerateTypes.UpperCaseLetter))
|
|
||||||
foreach (var c in _charTable[2])
|
|
||||||
*(pSource + sourceLen++) = c;
|
|
||||||
|
|
||||||
|
|
||||||
if (Opt.Type.HasFlag(Option.GenerateTypes.SpecialCharacter))
|
|
||||||
foreach (var c in _charTable[3])
|
|
||||||
*(pSource + sourceLen++) = c;
|
|
||||||
|
|
||||||
|
|
||||||
var randScope = new[] { 0, sourceLen };
|
|
||||||
for (var i = 0; i != Opt.Length; ++i) //
|
|
||||||
*(pDest + i) = *(pSource + randScope.Rand());
|
|
||||||
|
|
||||||
var result = new string(pDest, 0, Opt.Length);
|
|
||||||
ClipboardService.SetText(result);
|
|
||||||
Console.WriteLine($"已复制到剪贴板:{result}");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,21 +0,0 @@
|
|||||||
namespace Dot.Random;
|
|
||||||
|
|
||||||
[Verb("rand", HelpText = "随机数生成器")]
|
|
||||||
public class Option : IOption
|
|
||||||
{
|
|
||||||
[Flags]
|
|
||||||
public enum GenerateTypes
|
|
||||||
{
|
|
||||||
Number = 1
|
|
||||||
, LowerCaseLetter = 2
|
|
||||||
, UpperCaseLetter = 4
|
|
||||||
, SpecialCharacter = 8
|
|
||||||
}
|
|
||||||
|
|
||||||
[Value(1, MetaName = "长度", Required = true, HelpText = "随机数字长度")]
|
|
||||||
public int Length { get; set; }
|
|
||||||
|
|
||||||
|
|
||||||
[Value(0, MetaName = "生成类型", Required = true, HelpText = "BitSet 1:[0-9],2:[a-z],4:[A-Z],8:[ascii.0x21-0x2F]")]
|
|
||||||
public GenerateTypes Type { get; set; }
|
|
||||||
}
|
|
@ -27,13 +27,13 @@ public sealed class Main : Tool<Option>, IDisposable
|
|||||||
_disposed = true;
|
_disposed = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void FileHandle(string file)
|
private async ValueTask FileHandle(string file, CancellationToken _)
|
||||||
{
|
{
|
||||||
_step2Bar.Tick();
|
_step2Bar.Tick();
|
||||||
ShowMessage(1, 0, 0);
|
ShowMessage(1, 0, 0);
|
||||||
var spacesCnt = 0;
|
var spacesCnt = 0;
|
||||||
|
|
||||||
using var fsrw = OpenFileStream(file, FileMode.Open, FileAccess.ReadWrite);
|
await using var fsrw = OpenFileStream(file, FileMode.Open, FileAccess.ReadWrite);
|
||||||
|
|
||||||
if (Opt.ReadOnly) { //测试,只读模式
|
if (Opt.ReadOnly) { //测试,只读模式
|
||||||
ShowMessage(0, 1, 0);
|
ShowMessage(0, 1, 0);
|
||||||
@ -77,7 +77,7 @@ public sealed class Main : Tool<Option>, IDisposable
|
|||||||
_procedCnt += procedCnt;
|
_procedCnt += procedCnt;
|
||||||
_replaceCnt += removeCnt;
|
_replaceCnt += removeCnt;
|
||||||
_breakCnt += breakCnt;
|
_breakCnt += breakCnt;
|
||||||
_step2Bar.Message = string.Format(Strings.ShowMessageTemp, _procedCnt, _totalCnt, _replaceCnt, _breakCnt);
|
_step2Bar.Message = string.Format(Str.ShowMessageTemp, _procedCnt, _totalCnt, _replaceCnt, _breakCnt);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -89,25 +89,25 @@ public sealed class Main : Tool<Option>, IDisposable
|
|||||||
|
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
[SuppressMessage("ReSharper", "PossibleMultipleEnumeration")]
|
[SuppressMessage("ReSharper", "PossibleMultipleEnumeration")]
|
||||||
public override void Run()
|
public override async Task Run()
|
||||||
{
|
{
|
||||||
if (!Directory.Exists(Opt.Path))
|
if (!Directory.Exists(Opt.Path))
|
||||||
throw new ArgumentException(nameof(Opt.Path), string.Format(Strings.PathNotFound, Opt.Path));
|
throw new ArgumentException(nameof(Opt.Path), string.Format(Str.PathNotFound, Opt.Path));
|
||||||
|
|
||||||
|
|
||||||
using var step1Bar = new IndeterminateProgressBar(Strings.SearchingFile, DefaultProgressBarOptions);
|
using var step1Bar = new IndeterminateProgressBar(Str.SearchingFile, DefaultProgressBarOptions);
|
||||||
|
|
||||||
|
|
||||||
var fileList = EnumerateFiles(Opt.Path, Opt.Filter);
|
var fileList = EnumerateFiles(Opt.Path, Opt.Filter);
|
||||||
_totalCnt = fileList.Count();
|
_totalCnt = fileList.Count();
|
||||||
|
|
||||||
step1Bar.Message = string.Format(Strings.SearchingFileOK, _totalCnt);
|
step1Bar.Message = string.Format(Str.SearchingFileOK, _totalCnt);
|
||||||
step1Bar.Finished();
|
step1Bar.Finished();
|
||||||
if (_totalCnt == 0) return;
|
if (_totalCnt == 0) return;
|
||||||
|
|
||||||
|
|
||||||
_step2Bar = step1Bar.Spawn(_totalCnt, string.Empty, DefaultProgressBarOptions);
|
_step2Bar = step1Bar.Spawn(_totalCnt, string.Empty, DefaultProgressBarOptions);
|
||||||
|
|
||||||
Parallel.ForEach(fileList, FileHandle);
|
await Parallel.ForEachAsync(fileList, FileHandle);
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -1,4 +1,4 @@
|
|||||||
namespace Dot.RmBlank;
|
namespace Dot.RmBlank;
|
||||||
|
|
||||||
[Verb("rm-blank", HelpText = nameof(Strings.RemoveTrailingWhiteSpaces), ResourceType = typeof(Strings))]
|
[Verb("rm-blank", HelpText = nameof(Str.RemoveTrailingWhiteSpaces), ResourceType = typeof(Str))]
|
||||||
public class Option : DirOption { }
|
public class Option : DirOption { }
|
@ -11,6 +11,7 @@ public sealed class Main : Tool<Option>, IDisposable
|
|||||||
private ChildProgressBar _step2Bar;
|
private ChildProgressBar _step2Bar;
|
||||||
private int _totalCnt;
|
private int _totalCnt;
|
||||||
private int _trimCnt;
|
private int _trimCnt;
|
||||||
|
private readonly byte[] _utf8Bom = { 0xef, 0xbb, 0xbf };
|
||||||
public Main(Option opt) : base(opt) { }
|
public Main(Option opt) : base(opt) { }
|
||||||
|
|
||||||
|
|
||||||
@ -19,6 +20,17 @@ public sealed class Main : Tool<Option>, IDisposable
|
|||||||
Dispose(false);
|
Dispose(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private bool CreateTempFile(Stream fsr, string tmpFile)
|
||||||
|
{
|
||||||
|
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);
|
||||||
|
int data;
|
||||||
|
while ((data = fsr.ReadByte()) != -1) fsw.WriteByte((byte)data);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
private void Dispose(bool disposing)
|
private void Dispose(bool disposing)
|
||||||
{
|
{
|
||||||
if (_disposed) return;
|
if (_disposed) return;
|
||||||
@ -26,6 +38,37 @@ public sealed class Main : Tool<Option>, IDisposable
|
|||||||
_disposed = true;
|
_disposed = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private async ValueTask FileHandle(string file, CancellationToken _)
|
||||||
|
{
|
||||||
|
_step2Bar.Tick();
|
||||||
|
ShowMessage(1, 0, 0);
|
||||||
|
|
||||||
|
var tmpFile = $"{file}.tmp";
|
||||||
|
bool isReplaced;
|
||||||
|
await using (var fsr = OpenFileStream(file, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) {
|
||||||
|
if (Opt.ReadOnly) { //测试,只读模式
|
||||||
|
ShowMessage(0, 1, 0);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (fsr is null) {
|
||||||
|
ShowMessage(0, 0, 1);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
isReplaced = CreateTempFile(fsr, tmpFile);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (isReplaced) {
|
||||||
|
MoveFile(tmpFile, file);
|
||||||
|
ShowMessage(0, 1, 0);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
ShowMessage(0, 0, 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
private void ShowMessage(int procedCnt, int replaceCnt, int breakCnt)
|
private void ShowMessage(int procedCnt, int replaceCnt, int breakCnt)
|
||||||
{
|
{
|
||||||
@ -33,7 +76,7 @@ public sealed class Main : Tool<Option>, IDisposable
|
|||||||
_procedCnt += procedCnt;
|
_procedCnt += procedCnt;
|
||||||
_trimCnt += replaceCnt;
|
_trimCnt += replaceCnt;
|
||||||
_breakCnt += breakCnt;
|
_breakCnt += breakCnt;
|
||||||
_step2Bar.Message = string.Format(Strings.ShowMessageTemp, _procedCnt, _totalCnt, _trimCnt, _breakCnt);
|
_step2Bar.Message = string.Format(Str.ShowMessageTemp, _procedCnt, _totalCnt, _trimCnt, _breakCnt);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -44,60 +87,24 @@ public sealed class Main : Tool<Option>, IDisposable
|
|||||||
}
|
}
|
||||||
|
|
||||||
[SuppressMessage("ReSharper", "PossibleMultipleEnumeration")]
|
[SuppressMessage("ReSharper", "PossibleMultipleEnumeration")]
|
||||||
public override void Run()
|
public override async Task Run()
|
||||||
{
|
{
|
||||||
if (!Directory.Exists(Opt.Path))
|
if (!Directory.Exists(Opt.Path))
|
||||||
throw new ArgumentException(nameof(Opt.Path), string.Format(Strings.PathNotFound, Opt.Path));
|
throw new ArgumentException(nameof(Opt.Path), string.Format(Str.PathNotFound, Opt.Path));
|
||||||
|
|
||||||
|
|
||||||
var utf8Bom = new byte[] { 0xef, 0xbb, 0xbf };
|
using var step1Bar = new IndeterminateProgressBar(Str.SearchingFile, DefaultProgressBarOptions);
|
||||||
using var step1Bar = new IndeterminateProgressBar(Strings.SearchingFile, DefaultProgressBarOptions);
|
|
||||||
|
|
||||||
|
|
||||||
var fileList = EnumerateFiles(Opt.Path, Opt.Filter);
|
var fileList = EnumerateFiles(Opt.Path, Opt.Filter);
|
||||||
_totalCnt = fileList.Count();
|
_totalCnt = fileList.Count();
|
||||||
|
|
||||||
step1Bar.Message = string.Format(Strings.SearchingFileOK, _totalCnt);
|
step1Bar.Message = string.Format(Str.SearchingFileOK, _totalCnt);
|
||||||
step1Bar.Finished();
|
step1Bar.Finished();
|
||||||
if (_totalCnt == 0) return;
|
if (_totalCnt == 0) return;
|
||||||
|
|
||||||
_step2Bar = step1Bar.Spawn(_totalCnt, string.Empty, DefaultProgressBarOptions);
|
_step2Bar = step1Bar.Spawn(_totalCnt, string.Empty, DefaultProgressBarOptions);
|
||||||
|
|
||||||
Parallel.ForEach(fileList, file => {
|
await Parallel.ForEachAsync(fileList, FileHandle);
|
||||||
_step2Bar.Tick();
|
|
||||||
ShowMessage(1, 0, 0);
|
|
||||||
|
|
||||||
var tmpFile = $"{file}.tmp";
|
|
||||||
var isReplaced = false;
|
|
||||||
using (var fsr = OpenFileStream(file, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) {
|
|
||||||
if (Opt.ReadOnly) { //测试,只读模式
|
|
||||||
ShowMessage(0, 1, 0);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (fsr is null) {
|
|
||||||
ShowMessage(0, 0, 1);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
Span<byte> buffer = stackalloc byte[utf8Bom.Length];
|
|
||||||
var readLen = fsr.Read(buffer);
|
|
||||||
if (readLen == utf8Bom.Length && buffer.SequenceEqual(utf8Bom)) {
|
|
||||||
using var fsw = OpenFileStream(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,4 +1,4 @@
|
|||||||
namespace Dot.RmBom;
|
namespace Dot.RmBom;
|
||||||
|
|
||||||
[Verb("rm-bom", HelpText = nameof(Strings.TrimUtf8Bom), ResourceType = typeof(Strings))]
|
[Verb("rm-bom", HelpText = nameof(Str.TrimUtf8Bom), ResourceType = typeof(Str))]
|
||||||
public class Option : DirOption { }
|
public class Option : DirOption { }
|
@ -25,11 +25,7 @@ public sealed class Main : Tool<Option>
|
|||||||
public ReadOnlySpan<char> UrlEncode;
|
public ReadOnlySpan<char> UrlEncode;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Main(Option opt) : base(opt)
|
public Main(Option opt) : base(opt) { }
|
||||||
{
|
|
||||||
if (Opt.Text.NullOrEmpty()) Opt.Text = ClipboardService.GetText();
|
|
||||||
if (Opt.Text.NullOrEmpty()) throw new ArgumentException(Strings.InputTextIsEmpty);
|
|
||||||
}
|
|
||||||
|
|
||||||
private static Output BuildOutput(string text, Encoding enc)
|
private static Output BuildOutput(string text, Encoding enc)
|
||||||
{
|
{
|
||||||
@ -68,6 +64,22 @@ public sealed class Main : Tool<Option>
|
|||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static void ParseAndShow(string text)
|
||||||
|
{
|
||||||
|
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
|
||||||
|
|
||||||
|
var ansi = BuildOutput(text, Encoding.GetEncoding("gbk"));
|
||||||
|
var utf8 = BuildOutput(text, Encoding.UTF8);
|
||||||
|
var unicodeLittleEndian = BuildOutput(text, Encoding.Unicode);
|
||||||
|
var unicodeBigEndian = BuildOutput(text, Encoding.BigEndianUnicode);
|
||||||
|
|
||||||
|
|
||||||
|
PrintOutput(ansi);
|
||||||
|
PrintOutput(utf8);
|
||||||
|
PrintOutput(unicodeLittleEndian);
|
||||||
|
PrintOutput(unicodeBigEndian);
|
||||||
|
}
|
||||||
|
|
||||||
private static void PrintOutput(Output o)
|
private static void PrintOutput(Output o)
|
||||||
{
|
{
|
||||||
var outputTemp = $"""
|
var outputTemp = $"""
|
||||||
@ -89,20 +101,14 @@ html-decode: {o.HtmlDecode}
|
|||||||
Console.WriteLine(outputTemp);
|
Console.WriteLine(outputTemp);
|
||||||
}
|
}
|
||||||
|
|
||||||
public override void Run()
|
public override async Task Run()
|
||||||
{
|
{
|
||||||
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
|
if (Opt.Text.NullOrEmpty()) Opt.Text = await ClipboardService.GetTextAsync();
|
||||||
var ansi = BuildOutput(Opt.Text, Encoding.GetEncoding("gbk"));
|
if (Opt.Text.NullOrEmpty()) throw new ArgumentException(Str.InputTextIsEmpty);
|
||||||
var utf8 = BuildOutput(Opt.Text, Encoding.UTF8);
|
|
||||||
var unicodeLittleEndian = BuildOutput(Opt.Text, Encoding.Unicode);
|
|
||||||
var unicodeBigEndian = BuildOutput(Opt.Text, Encoding.BigEndianUnicode);
|
|
||||||
|
|
||||||
|
|
||||||
PrintOutput(ansi);
|
ParseAndShow(Opt.Text);
|
||||||
PrintOutput(utf8);
|
Console.Write(Str.PressAnyKey);
|
||||||
PrintOutput(unicodeLittleEndian);
|
|
||||||
PrintOutput(unicodeBigEndian);
|
|
||||||
Console.Write(Strings.PressAnyKey);
|
|
||||||
Console.ReadKey();
|
Console.ReadKey();
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -1,8 +1,8 @@
|
|||||||
namespace Dot.Text;
|
namespace Dot.Text;
|
||||||
|
|
||||||
[Verb("text", HelpText = nameof(Strings.HelpForText), ResourceType = typeof(Strings))]
|
[Verb("text", HelpText = nameof(Str.HelpForText), ResourceType = typeof(Str))]
|
||||||
public class Option : IOption
|
public class Option : IOption
|
||||||
{
|
{
|
||||||
[Value(0, HelpText = nameof(Strings.TextTobeProcessed), ResourceType = typeof(Strings))]
|
[Value(0, HelpText = nameof(Str.TextTobeProcessed), ResourceType = typeof(Str))]
|
||||||
public string Text { get; set; }
|
public string Text { get; set; }
|
||||||
}
|
}
|
194
src/Time/Main.cs
Normal file
194
src/Time/Main.cs
Normal file
@ -0,0 +1,194 @@
|
|||||||
|
using System.Diagnostics.CodeAnalysis;
|
||||||
|
using System.Net.Sockets;
|
||||||
|
using System.Runtime.InteropServices;
|
||||||
|
|
||||||
|
namespace Dot.Time;
|
||||||
|
|
||||||
|
public sealed class Main : Tool<Option>
|
||||||
|
{
|
||||||
|
private enum ServerStatues : byte
|
||||||
|
{
|
||||||
|
Ready
|
||||||
|
, Connecting
|
||||||
|
, Succeed
|
||||||
|
, Failed
|
||||||
|
}
|
||||||
|
|
||||||
|
[StructLayout(LayoutKind.Explicit)]
|
||||||
|
private ref struct Systemtime
|
||||||
|
{
|
||||||
|
[FieldOffset(6)] public ushort wDay;
|
||||||
|
[FieldOffset(4)] public ushort wDayOfWeek;
|
||||||
|
[FieldOffset(8)] public ushort wHour;
|
||||||
|
[FieldOffset(14)] public ushort wMilliseconds;
|
||||||
|
[FieldOffset(10)] public ushort wMinute;
|
||||||
|
[FieldOffset(2)] public ushort wMonth;
|
||||||
|
[FieldOffset(12)] public ushort wSecond;
|
||||||
|
[FieldOffset(0)] public ushort wYear;
|
||||||
|
}
|
||||||
|
|
||||||
|
private const int _MAX_DEGREE_OF_PARALLELISM = 10;
|
||||||
|
private const int _NTP_PORT = 123;
|
||||||
|
private readonly char[] _loading = { '-', '\\', '|', '/' };
|
||||||
|
private int _procedCnt;
|
||||||
|
private readonly int _serverCnt;
|
||||||
|
|
||||||
|
private readonly Dictionary<string, Server> _serverDictionary;
|
||||||
|
|
||||||
|
|
||||||
|
private readonly string[] _serverDomains = {
|
||||||
|
"ntp.ntsc.ac.cn", "cn.ntp.org.cn", "edu.ntp.org.cn"
|
||||||
|
, "cn.pool.ntp.org", "time.pool.aliyun.com", "time1.aliyun.com"
|
||||||
|
, "time2.aliyun.com", "time3.aliyun.com", "time4.aliyun.com"
|
||||||
|
, "time5.aliyun.com", "time6.aliyun.com", "time7.aliyun.com"
|
||||||
|
, "time1.cloud.tencent.com", "time2.cloud.tencent.com"
|
||||||
|
, "time3.cloud.tencent.com", "time4.cloud.tencent.com"
|
||||||
|
, "time5.cloud.tencent.com", "ntp.sjtu.edu.cn", "ntp.neu.edu.cn"
|
||||||
|
, "ntp.bupt.edu.cn", "ntp.shu.edu.cn", "pool.ntp.org", "0.pool.ntp.org"
|
||||||
|
, "1.pool.ntp.org", "2.pool.ntp.org", "3.pool.ntp.org"
|
||||||
|
, "asia.pool.ntp.org", "time1.google.com", "time2.google.com"
|
||||||
|
, "time3.google.com", "time4.google.com", "time.apple.com"
|
||||||
|
, "time1.apple.com", "time2.apple.com", "time3.apple.com"
|
||||||
|
, "time4.apple.com", "time5.apple.com", "time6.apple.com"
|
||||||
|
, "time7.apple.com", "time.windows.com", "time.nist.gov"
|
||||||
|
, "time-nw.nist.gov", "time-a.nist.gov", "time-b.nist.gov"
|
||||||
|
, "stdtime.gov.hk"
|
||||||
|
};
|
||||||
|
|
||||||
|
private int _successCnt;
|
||||||
|
|
||||||
|
|
||||||
|
public Main(Option opt) : base(opt)
|
||||||
|
{
|
||||||
|
_serverCnt = _serverDomains.Length;
|
||||||
|
_serverDictionary = _serverDomains.ToDictionary(x => x, _ => new Server { Status = ServerStatues.Ready });
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private TimeSpan GetNtpOffset(string server)
|
||||||
|
{
|
||||||
|
Span<byte> ntpData = stackalloc byte[48];
|
||||||
|
ntpData[0] = 0x1B;
|
||||||
|
using var socket
|
||||||
|
= new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp) {
|
||||||
|
ReceiveTimeout = Opt.Timeout
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
try {
|
||||||
|
socket.Connect(server, _NTP_PORT);
|
||||||
|
socket.Send(ntpData);
|
||||||
|
var timeBefore = DateTime.Now;
|
||||||
|
socket.Receive(ntpData);
|
||||||
|
var transferTime = DateTime.Now - timeBefore;
|
||||||
|
|
||||||
|
var intPart = ((ulong)ntpData[40] << 24) //
|
||||||
|
| ((ulong)ntpData[41] << 16) //
|
||||||
|
| ((ulong)ntpData[42] << 8) //
|
||||||
|
| ntpData[43];
|
||||||
|
var fractPart = ((ulong)ntpData[44] << 24) //
|
||||||
|
| ((ulong)ntpData[45] << 16) //
|
||||||
|
| ((ulong)ntpData[46] << 8) //
|
||||||
|
| ntpData[47];
|
||||||
|
var from1900Ms = intPart * 1000 + fractPart * 1000 / 0x100000000L;
|
||||||
|
var onlineTime = new DateTime(1900, 1, 1).AddMilliseconds((long)from1900Ms) + transferTime / 2;
|
||||||
|
return DateTime.UtcNow - onlineTime;
|
||||||
|
}
|
||||||
|
catch (Exception) {
|
||||||
|
return TimeSpan.Zero;
|
||||||
|
}
|
||||||
|
finally {
|
||||||
|
socket.Close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private async void Printing()
|
||||||
|
{
|
||||||
|
const string outputTemp = "{0,-30}\t{1}\t{2,20}\t{3,20}";
|
||||||
|
var rolling = 0;
|
||||||
|
|
||||||
|
Console.Clear();
|
||||||
|
while (true) {
|
||||||
|
await Task.Delay(100);
|
||||||
|
Console.SetCursorPosition(0, 0);
|
||||||
|
var row = //
|
||||||
|
_serverDictionary.Select(x //
|
||||||
|
=> string.Format(outputTemp, x.Key
|
||||||
|
, x.Value.Status == ServerStatues.Connecting
|
||||||
|
? _loading[++rolling % 4]
|
||||||
|
: ' ', x.Value.Status
|
||||||
|
, x.Value.Offset == TimeSpan.Zero
|
||||||
|
? string.Empty
|
||||||
|
: x.Value.Offset));
|
||||||
|
|
||||||
|
|
||||||
|
Console.WriteLine(outputTemp, Str.Server, ' ', Str.Status, Str.LocalClockOffset);
|
||||||
|
Console.WriteLine(string.Join(Environment.NewLine, row));
|
||||||
|
if (_procedCnt == _serverCnt) break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
[DllImport("Kernel32.dll")]
|
||||||
|
private static extern void SetLocalTime(Systemtime st);
|
||||||
|
|
||||||
|
private static void SetSysteTime(DateTime time)
|
||||||
|
{
|
||||||
|
var timeToSet = new Systemtime {
|
||||||
|
wDay = (ushort)time.Day
|
||||||
|
, wDayOfWeek = (ushort)time.DayOfWeek
|
||||||
|
, wHour = (ushort)time.Hour
|
||||||
|
, wMilliseconds = (ushort)time.Millisecond
|
||||||
|
, wMinute = (ushort)time.Minute
|
||||||
|
, wMonth = (ushort)time.Month
|
||||||
|
, wSecond = (ushort)time.Second
|
||||||
|
, wYear = (ushort)time.Year
|
||||||
|
};
|
||||||
|
SetLocalTime(timeToSet);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
[SuppressMessage("ReSharper", "AccessToDisposedClosure")]
|
||||||
|
public override async Task Run()
|
||||||
|
{
|
||||||
|
var tPrinting = Task.Run(Printing);
|
||||||
|
|
||||||
|
await Parallel.ForEachAsync(_serverDictionary
|
||||||
|
, new ParallelOptions { MaxDegreeOfParallelism = _MAX_DEGREE_OF_PARALLELISM }
|
||||||
|
, (server, _) => {
|
||||||
|
server.Value.Status = ServerStatues.Connecting;
|
||||||
|
var offset = GetNtpOffset(server.Key);
|
||||||
|
|
||||||
|
Interlocked.Increment(ref _procedCnt);
|
||||||
|
|
||||||
|
if (offset == TimeSpan.Zero) {
|
||||||
|
server.Value.Status = ServerStatues.Failed;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
server.Value.Status = ServerStatues.Succeed;
|
||||||
|
Interlocked.Increment(ref _successCnt);
|
||||||
|
server.Value.Offset = offset;
|
||||||
|
}
|
||||||
|
|
||||||
|
return ValueTask.CompletedTask;
|
||||||
|
});
|
||||||
|
|
||||||
|
tPrinting.Wait();
|
||||||
|
var avgOffset = TimeSpan.FromTicks((long)_serverDictionary //
|
||||||
|
.Where(x => x.Value.Status == ServerStatues.Succeed)
|
||||||
|
.Average(x => x.Value.Offset.Ticks));
|
||||||
|
|
||||||
|
|
||||||
|
Console.WriteLine(Str.NtpReceiveDone, _successCnt, _serverCnt, avgOffset.TotalMilliseconds);
|
||||||
|
if (!Opt.Sync) return;
|
||||||
|
Console.WriteLine();
|
||||||
|
SetSysteTime(DateTime.Now - avgOffset);
|
||||||
|
Console.WriteLine(Str.LocalTimeSyncDone);
|
||||||
|
}
|
||||||
|
|
||||||
|
private record Server
|
||||||
|
{
|
||||||
|
public TimeSpan Offset;
|
||||||
|
public ServerStatues Status;
|
||||||
|
}
|
||||||
|
}
|
11
src/Time/Option.cs
Normal file
11
src/Time/Option.cs
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
namespace Dot.Time;
|
||||||
|
|
||||||
|
[Verb("time", HelpText = nameof(Str.HelpForText), ResourceType = typeof(Str))]
|
||||||
|
public class Option : IOption
|
||||||
|
{
|
||||||
|
[Option('s', "sync", HelpText = nameof(Str.SyncToLocalTime), Default = false, ResourceType = typeof(Str))]
|
||||||
|
public bool Sync { get; set; }
|
||||||
|
|
||||||
|
[Option('t', "timeout", HelpText = nameof(Str.TimeoutMillSecs), Default = 3000, ResourceType = typeof(Str))]
|
||||||
|
public int Timeout { get; set; }
|
||||||
|
}
|
@ -26,7 +26,7 @@ public sealed class Main : Tool<Option>, IDisposable
|
|||||||
_disposed = true;
|
_disposed = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void FileHandle(string file)
|
private async ValueTask FileHandle(string file, CancellationToken _)
|
||||||
{
|
{
|
||||||
_step2Bar.Tick();
|
_step2Bar.Tick();
|
||||||
ShowMessage(1, 0, 0);
|
ShowMessage(1, 0, 0);
|
||||||
@ -36,7 +36,7 @@ public sealed class Main : Tool<Option>, IDisposable
|
|||||||
var isBin = false;
|
var isBin = false;
|
||||||
int data;
|
int data;
|
||||||
|
|
||||||
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.ReadOnly) { //测试,只读模式
|
||||||
ShowMessage(0, 1, 0);
|
ShowMessage(0, 1, 0);
|
||||||
return;
|
return;
|
||||||
@ -48,7 +48,7 @@ public sealed class Main : Tool<Option>, IDisposable
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
using var fsw = OpenFileStream(tmpFile, FileMode.OpenOrCreate, FileAccess.Write);
|
await using var fsw = OpenFileStream(tmpFile, FileMode.OpenOrCreate, FileAccess.Write);
|
||||||
|
|
||||||
while ((data = fsr.ReadByte()) != -1) {
|
while ((data = fsr.ReadByte()) != -1) {
|
||||||
switch (data) {
|
switch (data) {
|
||||||
@ -92,7 +92,7 @@ public sealed class Main : Tool<Option>, IDisposable
|
|||||||
_procedCnt += procedCnt;
|
_procedCnt += procedCnt;
|
||||||
_replaceCnt += replaceCnt;
|
_replaceCnt += replaceCnt;
|
||||||
_breakCnt += breakCnt;
|
_breakCnt += breakCnt;
|
||||||
_step2Bar.Message = string.Format(Strings.ShowMessageTemp, _procedCnt, _totalCnt, _replaceCnt, _breakCnt);
|
_step2Bar.Message = string.Format(Str.ShowMessageTemp, _procedCnt, _totalCnt, _replaceCnt, _breakCnt);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -103,25 +103,25 @@ public sealed class Main : Tool<Option>, IDisposable
|
|||||||
}
|
}
|
||||||
|
|
||||||
[SuppressMessage("ReSharper", "PossibleMultipleEnumeration")]
|
[SuppressMessage("ReSharper", "PossibleMultipleEnumeration")]
|
||||||
public override void Run()
|
public override async Task Run()
|
||||||
{
|
{
|
||||||
if (!Directory.Exists(Opt.Path))
|
if (!Directory.Exists(Opt.Path))
|
||||||
throw new ArgumentException(nameof(Opt.Path), string.Format(Strings.PathNotFound, Opt.Path));
|
throw new ArgumentException(nameof(Opt.Path), string.Format(Str.PathNotFound, Opt.Path));
|
||||||
|
|
||||||
|
|
||||||
using var step1Bar = new IndeterminateProgressBar(Strings.SearchingFile, DefaultProgressBarOptions);
|
using var step1Bar = new IndeterminateProgressBar(Str.SearchingFile, DefaultProgressBarOptions);
|
||||||
|
|
||||||
|
|
||||||
var fileList = EnumerateFiles(Opt.Path, Opt.Filter);
|
var fileList = EnumerateFiles(Opt.Path, Opt.Filter);
|
||||||
_totalCnt = fileList.Count();
|
_totalCnt = fileList.Count();
|
||||||
|
|
||||||
step1Bar.Message = string.Format(Strings.SearchingFileOK, _totalCnt);
|
step1Bar.Message = string.Format(Str.SearchingFileOK, _totalCnt);
|
||||||
step1Bar.Finished();
|
step1Bar.Finished();
|
||||||
if (_totalCnt == 0) return;
|
if (_totalCnt == 0) return;
|
||||||
|
|
||||||
|
|
||||||
_step2Bar = step1Bar.Spawn(_totalCnt, string.Empty, DefaultProgressBarOptions);
|
_step2Bar = step1Bar.Spawn(_totalCnt, string.Empty, DefaultProgressBarOptions);
|
||||||
|
|
||||||
Parallel.ForEach(fileList, FileHandle);
|
await Parallel.ForEachAsync(fileList, FileHandle);
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -1,4 +1,4 @@
|
|||||||
namespace Dot.ToLf;
|
namespace Dot.ToLf;
|
||||||
|
|
||||||
[Verb("tolf", HelpText = nameof(Strings.ConvertEndOfLineToLF), ResourceType = typeof(Strings))]
|
[Verb("tolf", HelpText = nameof(Str.ConvertEndOfLineToLF), ResourceType = typeof(Str))]
|
||||||
public class Option : DirOption { }
|
public class Option : DirOption { }
|
@ -13,7 +13,7 @@ public abstract class Tool<TOption> : ITool
|
|||||||
, ProgressCharacter = '\u2500'
|
, ProgressCharacter = '\u2500'
|
||||||
};
|
};
|
||||||
|
|
||||||
protected TOption Opt { get; set; }
|
protected TOption Opt { get; }
|
||||||
|
|
||||||
protected Tool(TOption opt)
|
protected Tool(TOption opt)
|
||||||
{
|
{
|
||||||
@ -61,9 +61,10 @@ public abstract class Tool<TOption> : ITool
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch (IOException) { }
|
catch (IOException) { }
|
||||||
|
|
||||||
return fsr;
|
return fsr;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public abstract void Run();
|
public abstract Task Run();
|
||||||
}
|
}
|
@ -13,6 +13,7 @@ public static class ToolsFactory
|
|||||||
, Pwd.Option o => new Pwd.Main(o)
|
, Pwd.Option o => new Pwd.Main(o)
|
||||||
, Text.Option o => new Text.Main(o)
|
, Text.Option o => new Text.Main(o)
|
||||||
, Guid.Option o => new Guid.Main(o)
|
, Guid.Option o => new Guid.Main(o)
|
||||||
|
, Time.Option o => new Time.Main(o)
|
||||||
, _ => throw new ArgumentOutOfRangeException(nameof(option))
|
, _ => throw new ArgumentOutOfRangeException(nameof(option))
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
@ -6,7 +6,7 @@
|
|||||||
<ImplicitUsings>enable</ImplicitUsings>
|
<ImplicitUsings>enable</ImplicitUsings>
|
||||||
<RootNamespace>Dot</RootNamespace>
|
<RootNamespace>Dot</RootNamespace>
|
||||||
<AssemblyName>dot</AssemblyName>
|
<AssemblyName>dot</AssemblyName>
|
||||||
<Version>1.1.0</Version>
|
<Version>1.1.1</Version>
|
||||||
<Authors>nsnail</Authors>
|
<Authors>nsnail</Authors>
|
||||||
<Copyright>Copyright (c) 2022 nsnail</Copyright>
|
<Copyright>Copyright (c) 2022 nsnail</Copyright>
|
||||||
<RepositoryUrl>https://github.com/nsnail/dot.git</RepositoryUrl>
|
<RepositoryUrl>https://github.com/nsnail/dot.git</RepositoryUrl>
|
||||||
@ -30,17 +30,17 @@
|
|||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<EmbeddedResource Update="Lang\Strings.resx">
|
<EmbeddedResource Update="Lang\Str.resx">
|
||||||
<Generator>PublicResXFileCodeGenerator</Generator>
|
<Generator>PublicResXFileCodeGenerator</Generator>
|
||||||
<LastGenOutput>Strings.Designer.cs</LastGenOutput>
|
<LastGenOutput>Str.Designer.cs</LastGenOutput>
|
||||||
</EmbeddedResource>
|
</EmbeddedResource>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<Compile Update="Lang\Strings.Designer.cs">
|
<Compile Update="Lang\Str.Designer.cs">
|
||||||
<DesignTime>True</DesignTime>
|
<DesignTime>True</DesignTime>
|
||||||
<AutoGen>True</AutoGen>
|
<AutoGen>True</AutoGen>
|
||||||
<DependentUpon>Strings.resx</DependentUpon>
|
<DependentUpon>Str.resx</DependentUpon>
|
||||||
</Compile>
|
</Compile>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user