|
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +// Licensed under the MIT license. See LICENSE file in the project root for full license information. |
| 3 | + |
| 4 | +namespace Microsoft.Testing.Platform.OutputDevice.Console; |
| 5 | + |
| 6 | +/// <summary> |
| 7 | +/// A collection of standard ANSI/VT100 control codes. |
| 8 | +/// </summary> |
| 9 | +internal static class AnsiCodes |
| 10 | +{ |
| 11 | + /// <summary> |
| 12 | + /// Escape character. |
| 13 | + /// </summary> |
| 14 | + public const string Esc = "\x1b"; |
| 15 | + |
| 16 | + /// <summary> |
| 17 | + /// The control sequence introducer. |
| 18 | + /// </summary> |
| 19 | + public const string CSI = "\x1b["; |
| 20 | + |
| 21 | + /// <summary> |
| 22 | + /// Select graphic rendition. |
| 23 | + /// </summary> |
| 24 | + /// <remarks> |
| 25 | + /// Print <see cref="CSI"/>color-code<see cref="SetColor"/> to change text color. |
| 26 | + /// </remarks> |
| 27 | + public const string SetColor = ";1m"; |
| 28 | + |
| 29 | + /// <summary> |
| 30 | + /// Select graphic rendition - set bold mode. |
| 31 | + /// </summary> |
| 32 | + /// <remarks> |
| 33 | + /// Print <see cref="CSI"/><see cref="SetBold"/> to change text to bold. |
| 34 | + /// </remarks> |
| 35 | + public const string SetBold = "1m"; |
| 36 | + |
| 37 | + /// <summary> |
| 38 | + /// A shortcut to reset color back to normal. |
| 39 | + /// </summary> |
| 40 | + public const string SetDefaultColor = CSI + "m"; |
| 41 | + |
| 42 | + /// <summary> |
| 43 | + /// Non-xterm extension to render a hyperlink. |
| 44 | + /// </summary> |
| 45 | + /// <remarks> |
| 46 | + /// Print <see cref="LinkPrefix"/>url<see cref="LinkInfix"/>text<see cref="LinkSuffix"/> to render a hyperlink. |
| 47 | + /// </remarks> |
| 48 | + public const string LinkPrefix = "\x1b]8;;"; |
| 49 | + |
| 50 | + /// <summary> |
| 51 | + /// <see cref="LinkPrefix"/>. |
| 52 | + /// </summary> |
| 53 | + public const string LinkInfix = "\x1b\\"; |
| 54 | + |
| 55 | + /// <summary> |
| 56 | + /// <see cref="LinkPrefix"/>. |
| 57 | + /// </summary> |
| 58 | + public const string LinkSuffix = "\x1b]8;;\x1b\\"; |
| 59 | + |
| 60 | + /// <summary> |
| 61 | + /// Moves up the specified number of lines and puts cursor at the beginning of the line. |
| 62 | + /// </summary> |
| 63 | + /// <remarks> |
| 64 | + /// Print <see cref="CSI"/>N<see cref="MoveUpToLineStart"/> to move N lines up. |
| 65 | + /// </remarks> |
| 66 | + public const string MoveUpToLineStart = "F"; |
| 67 | + |
| 68 | + /// <summary> |
| 69 | + /// Moves forward (to the right) the specified number of characters. |
| 70 | + /// </summary> |
| 71 | + /// <remarks> |
| 72 | + /// Print <see cref="CSI"/>N<see cref="MoveForward"/> to move N characters forward. |
| 73 | + /// </remarks> |
| 74 | + public const string MoveForward = "C"; |
| 75 | + |
| 76 | + /// <summary> |
| 77 | + /// Moves backward (to the left) the specified number of characters. |
| 78 | + /// </summary> |
| 79 | + /// <remarks> |
| 80 | + /// Print <see cref="CSI"/>N<see cref="MoveBackward"/> to move N characters backward. |
| 81 | + /// </remarks> |
| 82 | + public const string MoveBackward = "D"; |
| 83 | + |
| 84 | + /// <summary> |
| 85 | + /// Clears everything from cursor to end of screen. |
| 86 | + /// </summary> |
| 87 | + /// <remarks> |
| 88 | + /// Print <see cref="CSI"/><see cref="EraseInDisplay"/> to clear. |
| 89 | + /// </remarks> |
| 90 | + public const string EraseInDisplay = "J"; |
| 91 | + |
| 92 | + /// <summary> |
| 93 | + /// Clears everything from cursor to the end of the current line. |
| 94 | + /// </summary> |
| 95 | + /// <remarks> |
| 96 | + /// Print <see cref="CSI"/><see cref="EraseInLine"/> to clear. |
| 97 | + /// </remarks> |
| 98 | + public const string EraseInLine = "K"; |
| 99 | + |
| 100 | + /// <summary> |
| 101 | + /// Hides the cursor. |
| 102 | + /// </summary> |
| 103 | + public const string HideCursor = "\x1b[?25l"; |
| 104 | + |
| 105 | + /// <summary> |
| 106 | + /// Shows/restores the cursor. |
| 107 | + /// </summary> |
| 108 | + public const string ShowCursor = "\x1b[?25h"; |
| 109 | + |
| 110 | + /// <summary> |
| 111 | + /// Set progress state to a busy spinner. <br/> |
| 112 | + /// Note: this code works only on ConEmu terminals, and conflicts with push a notification code on iTerm2. |
| 113 | + /// </summary> |
| 114 | + /// <remarks> |
| 115 | + /// <see href="https://conemu.github.io/en/AnsiEscapeCodes.html#ConEmu_specific_OSC">ConEmu specific OSC codes.</see><br/> |
| 116 | + /// <see href="https://iterm2.com/documentation-escape-codes.html">iTerm2 proprietary escape codes.</see> |
| 117 | + /// </remarks> |
| 118 | + public const string SetBusySpinner = "\x1b]9;4;3;\x1b\\"; |
| 119 | + |
| 120 | + /// <summary> |
| 121 | + /// Remove progress state, restoring taskbar status to normal. <br/> |
| 122 | + /// Note: this code works only on ConEmu terminals, and conflicts with push a notification code on iTerm2. |
| 123 | + /// </summary> |
| 124 | + /// <remarks> |
| 125 | + /// <see href="https://conemu.github.io/en/AnsiEscapeCodes.html#ConEmu_specific_OSC">ConEmu specific OSC codes.</see><br/> |
| 126 | + /// <see href="https://iterm2.com/documentation-escape-codes.html">iTerm2 proprietary escape codes.</see> |
| 127 | + /// </remarks> |
| 128 | + public const string RemoveBusySpinner = "\x1b]9;4;0;\x1b\\"; |
| 129 | + |
| 130 | + public static string Colorize(string? s, TerminalColor color) |
| 131 | + => RoslynString.IsNullOrWhiteSpace(s) ? s ?? string.Empty : $"{CSI}{(int)color}{SetColor}{s}{SetDefaultColor}"; |
| 132 | + |
| 133 | + public static string MakeBold(string? s) |
| 134 | + => RoslynString.IsNullOrWhiteSpace(s) ? s ?? string.Empty : $"{CSI}{SetBold}{s}{SetDefaultColor}"; |
| 135 | + |
| 136 | + public static string MoveCursorBackward(int count) => $"{CSI}{count}{MoveBackward}"; |
| 137 | + |
| 138 | + /// <summary> |
| 139 | + /// Moves cursor to the specified column, or the rightmost column if <paramref name="column"/> is greater than the width of the terminal. |
| 140 | + /// </summary> |
| 141 | + /// <param name="column">Column index.</param> |
| 142 | + /// <returns>Control codes to set the desired position.</returns> |
| 143 | + public static string SetCursorHorizontal(int column) => $"{CSI}{column}G"; |
| 144 | +} |
0 commit comments