How To Use ANSI/VT100 Formatting in PowerShell (ooh, pretty colors!)

Welcome Forums General PowerShell Q&A How To Use ANSI/VT100 Formatting in PowerShell (ooh, pretty colors!)

Viewing 0 reply threads
  • Author
    Posts
    • #157464
      Senior Moderator
      Topics: 10
      Replies: 164
      Points: 880
      Helping Hand
      Rank: Major Contributor

      I’ve been working primarily in bash on CentOS and RHL, and just recently started working on some Windows 10 systems and trying to translate my skills from bash to PowerShell. It’s been… interesting.

      ANSI/VT100 formatting might not help you directly automate any tasks, but I’ve found that it can be really helpful to be able to adjust output for readability, and highlight important pieces of information. I wasn’t able to find a good writeup anywhere that explains the basics with regard to PowerShell, so I thought I’d lay it out. I am drawing primarily on the work done here by Fabien.

      This has been tested on Windows 10 Enterprise r1803, using PowerShell v5.1 (in native conhost.exe). As far as I can tell, everything should just work if you’re on that revision or higher, with no need to mess with virtual terminal settings as before.

      VT100 text formatting sequences follow the general format:

      To use this, we first need to get the escape character. For PSv5.1, this is not the same as the backtick “`” (grave-accent or backwards apostrophe) used to escape special characters. To get the escape character we need to call the specific ASCII character by number (in this case 27) using “[char]##”, and for ease of use store it in a variable:

      This will be applied at the beginning of each VT100 sequence.

      Text Styling and Resets
      The following styles are usable:

      • Bold
      • Underlined
      • Inverted

        there’s no equivalent for this in BBCode – it inverts the foreground and background colors (e.g. white text on black background becomes black text on white background)

      VT100 also has Dim ([3m), Blinking ([5m) and Hidden ([8m), but they don’t seem to be implemented in conhost.

      To remove the style, VT100 has the following reset sequences:

      • Bold Reset
      • oddly, this one reset doesn’t seem to be implemented properly in conhost (I assume it’s the terminal emulator and not PowerShell itself)

      • Underline Reset
      • Invert Reset
      • Reset All

        this one resets all formatting to default, including the color codes we’ll cover next

      There are also reset codes for Dim, Blinking and Hidden but they’re useless at the moment.

      Multiple styles can be applied at the same time by separating the numbers with semi-colons, and they can be individually turned off using the above resets. For example:

      will produce
      Bold Underlined Bold Only Normal Text

      16-Color Foreground & Background
      The standard 16-color set is assigned to its own numbers for both the text and the background, but the color seems a bit off for some of them. I’ve tried to replicate what they actually look like in conhost.

      • Default

        forces the terminal default text color, whatever it’s set to

      • Black
      • Red
      • Green
      • Yellow

        (appears slightly off-white)

      • Blue
      • Magenta

        (this one looks the least like the color it’s supposed to be)

      • Cyan
      • Light Gray
      • Dark Gray
      • Light Red
      • Light Green
      • Light Yellow
      • Light Blue
      • Light Magenta
      • Light Cyan
      • White

      The background colors are the same set, and appear essentially as text highlighting.

      256-Color Foreground & Background
      The conhost.exe terminal emulator supports the full 256 color range available with ANSI/VT100. The foreground color is applied using the sequence:

      Where ### is a number between 0 and 255. To apply the color to the background instead of the foreground, replace the 38 with 48.
      To see a chart with all of the colors, you can copy & paste this into your PS prompt:

      This code was originally written for bash by Sam Hocevar and falls under his license.
      Editing for PowerShell was done by me.

      I would really like to improve the way the color boxes in that bit of code are formatted. To reproduce the functionality of Linux’s printf I had to apply PadLeft() to the text being displayed and then handle the display with Write-Host -NoNewLine to prevent the really unnecessary newlines PowerShell wants to add all over the place. For various reasons, I’d love to avoid the use of Write-Host but I haven’t found another option for stripping out the newlines. Is there a better way to get the kind of control that printf offers?

Viewing 0 reply threads
  • The topic ‘How To Use ANSI/VT100 Formatting in PowerShell (ooh, pretty colors!)’ is closed to new replies.