namespace XFEExtension.NetCore.XFEConsole.Terminal;
///
/// 一组可组合的终端文字样式。
///
public readonly record struct XFETerminalStyle
{
/// 前景色;为空时不修改。
public XFETerminalColor? Foreground { get; init; }
/// 背景色;为空时不修改。
public XFETerminalColor? Background { get; init; }
/// 粗体或高亮。
public bool Bold { get; init; }
/// 暗淡显示。
public bool Dim { get; init; }
/// 斜体。
public bool Italic { get; init; }
/// 下划线。
public bool Underline { get; init; }
/// 缓慢闪烁。
public bool Blink { get; init; }
/// 交换前景色与背景色。
public bool Inverse { get; init; }
/// 隐藏文字。
public bool Hidden { get; init; }
/// 删除线。
public bool Strikethrough { get; init; }
///
/// 生成启用当前样式的 SGR 序列。
///
/// ANSI SGR 字符串。
public string ToSequence()
{
var parameters = new List(10);
if (Bold) parameters.Add("1");
if (Dim) parameters.Add("2");
if (Italic) parameters.Add("3");
if (Underline) parameters.Add("4");
if (Blink) parameters.Add("5");
if (Inverse) parameters.Add("7");
if (Hidden) parameters.Add("8");
if (Strikethrough) parameters.Add("9");
if (Foreground is { } foreground) parameters.Add(foreground.ToSgrParameters(true));
if (Background is { } background) parameters.Add(background.ToSgrParameters(false));
return parameters.Count == 0 ? string.Empty : $"\x1b[{string.Join(';', parameters)}m";
}
///
/// 把当前样式应用到文本,并可在结尾恢复默认样式。
///
/// 待装饰文本。
/// 是否追加样式重置序列。
/// 带 ANSI 样式的文本。
public string Apply(string text, bool reset = true) =>
$"{ToSequence()}{text}{(reset ? XFETerminalSequences.ResetStyle : string.Empty)}";
}