iox/ANSI.cs
/// <summary>
/// Print an ANSI-escaped string.
/// </summary>
/// <param name="str">String.</param>
Print an ANSI-escaped string.
Print an ANSI - escaped string .
static void WriteInternal (string str) {
			var bold = false;
			var boldCount = 0;
			var receivedEscape = false;
			var inEscapeSequence = false;
			var escapeSequenceFinished = false;
			var escapeSequence = new StringBuilder ();
			var savedForeground = Console.ForegroundColor;
			var savedBackground = Console.BackgroundColor;
			foreach (var chr in str) {
				if (escapeSequenceFinished) {
					byte code;
					var sequenceString = escapeSequence.ToString ();
					escapeSequence.Clear ();
					escapeSequenceFinished = false;
					if (!byte.TryParse (sequenceString, out code)) {
						continue; 
					}
					if (code == 0) {
						bold = false;
						boldCount = 0;
						Console.ForegroundColor = savedForeground;
						Console.BackgroundColor = savedBackground;
					}
					else if (code == 1) {
						bold = true;
						boldCount++;
					}
					else if (code == 22) {
						boldCount -= boldCount > 0 ? 1 : 0;
						bold &= boldCount != 0;
					}
					else if (code >= 30 && code <= 37) {
						Console.ForegroundColor = ANSIColor.ColorMap [code - 30 + (bold ? 8 : 0)];
					}
					else if (code == 39) {
						Console.ForegroundColor = savedForeground;
					}
					else if (code >= 40 && code <= 47) {
						Console.BackgroundColor = ANSIColor.ColorMap [code - 40 + (bold ? 8 : 0)];
					}
					else if (code == 49) {
						Console.BackgroundColor = savedBackground;
					}
				}
				if (inEscapeSequence) {
					if (chr != 'm') {
						escapeSequence.Append (chr);
						continue;
					}
					escapeSequenceFinished = true;
					inEscapeSequence = false;
					continue;
				}
				if (receivedEscape) {
					receivedEscape = false;
					if (chr == '[') {
						inEscapeSequence = true;
						continue;
					}
				}
				if (chr == ESC) {
					receivedEscape = true;
					continue;
				}
				Console.Write (chr);
			}
			Console.ForegroundColor = savedForeground;
			Console.BackgroundColor = savedBackground;
		}
static void WriteInternal ( string str ) { var bold = false ; var boldCount = 0 ; var receivedEscape = false ; var inEscapeSequence = false ; var escapeSequenceFinished = false ; var escapeSequence = new StringBuilder ( ) ; var savedForeground = Console . ForegroundColor ; var savedBackground = Console . BackgroundColor ; foreach ( var chr in str ) { if ( escapeSequenceFinished ) { byte code ; var sequenceString = escapeSequence . ToString ( ) ; escapeSequence . Clear ( ) ; escapeSequenceFinished = false ; if ( ! byte . TryParse ( sequenceString , out code ) ) { continue ; } if ( code == 0 ) { bold = false ; boldCount = 0 ; Console . ForegroundColor = savedForeground ; Console . BackgroundColor = savedBackground ; } else if ( code == 1 ) { bold = true ; boldCount ++ ; } else if ( code == 22 ) { boldCount -= boldCount > 0 ? 1 : 0 ; bold &= boldCount != 0 ; } else if ( code >= 30 && code <= 37 ) { Console . ForegroundColor = ANSIColor . ColorMap [ code - 30 + ( bold ? 8 : 0 ) ] ; } else if ( code == 39 ) { Console . ForegroundColor = savedForeground ; } else if ( code >= 40 && code <= 47 ) { Console . BackgroundColor = ANSIColor . ColorMap [ code - 40 + ( bold ? 8 : 0 ) ] ; } else if ( code == 49 ) { Console . BackgroundColor = savedBackground ; } } if ( inEscapeSequence ) { if ( chr != ' ' ) { escapeSequence . Append ( chr ) ; continue ; } escapeSequenceFinished = true ; inEscapeSequence = false ; continue ; } if ( receivedEscape ) { receivedEscape = false ; if ( chr == ' ' ) { inEscapeSequence = true ; continue ; } } if ( chr == ESC ) { receivedEscape = true ; continue ; } Console . Write ( chr ) ; } Console . ForegroundColor = savedForeground ; Console . BackgroundColor = savedBackground ; }
Print an ANSI-escaped string.
Print an ANSI - escaped string .
// Initialize state // Iterate over all characters // Test if an escape sequence is ready to be printed // Prepare parsing // Update state // Try parsing the escape sequence byte // bail out // Reset all // Enable bold // Disable bold // Set foreground color // Reset foreground color // Set background color // Reset background color // Test if we're inside of an escape sequence // Test if we're at the end of the sequence // Update state // Test if we've received the escape character // Test for ANSI escape character // Nothing special, just print the char // Reset colors
str
string
None