/// <summary> /// Writes Console Output /// </summary> private void OutPutString(byte[] screenTxt) { if (screenTxt.Count() > 0) { try { Console.Write(encode.GetString(screenTxt)); } catch (System.IO.IOException ex) { Debug.Write("Unable to Write to Console. IO Exception: " + ex.Message + " when trying to write: " + SharedFunc.ByteArrayToHexString(screenTxt)); } } }
/// <summary> /// Executes the action defined by the VT100/ANSI escape sequence /// </summary> private void EscapeSeqAction(byte[] payload) { //Debug.WriteLine(string.Format("Payload: {0} Data: {1}", // SharedFunc.ByteArrayToHexString(payload), // Encoding.ASCII.GetString(payload))); int lenght = payload.Length; string data = string.Empty; int num = 0, index = 2; // determine whether to lookup two digits or 1 if (lenght == 4) { index = 1; } //Debug.Assert(payload[0] == esc && payload[1] == bracket); if (lenght >= 3) { // Extract the data portion of the payload data = encode.GetString(payload, 2, (lenght - 3)); // split the data into strings string[] vars = data.Split(';'); // function code byte byte func = payload[lenght - 1]; #region Text Attributes <Esc> m if (func == 0x6D) // func: m { if (lenght == 3) { // Esc[m Turn off character attributes RestAttributes(); } else { foreach (string var in vars) { if (int.TryParse(var, out num)) { if (num < 10) { switch (num) { case 0: // Esc[0m Turn off character attributes RestAttributes(); break; case 1: // Esc[1m Turn bold mode on NativeMethods.AddIntensity(); intensity = true; break; case 2: // Esc[2m Turn low intensity mode on NativeMethods.RemoveIntensity(); intensity = false; break; case 3: // Esc[3m Standout. NativeMethods.AddIntensity(); intensity = true; break; case 4: // Esc[4m Turn underline mode on Debug.WriteLine(string.Format("Turn underline mode on")); break; case 5: // Esc[5m Turn blinking mode on Debug.WriteLine(string.Format("Turn blinking mode on")); break; case 7: // Esc[7m Turn reverse video on RollColors(); reversed = true; break; case 8: // Esc[8m Turn invisible text mode on InvisibleText(); break; default: Debug.WriteLine(string.Format("Unknown m number: {0}", num)); break; } } else if (num >= 30 && num <= 49) { switch (num) { case 30: // black foreground SetForegroundColor(ConsoleColor.Black); break; case 31: // red foreground SetForegroundColor(ConsoleColor.Red); break; case 32: // green foreground SetForegroundColor(ConsoleColor.Green); break; case 33: // yellow foreground SetForegroundColor(ConsoleColor.Yellow); break; case 34: // blue foreground SetForegroundColor(ConsoleColor.Blue); break; case 35: // magenta foreground SetForegroundColor(ConsoleColor.Magenta); break; case 36: // cyan foreground SetForegroundColor(ConsoleColor.Cyan); break; case 37: // white foreground SetForegroundColor(ConsoleColor.White); break; case 39: // default foreground SetForegroundColor(ConsoleColor.Gray); break; case 40: // black background SetBackgroundColor(ConsoleColor.Black); break; case 41: // red background SetBackgroundColor(ConsoleColor.Red); break; case 42: // green background SetBackgroundColor(ConsoleColor.Green); break; case 43: // yellow background SetBackgroundColor(ConsoleColor.Yellow); break; case 44: // blue background SetBackgroundColor(ConsoleColor.Blue); break; case 45: // magenta background SetBackgroundColor(ConsoleColor.Magenta); break; case 46: // cyan background SetBackgroundColor(ConsoleColor.Cyan); break; case 47: // white background SetBackgroundColor(ConsoleColor.Gray); break; case 49: // default background SetBackgroundColor(ConsoleColor.Black); break; default: Debug.WriteLine(string.Format("Unknown Color change: {0} Data: {1}", SharedFunc.ByteArrayToHexString(payload), Encoding.ASCII.GetString(payload))); break; } } else { Debug.WriteLine(string.Format("Uable to Parse Number in: {0} Data: {1}", SharedFunc.ByteArrayToHexString(payload), Encoding.ASCII.GetString(payload))); } } else { Debug.WriteLine(string.Format("Unable to parse: {0} Data: {1}", SharedFunc.ByteArrayToHexString(payload), Encoding.ASCII.GetString(payload))); } } } } #endregion #region Cursor Position <Esc> A - G Or <Esc> f else if ((func >= 0x41 && func <= 0x48) || (func == 0x66)) { int left = PositionLeft; int top = PositionTop; if (lenght == 3) { switch (func) { case 0x66: case 0x48: SetCursorPosition(0, 0); break; default: Debug.WriteLine(string.Format("Error converting to cursor move: {0} Data {1}: ", SharedFunc.ByteArrayToHexString(payload), Encoding.ASCII.GetString(payload))); break; } } else if (lenght > 3 && (func == 0x48 || func == 0x66)) { if (vars.Count() > 1) { if (int.TryParse(vars[0], out top) && int.TryParse(vars[1], out left)) { // remove 1 for zero based offset SetCursorPosition((top - 1), (left - 1)); } else { Debug.WriteLine(string.Format("Error converting to cursor move: {0} Data {1}: ", SharedFunc.ByteArrayToHexString(payload), Encoding.ASCII.GetString(payload))); } } else if (int.TryParse(vars[0], out top)) { // remove 1 for zero based offset left = Console.CursorLeft; SetCursorPosition((top - 1), left); } else { Debug.WriteLine(string.Format("Error converting to cursor move: {0} Data {1}: ", SharedFunc.ByteArrayToHexString(payload), Encoding.ASCII.GetString(payload))); } } else if (lenght > 3 && (func >= 0x41 && func <= 0x44)) // Move cursor up n lines { Direction dir = Direction.Invalid; List <int> dirs = new List <int>(); foreach (string var in vars) { if (int.TryParse(var, out num)) { dirs.Add(num); } } switch (func) { case 0x41: dir = Direction.Up; break; case 0x42: dir = Direction.Down; break; case 0x43: dir = Direction.Right; break; case 0x44: dir = Direction.Left; break; default: Debug.WriteLine(string.Format("Unknown Direction: {0} Data {1}: ", SharedFunc.ByteArrayToHexString(payload), Encoding.ASCII.GetString(payload))); break; } if (dirs.Count > 0 && dir != Direction.Invalid) { DirectionalMove(dirs.ToArray(), dir); } } else { Debug.WriteLine(string.Format("Move/Tab unknown: {0} Bytes: {1} Data {2}: ", lenght, SharedFunc.ByteArrayToHexString(payload), Encoding.ASCII.GetString(payload))); } } #endregion #region Set Console Mode <Esc>=h else if (func == 0x68) { // set mode if (payload[2] == 0x3D) { foreach (string var in vars) { if (int.TryParse(var.Substring(1), out num)) { switch (num) { case 0: // 0 40 x 25 monochrome (text) SetConsoleSize(40, 25); SetConsoleBufferSize(40, 25); break; case 1: // 1 40 x 25 color (text) SetConsoleSize(40, 25); SetConsoleBufferSize(40, 25); break; case 2: // 2 80 x 25 monochrome (text) SetConsoleSize(80, 25); SetConsoleBufferSize(80, 25); break; case 3: // 3 80 x 25 color (text) SetConsoleSize(80, 25); SetConsoleBufferSize(80, 25); break; case 7: // 7 Enables line wrapping NativeMethods.EnableWordWrap(); wordwrap = true; break; case 4: // 4 320 x 200 4-color (graphics) case 5: // 5 320 x 200 monochrome (graphics) case 6: // 6 640 x 200 monochrome (graphics) case 13: // 13 320 x 200 color (graphics) case 14: // 14 640 x 200 color (16-color graphics) case 15: // 15 640 x 350 monochrome (2-color graphics) case 16: // 16 640 x 350 color (16-color graphics) case 17: // 17 640 x 480 monochrome (2-color graphics) case 18: // 18 640 x 480 color (16-color graphics) case 19: // 19 320 x 200 color (256-color graphics) Debug.WriteLine(string.Format("Set Mode Graphics Mode Un-supported: {0} Number: {1} Data {2}: ", num, SharedFunc.ByteArrayToHexString(payload), Encoding.ASCII.GetString(payload))); break; default: Debug.WriteLine(string.Format("Set Mode <ESC>[= num. Unknown num: {0} Number: {1} Data {2}: ", num, SharedFunc.ByteArrayToHexString(payload), Encoding.ASCII.GetString(payload))); break; } } else { Debug.WriteLine(string.Format("Set Mode <ESC>[= num. Unable to convert num: {0} Lenght: {1} Data {2}: ", lenght, SharedFunc.ByteArrayToHexString(payload), Encoding.ASCII.GetString(payload))); } } } else { Debug.WriteLine(string.Format("Set Mode <ESC>[= num. 3rd char not = symbol: {0} Lenght: {1} Data {2}: ", lenght, SharedFunc.ByteArrayToHexString(payload), Encoding.ASCII.GetString(payload))); } } #endregion #region Clear Line <Esc> J & <Esc> K else if (func == 0x4A) // J { //Erase Down <ESC>[J //Erases the screen from the current position down to the bottom of the screen. if (lenght == 3) { ClearToDirection(Direction.Down); } else { foreach (string var in vars) { if (int.TryParse(var, out num)) { switch (num) { case 0: //Erase Up <ESC>[0J //Erases the screen from the current position up to the end the screen. ClearToDirection(Direction.Down); break; case 1: //Erase Up <ESC>[1J //Erases the screen from the current line up to the top of the screen. ClearToDirection(Direction.Up); break; case 2: //Erase Screen <ESC>[2J //Erases the screen with the background color and moves the cursor to home. Clear(); // reset the cursor position SetCursorPosition(0, 0); break; default: Debug.WriteLine(string.Format("Erase Screen <ESC>[num J unknown num: {0} Num: {1} Data: {2} ", num, SharedFunc.ByteArrayToHexString(payload), Encoding.ASCII.GetString(payload))); break; } } else { Debug.WriteLine(string.Format("Erase Line <ESC> J unable to convert number: {0} Bytes: {1} Data: {2}, {3} ", lenght, SharedFunc.ByteArrayToHexString(payload), Encoding.ASCII.GetString(payload), index)); } } } } else if (func == 0x4B) // K { //Erases from the current cursor position to the end of the current line. if (lenght == 3) { //Erase End of Line <ESC>[K ClearToDirection(Direction.Right); } else { foreach (string var in vars) { if (int.TryParse(var, out num)) { switch (num) { case 0: ClearToDirection(Direction.Right); break; case 1: ClearToDirection(Direction.Left); break; case 2: ClearLine(); break; default: Debug.WriteLine(string.Format("Erase Screen <ESC>[num K unknown num: {0} Num: {1} Data {2}: ", num, SharedFunc.ByteArrayToHexString(payload), Encoding.ASCII.GetString(payload))); break; } } else { Debug.WriteLine(string.Format("Erase Line <ESC>[ K unable to convert number: {0} Bytes: {1} Data {2}: ", lenght, SharedFunc.ByteArrayToHexString(payload), Encoding.ASCII.GetString(payload))); } } } } else { Debug.WriteLine(string.Format("Erase Line <ESC>[ ?? unable to convert function: {0} Bytes: {1} Data {2}: ", lenght, SharedFunc.ByteArrayToHexString(payload), Encoding.ASCII.GetString(payload))); } #endregion } else { Debug.WriteLine(string.Format("Malformed function lenght: {0} Bytes: {1} Data {2}: ", lenght, SharedFunc.ByteArrayToHexString(payload), Encoding.ASCII.GetString(payload))); } }