posh-git prompt makes PowerShell transcripts less readable

Created on 11 May 2016  Â·  14Comments  Â·  Source: dahlbyk/posh-git

With posh-git:

>

C:\devcd log-ssh
 [
master
 ≡
]
>

C:\dev\log-sshls


    Directory: C:\dev\log-ssh


Mode                LastWriteTime         Length Name
----                -------------         ------ ----
-a----        4/28/2016   6:21 PM          35815 LICENSE
-a----        4/28/2016   6:25 PM           4477 Log-SSH.pl
-a----        4/28/2016   6:21 PM             74 README.md

Without posh-git:

PS C:\dev> cd log-ssh
PS C:\dev\log-ssh> ls


    Directory: C:\dev\log-ssh


Mode                LastWriteTime     Length Name
----                -------------     ------ ----
-a----       2016-04-28     18:21      35815 LICENSE
-a----       2016-04-28     18:25       4477 Log-SSH.pl
-a----       2016-04-28     18:21         74 README.md

Area-GitStatus Enhancement

Most helpful comment

Completely forgot about this. ANSI support is in progress: #304

All 14 comments

What version of Windows/PowerShell? My transcripts on PS 4.0 seem to always show PS> as the prompt?

Windows:

Caption        : Microsoft Windows 7 Enterprise 
OSArchitecture : 64-bit

Powershell:

Name             : Windows PowerShell ISE Host
Version          : 5.0.10586.117

I'm using the following prompt replacement function, which I got from elsewhere in this repository. I'm thinking it's the standard thing to do, but maybe not:

    function global:prompt {
        $realLASTEXITCODE = $LASTEXITCODE
        $Host.UI.RawUI.ForegroundColor = $GitPromptSettings.DefaultForegroundColor
        Write-Host($pwd.ProviderPath) -nonewline
        Write-VcsStatus
        $global:LASTEXITCODE = $realLASTEXITCODE
        return "> "
        }

Ooh, it might have something to do with ISE? Do you see the same behavior in a normal host?

I do see similar behaviour:

PS>. 'C:\Users\«REDACTED»\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1'

C:\dev
>
PS>cd Log-SSH
C:\dev\Log-SSH
 [
master
 ≡
]
>
PS>ls


    Directory: C:\dev\Log-SSH


Mode                LastWriteTime         Length Name
----                -------------         ------ ----
-a----        4/28/2016   6:21 PM          35815 LICENSE
-a----        4/28/2016   6:25 PM           4477 Log-SSH.pl
-a----        4/28/2016   6:21 PM             74 README.md


C:\dev\Log-SSH
 [
master
 ≡
]
>
PS>exit
**********************
Windows PowerShell transcript end
End time: 20160511114455
**********************

The same thing happens for me, it looks like the transcript doesn't obey Write-Host's -NoNewLine setting.

C:\Users\bluewrite-host "test"; write-host "test"; write-host "tested"
test
test
tested
>
C:\Users\blueecho "now with nonewline"
now with nonewline
>
C:\Users\bluewrite-host -NoNewline "test"; write-host -NoNewline "test"; write-host -NoNewline "tested"
test
test
tested
>
>
>

Edit: It looks like each command's output is logged separately (which makes sense), so unless the number of Write-Hosts involved in the prompt can be reduced, I'm not sure what can be done about this.

I had noticed that. I guess it's really PowerShell's transcription logic that's broken, not posh-git. I'm intrigued that the > appears before the rest of the Write-Hosts. Somehow the return value of the prompt function is getting to the transcript before the strings that are being Write-Host-ed.

Very curious. Unless we were to replace the multi-Write-Host formatting with some sort of single ANSI color-ized string, I'm not sure there's anything we can do about this. Of course, now I am very curious if a multi-colored output is even possible with a single Write-Host.

I agree. I poked at the [Host.UI] object a little and got nowhere. Someone wrote a wrapper for Write-Host that processes ANSI color, but I think that, even should you be able to return the entire prompt, with colours, as a single string with ANSI codes, even then one would have to change the function that prints the return value of the global prompt function to something that understands ANSI codes. I don't know enough about PowerShell / .NET to dig that deep.

ANSI escape sequences are supported in the very latest Windows 10 insider builds (basically anything later than the November update).

PowerShell 5.1 (not yet available outside of the latest Windows Insider builds) adds a new property you can test to see if ANSI escape sequences are supported: $host.UI.SupportsVirtualTerminal

So you could write something like:

if ($PSVersionTable['PSVersion'] -ge "5.1" -and $host.UI.SupportsVirtualTerminal)
{
    $CSI = [char]0x1b + '['
    "${CSI}31mHello${CSI}32m World${CSI}0m"
}
else
{
    Write-Host -ForegroundColor DarkRed -NoNewline Hello; Write-Host -ForegroundColor DarkGreen ' World'
}

You can read more about which sequences are supported here: https://msdn.microsoft.com/en-us/library/windows/desktop/mt638032%28v=vs.85%29.aspx

Completely forgot about this. ANSI support is in progress: #304

develop now has basic ANSI support, and transcripts are indeed fixed:
image

Though something funky is going on with the prompt on lines with commands:
image

Not sure what to say about that one, @dahlbyk -- I mean, the escape sequences are in the log...

  1. PowerShell could strip them so they're not logged
  2. Or, you could remove them if you don't want them
  3. Or, you could read them somewhere that renders them (i.e. if you run that to the console, it'll render in color)

To clarify, by "something funky" I mean that the first and last lines with AnsiConsole have a truncated prompt string than doesn't include the escape sequences, while the middle two lines show the full prompt (with escape sequences, as expected).

I'm going to go ahead and close this. ANSI support in 1.0 seems to improve the situation enough.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

nairby picture nairby  Â·  6Comments

codedog picture codedog  Â·  7Comments

ewgoforth picture ewgoforth  Â·  8Comments

exbuzz picture exbuzz  Â·  7Comments

sryze picture sryze  Â·  4Comments