Ejemplo n.º 1
0
        public string GetAnsi(string text, Style style)
        {
            if (style is null)
            {
                throw new ArgumentNullException(nameof(style));
            }

            var codes = AnsiDecorationBuilder.GetAnsiCodes(style.Decoration);

            // Got foreground?
            if (style.Foreground != Color.Default)
            {
                codes = codes.Concat(
                    AnsiColorBuilder.GetAnsiCodes(
                        _profile.ColorSystem,
                        style.Foreground,
                        true));
            }

            // Got background?
            if (style.Background != Color.Default)
            {
                codes = codes.Concat(
                    AnsiColorBuilder.GetAnsiCodes(
                        _profile.ColorSystem,
                        style.Background,
                        false));
            }

            var result = codes.ToArray();

            if (result.Length == 0 && style.Link == null)
            {
                return(text);
            }

            var ansiCodes = string.Join(";", result);
            var ansi      = result.Length > 0
                ? $"\u001b[{ansiCodes}m{text}\u001b[0m"
                : text;

            if (style.Link != null && !_profile.Capabilities.Legacy)
            {
                var link = style.Link;

                // Empty links means we should take the URL from the text.
                if (link.Equals(Constants.EmptyLink, StringComparison.Ordinal))
                {
                    link = text;
                }

                var linkId = _linkHasher.GenerateId(link, text);
                ansi = $"\u001b]8;id={linkId};{link}\u001b\\{ansi}\u001b]8;;\u001b\\";
            }

            return(ansi);
        }
Ejemplo n.º 2
0
        private static string Build(Profile profile, string text, Style style)
        {
            if (profile is null)
            {
                throw new ArgumentNullException(nameof(profile));
            }
            else if (text is null)
            {
                throw new ArgumentNullException(nameof(text));
            }
            else if (style is null)
            {
                throw new ArgumentNullException(nameof(style));
            }

            var codes = AnsiDecorationBuilder.GetAnsiCodes(style.Decoration);

            // Got foreground?
            if (style.Foreground != Color.Default)
            {
                codes = codes.Concat(
                    AnsiColorBuilder.GetAnsiCodes(
                        profile.Capabilities.ColorSystem,
                        style.Foreground,
                        true));
            }

            // Got background?
            if (style.Background != Color.Default)
            {
                codes = codes.Concat(
                    AnsiColorBuilder.GetAnsiCodes(
                        profile.Capabilities.ColorSystem,
                        style.Background,
                        false));
            }

            var result = codes.ToArray();

            if (result.Length == 0 && style.Link == null)
            {
                return(text);
            }

            var ansi = result.Length > 0
                ? $"{SGR(result)}{text}{SGR(0)}"
                : text;

            if (style.Link != null && !profile.Capabilities.Legacy)
            {
                var link = style.Link;

                // Empty links means we should take the URL from the text.
                if (link.Equals(Constants.EmptyLink, StringComparison.Ordinal))
                {
                    link = text;
                }

                var linkId = _linkHasher.GenerateId(link, text);
                ansi = $"{ESC}]8;id={linkId};{link}{ESC}\\{ansi}{ESC}]8;;{ESC}\\";
            }

            return(ansi);
        }