Menu

Color in Console Mode

Ryan Gregg
2003-07-16
2003-07-21
  • Ryan Gregg

    Ryan Gregg - 2003-07-16

    I'm curious how you were able to do colored text in your console program.  I tried looking at the 1.0 source release, but it seems you didn't include the base classes which are responsible for that work.

    Any chance you could post all of the source for the project, or give me some details on how you pulled off the colored text?

    Thanks!

     
    • Laurent Rodier

      Laurent Rodier - 2003-07-21

      Ryan,

      I'm working hard on publishing a version 1.10 of Shell.NET that will also include a Windows form application for grep, touch, ... and haven't yet found time to release the whole source code of LR.DLL. Once I'll have make Shell.NET 1.10 (end of July), I'll spent a few time adding better comments to LR.DLL before publishing it (likely end of August).
      For the time being, here the whole source code of the ConsoleExt class:

      using System;
      using System.IO;
      using System.Runtime.InteropServices;
      using System.Text;
      using Microsoft.Win32;

      namespace LR
      {
          // **********************************************************************************************
          //
          // **********************************************************************************************
          /// <summary>
          /// The <c>ConsoleExt</c> class is a version of the <c>Console</c> class that supports
          /// colors. It also extends the <c>TextWriter</c> class to provide a smart in-place
          /// replacement of the <c>Console.Out</c> and <c>Console.Error</c> components.
          /// </summary>
          public class ConsoleExt: TextWriter
          {
              // **********************************************************************************************
              // PUBLIC CONSTANTS
              // **********************************************************************************************

              /// <summary>
              /// List the available types of <c>ConsoleExt</c> consoles. These types are
              /// closely matching the Win32 console types.
              /// </summary>
              public enum Type
              {
                  /// <summary>
                  /// Predefined standard input.
                  /// </summary>
                  StdIn = -10,

                  /// <summary>
                  /// Predefined standard output.
                  /// </summary>
                  StdOut = -11,

                  /// <summary>
                  /// Predefined standard error output.
                  /// </summary>
                  StdErr = -12
              }

              /// <summary>
              /// List of the colors available for the <c>ConsoleExt</c> consoles.
              /// </summary>
              public enum Color
              {
                  /// <summary>
                  /// Black color.
                  /// </summary>
                  Black = 0x0000,

                  /// <summary>
                  /// Blue color.
                  /// </summary>
                  Blue = 0x0001,

                  /// <summary>
                  /// Green color.
                  /// </summary>
                  Green = 0x0002,

                  /// <summary>
                  /// Cyan color.
                  /// </summary>
                  Cyan = 0x0003,

                  /// <summary>
                  /// Red color.
                  /// </summary>
                  Red    = 0x0004,

                  /// <summary>
                  /// Magenta color.
                  /// </summary>
                  Magenta = 0x0005,

                  /// <summary>
                  /// Brown color.
                  /// </summary>
                  Brown = 0x0006,

                  /// <summary>
                  /// Grey color.
                  /// </summary>
                  Grey = 0x0007,

                  /// <summary>
                  /// White color.
                  /// </summary>
                  White = 0x0008,

                  /// <summary>
                  /// Light blue color.
                  /// </summary>
                  LightBlue = Blue | White,

                  /// <summary>
                  /// Green color.
                  /// </summary>
                  LightGreen = Green | White,

                  /// <summary>
                  /// Cyan color.
                  /// </summary>
                  LightCyan =  Cyan | White,

                  /// <summary>
                  /// Light red color.
                  /// </summary>
                  LightRed = Red  | White,

                  /// <summary>
                  /// Light magenta color.
                  /// </summary>
                  LightMagenta =  Magenta | White,

                  /// <summary>
                  /// Yellow color.
                  /// </summary>
                  Yellow = Brown | White,

                  /// <summary>
                  /// Light grey color.
                  /// </summary>
                  LightGrey =  Grey | White
              }

              // **********************************************************************************************
              // PRIVATE FIELDS
              // **********************************************************************************************

              /// <summary>
              /// Identifier of the system console.
              /// </summary>
              private IntPtr m_consoleId;

              /// <summary>
              /// Console writer associated with this console.
              /// </summary>
              private TextWriter m_writer;

              // **********************************************************************************************
              // PUBLIC FIELDS
              // **********************************************************************************************

              /// <summary>
              /// Default background color.
              /// </summary>
              public Color DefaultBackground = Color.Black;

              /// <summary>
              /// Default foreground color.
              /// </summary>
              public Color DefaultForeground = Color.Grey;

              // **********************************************************************************************
              // PUBLIC PROPERTIES
              // **********************************************************************************************

              /// <summary>
              /// Gets the writer associated with this console.
              /// </summary>
              public TextWriter Writer
              {
                  get
                  {
                      return this.m_writer;
                  }
              }

              // **********************************************************************************************
              // PUBLIC METHODS
              // **********************************************************************************************

              /// <summary>
              /// Creates a new console instance associated with the standard output.
              /// </summary>
              public ConsoleExt()
                  : this(Type.StdOut)
              {
              }

              /// <summary>
              /// Creates a new console instance of a given type.
              /// </summary>
              /// <param name="type"></param>
              public ConsoleExt(Type type)
                  : this(type, Color.Black, Color.Grey)
              {
              }

              /// <summary>
              /// Creates a new console instance of a given type with a default set of colors.
              /// </summary>
              /// <param name="type"></param>
              /// <param name="bgColor"></param>
              /// <param name="fgColor"></param>
              public ConsoleExt(Type type, Color bgColor, Color fgColor)
              {
                  // initialize console
                  this.SetConsoleType(Type.StdOut);

                  // set default colors
                  this.DefaultBackground = bgColor;
                  this.DefaultForeground = fgColor;
              }

              /// <summary>
              /// Sets the foreground color of the next outputs.
              /// </summary>
              /// <param name="color"></param>
              public void SetForeground(Color color)
              {
                  this.SetColor(this.DefaultBackground, color);
              }

              /// <summary>
              /// Sets the default foreground and background colors of the next outputs.
              /// </summary>
              public void SetColor()
              {
                  this.SetColor(this.DefaultBackground, this.DefaultForeground);
              }

              /// <summary>
              /// Inverts the current default foreground and background colors for the
              /// next outputs.
              /// </summary>
              public void SetReverseColor()
              {
                  this.SetColor(this.DefaultForeground, this.DefaultBackground);
              }

              /// <summary>
              /// Sets the foreground and background colors of the next outputs.
              /// </summary>
              /// <param name="bgColor"></param>
              /// <param name="fgColor"></param>
              public void SetColor(Color bgColor, Color fgColor)
              {
                  // build color
                  int attr = (int)fgColor + 16 * (int)bgColor;

                  // set the actual color
                  ConsoleExt.SetConsoleTextAttribute(this.m_consoleId, attr);
              }

              /// <summary>
              /// Writes a newline terminated message onto the current output using a
              /// specified set of background and foreground colors.
              /// </summary>
              /// <param name="bgColor"></param>
              /// <param name="fgColor"></param>
              /// <param name="format"></param>
              /// <param name="args"></param>
              public void WriteLine(Color bgColor, Color fgColor, string format, params object[] args)
              {
                  this.SetColor(bgColor, fgColor);
                  this.m_writer.WriteLine(format, args);
                  this.SetColor();
              }

              /// <summary>
              /// Writes a newline terminated message onto the current output using a
              /// specified set of background and foreground colors.
              /// </summary>
              /// <param name="bgColor"></param>
              /// <param name="fgColor"></param>
              /// <param name="format"></param>
              /// <param name="args"></param>
              public void Write(Color bgColor, Color fgColor, string format, params object[] args)
              {
                  this.SetColor(bgColor, fgColor);
                  this.m_writer.Write(format, args);
                  this.SetColor();
              }

              // **********************************************************************************************
              // IMPLEMENTED INTERFACE: TextWriter
              // **********************************************************************************************

              /// <summary>
              /// Returns the text encoding of the console.
              /// </summary>
              public override Encoding Encoding
              {
                  get
                  {
                      return this.m_writer.Encoding;
                  }
              }

              /// <summary>
              /// Gets/sets the newline character sequence.
              /// </summary>
              public override string NewLine
              {
                  get
                  {
                      return this.m_writer.NewLine;
                  }
                  set
                  {
                      this.m_writer.NewLine = value;
                  }
              }

              /// <summary>
              /// Closes the current writer and releases resources associated with it.
              /// </summary>
              public override void Close()
              {
                  this.m_writer.Close();
              }

              /// <summary>
              /// Commits pending writtings.
              /// </summary>
              public override void Flush()
              {
                  this.m_writer.Flush();
              }

              /// <summary>
              /// Writes a newline terminated message onto the current output.
              /// </summary>
              /// <param name="format"></param>
              /// <param name="args"></param>
              public override void WriteLine(string format, params object[] args)
              {
                  this.m_writer.WriteLine(format, args);
              }

              /// <summary>
              /// Writes a message onto the current output.
              /// </summary>
              /// <param name="format"></param>
              /// <param name="args"></param>
              public override void Write(string format, params object[] args)
              {
                  this.m_writer.Write(format, args);
              }

              /// <summary>
              /// Writes a character onto the current output.
              /// </summary>
              /// <param name="c"></param>
              public override void Write(char c)
              {
                  this.m_writer.Write(c);
              }

              /// <summary>
              /// Writes a string onto the current output.
              /// </summary>
              /// <param name="s"></param>
              public override void Write(string s)
              {
                  this.m_writer.Write(s);
              }

              // **********************************************************************************************
              // PRIVATE METHODS
              // **********************************************************************************************

              /// <summary>
              /// Initialized this console instance of a given type.
              /// </summary>
              /// <param name="type"></param>
              private void SetConsoleType(Type type)
              {
                  // set writer
                  switch (type)
                  {
                      case Type.StdErr:
                          this.m_writer = Console.Error;
                          break;

                      default:
                          this.m_writer = Console.Out;
                          break;
                  }

                  // set system id
                  this.m_consoleId = ConsoleExt.GetStdHandle((int)type);
              }

              // **********************************************************************************************
              // PRIVATE STATIC METHODS
              // **********************************************************************************************

              /// <summary>
              /// Returns the system identifier of a console identified by a logical name.
              /// </summary>
              [DllImportAttribute("Kernel32.dll")]
              private static extern IntPtr GetStdHandle(int id);

              /// <summary>
              /// Sets the foreground and background color of the next output on a given
              /// console.
              /// </summary>
              [DllImportAttribute("Kernel32.dll")]
              private static extern bool SetConsoleTextAttribute(IntPtr handle, int wAttribute);
          }
      }

       

Log in to post a comment.