Source/NFX/IO/ConsoleUtils.cs
/// <summary>
/// Outputs colored text from content supplied in an HTML-like grammar
/// </summary>
Outputs colored text from content supplied in an HTML-like grammar
Outputs colored text from content supplied in an HTML - like grammar
public static void WriteMarkupContent(string content, char open, char close)
    {
      TokenParser parser = new TokenParser(content, open, close);
      Stack<ConsoleColor> stack = new Stack<ConsoleColor>();
      bool collapsespaces = false;
      foreach (TokenParser.Token tok in parser)
      {
        if (tok.IsSimpleText)
        {
          if (collapsespaces)
            Console.Write(tok.Content.Trim());
          else
            Console.Write(tok.Content);
          continue;
        }
        string name = tok.Name.ToUpperInvariant().Trim();
        if (name == "LITERAL")
        {
          collapsespaces = false;
          continue;
        }
        if (name == "HTML")
        {
          collapsespaces = true;
          continue;
        }
        if (name == "BR")
        {
          Console.WriteLine();
          continue;
        }
        if ((name == "SP") || (name == "SPACE"))
        {
          string txt = " ";
          int cnt = 1;
          try { cnt = int.Parse(tok["COUNT"]); }
          catch { }
          while (txt.Length < cnt) txt += " ";
          Console.Write(txt);
          continue;
        }
        if (name == "PUSH")
        {
          stack.Push(Console.ForegroundColor);
          stack.Push(Console.BackgroundColor);
          continue;
        }
        if (name == "POP")
        {
          if (stack.Count > 1)
          {
            Console.BackgroundColor = stack.Pop();
            Console.ForegroundColor = stack.Pop();
          }
          continue;
        }
        if (name == "RESET")
        {
          Console.ResetColor();
          continue;
        }
        if ((name == "F") || (name == "FORE") || (name == "FOREGROUND"))
        {
          try
          {
            ConsoleColor clr = (ConsoleColor)Enum.Parse(typeof(ConsoleColor), tok["COLOR"], true);
            Console.ForegroundColor = clr;
          }
          catch { }
          continue;
        }
        if ((name == "B") || (name == "BACK") || (name == "BACKGROUND"))
        {
          try
          {
            ConsoleColor clr = (ConsoleColor)Enum.Parse(typeof(ConsoleColor), tok["COLOR"], true);
            Console.BackgroundColor = clr;
          }
          catch { }
          continue;
        }
        if ((name == "J") || (name == "JUST") || (name == "JUSTIFY"))
        {
          try
          {
            int width = int.Parse(tok["WIDTH"]);
            direction dir = (direction)Enum.Parse(typeof(direction), tok["DIR"], true);
            string txt = tok["TEXT"];
            switch (dir)
            {
              case direction.Right:
                {
                  while (txt.Length < width) txt = " " + txt;
                  break;
                }
              case direction.Center:
                {
                  while (txt.Length < width) txt = " " + txt + " ";
                  if (txt.Length > width) txt = txt.Substring(0, txt.Length - 1);
                  break;
                }
              default:
                {
                  while (txt.Length < width) txt = txt + " ";
                  break;
                }
            }
            Console.Write(txt);
          }
          catch { }
          continue;
        }
      }
    }
public static void WriteMarkupContent ( string content , char open , char close ) { TokenParser parser = new TokenParser ( content , open , close ) ; Stack < ConsoleColor > stack = new Stack < ConsoleColor > ( ) ; bool collapsespaces = false ; foreach ( TokenParser . Token tok in parser ) { if ( tok . IsSimpleText ) { if ( collapsespaces ) Console . Write ( tok . Content . Trim ( ) ) ; else Console . Write ( tok . Content ) ; continue ; } string name = tok . Name . ToUpperInvariant ( ) . Trim ( ) ; if ( name == " " ) { collapsespaces = false ; continue ; } if ( name == " " ) { collapsespaces = true ; continue ; } if ( name == " " ) { Console . WriteLine ( ) ; continue ; } if ( ( name == " " ) || ( name == " " ) ) { string txt = " " ; int cnt = 1 ; try { cnt = int . Parse ( tok [ " " ] ) ; } catch { } while ( txt . Length < cnt ) txt += " " ; Console . Write ( txt ) ; continue ; } if ( name == " " ) { stack . Push ( Console . ForegroundColor ) ; stack . Push ( Console . BackgroundColor ) ; continue ; } if ( name == " " ) { if ( stack . Count > 1 ) { Console . BackgroundColor = stack . Pop ( ) ; Console . ForegroundColor = stack . Pop ( ) ; } continue ; } if ( name == " " ) { Console . ResetColor ( ) ; continue ; } if ( ( name == " " ) || ( name == " " ) || ( name == " " ) ) { try { ConsoleColor clr = ( ConsoleColor ) Enum . Parse ( typeof ( ConsoleColor ) , tok [ " " ] , true ) ; Console . ForegroundColor = clr ; } catch { } continue ; } if ( ( name == " " ) || ( name == " " ) || ( name == " " ) ) { try { ConsoleColor clr = ( ConsoleColor ) Enum . Parse ( typeof ( ConsoleColor ) , tok [ " " ] , true ) ; Console . BackgroundColor = clr ; } catch { } continue ; } if ( ( name == " " ) || ( name == " " ) || ( name == " " ) ) { try { int width = int . Parse ( tok [ " " ] ) ; direction dir = ( direction ) Enum . Parse ( typeof ( direction ) , tok [ " " ] , true ) ; string txt = tok [ " " ] ; switch ( dir ) { case direction . Right : { while ( txt . Length < width ) txt = " " + txt ; break ; } case direction . Center : { while ( txt . Length < width ) txt = " " + txt + " " ; if ( txt . Length > width ) txt = txt . Substring ( 0 , txt . Length - 1 ) ; break ; } default : { while ( txt . Length < width ) txt = txt + " " ; break ; } } Console . Write ( txt ) ; } catch { } continue ; } } }
Outputs colored text from content supplied in an HTML-like grammar
Outputs colored text from content supplied in an HTML - like grammar
content
string
open
char
close
char