Skip to content

Commit

Permalink
style: 更改代码样式以符合标准
Browse files Browse the repository at this point in the history
  • Loading branch information
Zaitonn committed Nov 19, 2024
1 parent c9396c9 commit 66afcfb
Show file tree
Hide file tree
Showing 124 changed files with 1,266 additions and 219 deletions.
9 changes: 7 additions & 2 deletions src/Serein.Cli/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -98,23 +98,28 @@ private static void BuildApp()
var serverSwitcher = app.Services.GetRequiredService<ServerSwitcher>();

if (SereinAppBuilder.StartForTheFirstTime)
{
ShowWelcomePage(logger);
}

if (FileLoggerProvider.IsEnabled)
{
ShowWarningOfLogMode(logger);

}
app.Start();

serverSwitcher.Initialize();
updateChecker.Updated += (_, e) =>
updateChecker.Updated += (_, _) =>
{
if (updateChecker.Newest is not null)
{
logger.LogInformation(
"发现新版本:{}{}发布地址:{}",
updateChecker.Newest.TagName,
Environment.NewLine,
updateChecker.Newest.Url
);
}
};

app.WaitForShutdown();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ private void OnCancelKeyPress(object? sender, ConsoleCancelEventArgs e)

_logger.LogError("当前还有以下{}个服务器未关闭", servers.Count());
foreach (var kv in servers)
{
_logger.LogError("▫ {} (Id:{})", kv.Value.Configuration.Name, kv.Key);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ private IEnumerable<CompletionItem> GetServerCompletionItem()
{
return _serverManager.Servers.Select(
(kv) =>
new CompletionItem(kv.Key, getExtendedDescription: (token) => GetDescription(kv))
new CompletionItem(kv.Key, getExtendedDescription: (_) => GetDescription(kv))
);

static Task<FormattedString> GetDescription(KeyValuePair<string, Server> kv)
Expand Down Expand Up @@ -67,14 +67,18 @@ private IEnumerable<CompletionItem> GetPluginIdCompletionItem()
var dictionary = new Dictionary<string, IPlugin>();

foreach (var kv in _jsPluginLoader.Plugins)
{
dictionary.TryAdd(kv.Key, kv.Value);
}

foreach (var kv in _netPluginLoader.Plugins)
{
dictionary.TryAdd(kv.Key, kv.Value);
}

return dictionary.Select(
(kv) =>
new CompletionItem(kv.Key, getExtendedDescription: (token) => GetDescription(kv))
new CompletionItem(kv.Key, getExtendedDescription: (_) => GetDescription(kv))
);

static Task<FormattedString> GetDescription(KeyValuePair<string, IPlugin> kv)
Expand Down Expand Up @@ -106,7 +110,7 @@ private static IEnumerable<CompletionItem> LoadFrom<THandler>()
(attr) =>
new CompletionItem(
attr.Command,
getExtendedDescription: (token) =>
getExtendedDescription: (_) =>
Task.FromResult(new FormattedString(attr.Description))
)
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ CancellationToken cancellationToken
StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries
);
if (args.Length <= 1) // 根命令
{
return Task.FromResult<IReadOnlyList<CompletionItem>>(
[
.. _commandProvider
Expand All @@ -48,6 +49,7 @@ .. _commandProvider
.ThenBy((item) => item.ReplacementText[0]),
]
);
}

if (args.Length > 0)
switch (args[0])
Expand Down Expand Up @@ -113,11 +115,16 @@ .. GetServerCompletionItem()
private static int CalculateRelevance(string word, string input)
{
for (int i = word.Length; i > 0; i--)
{
if (input == word[..i])
{
return i;
}
else if (input.Contains(word[..i]))
{
return i - 10;

}
}
return int.MinValue;
}
}
21 changes: 15 additions & 6 deletions src/Serein.Cli/Services/Interaction/CommandProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,14 @@ public CommandProvider(IServiceProvider serviceProvider)
var type = command.GetType();
var attribute = type.GetCustomAttribute<CommandNameAttribute>();
if (attribute is null)
{
continue;

}
dict[attribute.RootCommand] = command;
if (attribute.RootCommand == "help")
{
dict["?"] = command;

}
GenerateHelpPage(list, stringBuilder, type);
}

Expand All @@ -71,23 +73,28 @@ Type type
var nameAttribute = type.GetCustomAttribute<CommandNameAttribute>();
var descriptionAttribute = type.GetCustomAttribute<CommandDescriptionAttribute>();
if (nameAttribute is null || descriptionAttribute is null)
{
return;
}

stringBuilder.AppendLine($"■ {nameAttribute.RootCommand} {nameAttribute.Name}");

stringBuilder.AppendLine(" ▢ 描述");
foreach (var line in descriptionAttribute.Lines)
{
stringBuilder.AppendLine($" ▫ {line}");

}

var childrenAttributes = type.GetCustomAttributes<CommandChildrenAttribute>();
if (childrenAttributes.Any())
{
stringBuilder.AppendLine(" ▢ 用法");
foreach (var child in childrenAttributes)
{
stringBuilder.AppendLine(
$" ▫ {nameAttribute.RootCommand} {child.Command} {child.Description}"
);
$" ▫ {nameAttribute.RootCommand} {child.Command} {child.Description}"
);
}
}

