Example #1
0
        public TextStyle ToTextStyle()
        {
            int fgNum = Fg;

            if (IsBold && fgNum < 8)
            {
                fgNum += 8;
            }
            Color          fg    = Ansi.AnsiColor256ToColor(fgNum);
            Color          bg    = Ansi.AnsiColor256ToColor(Bg);
            TextStyleFlags flags = TextStyleFlags.Empty;

            if (IsBold)
            {
                flags |= TextStyleFlags.HighIntensity;
            }
            if (IsInverse)
            {
                flags |= TextStyleFlags.Inverse;
            }
            return(new TextStyle(fg, bg, flags));
        }
Example #2
0
        public static ColorMessage CreateFromAnsi(string str, AnsiTextStyle lastStyle)
        {
            ColorMessage colorMsg = Ansi.ParseAnsi(str, ref lastStyle);

            return(colorMsg);
        }
Example #3
0
        public string ToAnsiString(bool use256)
        {
            StringBuilder sb = new StringBuilder(m_text.ToString());

            for (int i = m_metaData.Count - 1; i >= 0; i--)
            {
                MetaData  md    = m_metaData[i];
                TextStyle style = md.m_style;

                StringBuilder esb = new StringBuilder();

                if (use256)
                {
                    esb.Append(ESC);
                    esb.Append('[');
                    if (style.IsHighIntensity)
                    {
                        esb.Append('1');
                    }
                    else
                    {
                        esb.Append("22");
                    }
                    esb.Append('m');

                    esb.Append(ESC);
                    esb.Append('[');
                    if (style.IsReverse)
                    {
                        esb.Append('7');
                    }
                    else
                    {
                        esb.Append("27");
                    }
                    esb.Append('m');

                    if (!style.Fg.IsEmpty)
                    {
                        if (style.Fg.IsDefault)
                        {
                            esb.Append(ESC);
                            esb.Append("[39m");
                        }
                        else
                        {
                            esb.Append(ESC);
                            esb.Append("[38;5;");
                            esb.Append(Ansi.ColorToAnsiColor256(style.Fg));
                            esb.Append('m');
                        }
                    }

                    if (!style.Bg.IsEmpty)
                    {
                        if (style.Bg.IsDefault)
                        {
                            esb.Append(ESC);
                            esb.Append("[49m");
                        }
                        else
                        {
                            esb.Append(ESC);
                            esb.Append("[48;5;");
                            esb.Append(Ansi.ColorToAnsiColor256(style.Bg));
                            esb.Append('m');
                        }
                    }
                }
                else
                {
                    // ESC[x;ym
                    int  fg;
                    int  bg;
                    bool bold = false;

                    if (style.Fg.IsDefault)
                    {
                        fg = 39;                         // reset fg
                    }
                    else if (style.Fg.IsEmpty)
                    {
                        fg = -1;
                    }
                    else
                    {
                        fg = 30 + Ansi.ColorToAnsiColor8(style.Fg, out bold);
                    }

                    if (style.Bg.IsDefault)
                    {
                        bg = 49;                         // reset bg
                    }
                    else if (style.Bg.IsEmpty)
                    {
                        bg = -1;
                    }
                    else
                    {
                        bool dummy;
                        bg = 40 + Ansi.ColorToAnsiColor8(style.Bg, out dummy);
                    }

                    //Console.WriteLine("\nFG {0} -> {1}, {2}", md.m_fgColor, fg, bold);
                    //Console.WriteLine("\nBG {0} -> {1}", md.m_bgColor, bg);
                    esb.Append(ESC);
                    esb.Append('[');

                    if (bold || style.IsHighIntensity)
                    {
                        esb.Append('1');
                    }
                    else
                    {
                        esb.Append("22");
                    }

                    esb.Append(';');
                    if (style.IsReverse)
                    {
                        esb.Append("7");
                    }
                    else
                    {
                        esb.Append("27");
                    }

                    if (fg != -1)
                    {
                        esb.Append(';');
                        esb.Append(fg.ToString());
                    }

                    if (bg != -1)
                    {
                        esb.Append(';');
                        esb.Append(bg.ToString());
                    }

                    esb.Append('m');
                }

                sb.Insert(md.m_index, esb.ToString());
            }

            if (m_metaData.Count > 0)
            {
                sb.Append(String.Format("RESET{0}[0m", ESC));
            }

            return(sb.ToString());
        }