5

I use various Linux programs on my Windows machine; some of them have colorized output. GnuWin32 and so forth.

However, Windows PowerShell does not support Linux colors; it gets a message like ←[0m31m(which is the color control code), and renders that instead of the color.

Is there any way around this?

6 Answers 6

3

Powershell is just a text-mode program, running in the Windows console. The Windows console does not support ANSI escape sequences which is what you are seeing.

To fix it, it depends a lot on which program you are using. I am guessing you use cygwin to run these scripts?

2
  • In this instance, I was using a python program, mercurial. Dec 18, 2009 at 19:03
  • I wish you had said Perl, then I would tell you about the Win32::Console module for Perl :) I am not sure what you can do to get colors with Windows-Python. Dec 18, 2009 at 20:40
3

You can use Cygwin to run PowerShell and PowerShell scripts:

alt text

Cygwin can handle the escape sequences.

2
  • What if I don't want to use cygwin? Dec 18, 2009 at 19:03
  • 1
    Then you can take any of the other suggestions here
    – Daniel H
    Mar 29, 2010 at 17:57
3

For posterity

You can use ANSICON to display ANSI escape sequences in Windows consoles, including Powershell. This should give you color.

This issue thread on getting it to work with PowerShell has a few different solutions as there is more than one way to do it.

3

I was also searching for a good solution, but I couldn't find any ...

Here is a short PowerShell script to translate ANSI color codes:

function Open-Colored([String] $Filename)
  { Write-Colored(cat -Raw $Filename) }

function Write-Colored([String] $text)
  { # split text at ESC-char
    $split = $text.Split([char] 27)
    foreach ($line in $split)
      { if ($line[0] -ne '[')
          { Write-Host $line -NoNewline }
        else
          { if     (($line[1] -eq '0') -and ($line[2] -eq 'm')) { Write-Host $line.Substring(3) -NoNewline }
            elseif (($line[1] -eq '3') -and ($line[3] -eq 'm'))
              { # normal color codes
                if     ($line[2] -eq '0') { Write-Host $line.Substring(4) -NoNewline -ForegroundColor Black       }
                elseif ($line[2] -eq '1') { Write-Host $line.Substring(4) -NoNewline -ForegroundColor DarkRed     }
                elseif ($line[2] -eq '2') { Write-Host $line.Substring(4) -NoNewline -ForegroundColor DarkGreen   }
                elseif ($line[2] -eq '3') { Write-Host $line.Substring(4) -NoNewline -ForegroundColor DarkYellow  }
                elseif ($line[2] -eq '4') { Write-Host $line.Substring(4) -NoNewline -ForegroundColor DarkBlue    }
                elseif ($line[2] -eq '5') { Write-Host $line.Substring(4) -NoNewline -ForegroundColor DarkMagenta }
                elseif ($line[2] -eq '6') { Write-Host $line.Substring(4) -NoNewline -ForegroundColor DarkCyan    }
                elseif ($line[2] -eq '7') { Write-Host $line.Substring(4) -NoNewline -ForegroundColor Gray        }
              }
            elseif (($line[1] -eq '3') -and ($line[3] -eq ';') -and ($line[5] -eq 'm'))
              { # bright color codes
                if     ($line[2] -eq '0') { Write-Host $line.Substring(6) -NoNewline -ForegroundColor DarkGray    }
                elseif ($line[2] -eq '1') { Write-Host $line.Substring(6) -NoNewline -ForegroundColor Red         }
                elseif ($line[2] -eq '2') { Write-Host $line.Substring(6) -NoNewline -ForegroundColor Gree        }
                elseif ($line[2] -eq '3') { Write-Host $line.Substring(6) -NoNewline -ForegroundColor Yellow      }
                elseif ($line[2] -eq '4') { Write-Host $line.Substring(6) -NoNewline -ForegroundColor Blue        }
                elseif ($line[2] -eq '5') { Write-Host $line.Substring(6) -NoNewline -ForegroundColor Magenta     }
                elseif ($line[2] -eq '6') { Write-Host $line.Substring(6) -NoNewline -ForegroundColor Cyan        }
                elseif ($line[2] -eq '7') { Write-Host $line.Substring(6) -NoNewline -ForegroundColor White       }
              }
          }
      }
  }

Usage:

Open-Colored .\myColoredLogfile.log

Currently, I have no idea how to integrate this into a pages (e.g. less) or how to write a own simple pager like less.

PSCX comes with a less clone, but it has no ANSI color code support (yet)...

0

You could use a Linux virtual machine to launch these scripts. This is a big thing for a small result if it's just about showing colors, but it can't hurt to run the scripts on the platform they were made for.

1
  • yeah, that's too big to justify for this one thing. Plus I don't have a VM hypervisor running, and I do not want to install one. Dec 18, 2009 at 16:49
0

You said you were using Mercurial to run the Linux programs. Assuming you're referring to the source control system (which I didn't realize could run Linux programs; I only use it for source control), there's an extension that will let you show color. Wincolor will intercept the ANSI color control and call the appropriate Windows functions. It seems to work no matter what, but I think it works better with pyreadline installed.

Note: I have treid this without pyreadline, and it didn't work for me. I have not yet tried it with pyreadline.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .