예제 #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(
                        _capabilities.ColorSystem,
                        style.Foreground,
                        true));
            }

            // Got background?
            if (style.Background != Color.Default)
            {
                codes = codes.Concat(
                    AnsiColorBuilder.GetAnsiCodes(
                        _capabilities.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 && !_capabilities.LegacyConsole)
            {
                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);
        }
예제 #2
0
        public static string GetAnsi(
            ColorSystem system,
            string text,
            Decoration decoration,
            Color foreground,
            Color background)
        {
            var codes = AnsiDecorationBuilder.GetAnsiCodes(decoration);

            // Got foreground?
            if (foreground != Color.Default)
            {
                codes = codes.Concat(AnsiColorBuilder.GetAnsiCodes(system, foreground, foreground: true));
            }

            // Got background?
            if (background != Color.Default)
            {
                codes = codes.Concat(AnsiColorBuilder.GetAnsiCodes(system, background, foreground: false));
            }

            var result = codes.ToArray();

            if (result.Length == 0)
            {
                return(text);
            }

            var lol = string.Concat(
                "\u001b[",
                string.Join(";", result),
                "m",
                text,
                "\u001b[0m");

            return(lol);
        }