5

I have created a powershell configuration file that modifies the prompt to add some color (depending on things like git branch, etc.)

This looks good, right up until I've executed a console program, like git, that also outputs colorized text. After this it seems Powershell decides to output escape codes verbatim.

Easier to understand with a screenshot:

example of output

  1. is the output before I run git, notice the colorized output of the text "colorized >"
  2. is the output after I run git, notice that the escape codes are output verbatim

Here's my Powershell configuration file:

function prompt {
    "" + [char]0x001b + "[35m colorized >" + [char]0x001b + "[0m"
}

Is there a trick to re-enable the functionality of the ANSI codes or is this just some quirk of how either Powershell or git outputs text? Note that git is just an example, I've found several programs that messes this up, but not all of them do.

MSBuild, for example, works just fine.

Lasse V. Karlsen
  • 3,656
  • 10
  • 41
  • 58

1 Answers1

3

Oh hey! I ran into this problem myself when I excitedly go into colorizing EVERYTHING with ANSI.

First of all, I took this (can't remember how I stumbled on it initially but many thanks to original coder) https://github.com/bozho/AnsiColorOut/blob/master/AnsiColorOut/Console.cs

Then in my profile I make sure to add it

Add-Type -Path C:\Users\audaxdreik\Documents\WindowsPowerShell\Console.cs

And then I create a quick custom wrapper in my profile for known problem programs that I use a lot, namely git

$gitPath = (Get-Command -Name git).Source
function git {
    & $gitPath $args
    [Bozho.PowerShell.Console]::EnableVirtualTerminalProcessing()
}
audaxdreik
  • 31
  • 2