2

I tried this: How to colorize the Powershell prompt?

function prompt  
{  
    $ESC = [char]27
    "$ESC[46mPS $($executionContext.SessionState.Path.CurrentLocation)$('>' * ($nestedPromptLevel + 1)) $ESC[0m"  
}

And tried this: Using ANSI escape codes in Powershell prompt to add color, looses meaning after executing some console processes, like git

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

Both options work, but produce same weired problem. When I print quotes( ' or " ), suddenly, right after prompt - character 'm' appears. It does not influes functionality, but this character persists when copying text, so its very annoying to remove 'm' after each paste. I'm using ConEmu terminal, but problem persist in standard Windows PowerShell app too.

Here is the picture: PowerShell prompt screenshot.

PowerShell info: PSVersion - 5.1.19041.610, BuildVersion - 10.0.19041.610, CLRVersion - 4.0.30319.42000.

Also tried this: PowerShell prompt with random color, and no weired characters appear here, but this prompt doesn't change color dynamicaly when quoting, like normal prompt do.

function prompt {
    $color = Get-Random -Min 1 -Max 16
    Write-Host ("PS " + $(Get-Location) +">") -NoNewLine `
     -ForegroundColor $Color
    return " "
}
webdev4422
  • 41
  • 5

1 Answers1

0

Starting from your first example, I'd suggest to move the « reset color » ANSI escape sequence before the '>' character. Also i prefer to change text foreground color instead of background.

function prompt  
{  
    $ESC = [char]27
    "$ESC[36mPS $($executionContext.SessionState.Path.CurrentLocation)$ESC[0m$('>' * ($nestedPromptLevel + 1)) "  
}