Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prompt function with Write-Host on MacOS 10.12.5 #4191

Closed
mikederksen opened this issue Jul 6, 2017 · 12 comments

Comments

Projects
None yet
10 participants
@mikederksen
Copy link

commented Jul 6, 2017

Steps to reproduce

When overwriting the prompt function as follows:

function prompt {
    Write-Host "Something> "
    " "
}

The reason why one would want to manually use Write-Host for outputting prompt values, is to be able to use colors. This is done in posh-git for example. In windows this works as expected.

Expected behavior

Something> --input goes here--

Actual behavior

Something>
 Something>
--cursor position--

When i start typing the input appears as follows:

Something>
--input appears here--

Environment data

> $PSVersionTable
Name                           Value                                           
----                           -----                                           
PSVersion                      6.0.0-beta                                      
PSEdition                      Core                                            
BuildVersion                   3.0.0.0                                         
CLRVersion                                                                     
GitCommitId                    v6.0.0-beta.1                                   
OS                             Darwin 16.6.0 Darwin Kernel Version 16.6.0: F...
Platform                       Unix                                            
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}                         
PSRemotingProtocolVersion      2.3                                             
SerializationVersion           1.1.0.1                                         
WSManStackVersion              3.0 

@mikederksen mikederksen changed the title Prompt function with Write-Host Prompt function with Write-Host on MacOS 10.12.5 Jul 6, 2017

@alexandair

This comment has been minimized.

Copy link
Contributor

commented Jul 6, 2017

Don't use Write-Host at all. Just specify a string.
Or, use Write-Host -NoNewLine.

@mikederksen

This comment has been minimized.

Copy link
Author

commented Jul 6, 2017

Write-Host -NoNewLine gives the same result. Returning a string is fine for normal text, but when one wants colored text this method doesn't suffice.

@rkeithhill

This comment has been minimized.

Copy link
Contributor

commented Jul 6, 2017

but when one wants colored text this method doesn't suffice.

Sure it does. Try "`e[32mMy Green prompt>`e[0m ". See the MSDN docs on the rest of the VT seqs. Oh, and you can test if the current host supports these with $host.UI.SupportsVirtualTerminal

@mikederksen

This comment has been minimized.

Copy link
Author

commented Jul 6, 2017

Oh, didn't know that! Thanks for the advice. We can consider this issue resolved.

@mikederksen mikederksen closed this Jul 6, 2017

@mikederksen mikederksen reopened this Jul 6, 2017

@mikederksen mikederksen closed this Jul 6, 2017

@mikederksen

This comment has been minimized.

Copy link
Author

commented Jul 6, 2017

Oke, using "e[32mMy Green prompt>e[0m " works, until i start typing. Once i start typing the prompt changes to [32mMy Green pro

@mikederksen mikederksen reopened this Jul 6, 2017

@rkeithhill

This comment has been minimized.

Copy link
Contributor

commented Jul 6, 2017

There is a known issue with the ESC sequences used on the same line where input happens and you have PSReadline imported. You can read about the issue here #3867 and here PowerShell/PSReadLine#470.

On my Ubuntu VM, I split my prompt to two lines and put all the colored stuff on the first line and then I put an uncolored > on the second line. It's a workaround until PSReadline is fixed.

Also consider moving to beta.3. :-)

@jpogran

This comment has been minimized.

Copy link

commented Jul 11, 2017

FWIW, on beta.3 this is still an issue. This shows up on Terminal.app and iterm2 with just PSReadline and the example prompt in this issue. Things are resolved if PSReadline module is removed, but all history, tab completing, etc functionality is gone (obviously).

PowerShell v6.0.0-beta.3
Copyright (C) Microsoft Corporation. All rights reserved.

PS /Users/jpogran> function prompt {                                                                                                   >>     Write-Host "Something> "                                                                                                        >>     " "                                                                                                                             >> }
Something>
 Something>
Something>
 "foo"
foo
Something>
 $PSVersionTable

Name                           Value
----                           -----
PSVersion                      6.0.0-beta
PSEdition                      Core
GitCommitId                    v6.0.0-beta.3
OS                             Darwin 16.6.0 Darwin Kernel Version 16.6.0: Fri Apr 14 16:21:16 PDT 2017; root:xnu-3789.60.24~6/RELE...
Platform                       Unix
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0


Something>
 Something>

@electricduck

This comment has been minimized.

Copy link

commented Aug 14, 2017

This happens on Linux too, just to be aware

@lzybkr

This comment has been minimized.

Copy link
Member

commented Aug 14, 2017

This is PowerShell/PSReadLine#468

I'm quite far along with the real fix - the ansi_render branch fixes the problem on Windows, but that branch still has some Windows specific code, I haven't finished porting everything yet.

@stuartleeks

This comment has been minimized.

Copy link

commented Nov 22, 2017

I'm not clear why this issue is closed.

In PowerShell on Windows (either powershell or pwsh) I can set the prompt with function prompt {Write-Host "myprompt>" -NoNewline; ' '} and the prompt that I will get is myprompt>

If I try this on Linux then then prompt that I get is myprompt> myprompt>

Since this is a difference in behaviour from the full powershell, and pwsh on Windows is consistent with the previous behaviour, this feels like it is broken on Linux/macOS.

Looking back at the thread it seems that @lzybkr is having to work around this in PSReadline. I'm a big fan of posh-git and want to set this up for pwsh in WSL and on native Linux, but not fixing this in pwsh means that any libs that depend on the pre-existing behaviour are broken on pwsh on mac/Linux :-(

Look forward to your thoughts

@markekraus

This comment has been minimized.

Copy link
Collaborator

commented Nov 22, 2017

@stuartleeks I assume it was closed because this is a PSReadline issue, and not PowerShell issue:

Remove-Module PSReadLine
function prompt  
{  
    $color = Get-Random -Min 1 -Max 16  
    Write-Host ("PS " + $(Get-Location) +">") -NoNewLine -ForegroundColor $Color  
    return " "  
}

That's the example in about_Prompts. If you were to run that without Remove-Module PSReadLine, you get the double prompt behavior. But, without PSReadLine loaded, prompt works as expected.

@stuartleeks

This comment has been minimized.

Copy link

commented Nov 22, 2017

Ah, ok... I missed the initial PSReadline reference - thanks :-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.