stringBuilder.AppendLine();
Expand All @@ -101,7 +108,7 @@ CommandDescriptionAttribute descriptionAttribute
{
return new(
nameAttribute.RootCommand,
getExtendedDescription: (cancellationToken) =>
getExtendedDescription: (_) =>
{
var stringBuilder = new StringBuilder();
stringBuilder.AppendLine(nameAttribute.Name);
Expand All @@ -111,7 +118,9 @@ CommandDescriptionAttribute descriptionAttribute
{
stringBuilder.AppendLine("描述");
foreach (var line in descriptionAttribute.Lines)
{
stringBuilder.AppendLine($"▫ {line}");
}
}

return Task.FromResult<FormattedString>(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace Serein.Cli.Services.Interaction.Handlers;

[CommandName("clear", "清屏")]
[CommandDescription(["清除控制台所有输出"])]
public sealed class ClearScreenHandler() : CommandHandler
public sealed class ClearScreenHandler : CommandHandler
{
public override void Invoke(IReadOnlyList<string> args)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Serein.Cli.Services.Interaction.Handlers;

public abstract class CommandHandler()
public abstract class CommandHandler
{
public abstract void Invoke(IReadOnlyList<string> args);
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ WsConnectionManager wsConnectionManager
public override void Invoke(IReadOnlyList<string> args)
{
if (args.Count == 1)
{
throw new InvalidArgumentException("缺少参数。可用值:\"info\"\"open\"\"close\"");
}

switch (args[1].ToLowerInvariant())
{
Expand Down
4 changes: 3 additions & 1 deletion src/Serein.Cli/Services/Interaction/Handlers/ExitHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
using Microsoft.Extensions.Logging;

using Serein.Cli.Models;
using Serein.Core.Models.Server;
using Serein.Core.Services.Servers;

namespace Serein.Cli.Services.Interaction.Handlers;
Expand All @@ -30,7 +29,10 @@ public override void Invoke(IReadOnlyList<string> args)
var servers = _serverManager.Servers.Where((kv) => kv.Value.Status);

_logger.LogError("当前还有以下{}个服务器未关闭", servers.Count());

foreach (var kv in servers)
{
_logger.LogError("- {} (Id={})", kv.Value.Configuration.Name, kv.Key);
}
}
}
14 changes: 14 additions & 0 deletions src/Serein.Cli/Services/Interaction/Handlers/PluginHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,19 +64,33 @@ public override void Invoke(IReadOnlyList<string> args)

case "disable" when args.Count == 3:
if (args.Count == 2)
{
throw new InvalidArgumentException("缺少插件Id");
}

if (_jsPluginLoader.Plugins.TryGetValue(args[2], out var jsPlugin))
{
if (jsPlugin.IsEnabled)
{
jsPlugin.Disable();
}
else
{
throw new InvalidOperationException("插件已经被禁用");
}
}

if (_netPluginLoader.Plugins.TryGetValue(args[2], out var netPlugin))
{
if (netPlugin.IsEnabled)
{
netPlugin.Disable();
}
else
{
throw new InvalidOperationException("插件已经被禁用");
}
}
break;

default:
Expand Down
10 changes: 10 additions & 0 deletions src/Serein.Cli/Services/Interaction/Handlers/ServerHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,11 @@ ServerSwitcher serverSwitcher
public override void Invoke(IReadOnlyList<string> args)
{
if (args.Count == 1)
{
throw new InvalidArgumentException(
"缺少参数。可用值:\"info\"\"start\"\"stop\"\"terminate\"\"switch\""
);
}

if (args[1].Equals("list", StringComparison.InvariantCultureIgnoreCase))
{
Expand All @@ -62,18 +64,24 @@ public override void Invoke(IReadOnlyList<string> args)
args[1].Equals("switch", StringComparison.InvariantCultureIgnoreCase)
&& args.Count == 2
)
{
throw new InvalidArgumentException("缺少服务器Id。");
}

var id = args.Count == 3 ? args[2] : _serverSwitcher.CurrentId;

if (string.IsNullOrEmpty(id))
{
throw new InvalidArgumentException(
"缺少服务器Id。"
+ "你可以在命令末尾添加服务器Id或使用\"server switch <id>\"选择你要控制的服务器"
);
}

if (!_serverManager.Servers.TryGetValue(id, out Server? server))
{
throw new InvalidArgumentException("指定的服务器不存在");
}

switch (args[1].ToLowerInvariant())
{
Expand Down Expand Up @@ -119,7 +127,9 @@ public override void Invoke(IReadOnlyList<string> args)
server.Start();

if (string.IsNullOrEmpty(_settingProvider.Value.Application.CliCommandHeader))
{
_settingProvider.Value.Application.CliCommandHeader = "//";
}

_logger.LogWarning(
"服务器已启动,输入的命令将转发至服务器。若要执行Serein的命令,你需要在命令前加上\"{}\"",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
using System.Collections.Generic;

using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;

using Serein.Cli.Models;
Expand Down
4 changes: 4 additions & 0 deletions src/Serein.Cli/Services/Interaction/InputHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,11 @@ public void Handle(IReadOnlyList<string> args)
}

if (!_commandProvider.Handlers.TryGetValue(args[0].ToLowerInvariant(), out var parser))
{
_logger.LogError("未知命令。请使用\"help\"查看所有命令");
}
else
{
try
{
parser.Invoke(args);
Expand All @@ -35,5 +38,6 @@ public void Handle(IReadOnlyList<string> args)
{
_logger.LogError(e, "运行命令时出现异常");
}
}
}
}
Loading

0 comments on commit 66afcfb

Please sign in to comment.