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

1.0 beta prompt incorrect with conemu #592

Closed
spooky opened this issue Jun 29, 2018 · 42 comments
Closed

1.0 beta prompt incorrect with conemu #592

spooky opened this issue Jun 29, 2018 · 42 comments

Comments

@spooky
Copy link

spooky commented Jun 29, 2018

Because of the ssh-agent/windows update problems, I've updated to 1.0.0 beta posh-git but that seems to break the prompt integration with conemu (technically, cmder version 1.3.6 which uses conemu 180528).

The git part is placed at the end (after lambda sign in the second line) instead of after the direcotry in the first line (as it was with 0.7.3)

@dahlbyk
Copy link
Owner

dahlbyk commented Jun 29, 2018

Interesting, I wouldn't expect the console host to have any impact, assuming it's correctly handling ANSI control sequences. Can you share a screenshot, or text that demonstrates what's happening? Are you customizing $GitPromptSettings at all?

@dahlbyk
Copy link
Owner

dahlbyk commented Jun 29, 2018

Or are you using oh-my-posh?

@spooky
Copy link
Author

spooky commented Jun 30, 2018

using just posh-git, no customizations on the $GitPromptSettings. I should be able to share a screenshot around monday.

@rkeithhill
Copy link
Collaborator

Can you post the output you get from running $PoshGitLastPrompt?

@spooky
Copy link
Author

spooky commented Jul 3, 2018

image

As for the output - runnign Write-Host $PoshGitLastPrompt outputs nothing.

@rkeithhill
Copy link
Collaborator

rkeithhill commented Jul 4, 2018

Have you defined your own prompt function? If you're using the default posh-git prompt and you have 1.0.0-beta2 installed, you should see output from that variable like this:

~\GitHub\dahlbyk\posh-git [master ≡]> $PoshGitLastPrompt
~\GitHub\dahlbyk\posh-git $([char]27)[93m[$([char]27)[0m$([char]27)[96mmaster$([char]27)[0m$([char]27)[96m ≡$([char]27)[0m$([char]27)[93m]$([char]27)[0m>

You may have beta1 installed. You can check with this command (get-module posh-git).privatedata.psdata.prerelease. If you are still on beta1, run Update-Module posh-git -AllowPrerelease.

@spooky
Copy link
Author

spooky commented Jul 4, 2018

on beta2, output in 'normal' powershell terminal is $([char]27)[93m[$([char]27)[0m$([char]27)[96mmaster$([char]27)[0m$([char]27)[31m$([char]27)[49m +2$([char]27)[0m$([char]27)[31m$([char]27)[49m ~1$([char]27)[0m$([char]27)[31m$([char]27)[49m -0$([char]27)[0m$([char]27)[31m !$([char]27)[0m$([char]27)[93m]$([char]27)[0m> and the prompt is fine here.

Output in cmder still empty, still with the same issue.

@dahlbyk
Copy link
Owner

dahlbyk commented Jul 4, 2018

Does $GitPromptSettings.AnsiConsole return false? Try setting $GitPromptSettings.AnsiConsole = $true.

@dahlbyk
Copy link
Owner

dahlbyk commented Jul 4, 2018

FYI, we do check a ConEmu-specific environment variable for determining ANSI support:

[bool]$AnsiConsole = $Host.UI.SupportsVirtualTerminal -or ($Env:ConEmuANSI -eq "ON")

@CollinChaffin
Copy link

I have to disagree. This broke how many of us in the community use it in conjunction with conemu because you completely destroyed backward compatibility from .73 to v1.0. Anything we had in our profiles and prompts broke, and as a DEV myself for the life of me, I'm still trying to figure out why you went in that direction.

For instance, in .73 there was an actual example including various functions (like setting console titlebar for git etc.). In version 1.0, however, you've now removed the example.ps1 for the profile and literally more than QUADRUPLED your lines of code to over 174 lines of code+text for a function named "Add-PoshGitToProfile" that literally does exactly what this SINGLE LINE of code does, no more - no less:

if ((Test-Path -LiteralPath $profilePath) -and ((Get-AuthenticodeSignature $profilePath).SignerCertificate) -and (!((@(Get-Content $profilePath -ErrorAction SilentlyContinue) -match 'posh-git').Count -gt 0))) { Add-Content -LiteralPath $profilePath -Value "`nImport-Module '$modulePath'" -Encoding UTF8}

It seems the profile addition functions/example in v1 now boils down (and is very limited to) "add import-module to your profile"...which also could have at that point probably even been left out of the code and mentioned in the readme. :)

So my question is, are we really saying in today's age of PoSH that the future direction of v1.0 can't at least continue to provide the handful of really useful services that were present in v7.3 and prior? And if not, can I ask why and what the show-stopping hurdles are, so perhaps we can contribute solutions to help?

@rkeithhill
Copy link
Collaborator

I have to disagree.

What exactly in this issue are you disagreeing with? Also, Add-PoshGitToProfile was added in v0.7x, not v1.

can't at least continue to provide the handful of really useful services that were present in v7.3 and prior?

Which services (commands) are you referring to? We've mostly added functionality in v1:


CommandType     Name                                               Version    Source
-----------     ----                                               -------    ------
Function        Add-PoshGitToProfile                               1.0.0.0    posh-git
Function        Expand-GitCommand                                  1.0.0.0    posh-git
Function        Format-GitBranchName                               1.0.0.0    posh-git
Function        Get-GitBranchStatusColor                           1.0.0.0    posh-git
Function        Get-GitDirectory                                   1.0.0.0    posh-git
Function        Get-GitStatus                                      1.0.0.0    posh-git
Function        Get-PromptConnectionInfo                           1.0.0.0    posh-git
Function        Get-PromptPath                                     1.0.0.0    posh-git
Function        prompt                                             1.0.0.0    posh-git
Function        TabExpansion                                       1.0.0.0    posh-git
Function        tgit                                               1.0.0.0    posh-git
Function        Update-AllBranches                                 1.0.0.0    posh-git
Function        Write-GitBranchName                                1.0.0.0    posh-git
Function        Write-GitBranchStatus                              1.0.0.0    posh-git
Function        Write-GitIndexStatus                               1.0.0.0    posh-git
Function        Write-GitStashCount                                1.0.0.0    posh-git
Function        Write-GitStatus                                    1.0.0.0    posh-git
Function        Write-GitWorkingDirStatus                          1.0.0.0    posh-git
Function        Write-GitWorkingDirStatusSummary                   1.0.0.0    posh-git
Function        Write-Prompt                                       1.0.0.0    posh-git
Function        Write-VcsStatus                                    1.0.0.0    posh-git

The big exception is the removal of the ssh functionality to the new posh-sshell module.

@CollinChaffin
Copy link

Well, if I do nothing else but upgrade from .73 to v1 like the OP above, and from the old profile example had this in my profile after of course a simple load of the module:

$Global:GitPromptSettings.EnableWindowTitle = "posh~git ~ <$SessionTitleName>"

....DO SOME OTHER LOADING.....

Start-SshAgent -Quiet

I am greeted with (mind you this is simply the removal of that method being populated by default and nothing to do with conemu) when the ONLY thing I changed was having ONLY 0.7.3 installed/loading on first screenshot, and simply upgraded it to either published 1.0.0.beta2, or even unzipped current Sept 15th build as screenshot #2 here:

Version 0.7.3:
output1

Version 1.0.0.beta2 (either PSGallery or Github 9/15/2018)
output2

As you can see, the screen turns red with this dumbed-down profile with any/all the SSHd commands prior to v1 no longer functioning at all, but things like the EnableWindowTitle not being properly initialized any longer and throwing errors. With the exception of this specific test vm not having the ssh-agent in place (easily fixed) 0.7.3 is totally free of any big ugly red terminating errors.

I can also demonstrate loading the default Posh console without any profile at all, loading only psreadline v2 and posh-git beta v1.0, and issuing the same commands as in this screenshot and still receiving the same glaring terminating errors so it is easily reproducible and not in the way I am calling it which was originally straight out of the provided example profile info. And since these errors are present when run in a straight Posh console without conemu even in the picture, so the conemu issue are simply snowballing from here.

Again, my point was not to upset you I was just trying to approach it logically that if you can afford to write and add almost 200 lines of text that is not "reusable" in any way and is hard-coded for a single purpose of adding a single hard-coded line of text (import-module posh-git) into the users' profile, then with that throughput it might not be unrealistic to think we could maybe find some ways to guard against breaking the users' of this module's profiles a little bit better and address these reproducible issues that tend to be a real PITA when they pop up after a simple "update-module". :)

@rkeithhill
Copy link
Collaborator

my point was not to upset you

No worries and I'm not upset at all. Was just trying to figure out what you disagreed with. :-)

We plan to keep v0.7x around for a while so you don't have to upgrade to v1. Typically the nature of a major version change is indicative of one or more breaking changes. In order to do the following, it was necessary to make some breaking changes:

  • Support Windows 10 new ANSI seq in the update console host. This allows you to use 24 bit colors in your prompt.
  • Support PowerShell Core on all the platforms it supports.
  • Separate out the SSH functionality into its own module because the two sets of code are largely orthogonal.
  • Reorganize the GitPromptSettings to be more object-oriented and display color choices better and make it easier to customize the prompt without having to write your own custom prompt.

To fix your above errors - if you do not want to stick with v0.7x - install posh-sshell. That will provide you with the Start-SshAgent command. As for the EnableWindowTitle command, that changes to:

$GitPromptSettings.WindowTitle = "posh~git ~ <$SessionTitleName>"

Perhaps we should have a v0.7x to v1 migration guide?

@dahlbyk
Copy link
Owner

dahlbyk commented Sep 24, 2018

A migration guide would certainly be a good idea. I do wonder if we could include anything in v1 to deprecate rather than break folks using v0 in a way we can't cleanly upgrade. Two examples:

  1. Include placeholder functions for the SSH stuff, with an error directing to the new module.
  2. Adding getters/setters for the v0 settings properties that warn but pass through to the new location. Is that even possible with PS classes?

@rkeithhill
Copy link
Collaborator

rkeithhill commented Sep 30, 2018

The problem with adding the SSH "redirect info" commands is that when the user does install posh-sshell there will be conflicting commands. The one PowerShell picks will be based on import order. And when the user installs posh-sshell they'll have to use -AllowClobber.

Perhaps a better way to approach this is to use the CommandNotFoundAction e.g.:

image

The above is just a proof of concept. We'd have to add all the ssh commands to the list and also check to see if there's an existing CNFA handler and if so, then invoke it - returning whatever it returns.

@dahlbyk
Copy link
Owner

dahlbyk commented Sep 30, 2018

CNFA could work, though it's weird that it's not built to be composable.

Could the shims check if posh-sshell is installed and pass-thru? Could we avoid exporting the shims if we detect it's installed?

@rkeithhill
Copy link
Collaborator

Passthrus still incur the issues of duplicate commands i.e. import order and requires -AllowClobber. We can conditionally export the shims if posh-sshell is not installed.

Overall I think the CNFA approach is better - certainly much simpler to implement. The lack of composability is unfortunate but is no different than the TabExpansion function where we invoke the previous TabExpanion function (if there was one).

@JamesSkemp
Copy link

I'm running into a similar issue as @spooky was.

I was running Cmder on Windows 10, with PowerShell 6.0.1, posh-git 0.7.1.

Upgraded to PowerShell 6.1.0 and my prompt and output text ended up the same color. Upgraded to 0.7.3, noticed that posh-git 1.0.0 had PS Core support, so upgraded to 1.0.0 beta2.

image

I think these were the last debug steps before this issue swung on a slight tangent. :)

@rkeithhill
Copy link
Collaborator

What does your prompt function look like? Execute get-command prompt | % definition and list the output here, if you don't mind.

@JamesSkemp
Copy link

Sorry for the delay:

$realLASTEXITCODE = $LASTEXITCODE
$host.UI.RawUI.WindowTitle = Microsoft.PowerShell.Management\Split-Path $pwd.ProviderPath -Leaf
PrePrompt | Microsoft.PowerShell.Utility\Write-Host -NoNewline
CmderPrompt
Microsoft.PowerShell.Utility\Write-Host "`nλ " -NoNewLine -ForegroundColor "DarkGray"
PostPrompt | Microsoft.PowerShell.Utility\Write-Host -NoNewline
$global:LASTEXITCODE = $realLASTEXITCODE
return " "

@rkeithhill
Copy link
Collaborator

rkeithhill commented Oct 17, 2018

Hmm, that is not the default posh-git prompt function. I'm guessing some other module is providing your prompt function. It could be that it's hooking the posh-git prompt and then displaying it via the PostPrompt command invocation, which would explain why the posh-git status shows up after the Cmder prompt.

Do you get any source/module info if you execute: get-command PostPrompt?

@JamesSkemp
Copy link

CommandType Name Version Source
Function PostPrompt

I am using Cmder on top of ConEmu, but starting ConEmu directly results in the same behavior.

No changes to ConEmu/Cmder with the other changes I made.

@rkeithhill
Copy link
Collaborator

What are the contents of PostPrompt - run Get-Command PostPrompt | % Definition.

@JamesSkemp
Copy link

Hmm. It appears to be nothing?

@rkeithhill
Copy link
Collaborator

rkeithhill commented Oct 18, 2018

Thanks for checking. I have found that the prompt function is supplied by Cmder:

https://github.com/cmderdev/cmder/blob/c5a6afbf5b0d15703da4ecef75d0e845c9c27707/vendor/profile.ps1#L104-L126

The posh-git status summary is displayed by the call to checkGit() which in turn calls the posh-git command Write-VcsStatus.

One change in posh-git v1 is to support VT sequences for hosts that support it. What this effectively means is that the Write-VcsStatus command will not use Write-Host to write the git status on a host that supports VT. In that case, it returns a string that contains the git status decorated with VT esc sequences to color various part of the status. So checkGit() returns a string but that string isn't displayed until after the prompt function returns which is also after all the Write-Host calls.

Try setting $GitPromptSettings.AnsiConsole = $false and see if that clears this up. When this setting is $false, posh-git reverts back to using Write-Host.

@rkeithhill
Copy link
Collaborator

rkeithhill commented Oct 18, 2018

@dahlbyk If the above fix works, we could use the existence of $env:CMDER_ROOT to default AnsiConsole to $false. See https://github.com/cmderdev/cmder/blob/master/vendor/profile.ps1#L26

Or after looking at this: https://github.com/cmderdev/cmder/blob/master/vendor/profile.ps1#L69, maybe we should submit a PR to cmder to make it handle different versions of posh-git better. :-)

@JamesSkemp
Copy link

JamesSkemp commented Oct 18, 2018

Thanks for digging deep! That appears to semi-resolve the issue:

Now:
image

Before I decided to update software that wasn't broken ;) :
image

Only differences are the presence of the PS (which may be a PS 6.1.x thing - I'm going to spend some time looking into it) and that it's per-session/reverts back to true (which I'm assuming is to be expected / it's checked and set on every startup).

Your suggestions sound good to me; please let me know how/if I can assist in anyway!

Update

I don't work with PowerShell in any sort of advanced way, but for anyone in a similar situation, about_Prompts has information on customizing the PowerShell prompt.

@rkeithhill
Copy link
Collaborator

rkeithhill commented Oct 18, 2018

Thanks. We're getting closer. That PS > usually happens when the prompt function returns no string e.g.:

function prompt { Write-Host "$pwd> " -NoNewline }

or there is an error generated in the prompt:

function prompt { Write-Host "$pwd> " -NoNewline; throw "oops"; " " }

I suspect it is the latter because line 125 of the Cmder prompt function is return " ". Could you open up a fresh Cmder console and after you get your first prompt, execute: $error.

@JamesSkemp
Copy link

Cmder error. :)

Get-command : The term 'vim' is not recognized as the name of a cmdlet, function, script file, or operable program.
Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
At D:\Downloads\cmder\vendor\profile.ps1:44 char:5

  • Get-command -Name "vim" -ErrorAction Stop >$null
    
  • ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    
  • CategoryInfo : ObjectNotFound: (vim:String) [Get-Command], CommandNotFoundException
  • FullyQualifiedErrorId : CommandNotFoundException,Microsoft.PowerShell.Commands.GetCommandCommand

Unless you see something, I'll raise it as an error with them? https://github.com/cmderdev/cmder/blob/master/vendor/profile.ps1#L44

(I did upgrade Cmder to the current version from what I was at to see if that would resolve it; it did not.)

@rkeithhill
Copy link
Collaborator

Hmm, but looking at that code, they catch the error and that code isn't executed within the prompt so that shouldn't be the cause of this problem. Keep in mind that the $error collection records all errors whether or not they were caught. Was there only that one error?

@JamesSkemp
Copy link

Good to know.

It was the only error reported. Went into a git repo and ran a few other commands that might verify that:

image

@rkeithhill
Copy link
Collaborator

rkeithhill commented Oct 18, 2018

I think this is something we're doing in posh-git v1. I can replicate it with the following prompt function in a regular PowerShell console:

function prompt { $GitPromptSettings.AnsiConsole = 0; Write-VcsStatus; " " }

Now that I have a repro I should be able to figure out what is going on here. Hopefully we will have a beta3 out in a few weeks with a fix for this.

@JamesSkemp
Copy link

Excellent. Thank you again for helping to troubleshoot this!

@rkeithhill
Copy link
Collaborator

rkeithhill commented Oct 18, 2018

And voila, I have figured it out. When AnsiConsole is set to 0 (off), Write-VcsStatus is returning an empty string. Returning an empty (or no) string from a prompt will result in PowerShell displaying PS> because it thinks that prompt function failed and the user should be given some default prompt. But when you return a collection of strings - the empty string returned by Write-VcsStatus and the " " - PowerShell gets confused and again, displays the default prompt e.g.:

function prompt { Write-Host "$pwd> " -NoNewline; ''; ' ' }

Results in this prompt C:\Users\hillr\Git\KLM> PS>. The good news is that I've already queued up a PR to fix this (#617). So I think the only additional thing we need to add is a check for $env:CMDER_ROOT that disables the AnsiConsole setting by default.

@rkeithhill
Copy link
Collaborator

This should be fixed by #617 which should be available in 1.0.0-beta3.

@damiothar
Copy link

damiothar commented May 12, 2021

Hi, Im still having this issue:
image

I tried to set the cmder/config/user_profile.ps1 file like this, but it works only before the second prompt input

[ScriptBlock]$PostPrompt = {
    $GitPromptSettings.AnsiConsole = false;
}

Also it displays the "PS>" when Im not in a git repository
image

@rkeithhill could you reopen this issue please?

@Lsdenis
Copy link

Lsdenis commented May 27, 2021

Hey, everyone!
Same problem here:
image

$GitPromptSettings.AnsiConsole = $false fixes the problem, but only in current session.
$error returns same error as @JamesSkemp attached years ago.

@rkeithhill
Copy link
Collaborator

@dothar @Lsdenis You need to put that line in your profile script. From PowerShell, execute notepad $PROFILE.CurrentUserAllHosts if you see a line in that file like Import-Module posh-git put the line $GitPromptSettings.AnsiConsole = $false after it. If you don't see the import-module line in this file OR the file does not exist, then try notepad $PROFILE.CurrentUserCurrentHost.

Essentially you are looking for the Import-Module posh-git command in one of your profile scripts and you're going to add the line $GitPromptSettings.AnsiConsole = $false after the module is imported. Also note that you are setting that value to $false and NOT false.

@Lsdenis
Copy link

Lsdenis commented Jun 2, 2021

hey @rkeithhill!
thanks for the info, however it doesn't work for me :(

tried both CurrentUserAllHosts and CurrentUserCurrentHost.
Made some small investigations:

  • if you make changes here:
    image
    it doesn't help, with or without > $null, was lazy to google syntax :)
  • if you make changes like this:
    image
    works fine except first line :)

Hope that helps.

Got some errors during investigations, all of them were about 'AnsiConsole doesn't exists' :)

@rkeithhill
Copy link
Collaborator

rkeithhill commented Jun 6, 2021

Your Import-Git() function is only importing posh-git and executing your $GitPromptSettings.AnsiConsole = false IF you have a version of posh-git less than or equal to 0.6.1.20160330. That script will not execute for posh-git 1.0.0.

That is not a posh-git installed function. I have no idea what is up with that version check. Anyway, change that function to:

function Import-Git() {
    Import-Module posh-git > $null
    $GitPromptSettings.AnsiConsole = $false
    $true
}

And see how it behaves.

BTW that function comes from Cmder - https://github.com/cmderdev/cmder/blob/8d6ce3e4acf86c9418adf1114b0d261e71c5a511/vendor/psmodules/Cmder.ps1#L18-L29

@Lsdenis
Copy link

Lsdenis commented Jun 7, 2021

Thanks, @rkeithhill, works just as needed!

For anyone else who's using that snipper replace false with $false :)

@rkeithhill
Copy link
Collaborator

Thanks for catching that (copy/paste error). I updated the snippet above.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants