using System.Diagnostics;
using XFEExtension.NetCore.XFEConsole.Options;
using XFEExtension.NetCore.XFEConsole.Terminal;
using XFEExtension.NetCore.XFEConsole.Utilities.Helpers;
using XFEExtension.NetCore.XFETransform;
using XFEExtension.NetCore.XFETransform.ObjectInfoAnalyzer;
using XFEExtension.NetCore.XFETransform.StringConverter;
namespace XFEExtension.NetCore.XFEConsole;
///
/// XFE控制台
///
public static class XFEConsole
{
///
/// 是否在本地调试的Debug中展示
///
///
/// 默认为 true
///
public static bool ShowInDebug { get; set; } = true;
///
/// 使用控制台颜色
///
///
/// 默认为 true
///
public static bool UseConsoleColor { get; set; } = true;
///
/// 是否在本地控制台中显示
///
///
/// 默认为 false
///
public static bool ShowInLocalConsole { get; set; } = true;
///
/// 是否自动解析对象,而非直接输出对象的.ToString()方法
///
///
/// 默认为 true
///
public static bool AutoAnalyzeObject { get; set; } = true;
///
/// 是否启动日志记录
///
public static bool EnableLog { get; set; }
///
/// 当前日志
///
public static XFELog Log { get; set; } = new XFEFileLog();
///
/// 客户端列表
///
public static List ClientList { get; set; } = [];
///
/// 当前由调试程序托管的远程控制台服务器列表。
///
public static List ProgramServerList { get; set; } = [];
///
/// 当前控制台输出流
///
public static XFEConsoleTextWriter? CurrentConsoleTextWriter { get; set; }
///
/// 当前本地终端能力快照。
///
public static XFETerminalCapabilities TerminalCapabilities => XFETerminal.Capabilities;
///
/// 设置 Windows Terminal 标签页及任务栏进度。
///
/// 进度状态。
/// 0 到 100 的进度。
/// 是否已发送给 Windows Terminal。
public static bool SetTerminalProgress(XFETerminalProgressState state, int progress = 0) =>
XFETerminal.SetTaskbarProgress(state, progress);
///
/// 创建同时支持行内显示与 Windows Terminal 任务栏的进度条。
///
/// 进度条选项。
/// 可选输出目标。
/// 进度条对象。
public static XFETerminalProgressBar CreateTerminalProgressBar(
XFETerminalProgressBarOptions? options = null,
TextWriter? writer = null) => XFETerminal.CreateProgressBar(options, writer);
///
/// 生成可直接输出的终端标题艺术字。
///
/// 标题文本。
/// 艺术字选项。
/// 艺术字字符串。
public static string GenerateTitleArt(string text, XFETerminalTitleArtOptions? options = null) =>
XFETerminalTitleArt.Generate(text, options);
///
/// 直接向本地终端显示标题艺术字。
///
/// 标题文本。
/// 艺术字选项。
/// 可选输出目标。
public static void WriteTitleArt(
string text,
XFETerminalTitleArtOptions? options = null,
TextWriter? writer = null) => XFETerminalTitleArt.Write(text, options, writer);
///
/// 使用XFE控制台
///
/// IP地址
/// 客户端名称
/// 客户端ID
/// 密码
/// 是否连接成功
public static async Task UseXFEConsole(string ipAddress, string name, string id, string password)
{
SetConsoleOutput();
return await ConnectConsole(ipAddress, name, id, password);
}
///
/// 使用XFE控制台
///
/// 端口
/// 密码
/// 是否连接成功
public static async Task UseXFEConsole(int port = 3280, string password = "") => await UseXFEConsole($"ws://localhost:{port}/", AppDomain.CurrentDomain.FriendlyName, Guid.NewGuid().ToString(), password);
///
/// 以服务器模式使用 XFE 控制台,允许工具箱主动连接当前调试程序。
///
/// 监听端口。
/// 是否仅允许本机工具箱连接;远程调试时应设为 false。
/// 工具箱连接密码。
/// 显示在工具箱中的程序名称。
/// 程序唯一标识。
/// 已启动的调试程序服务器。
public static async Task UseXFEConsoleServer(
int port = 3280,
bool localOnly = true,
string password = "",
string? programName = null,
string? programId = null)
{
SetConsoleOutput();
var server = new XFEConsoleProgramServer(port, localOnly, password, programName, programId);
await server.StartAsync().ConfigureAwait(false);
ProgramServerList.Add(server);
return server;
}
///
/// 使用XFE控制台日志
///
public static void UseXFEConsoleLog(XFEConsoleLogOptions? xFEConsoleLogOptions = null)
{
EnableLog = true;
xFEConsoleLogOptions ??= new XFEConsoleLogOptions();
switch (xFEConsoleLogOptions.LogType)
{
case Models.LogType.MemoryLog:
Log = new XFEMemoryLog();
break;
case Models.LogType.FileLog:
default:
break;
}
Log.AutoAddTimeInfo = xFEConsoleLogOptions.AutoApplyTimeInfo;
Log.LogTextMaximizeLength = xFEConsoleLogOptions.LogTextMaximizeLength;
if (xFEConsoleLogOptions.UseAnsiConsoleEncoding)
ConsoleAnsi.Enable();
SetConsoleOutput();
}
///
/// 使用XFE控制台日志
///
///
public static void UseXFEConsoleLog(Action optionBuilder)
{
var options = new XFEConsoleLogOptions();
optionBuilder(options);
UseXFEConsoleLog(options);
}
///
/// 停止XFE控制台
///
///
public static async Task StopXFEConsole()
{
if (CurrentConsoleTextWriter is not null)
Console.SetOut(CurrentConsoleTextWriter.OriginalTextWriter);
foreach (var client in ClientList.ToArray())
await client.Client.DisconnectAsync().ConfigureAwait(false);
foreach (var server in ProgramServerList.ToArray())
await server.StopAsync().ConfigureAwait(false);
ClientList.Clear();
ProgramServerList.Clear();
CurrentConsoleTextWriter = null;
}
///
/// 设置XFE控制台
///
///
public static void SetConsoleOutput()
{
if (Console.Out is XFEConsoleTextWriter currentWriter)
{
CurrentConsoleTextWriter = currentWriter;
return;
}
CurrentConsoleTextWriter = new(Console.Out);
Console.SetOut(CurrentConsoleTextWriter);
}
///
/// 连接XFE控制台
///
/// IP地址
/// 客户端名称
/// 客户端ID
/// 密码
/// 是否连接成功
public static async Task ConnectConsole(string ipAddress, string name, string id, string password)
{
var client = new XFEConsoleProgramClient(ipAddress, name, id, password);
if (await client.Connect())
{
ClientList.Add(client);
return true;
}
return false;
}
///
/// 输出对象信息
///
/// 对象
/// 对象注释
/// 仅解析属性
/// 仅解析公共属性或字段
///
public static async Task WriteObject(object? obj, bool onlyProperty = false, bool onlyPublic = true, string remarkName = "分析对象")
{
string? objectInfo;
try
{
objectInfo = obj is null ? $"[foldblock color: white #9898e7 title: 分析对象:{obj?.GetType().Name ?? "空对象"} text: 对象内容为空]" : $"[foldblock color: white #9898e7 title: 分析对象:{obj.GetType().Name} text: {XFEConverter.GetObjectInfo(StringConverter.ColoredObjectAnalyzer, remarkName, ObjectPlace.Main, 0, [obj], obj.GetType(), obj, onlyProperty, onlyPublic).OutPutObject()}]";
}
catch (Exception ex)
{
objectInfo = $"[foldblock color: white #ff0000 title: 错误:{ex.Message} text: {ex}]";
}
if (ShowInDebug)
Debug.WriteLine(objectInfo);
if (ShowInLocalConsole)
await (CurrentConsoleTextWriter?.OriginalTextWriter.WriteLineAsync(objectInfo) ?? Task.CompletedTask);
await OutputToRemoteConsolesAsync(objectInfo, true).ConfigureAwait(false);
}
///
/// 向已连接的XFE控制台输出一条消息
///
/// 文本
///
public static void WriteLine(string? text)
{
if (text is null) return;
if (ShowInDebug)
Debug.WriteLine(text);
if (EnableLog)
{
var log = Log.WriteLine(text, out _);
if (ShowInLocalConsole)
CurrentConsoleTextWriter?.OriginalTextWriter.WriteLine(log.ToString(UseConsoleColor));
}
else if (ShowInLocalConsole)
{
CurrentConsoleTextWriter?.OriginalTextWriter.WriteLine(text);
}
OutputToRemoteConsolesAsync($"[color {ConvertConsoleColorToString(Console.ForegroundColor)} {ConvertConsoleColorToString(Console.BackgroundColor)}]{text}", true).GetAwaiter().GetResult();
}
///
/// 向已连接的XFE控制台输出一条消息
///
/// 文本
///
public static void Write(string? text)
{
if (text is null) return;
if (ShowInDebug)
Debug.WriteLine(text);
if (EnableLog)
{
var log = Log.Write(text, out var isHead);
if (ShowInLocalConsole && !Log.RecordOnlyOnWriteLine)
CurrentConsoleTextWriter?.OriginalTextWriter.Write($"{(Log.AutoAddTimeInfo && isHead ? XFELogEntry.TimeToString(log.Time) : string.Empty)}{text}");
}
else if (ShowInLocalConsole)
{
CurrentConsoleTextWriter?.OriginalTextWriter.Write(text);
}
OutputToRemoteConsolesAsync($"[color {ConvertConsoleColorToString(Console.ForegroundColor)} {ConvertConsoleColorToString(Console.BackgroundColor)}]{text}", false).GetAwaiter().GetResult();
}
///
/// 向已连接的XFE控制台输出一条消息
///
/// 文本
///
public static async Task WriteLineAsync(string? text)
{
if (text is null) return;
if (ShowInDebug)
Debug.WriteLine(text);
if (EnableLog)
{
var log = Log.WriteLine(text, out _);
if (ShowInLocalConsole)
await (CurrentConsoleTextWriter?.OriginalTextWriter.WriteLineAsync(log.ToString(UseConsoleColor)) ?? Task.CompletedTask);
}
else if (ShowInLocalConsole)
{
await (CurrentConsoleTextWriter?.OriginalTextWriter.WriteLineAsync(text) ?? Task.CompletedTask);
}
await OutputToRemoteConsolesAsync($"[color {ConvertConsoleColorToString(Console.ForegroundColor)} {ConvertConsoleColorToString(Console.BackgroundColor)}]{text}", true).ConfigureAwait(false);
}
///
/// 向已连接的XFE控制台输出一条消息
///
/// 文本
///
public static async Task WriteAsync(string? text)
{
if (text is not null)
{
if (ShowInDebug)
Debug.WriteLine(text);
if (EnableLog)
{
var log = Log.Write(text, out var isHead);
if (ShowInLocalConsole && !Log.RecordOnlyOnWriteLine)
await (CurrentConsoleTextWriter?.OriginalTextWriter.WriteAsync($"{(Log.AutoAddTimeInfo && isHead ? XFELogEntry.TimeToString(log.Time) : string.Empty)}{text}") ?? Task.CompletedTask);
}
else if (ShowInLocalConsole)
{
await (CurrentConsoleTextWriter?.OriginalTextWriter.WriteAsync(text) ?? Task.CompletedTask);
}
await OutputToRemoteConsolesAsync($"[color {ConvertConsoleColorToString(Console.ForegroundColor)} {ConvertConsoleColorToString(Console.BackgroundColor)}]{text}", false).ConfigureAwait(false);
}
}
private static async Task OutputToRemoteConsolesAsync(string message, bool isLine)
{
foreach (var client in ClientList.ToArray())
await client.OutputMessage(message, isLine).ConfigureAwait(false);
foreach (var server in ProgramServerList.ToArray())
await server.OutputMessage(message, isLine).ConfigureAwait(false);
}
///
/// 将控制台颜色转为颜色代码
///
/// 控制台颜色
/// 颜色代码
public static string ConvertConsoleColorToString(ConsoleColor consoleColor) => consoleColor switch
{
ConsoleColor.Black => "black",
ConsoleColor.DarkBlue => "#0037da",
ConsoleColor.DarkGreen => "#13a10e",
ConsoleColor.DarkCyan => "#3a96dd",
ConsoleColor.DarkRed => "#c50f1f",
ConsoleColor.DarkMagenta => "#881798",
ConsoleColor.DarkYellow => "#c19c00",
ConsoleColor.Gray => "#cccccc",
ConsoleColor.DarkGray => "#767676",
ConsoleColor.Blue => "#0037da",
ConsoleColor.Green => "#16c60c",
ConsoleColor.Cyan => "#61d6d6",
ConsoleColor.Red => "#e74856",
ConsoleColor.Magenta => "#b4009e",
ConsoleColor.Yellow => "#f9f1a5",
ConsoleColor.White => "white",
_ => "Transparent",
};
}