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.

  • I am able to replicate this. I wonder if ANSICON would help. – root Nov 1 '17 at 14:10
  • You can use colorization in the prompt function without needing escape codes (use Write-Host instead). – Bill_Stewart Nov 1 '17 at 19:24
  • @Bill_Stewart OK, I'll test this. – Lasse V. Karlsen Nov 2 '17 at 7:38
  • This has been driving me nuts for a long time too. Would love to see a solution! – Collin Chaffin May 20 '18 at 18:02
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()
}
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.