using System.Text;
using System.Text.RegularExpressions;
using Spectre.Console;
namespace N_m3u8DL_RE.Common.Log;
public class NonAnsiWriter : TextWriter
{
public override Encoding Encoding => Console.OutputEncoding;
private string? _lastOut = "";
public override void Write(char value)
{
Console.Write(value);
}
public override void Write(string? value)
{
if (_lastOut == value)
{
return;
}
_lastOut = value;
RemoveAnsiEscapeSequences(value);
}
private void RemoveAnsiEscapeSequences(string? input)
{
// Use regular expression to remove ANSI escape sequences
string output = Regex.Replace(input ?? "", @"\x1B\[(\d+;?)+m", "");
output = Regex.Replace(output, @"\[\??\d+[AKlh]", "");
output = Regex.Replace(output,"[\r\n] +","");
if (string.IsNullOrWhiteSpace(output))
{
return;
}
Console.Write(output);
}
}
///
/// A console capable of writing ANSI escape sequences.
///
public static class CustomAnsiConsole
{
public static IAnsiConsole Console { get; set; } = AnsiConsole.Console;
public static void InitConsole(bool forceAnsi, bool noAnsiColor)
{
if (forceAnsi)
{
var ansiConsoleSettings = new AnsiConsoleSettings();
if (noAnsiColor)
{
ansiConsoleSettings.Out = new AnsiConsoleOutput(new NonAnsiWriter());
}
ansiConsoleSettings.Interactive = InteractionSupport.Yes;
ansiConsoleSettings.Ansi = AnsiSupport.Yes;
Console = AnsiConsole.Create(ansiConsoleSettings);
Console.Profile.Width = int.MaxValue;
}
else
{
var ansiConsoleSettings = new AnsiConsoleSettings();
if (noAnsiColor)
{
ansiConsoleSettings.Out = new AnsiConsoleOutput(new NonAnsiWriter());
}
Console = AnsiConsole.Create(ansiConsoleSettings);
}
}
///
/// Writes the specified markup to the console.
///
/// The value to write.
public static void Markup(string value)
{
Console.Markup(value);
}
///
/// Writes the specified markup, followed by the current line terminator, to the console.
///
/// The value to write.
public static void MarkupLine(string value)
{
Console.MarkupLine(value);
}
}