diff --git a/src/Microsoft.Tye.Hosting/Ansi2Html/Constants.cs b/src/Microsoft.Tye.Hosting/Ansi2Html/Constants.cs new file mode 100644 index 00000000..10a7243f --- /dev/null +++ b/src/Microsoft.Tye.Hosting/Ansi2Html/Constants.cs @@ -0,0 +1,55 @@ +using System.Collections.Generic; + +namespace Microsoft.Tye.Hosting.Ansi2Html +{ + public static class Constants + { + public const string Red = "#800000"; + public const string Black = "#000000"; + public const string Green = "#008000"; + public const string Yellow = "#808000"; + public const string Blue = "#000080"; + public const string Purple = "#800080"; + public const string Cyan = "#008080"; + public const string LightGray = "#c0c0c0"; + public const string DarkGray = "#808080"; + public const string BrightRed = "#ff0000"; + public const string BrightGreen = "#00ff00"; + public const string BrightYellow = "#ffff00"; + public const string BrightBlue = "#0000ff"; + public const string BrightPurple = "#ff00ff"; + public const string BrightCyan = "#00ffff"; + public const string White = "#ffffff"; + + public static Dictionary ColorMap = new Dictionary() + { + { "0", Black }, // Black + { "1", Red }, // Red + { "2", Green }, // Green + { "3", Yellow }, // Yellow + { "4", Blue }, // Blue + { "5", Purple }, // Purple + { "6", Cyan }, // Cyan + { "7", LightGray }, // Light Gray + { "8", DarkGray }, // Dark Gray + { "9", BrightRed }, // Bright Red + { "10", BrightGreen }, // Bright Green + { "11", BrightYellow }, // Bright Yellow + { "12", BrightBlue }, // Bright Blue + { "13", BrightPurple }, // Bright Purple + { "14", BrightCyan }, // Bright Cyan + { "15", White } // White + }; + + public static class SelectGraphicRenditionParameters + { + public const int Reset = 0; + + public static HashSet SetForeground = new HashSet() + { + 30, 31, 32, 33, 34, 35, 36, 37,38 + }; + + } + } +} diff --git a/src/Microsoft.Tye.Hosting/Ansi2Html/Converter.cs b/src/Microsoft.Tye.Hosting/Ansi2Html/Converter.cs new file mode 100644 index 00000000..abb60071 --- /dev/null +++ b/src/Microsoft.Tye.Hosting/Ansi2Html/Converter.cs @@ -0,0 +1,49 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Text.RegularExpressions; + +namespace Microsoft.Tye.Hosting.Ansi2Html +{ + public class Converter + { + private readonly Regex _rule = new Regex("\u001b\\[(?\\d+)(?;\\d+)*m", RegexOptions.Compiled | RegexOptions.Multiline | RegexOptions.IgnoreCase); + + public string Parse(string input) + { + var htmlText = _rule.Replace(input, match => + { + var code = Convert.ToInt32(match.Groups["code"].Value); + var args = match.Groups["args"].Value; + List attributes = args.Split(';').ToList(); + + var tagBuilder = new StringBuilder(); + if (code == Constants.SelectGraphicRenditionParameters.Reset) + { + tagBuilder.Append(""); + } + else + { + var color = Constants.White; + if (attributes.Count > 0) + { + string colorCode = attributes.Last(); + if (Constants.ColorMap.ContainsKey(colorCode)) + { + color = Constants.ColorMap[colorCode]; + } + } + + tagBuilder.Append(""); + } + + return tagBuilder.ToString(); + }); + + return htmlText; + } + } +} diff --git a/src/Microsoft.Tye.Hosting/Dashboard/Pages/Logs.razor b/src/Microsoft.Tye.Hosting/Dashboard/Pages/Logs.razor index 34130514..f801f10b 100644 --- a/src/Microsoft.Tye.Hosting/Dashboard/Pages/Logs.razor +++ b/src/Microsoft.Tye.Hosting/Dashboard/Pages/Logs.razor @@ -1,6 +1,8 @@ @page "/logs/{ServiceName}" +@using Microsoft.Tye.Hosting.Ansi2Html; @inject IJSRuntime JS @inject Application application +@inject Converter ansi2HtmlParser @implements IDisposable