Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections.Immutable;
- using System.Runtime.Versioning;
- namespace ControlSequences;
- using static System.Console;
- /// <summary>
- /// Collection of CSI console commands.
- /// </summary>
- /// <remarks>
- /// <seealso href="https://docs.microsoft.com/en-us/windows/console/console-virtual-terminal-sequences#text-formatting" />
- /// </remarks>
- public static class ControlSequenceIntroducer
- {
- public const string ESC = "\x1b";
- public const string CSI = "\x1b[";
- public const string OSC = "\x1b]";
- public const string ST = "\x1b\x5c";
- public const string DEC_LINE_MODE = "\x1b(0";
- public const string ASCII_MODE = "\x1b(B";
- public const string RESETCOLOR = "\x1b[0m";
- public const string RESETFOREGROUND = "\x1b[39m";
- public const string RESETBACKGROUND = "\x1b[49m";
- public const string RGB_FOREGROUND_FORMAT = "38;2;{0};{1};{2}";
- public const string RGB_BACKGROUND_FORMAT = "48;2;{0};{1};{2}";
- public const string PALETTE_FOREGROUND_FORMAT = "38;5;{0}";
- public const string PALETTE_BACKGROUND_FORMAT = "48;5;{0}";
- /// <summary>
- /// Writes the control sequence to the console.
- /// </summary>
- /// <param name="value">The control sequence.</param>
- internal static void SendCommand(string value) => Write(value);
- /// <inheritdoc cref="SendCommand(string)"/>
- internal static void SendCommand(string format, params object[] values) => Write(format, values);
- /// <inheritdoc cref="SendCommand(string)"/>
- internal static void SendCommand(char value) => Write(value);
- /// <summary>
- /// Get the Console WindowWidth and WindowHeight property values.
- /// </summary>
- /// <returns>(int Width, int Height) tuple.</returns>
- public static (int Width, int Height) GetWindowSize() => (WindowWidth, WindowHeight);
- /// <summary>Replaces text on the screen with space characters.</summary>
- public static void ClearDisplay() => EraseDisplay(2);
- public static void EnterVirtualBuffer() => SendCommand($"{CSI}?1049h");
- public static void EnterMainBuffer() => SendCommand($"{CSI}?1049l");
- public static void EnterLineMode() => SendCommand(DEC_LINE_MODE);
- public static void ExitLineMode() => SendCommand(ASCII_MODE);
- #region TEXT
- /*
- ESC [ <n> @ ICH Insert Character Insert <n> spaces at the current cursor position, shifting all existing text to the right. Text exiting the screen to the right is removed.
- ESC [ <n> P DCH Delete Character Delete <n> characters at the current cursor position, shifting in space characters from the right edge of the screen.
- ESC [ <n> X ECH Erase Character Erase <n> characters from the current cursor position by overwriting them with a space character.
- ESC [ <n> L IL Insert Line Inserts <n> lines into the buffer at the cursor position. The line the cursor is on, and lines below it, will be shifted downwards.
- ESC [ <n> M DL Delete Line Deletes <n> lines from the buffer, starting with the row the cursor is on.
- */
- /// <summary>
- /// Insert <paramref name="n"/> spaces at the current cursor position, shifting all existing text to the right. Text exiting the screen to the right is removed.
- /// </summary>
- /// <param name="n"></param>
- public static void InsertCharacter(int n = 1) => SendCommand($"{CSI}{n}@");
- /// <summary>
- /// Delete <paramref name="n"/> characters at the current cursor position, shifting in space characters from the right edge of the screen.
- /// </summary>
- /// <param name="n">Number of characters to delete.</param>
- public static void DeleteCharacter(int n = 1) => SendCommand($"{CSI}{n}P");
- /// <summary>
- /// Erase <paramref name="n"/> characters from the current cursor position by overwriting them with a space character.
- /// </summary>
- /// <param name="n">Number of characters to replace with space.</param>
- public static void EraseCharacter(int n = 1) => SendCommand($"{CSI}{n}X");
- /// <summary>
- /// Inserts <paramref name="n"/> lines into the buffer at the cursor position. The line the cursor is on, and lines below it, will be shifted downwards.
- /// </summary>
- /// <param name="n">Number of lines to insert after current cursor position.</param>
- public static void InsertLine(int n = 1) => SendCommand($"{CSI}{n}L");
- /// <summary>
- /// Deletes <paramref name="n"/> lines from the buffer, starting with the row the cursor is on.
- /// </summary>
- /// <param name="n">Number of lines to delete.</param>
- public static void DeleteLine(int n = 1) => SendCommand($"{CSI}{n}M");
- /*
- For the following commands, the parameter <n> has 3 valid values:
- 0 erases from the current cursor position (inclusive) to the end of the line/display
- 1 erases from the beginning of the line/display up to and including the current cursor position
- 2 erases the entire line/display
- ESC [ <n> J ED Erase in Display Replace all text in the current viewport/screen specified by <n> with space characters
- ESC [ <n> K EL Erase in Line Replace all text on the line with the cursor specified by <n> with space characters
- */
- /// <summary>
- /// Replaces text on the screen with space characters.
- /// </summary>
- /// <param name="n">
- /// <list type="table">
- /// <item>
- /// <term>0</term>
- /// <description>Erase from current position to end of the display.</description>
- /// </item>
- /// <item>
- /// <term>1</term>
- /// <description>Erase from beginning of the display to current position.</description>
- /// </item>
- /// <item>
- /// <term>2</term>
- /// <description>Erase the entire the display.</description>
- /// </item>
- /// </list>
- /// </param>
- public static void EraseDisplay(int n = 0) => SendCommand($"{CSI}{n}J");
- /// <summary>
- /// Replaces text on the current line with space characters.
- /// </summary>
- /// <param name="n">
- /// <list type="table">
- /// <item>
- /// <term>0</term>
- /// <description>Erase from current position to end of the line.</description>
- /// </item>
- /// <item>
- /// <term>1</term>
- /// <description>Erase from beginning of the line to current position.</description>
- /// </item>
- /// <item>
- /// <term>2</term>
- /// <description>Erase the entire line.</description>
- /// </item>
- /// </list>
- /// </param>
- public static void EraseLine(int n = 0) => SendCommand($"{CSI}{n}K");
- #endregion TEXT
- #region CURSOR
- /*
- ESC M RI Reverse Index – Performs the reverse operation of \n, moves cursor up one line, maintains horizontal position, scrolls buffer if necessary*
- ESC 7 DECSC Save Cursor Position in Memory**
- ESC 8 DECSR Restore Cursor Position from Memory**
- */
- /// <summary>
- /// Performs the reverse operation of \n, moves cursor up one line, maintains horizontal position, scrolls buffer if necessary
- /// </summary>
- /// <remarks>If there are scroll margins set, reversing inside the margins will scroll only the contents of the margins, and leave the viewport unchanged.
- /// See <seealso cref="SetScrollRegion"/>
- /// </remarks>
- public static void ReverseCursor() => SendCommand($"{ESC}M");
- /// <summary>
- /// Save Cursor Position in Memory.
- /// </summary>
- /// <remarks>
- /// The only way to access the saved value is with the restore command <see cref="RestoreCursorPosition"/>.
- /// </remarks>
- public static void SaveCursorPosition() => SendCommand($"{ESC}7");
- /// <summary>
- /// Restore Cursor Position from Memory.
- /// </summary>
- /// <remarks>
- /// There will be no value saved in memory until the first use of the save command <see cref="SaveCursorPosition"/>.
- /// </remarks>
- public static void RestoreCursorPosition() => SendCommand($"{ESC}8");
- /*
- ESC [ <n> A CUU Cursor Up Cursor up by <n>
- ESC [ <n> B CUD Cursor Down Cursor down by <n>
- ESC [ <n> C CUF Cursor Forward Cursor forward (Right) by <n>
- ESC [ <n> D CUB Cursor Backward Cursor backward (Left) by <n>
- ESC [ <n> E CNL Cursor Next Line Cursor down <n> lines from current position
- ESC [ <n> F CPL Cursor Previous Line Cursor up <n> lines from current position
- ESC [ <n> G CHA Cursor Horizontal Absolute Cursor moves to <n>th position horizontally in the current line
- ESC [ <n> d VPA Vertical Line Position Absolute Cursor moves to the <n>th position vertically in the current column
- ESC [ <y> ; <x> H CUP Cursor Position * Cursor moves to <x>; <y> coordinate within the viewport, where <x> is the column of the <y> line
- ESC [ <y> ; <x> f HVP Horizontal Vertical Position * Cursor moves to <x>; <y> coordinate within the viewport, where <x> is the column of the <y> line
- ESC [ s ANSISYSSC Save Cursor – Ansi.sys emulation **With no parameters, performs a save cursor operation like DECSC
- ESC [ u ANSISYSSC Restore Cursor – Ansi.sys emulation **With no parameters, performs a restore cursor operation like DECRC
- */
- /// <summary>
- /// Cursor up by <paramref name="n"/>
- /// </summary>
- public static void CursorUp(int n = 1) => SendCommand($"{CSI}{n}A");
- /// <summary>
- /// Cursor down by <paramref name="n"/>
- /// </summary>
- /// <param name="n"></param>
- public static void CursorDown(int n = 1) => SendCommand($"{CSI}{n}B");
- /// <summary>
- /// Cursor forward (Right) by <paramref name="n"/>
- /// </summary>
- /// <param name="n"></param>
- public static void CursorForward(int n = 1) => SendCommand($"{CSI}{n}C");
- /// <summary>
- /// Cursor backward (Left) by <paramref name="n"/>
- /// </summary>
- /// <param name="n"></param>
- public static void CursorBackward(int n = 1) => SendCommand($"{CSI}{n}D");
- /// <summary>
- /// Cursor down <paramref name="n"/> lines from current position
- /// </summary>
- /// <param name="n"></param>
- public static void CursorNextLine(int n = 1) => SendCommand($"{CSI}{n}E");
- /// <summary>
- /// Cursor up <paramref name="n"/> lines from current position
- /// </summary>
- /// <param name="n"></param>
- public static void CursorPreviousLine(int n = 1) => SendCommand($"{CSI}{n}F");
- /// <summary>
- /// Cursor moves to <paramref name="n"/>th position horizontally in the current line
- /// </summary>
- /// <param name="n"></param>
- public static void CursorHorizontal(int n = 1) => SendCommand($"{CSI}{n}G");
- /// <summary>
- /// Cursor moves to the <paramref name="n"/>th position vertically in the current column
- /// </summary>
- /// <param name="n"></param>
- public static void CursorVertical(int n = 1) => SendCommand($"{CSI}{n}d");
- /// <summary>
- /// Cursor moves to <paramref name="x"/>, <paramref name="y"/> coordinate within the viewport, where <paramref name="x"/> is the column of the <paramref name="y"/> line
- /// </summary>
- public static void CursorHorizontalVertical(int x = 1, int y = 1) => SendCommand($"{CSI}{y};{x}f");
- /// <summary>
- /// Set the cursor position using x, y one-based indexes.
- /// </summary>
- public static void SetCursorPosition(int x = 1, int y = 1) => SendCommand(ToCursorPosition(x, y));
- /// <inheritdoc cref="Console.GetCursorPosition"/>
- public static (int X, int Y) GetCursorPosition() => (CursorLeft + 1, CursorTop + 1);
- /// <summary>
- /// Get the CSI command for the x, y one-based index cursor position.
- /// </summary>
- internal static string ToCursorPosition(int x, int y) => $"{CSI}{y};{x}H";
- /*
- ESC [ ? 12 h ATT160 Text Cursor Enable Blinking Start the cursor blinking
- ESC [ ? 12 l ATT160 Text Cursor Disable Blinking Stop blinking the cursor
- ESC [ ? 25 h DECTCEM Text Cursor Enable Mode Show Show the cursor
- ESC [ ? 25 l DECTCEM Text Cursor Enable Mode Hide Hide the cursor
- */
- public static void EnableCursorBlink() => SendCommand($"{CSI}?12h");
- public static void DisableCursorBlink() => SendCommand($"{CSI}?12l");
- public static void ShowCursor() => SendCommand($"{CSI}?25h");
- public static void HideCursor() => SendCommand($"{CSI}?25l");
- #endregion CURSOR
- #region VIEWPORT
- /*
- ESC [ <n> S SU Scroll Up Scroll text up by <n>. Also known as pan down, new lines fill in from the bottom of the screen
- ESC [ <n> T SD Scroll Down Scroll down by <n>. Also known as pan up, new lines fill in from the top of the screen
- */
- /// <summary>
- /// Scroll text up by <paramref name="n"/>. Also known as pan down, new lines fill in from the bottom of the screen
- /// </summary>
- /// <remarks>Scroll refers to which direction the text moves during the operation.</remarks>
- public static void ScrollUp(int n = 1) => SendCommand($"{CSI}{n}S");
- /// <summary>
- /// Scroll down by <paramref name="n"/>. Also known as pan up, new lines fill in from the top of the screen
- /// </summary>
- /// <remarks>Scroll refers to which direction the text moves during the operation.</remarks>
- public static void ScrollDown(int n = 1) => SendCommand($"{CSI}{n}T");
- /*
- ESC [ <t> ; <b> r DECSTBM Set Scrolling Region Sets the VT scrolling margins of the viewport.
- */
- /// <summary>
- /// Sets the scrolling margins of the viewport. If the parameters are omitted, <paramref name="top"/> defaults to 1 and <paramref name="bottom"/> defaults to the current viewport height.
- /// </summary>
- /// <remarks>Useful for having a portion of the screen that doesn’t scroll when the rest of the screen is filled, such as having a title bar at the top or a status bar at the bottom of your application.</remarks>
- /// <param name="top">Top margin.</param>
- /// <param name="bottom">Bottom margin.</param>
- public static void SetScrollRegion(int top, int bottom) => SendCommand($"{CSI}{top};{bottom}r");
- /// <inheritdoc cref="SetScrollRegion(int, int)"/>
- public static void SetScrollRegion(int top) => SendCommand($"{CSI}{top}r");
- /// <inheritdoc cref="SetScrollRegion(int, int)"/>
- public static void SetScrollRegion() => SendCommand($"{CSI}r");
- #endregion VIEWPORT
- #region TABS
- /*
- ESC H HTS Horizontal Tab Set Sets a tab stop in the current column the cursor is in.
- ESC [ <n> I CHT Cursor Horizontal (Forward) Tab Advance the cursor to the next column (in the same row) with a tab stop. If there are no more tab stops, move to the last column in the row. If the cursor is in the last column, move to the first column of the next row.
- ESC [ <n> Z CBT Cursor Backwards Tab Move the cursor to the previous column (in the same row) with a tab stop. If there are no more tab stops, moves the cursor to the first column. If the cursor is in the first column, doesn’t move the cursor.
- ESC [ 0 g TBC Tab Clear (current column) Clears the tab stop in the current column, if there is one. Otherwise does nothing.
- ESC [ 3 g TBC Tab Clear (all columns) Clears all currently set tab stops.
- */
- /// <summary>
- /// Move to the next tabstop.
- /// </summary>
- public static void NextTabstop(int n = 1) => SendCommand($"{CSI}{n}I");
- //public static void NextTabstop() => SendCommand('\t');
- /// <summary>
- /// Sets a tab stop in the current column the cursor is in.
- /// </summary>
- public static void SetTabstop() => SendCommand($"{ESC}H");
- /// <summary>
- /// Sets a tab stop in the column specified by <paramref name="x"/>.
- /// </summary>
- /// <param name="x">The column to move to, one-based index.</param>
- /// <param name="restoreCursorPosition">True to move back to the original cursor position after setting the tabstop, otherwise stay at the new position. Default is true.</param>
- public static void SetTabstop(int x, bool restoreCursorPosition = true)
- {
- CommandReturnPosition(SetTabstop, x, 1, restoreCursorPosition);
- }
- /// <summary>
- /// Clears the tab stop in the current column, if there is one. Otherwise does nothing.
- /// </summary>
- public static void ClearTabstop() => SendCommand($"{CSI}0g");
- /// <summary>
- /// Clears the tab stop in the column specified by <paramref name="x"/>.
- /// </summary>
- /// <param name="x">The column to move to, one-based index.</param>
- /// <param name="restoreCursorPosition">True to move back to the original cursor position after setting the tabstop, otherwise stay at the new position. Default is true.</param>
- public static void ClearTabstop(int x, bool restoreCursorPosition = true)
- {
- CommandReturnPosition(ClearTabstop, x, 1, restoreCursorPosition);
- }
- /// <summary>
- /// Clears all currently set tab stops.
- /// </summary>
- public static void ClearTabstops() => SendCommand($"{CSI}3g");
- internal static void CommandReturnPosition(Action command, int x, int y, bool restoreCursorPosition)
- {
- if (command == null)
- throw new ArgumentNullException(nameof(command));
- if (restoreCursorPosition)
- SaveCursorPosition();
- SetCursorPosition(x, y);
- command();
- if (restoreCursorPosition)
- RestoreCursorPosition();
- }
- #endregion TABS
- #region COLORS
- /// <summary>
- /// Get the CSI command to set console colors.
- /// </summary>
- /// <remarks>
- /// <list type="table">
- /// <listheader>
- /// <description>Syntax</description>
- /// <description>Type</description>
- /// <description>Description</description>
- /// </listheader>
- /// <item>
- /// <term>38;2;r;g;b</term>
- /// <term>RGB</term>
- /// <description>Sequence of numbers to set the foreground color.</description>
- /// </item>
- /// <item>
- /// <term>48;2;r;g;b</term>
- /// <term>RGB</term>
- /// <description>Sequence of numbers to set the background color.</description>
- /// </item>
- /// <item>
- /// <term>38;5;color</term>
- /// <term>Palette</term>
- /// <description>Sequence of numbers to set the foreground color from the 88_255 palette.</description>
- /// </item>
- /// <item>
- /// <term>48;5;color</term>
- /// <term>Palette</term>
- /// <description>Sequence of numbers to set the background color from the 88_255 palette.</description>
- /// </item>
- /// <item>For SGR sequences, see <see cref="SGR"/></item>
- /// <item>
- /// <term>0</term>
- /// <description>Restore colors.</description>
- /// </item>
- /// <item>
- /// <term>1</term>
- /// <description>Bright foreground.</description>
- /// </item>
- /// <item>
- /// <term>4</term>
- /// <description>Underline</description>
- /// </item>
- /// <item>
- /// <term>7</term>
- /// <description>Swap to negative.</description>
- /// </item>
- /// </list>
- /// For more: <seealso href="https://docs.microsoft.com/en-us/windows/console/console-virtual-terminal-sequences#text-formatting" />
- /// </remarks>
- /// <param name="colors"></param>
- /// <returns></returns>
- /// <seealso cref="SGR"/>
- public static string ColorFormat(params int[] colors) => colors switch
- {
- null => RESETCOLOR,
- { Length: 0 } => RESETCOLOR,
- { Length: 1 } when colors[0] is 0 => RESETCOLOR,
- // todo: with list-patterns in preview:
- // [] => RESETCOLOR, // empty or null
- // [0] => RESETCOLOR, // only one item as it is 0
- _ => $"{CSI}{string.Join(';', colors)}m"
- };
- /// <summary> Uses a CSI command to set console colors.</summary>
- /// <inheritdoc cref="ColorFormat(int[])"/>
- public static void SetColor(params int[] colors) => SendCommand(ColorFormat(colors));
- public static void SetSgr(SgrColors color) => SetSgr((int)color);
- public static void SetSgr(int color) => SendCommand(ColorFormat(color));
- public static void SetForegroundPalette(int color) => SetColor(38, 5, color);
- public static void SetBackgroundPalette(int color) => SetColor(48, 5, color);
- public static void SetForeground(int r, int g, int b) => SetColor(38, 2, r, g, b);
- public static void SetBackground(int r, int g, int b) => SetColor(48, 2, r, g, b);
- public static void ResetColor() => SendCommand(RESETCOLOR);
- #endregion COLORS
- public static readonly ImmutableDictionary<char, (char Value, string Description)> DecLinemodeMap = ImmutableDictionary.CreateRange(new Dictionary<char, (char, string)>
- {
- {'j', ( '┘', "CornerBottomRight" ) },
- {'k', ( '┐', "CornerTopRight" ) },
- {'l', ( '┌', "CornerTopLeft" ) },
- {'m', ( '└', "CornerBottomLeft" ) },
- {'n', ( '┼', "IntersectionMiddle" ) },
- {'q', ( '─', "Horizontal" ) },
- {'t', ( '├', "IntersectionLeft" ) },
- {'u', ( '┤', "IntersectionRight" ) },
- {'v', ( '┴', "IntersectionBottom" ) },
- {'w', ( '┬', "IntersectionTop" ) },
- {'x', ( '│', "Vertical" ) },
- });
- public static class DecLineMode
- {
- /*
- ┘ 0x6a 'j'
- ┐ 0x6b 'k'
- ┌ 0x6c 'l'
- └ 0x6d 'm'
- ┼ 0x6e 'n'
- ─ 0x71 'q'
- ├ 0x74 't'
- ┤ 0x75 'u'
- ┴ 0x76 'v'
- ┬ 0x77 'w'
- │ 0x78 'x'
- */
- public static readonly char CornerBottomRight = 'j';
- public static readonly char CornerTopRight = 'k';
- public static readonly char CornerTopLeft = 'l';
- public static readonly char CornerBottomLeft = 'm';
- public static readonly char IntersectionMiddle = 'n';
- public static readonly char Horizontal = 'q';
- public static readonly char IntersectionLeft = 't';
- public static readonly char IntersectionRight = 'u';
- public static readonly char IntersectionBottom = 'v';
- public static readonly char IntersectionTop = 'w';
- public static readonly char Vertical = 'x';
- }
- [SupportedOSPlatform("windows")]
- public static void Palette()
- {
- EnterMainBuffer();
- int tabStops = 36;
- int tabSize = 6;
- var size = tabStops * tabSize;
- try
- {
- WindowWidth = size;
- }
- catch (IOException ex)
- {
- WriteLine(ex);
- throw;
- }
- // Tab stops
- SendCommand($"{CSI}3g");
- for (int t = 1; t < tabStops; t++)
- {
- SendCommand($"{CSI}1;{t * tabSize}H{ESC}H");
- }
- //Write($"{CSI}3;{size.Y - 2}");
- SendCommand($"{CSI}1;1H");
- // Write data
- int lines = 256;
- for (int i = 16; i < lines;)
- {
- for (int j = 0; j < tabStops; j++)
- {
- SendCommand($"{CSI}38;5;{i}m{i},");
- SendCommand($"\t");
- i++;
- }
- SendCommand($"\t");
- }
- SendCommand(CSI + "m");
- }
- public static void OtherFormats()
- {
- SendCommand("\x1b[31mThis text has a red foreground using SGR.31.\r\n");
- SendCommand("\x1b[1mThis text has a bright (bold) red foreground using SGR.1 to affect the previous color setting.\r\n");
- SendCommand("\x1b[mThis text has returned to default colors using SGR.0 implicitly.\r\n");
- SendCommand("\x1b[34;46mThis text shows the foreground and background change at the same time.\r\n");
- SendCommand("\x1b[0mThis text has returned to default colors using SGR.0 explicitly.\r\n");
- SendCommand("\x1b[31;32;33;34;35;36;101;102;103;104;105;106;107mThis text attempts to apply many colors in the same command. Note the colors are applied from left to right so only the right-most option of foreground cyan (SGR.36) and background bright white (SGR.107) is effective.\r\n");
- SendCommand("\x1b[39mThis text has restored the foreground color only.\r\n");
- SendCommand("\x1b[49mThis text has restored the background color only.\r\n");
- }
- public static void Win10AnniversaryFeatures()
- {
- void PrintVerticalBorder()
- {
- SendCommand(ESC + "(0"); // Enter Line drawing mode
- SendCommand(CSI + "104;93m"); // bright yellow on bright blue
- SendCommand("x"); // in line drawing mode, \x78 -> \u2502 "Vertical Bar"
- SendCommand(CSI + "0m"); // restore color
- SendCommand(ESC + "(B"); // exit line drawing mode
- }
- void PrintHorizontalBorder((int X, int Y) size, bool isTop)
- {
- SendCommand(ESC + "(0"); // Enter Line drawing mode
- SendCommand(CSI + "104;93m"); // Make the border bright yellow on bright blue
- SendCommand(isTop ? "l" : "m"); // print left corner
- for (int i = 1; i < size.X - 1; i++)
- SendCommand("q"); // in line drawing mode, \x71 -> \u2500 "HORIZONTAL SCAN LINE-5"
- SendCommand(isTop ? "k" : "j"); // print right corner
- SendCommand(CSI + "0m");
- SendCommand(ESC + "(B"); // exit line drawing mode
- }
- void PrintStatusLine(string message, (int X, int Y) size)
- {
- SendCommand(CSI + "{0};1H", size.Y);
- SendCommand(CSI + "K"); // clear the line
- SendCommand(message);
- }
- var size = (X: WindowWidth, Y: WindowHeight);
- // Enter the alternate buffer
- SendCommand(CSI + "?1049h");
- // Clear screen, tab stops, set, stop at columns 16, 32
- SendCommand(CSI + "1;1H");
- SendCommand(CSI + "2J"); // Clear screen
- int iNumTabStops = 4; // (0, 20, 40, width)
- SendCommand(CSI + "3g"); // clear all tab stops
- SendCommand(CSI + "1;20H"); // Move to column 20
- SendCommand(ESC + "H"); // set a tab stop
- SendCommand(CSI + "1;40H"); // Move to column 40
- SendCommand(ESC + "H"); // set a tab stop
- // Set scrolling margins to 3, h-2
- SendCommand(CSI + "3;{0}r", size.Y - 2);
- int numLines = size.Y - 4;
- SendCommand(CSI + "1;1H");
- SendCommand(CSI + "102;30m");
- SendCommand("Windows 10 Anniversary Update - VT Example");
- SendCommand(CSI + "0m");
- // Print a top border - Yellow
- SendCommand(CSI + "2;1H");
- PrintHorizontalBorder(size, true);
- // // Print a bottom border
- SendCommand(CSI + "{0};1H", size.Y - 1);
- PrintHorizontalBorder(size, false);
- // draw columns
- SendCommand(CSI + "3;1H");
- int line;
- for (line = 0; line < numLines * iNumTabStops; line++)
- {
- PrintVerticalBorder();
- if (line + 1 != numLines * iNumTabStops) // don't advance to next line if this is the last line
- SendCommand("\t"); // advance to next tab stop
- }
- PrintStatusLine("Press any key to see text printed between tab stops.", size);
- ReadKey(true);
- // Fill columns with output
- SendCommand(CSI + "3;1H");
- for (line = 0; line < numLines; line++)
- {
- for (var tab = 0; tab < iNumTabStops - 1; tab++)
- {
- PrintVerticalBorder();
- SendCommand("line={0}", line);
- SendCommand("\t"); // advance to next tab stop
- }
- PrintVerticalBorder();// print border at right side
- if (line + 1 != numLines)
- SendCommand("\t"); // advance to next tab stop, (on the next line)
- }
- PrintStatusLine("Press any key to demonstrate scroll margins", size);
- ReadKey(true);
- SendCommand(CSI + "3;1H");
- for (line = 0; line < numLines * 2; line++)
- {
- SendCommand(CSI + "K"); // clear the line
- for (var tab = 0; tab < iNumTabStops - 1; tab++)
- {
- PrintVerticalBorder();
- SendCommand("line={0}", line);
- SendCommand("\t"); // advance to next tab stop
- }
- PrintVerticalBorder(); // print border at right side
- if (line + 1 != numLines * 2)
- {
- SendCommand("\n"); //Advance to next line. If we're at the bottom of the margins, the text will scroll.
- SendCommand("\r"); //return to first col in buffer
- }
- }
- PrintStatusLine("Press any key to exit", size);
- ReadKey(true);
- // Exit the alternate buffer
- SendCommand(CSI + "?1049l");
- }
- }
- /// <summary>
- /// Set Graphics Rendition
- /// </summary>
- public static class SGR
- {
- /// <summary>Returns all attributes to the default state prior to modification.</summary>
- public static readonly int Default = (int)SgrColors.Default;
- /// <summary>Applies brightness/intensity flag to foreground color.</summary>
- public static readonly int BoldBright = (int)SgrColors.BoldBright;
- /// <summary>Removes brightness/intensity flag from foreground color.</summary>
- public static readonly int NoBoldBright = (int)SgrColors.NoBoldBright;
- /// <summary>Adds underline.</summary>
- public static readonly int Underline = (int)SgrColors.Underline;
- /// <summary>Removes underline.</summary>
- public static readonly int NoUnderline = (int)SgrColors.NoUnderline;
- /// <summary>Swaps foreground and background colors.</summary>
- public static readonly int Negative = (int)SgrColors.Negative;
- /// <summary>Returns foreground/background to normal.</summary>
- public static readonly int PositiveNoNegative = (int)SgrColors.PositiveNoNegative;
- /// <summary>Applies non-bold/bright black to foreground.</summary>
- public static readonly int ForegroundBlack = (int)SgrColors.ForegroundBlack;
- /// <summary>Applies non-bold/bright red to foreground.</summary>
- public static readonly int ForegroundRed = (int)SgrColors.ForegroundRed;
- /// <summary>Applies non-bold/bright green to foreground.</summary>
- public static readonly int ForegroundGreen = (int)SgrColors.ForegroundGreen;
- /// <summary>Applies non-bold/bright yellow to foreground.</summary>
- public static readonly int ForegroundYellow = (int)SgrColors.ForegroundYellow;
- /// <summary>Applies non-bold/bright blue to foreground.</summary>
- public static readonly int ForegroundBlue = (int)SgrColors.ForegroundBlue;
- /// <summary>Applies non-bold/bright magenta to foreground.</summary>
- public static readonly int ForegroundMagenta = (int)SgrColors.ForegroundMagenta;
- /// <summary>Applies non-bold/bright cyan to foreground.</summary>
- public static readonly int ForegroundCyan = (int)SgrColors.ForegroundCyan;
- /// <summary>Applies non-bold/bright white to foreground.</summary>
- public static readonly int ForegroundWhite = (int)SgrColors.ForegroundWhite;
- /// <summary>Applies extended color value to the foreground in the palette or rgb format.</summary>
- public static readonly int ForegroundExtended = (int)SgrColors.ForegroundExtended;
- /// <summary>Applies only the foreground portion of the defaults (see 0)</summary>
- public static readonly int ForegroundDefault = (int)SgrColors.ForegroundDefault;
- /// <summary>Applies non-bold/bright black to background</summary>
- public static readonly int BackgroundBlack = (int)SgrColors.BackgroundBlack;
- /// <summary>Applies non-bold/bright red to background.</summary>
- public static readonly int BackgroundRed = (int)SgrColors.BackgroundRed;
- /// <summary>Applies non-bold/bright green to background.</summary>
- public static readonly int BackgroundGreen = (int)SgrColors.BackgroundGreen;
- /// <summary>Applies non-bold/bright yellow to background.</summary>
- public static readonly int BackgroundYellow = (int)SgrColors.BackgroundYellow;
- /// <summary>Applies non-bold/bright blue to background.</summary>
- public static readonly int BackgroundBlue = (int)SgrColors.BackgroundBlue;
- /// <summary>Applies non-bold/bright magenta to background.</summary>
- public static readonly int BackgroundMagenta = (int)SgrColors.BackgroundMagenta;
- /// <summary>Applies non-bold/bright cyan to background.</summary>
- public static readonly int BackgroundCyan = (int)SgrColors.BackgroundCyan;
- /// <summary>Applies non-bold/bright white to background.</summary>
- public static readonly int BackgroundWhite = (int)SgrColors.BackgroundWhite;
- /// <summary>Applies extended color value to the background in the palette or rgb format.</summary>
- public static readonly int BackgroundExtended = (int)SgrColors.BackgroundExtended;
- /// <summary>Applies only the background portion of the defaults (see 0)</summary>
- public static readonly int BackgroundDefault = (int)SgrColors.BackgroundDefault;
- /// <summary>Applies bold/bright black to foreground.</summary>
- public static readonly int BrightForegroundBlack = (int)SgrColors.BrightForegroundBlack;
- /// <summary>Applies bold/bright red to foreground.</summary>
- public static readonly int BrightForegroundRed = (int)SgrColors.BrightForegroundRed;
- /// <summary>Applies bold/bright green to foreground.</summary>
- public static readonly int BrightForegroundGreen = (int)SgrColors.BrightForegroundGreen;
- /// <summary>Applies bold/bright yellow to foreground.</summary>
- public static readonly int BrightForegroundYellow = (int)SgrColors.BrightForegroundYellow;
- /// <summary>Applies bold/bright blue to foreground.</summary>
- public static readonly int BrightForegroundBlue = (int)SgrColors.BrightForegroundBlue;
- /// <summary>Applies bold/bright magenta to foreground.</summary>
- public static readonly int BrightForegroundMagenta = (int)SgrColors.BrightForegroundMagenta;
- /// <summary>Applies bold/bright cyan to foreground.</summary>
- public static readonly int BrightForegroundCyan = (int)SgrColors.BrightForegroundCyan;
- /// <summary>Applies bold/bright white to foreground.</summary>
- public static readonly int BrightForegroundWhite = (int)SgrColors.BrightForegroundWhite;
- /// <summary>Applies bold/bright black to background.</summary>
- public static readonly int BrightBackgroundBlack = (int)SgrColors.BrightBackgroundBlack;
- /// <summary>Applies bold/bright red to background.</summary>
- public static readonly int BrightBackgroundRed = (int)SgrColors.BrightBackgroundRed;
- /// <summary>Applies bold/bright green to background.</summary>
- public static readonly int BrightBackgroundGreen = (int)SgrColors.BrightBackgroundGreen;
- /// <summary>Applies bold/bright yellow to background.</summary>
- public static readonly int BrightBackgroundYellow = (int)SgrColors.BrightBackgroundYellow;
- /// <summary>Applies bold/bright blue to background.</summary>
- public static readonly int BrightBackgroundBlue = (int)SgrColors.BrightBackgroundBlue;
- /// <summary>Applies bold/bright magenta to background.</summary>
- public static readonly int BrightBackgroundMagenta = (int)SgrColors.BrightBackgroundMagenta;
- /// <summary>Applies bold/bright cyan to background.</summary>
- public static readonly int BrightBackgroundCyan = (int)SgrColors.BrightBackgroundCyan;
- /// <summary>Applies bold/bright white to background.</summary>
- public static readonly int BrightBackgroundWhite = (int)SgrColors.BrightBackgroundWhite;
- }
- /// <summary>
- /// Set Graphics Rendition
- /// </summary>
- public enum SgrColors
- {
- /// <summary>Returns all attributes to the default state prior to modification.</summary>
- Default = 0,
- /// <summary>Applies brightness/intensity flag to foreground color.</summary>
- BoldBright = 1,
- /// <summary>Removes brightness/intensity flag from foreground color.</summary>
- NoBoldBright = 22,
- /// <summary>Adds underline.</summary>
- Underline = 4,
- /// <summary>Removes underline.</summary>
- NoUnderline = 24,
- /// <summary>Swaps foreground and background colors.</summary>
- Negative = 7,
- /// <summary>Returns foreground/background to normal.</summary>
- PositiveNoNegative = 27,
- /// <summary>Applies non-bold/bright black to foreground.</summary>
- ForegroundBlack = 30,
- /// <summary>Applies non-bold/bright red to foreground.</summary>
- ForegroundRed = 31,
- /// <summary>Applies non-bold/bright green to foreground.</summary>
- ForegroundGreen = 32,
- /// <summary>Applies non-bold/bright yellow to foreground.</summary>
- ForegroundYellow = 33,
- /// <summary>Applies non-bold/bright blue to foreground.</summary>
- ForegroundBlue = 34,
- /// <summary>Applies non-bold/bright magenta to foreground.</summary>
- ForegroundMagenta = 35,
- /// <summary>Applies non-bold/bright cyan to foreground.</summary>
- ForegroundCyan = 36,
- /// <summary>Applies non-bold/bright white to foreground.</summary>
- ForegroundWhite = 37,
- /// <summary>Applies extended color value to the foreground in the palette or rgb format.</summary>
- ForegroundExtended = 38,
- /// <summary>Applies only the foreground portion of the defaults (see 0)</summary>
- ForegroundDefault = 39,
- /// <summary>Applies non-bold/bright black to background</summary>
- BackgroundBlack = 40,
- /// <summary>Applies non-bold/bright red to background.</summary>
- BackgroundRed = 41,
- /// <summary>Applies non-bold/bright green to background.</summary>
- BackgroundGreen = 42,
- /// <summary>Applies non-bold/bright yellow to background.</summary>
- BackgroundYellow = 43,
- /// <summary>Applies non-bold/bright blue to background.</summary>
- BackgroundBlue = 44,
- /// <summary>Applies non-bold/bright magenta to background.</summary>
- BackgroundMagenta = 45,
- /// <summary>Applies non-bold/bright cyan to background.</summary>
- BackgroundCyan = 46,
- /// <summary>Applies non-bold/bright white to background.</summary>
- BackgroundWhite = 47,
- /// <summary>Applies extended color value to the background in the palette or rgb format.</summary>
- BackgroundExtended = 48,
- /// <summary>Applies only the background portion of the defaults (see 0)</summary>
- BackgroundDefault = 49,
- /// <summary>Applies bold/bright black to foreground.</summary>
- BrightForegroundBlack = 90,
- /// <summary>Applies bold/bright red to foreground.</summary>
- BrightForegroundRed = 91,
- /// <summary>Applies bold/bright green to foreground.</summary>
- BrightForegroundGreen = 92,
- /// <summary>Applies bold/bright yellow to foreground.</summary>
- BrightForegroundYellow = 93,
- /// <summary>Applies bold/bright blue to foreground.</summary>
- BrightForegroundBlue = 94,
- /// <summary>Applies bold/bright magenta to foreground.</summary>
- BrightForegroundMagenta = 95,
- /// <summary>Applies bold/bright cyan to foreground.</summary>
- BrightForegroundCyan = 96,
- /// <summary>Applies bold/bright white to foreground.</summary>
- BrightForegroundWhite = 97,
- /// <summary>Applies bold/bright black to background.</summary>
- BrightBackgroundBlack = 100,
- /// <summary>Applies bold/bright red to background.</summary>
- BrightBackgroundRed = 101,
- /// <summary>Applies bold/bright green to background.</summary>
- BrightBackgroundGreen = 102,
- /// <summary>Applies bold/bright yellow to background.</summary>
- BrightBackgroundYellow = 103,
- /// <summary>Applies bold/bright blue to background.</summary>
- BrightBackgroundBlue = 104,
- /// <summary>Applies bold/bright magenta to background.</summary>
- BrightBackgroundMagenta = 105,
- /// <summary>Applies bold/bright cyan to background.</summary>
- BrightBackgroundCyan = 106,
- /// <summary>Applies bold/bright white to background.</summary>
- BrightBackgroundWhite = 107,
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement