Exemplo n.º 1
0
        private static void SetAnsiAttribute(string token, AnsiTextAttribute attributes)
        {
            var value = token.Replace("ansi-text-", string.Empty);

            if (string.IsNullOrWhiteSpace(value))
            {
                return;
            }

            AnsiAttribute attribute;

            if (!Enum.TryParse(value, true, out attribute))
            {
                return;
            }

            switch (attribute)
            {
            case AnsiAttribute.Bold:
                attributes.IsBold = true;
                return;

            case AnsiAttribute.Underline:
                attributes.IsUnderline = true;
                return;
            }
        }
Exemplo n.º 2
0
        private static void VisitParser(IHtmlSpanElement element, StringBuilder sb)
        {
            var ws = new Regex(@"\s+", RegexOptions.IgnoreCase | RegexOptions.IgnorePatternWhitespace | RegexOptions.Multiline);

            if (element == null)
            {
                return;
            }

            var attribute = new AnsiTextAttribute();

            ParseAnsiClass(element.ClassList, attribute);
            sb.Append(attribute);
            sb.Append(ws.Replace(element.TextContent.TrimStart(), " "));
        }
Exemplo n.º 3
0
        private static void SetAnsiForeground(string token, AnsiTextAttribute attributes)
        {
            var color = token.Replace("ansi-fg-", string.Empty);

            if (string.IsNullOrWhiteSpace(color))
            {
                return;
            }

            AnsiForegroundColor foregroundColor;

            if (!Enum.TryParse(color, true, out foregroundColor))
            {
                return;
            }

            attributes.ForegroundColor = (int)foregroundColor;
        }
Exemplo n.º 4
0
        private static void ParseAnsiClass(ITokenList tokens, AnsiTextAttribute attributes)
        {
            foreach (var token in tokens)
            {
                if (token == "tab")
                {
                    attributes.IsTab = true;
                    return;
                }

                if (!token.StartsWith("ansi-"))
                {
                    continue;
                }

                if (token.StartsWith("ansi-bg"))
                {
                    SetAnsiBackground(token, attributes);
                    continue;
                }
                if (token.StartsWith("ansi-fg"))
                {
                    SetAnsiForeground(token, attributes);
                    continue;
                }
                if (token.StartsWith("ansi-text"))
                {
                    SetAnsiAttribute(token, attributes);
                    continue;
                }

                switch (token)
                {
                case "ansi-cls":
                    attributes.IsCls = true;
                    break;
                }
            }
        }
Exemplo n.º 5
0
        private static void VisitParser(IHtmlDivElement element, StringBuilder sb)
        {
            if (element == null)
            {
                return;
            }

            var attributes = new AnsiTextAttribute();

            ParseAnsiClass(element.ClassList, attributes);
            if (attributes.IsCls)
            {
                sb.Append(ClearScreenAndHomeCursor);
            }

            foreach (var child in element.Children)
            {
                VisitParser(child as IHtmlSpanElement, sb);
                VisitParser(child as IHtmlDivElement, sb);
            }

            sb.Append(Environment.NewLine);
        }