<refactor>

This commit is contained in:
nsnail 2022-12-05 20:22:18 +08:00
parent 7a0efaf015
commit 3cb3b13f98
22 changed files with 70 additions and 25 deletions

View File

@ -1,6 +1,6 @@
namespace Dot.Color;
public sealed class Main : Tool<Option>
public sealed class Main : ToolBase<Option>
{
public Main(Option opt) : base(opt) { }

View File

@ -1,4 +1,4 @@
namespace Dot.Color;
[Verb("color", HelpText = nameof(Str.ScreenPixelTool), ResourceType = typeof(Str))]
public class Option : IOption { }
public class Option : OptionBase { }

View File

@ -1,6 +1,6 @@
namespace Dot;
public class DirOption : IOption
public class DirOption : OptionBase
{
[Option('f', "filter", HelpText = nameof(Str.FileSearchPattern), Default = "*.*", ResourceType = typeof(Str))]
public string Filter { get; set; }

View File

@ -2,7 +2,7 @@ using TextCopy;
namespace Dot.Guid;
public sealed class Main : Tool<Option>
public sealed class Main : ToolBase<Option>
{
public Main(Option opt) : base(opt) { }

View File

@ -1,7 +1,7 @@
namespace Dot.Guid;
[Verb("guid", HelpText = nameof(Str.GuidTool), ResourceType = typeof(Str))]
public class Option : IOption
public class Option : OptionBase
{
[Option('u', "upper", HelpText = nameof(Str.UseUppercase), Default = false, ResourceType = typeof(Str))]
public bool Upper { get; set; } //normal options here

View File

@ -3,7 +3,7 @@ using System.Net.Sockets;
namespace Dot.IP;
public sealed class Main : Tool<Option>
public sealed class Main : ToolBase<Option>
{
public Main(Option opt) : base(opt) { }

View File

@ -1,4 +1,4 @@
namespace Dot.IP;
[Verb("ip", HelpText = nameof(Str.Ip), ResourceType = typeof(Str))]
public class Option : IOption { }
public class Option : OptionBase { }

View File

@ -131,6 +131,15 @@ namespace Dot.Lang {
}
}
/// <summary>
/// Looks up a localized string similar to 执行命令后保留会话.
/// </summary>
public static string KeepSession {
get {
return ResourceManager.GetString("KeepSession", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to Local clock offset.
/// </summary>
@ -194,6 +203,15 @@ namespace Dot.Lang {
}
}
/// <summary>
/// Looks up a localized string similar to NTP 服务器标准时钟: {0}.
/// </summary>
public static string NtpServerTime {
get {
return ResourceManager.GetString("NtpServerTime", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to 指定的路径“{0}”不存在.
/// </summary>

View File

@ -131,4 +131,10 @@
<data name="Ip" xml:space="preserve">
<value>IP tools</value>
</data>
<data name="KeepSession" xml:space="preserve">
<value>Keep the session after executing the command</value>
</data>
<data name="NtpServerTime" xml:space="preserve">
<value>NTP server standard clock: {0}</value>
</data>
</root>

View File

@ -60,6 +60,9 @@
<data name="FileSearchPattern" xml:space="preserve">
<value>文件通配符</value>
</data>
<data name="KeepSession" xml:space="preserve">
<value>执行命令后保留会话</value>
</data>
<data name="TimeoutMillSecs" xml:space="preserve">
<value>连接NTP服务器超时时间 (毫秒)</value>
</data>
@ -121,6 +124,9 @@
<data name="LocalTimeOffset" xml:space="preserve">
<value>{0}, 本机时钟偏移: {1} ms</value>
</data>
<data name="NtpServerTime" xml:space="preserve">
<value>NTP 服务器标准时钟: {0}</value>
</data>
<data name="LocalTimeSyncDone" xml:space="preserve">
<value>本机时间已同步</value>
</data>

7
src/Option.cs Normal file
View File

@ -0,0 +1,7 @@
namespace Dot;
public abstract class OptionBase : IOption
{
[Option('k', "keep-session", HelpText = nameof(Str.KeepSession), Default = false, ResourceType = typeof(Str))]
public virtual bool KeepSession { get; set; }
}

View File

@ -12,8 +12,14 @@ Type[] LoadVerbs()
async Task Run(object args)
{
var tool = ToolsFactory.Create(args as IOption);
var option = args as OptionBase;
var tool = ToolsFactory.Create(option);
await tool.Run();
if (option!.KeepSession) {
Console.WriteLine();
Console.WriteLine(Str.PressAnyKey);
Console.ReadKey();
}
}

View File

@ -3,7 +3,7 @@ using TextCopy;
namespace Dot.Pwd;
public sealed class Main : Tool<Option>
public sealed class Main : ToolBase<Option>
{
private readonly char[][] _charTable = {
"0123456789".ToCharArray() //

View File

@ -1,7 +1,7 @@
namespace Dot.Pwd;
[Verb("pwd", HelpText = nameof(Str.RandomPasswordGenerator), ResourceType = typeof(Str))]
public class Option : IOption
public class Option : OptionBase
{
[Flags]
public enum GenerateTypes

View File

@ -3,7 +3,7 @@ using NSExt.Extensions;
namespace Dot.RmBlank;
public sealed class Main : Tool<Option>, IDisposable
public sealed class Main : ToolBase<Option>, IDisposable
{
private int _breakCnt;
private bool _disposed;

View File

@ -2,7 +2,7 @@ using System.Diagnostics.CodeAnalysis;
namespace Dot.RmBom;
public sealed class Main : Tool<Option>, IDisposable
public sealed class Main : ToolBase<Option>, IDisposable
{
private int _breakCnt;
private bool _disposed;

View File

@ -5,7 +5,7 @@ using TextCopy;
namespace Dot.Text;
public sealed class Main : Tool<Option>
public sealed class Main : ToolBase<Option>
{
private ref struct Output
{
@ -108,7 +108,5 @@ html-decode: {o.HtmlDecode}
ParseAndShow(Opt.Text);
Console.Write(Str.PressAnyKey);
Console.ReadKey();
}
}

View File

@ -1,7 +1,7 @@
namespace Dot.Text;
[Verb("text", HelpText = nameof(Str.TextTool), ResourceType = typeof(Str))]
public class Option : IOption
public class Option : OptionBase
{
[Value(0, HelpText = nameof(Str.TextTobeProcessed), ResourceType = typeof(Str))]
public string Text { get; set; }

View File

@ -5,7 +5,7 @@ using System.Runtime.InteropServices;
namespace Dot.Time;
public sealed class Main : Tool<Option>
public sealed class Main : ToolBase<Option>
{
private record Server
{
@ -217,16 +217,20 @@ public sealed class Main : Tool<Option>
Console.WriteLine(Str.NtpReceiveDone, _successCnt, _serverCnt, avgOffset.TotalMilliseconds);
if (!Opt.Sync) {
if (!Opt.KeepSession) return;
var waitObj = new ManualResetEvent(false);
var _ = Task.Run(async () => {
var top = Console.GetCursorPosition().Top;
while (true) {
Console.SetCursorPosition(0, top);
Console.Write(Str.NtpServerTime, (DateTime.Now - avgOffset).ToString("O"));
waitObj.Set();
await Task.Delay(100);
Console.SetCursorPosition(0, Console.GetCursorPosition().Top);
Console.Write(Str.ServerTime, (DateTime.Now - avgOffset).ToString("O"));
Console.Write(@", {0}", Str.PressAnyKey);
}
// ReSharper disable once FunctionNeverReturns
});
Console.ReadKey();
waitObj.WaitOne();
return;
}

View File

@ -1,7 +1,7 @@
namespace Dot.Time;
[Verb("time", HelpText = nameof(Str.TimeTool), ResourceType = typeof(Str))]
public class Option : IOption
public class Option : OptionBase
{
[Option('s', "sync", HelpText = nameof(Str.SyncToLocalTime), Default = false, ResourceType = typeof(Str))]
public bool Sync { get; set; }

View File

@ -2,7 +2,7 @@ using System.Diagnostics.CodeAnalysis;
namespace Dot.ToLf;
public sealed class Main : Tool<Option>, IDisposable
public sealed class Main : ToolBase<Option>, IDisposable
{
private int _breakCnt;
private bool _disposed;

View File

@ -1,6 +1,6 @@
namespace Dot;
public abstract class Tool<TOption> : ITool
public abstract class ToolBase<TOption> : ITool where TOption : OptionBase
{
protected readonly ProgressBarOptions //
DefaultProgressBarOptions = new() {
@ -15,7 +15,7 @@ public abstract class Tool<TOption> : ITool
protected TOption Opt { get; }
protected Tool(TOption opt)
protected ToolBase(TOption opt)
{
Opt = opt;
}