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

Falls back to standard prompt after failed command #236

Closed
2 tasks done
firlus opened this issue Dec 14, 2020 · 10 comments
Closed
2 tasks done

Falls back to standard prompt after failed command #236

firlus opened this issue Dec 14, 2020 · 10 comments
Labels
🐛 bug Something isn't working powershell

Comments

@firlus
Copy link

firlus commented Dec 14, 2020

Prerequisites

  • I have read and understand the CONTRIBUTING guide
  • I looked for duplicate issues before submitting this one

Description

When I type in a command that e.g. does not exists, the prompt shows only PS> after that until I open a new console window.

Environment

  • Oh my Posh version: 3
  • Theme: Agnoster
  • Operating System: Windows 10
  • Shell: Powershell
  • Terminal: Windows Terminal

Steps to Reproduce

This is my $PROFILE:

[ScriptBlock]$Prompt = {
    $lastCommandSuccess = $?
    $realLASTEXITCODE = $global:LASTEXITCODE
    $errorCode = 0
    if ($lastCommandSuccess -eq $false) {
        #native app exit code
        if ($realLASTEXITCODE -is [int] -and $realLASTEXITCODE -gt 0) {
            $errorCode = $realLASTEXITCODE
        }
        else {
            $errorCode = 1
        }
    }

    $executionTime = -1
    $history = Get-History -ErrorAction Ignore -Count 1
    if ($null -ne $history) {
        $executionTime = $history.Duration.TotalMilliseconds
    }

    $startInfo = New-Object System.Diagnostics.ProcessStartInfo
    $startInfo.FileName = "C:\tools\oh-my-posh.exe"
    $cleanPWD = $PWD.ProviderPath.TrimEnd("\")
    $startInfo.Arguments = "-config=""$env:USERPROFILE\.poshthemes\agnoster.omp.json"" -error=$errorCode -pwd=""$cleanPWD"" -execution-time=$executionTime"
    $startInfo.Environment["TERM"] = "xterm-256color"
    $startInfo.CreateNoWindow = $true
    $startInfo.StandardOutputEncoding = [System.Text.Encoding]::UTF8
    $startInfo.RedirectStandardOutput = $true
    $startInfo.UseShellExecute = $false
    if ($PWD.Provider.Name -eq 'FileSystem') {
      $startInfo.WorkingDirectory = $PWD.ProviderPath
    }
    $process = New-Object System.Diagnostics.Process
    $process.StartInfo = $startInfo
    $process.Start() | Out-Null
    $standardOut = $process.StandardOutput.ReadToEnd()
    $process.WaitForExit()
    $standardOut
    $global:LASTEXITCODE = $realLASTEXITCODE
    #remove temp variables
    Remove-Variable realLASTEXITCODE -Confirm:$false
    Remove-Variable lastCommandSuccess -Confirm:$false
}
Set-Item -Path Function:prompt -Value $Prompt -Force

Expected behavior: See nice prompt again (Even with some form of error sign?)

Actual behavior: Falls back to PS>-prompt

It seems like it fails to run something in the $PROFILE after a failed command, but this is just a guess...

@firlus
Copy link
Author

firlus commented Dec 14, 2020

I was wrong about when it happens, it happens even after a successful command.

@firlus
Copy link
Author

firlus commented Dec 14, 2020

I did some debugging, $history.Duration.TotalMilliseconds was not set (but $history was for some weird reason, making the cli parameter for oh-my-posh null which led to it not executing. I edited my config so that it ignores the history, now it works.

I don't know whether this is a problem on my system...

@JanDeDobbeleer
Copy link
Owner

JanDeDobbeleer commented Dec 14, 2020

I will however take that into consideration and adjust the code to not fail on that.
@firlus what did you change exactly? Maybe I can use your working setup as an example.

@mendoncaftw
Copy link

I have the exact same result, how can I go about debugging the profile script?

@JanDeDobbeleer
Copy link
Owner

I have the exact same result, how can I go about debugging the profile script?

I will create a small fix for this based on @firlus fix.

JanDeDobbeleer added a commit that referenced this issue Dec 14, 2020
@JanDeDobbeleer
Copy link
Owner

JanDeDobbeleer commented Dec 14, 2020

@mendoncaftw @firlus I did a quick commit which could fix it, would be great if you can confirm this fixes the issue. Still wonderding why it doesn't have a history with timings, but that's something else.

@JanDeDobbeleer JanDeDobbeleer added 🐛 bug Something isn't working powershell labels Dec 14, 2020
@mendoncaftw
Copy link

@JanDeDobbeleer I looked at your PR and changed the script on the profile but I continued to have the default PS> prompt.
I installed through scoop, noticed I didn't have access to any 'Get-PoshThemes' and 'Set-PoshPrompt'. I noticed those functions being defined in the same file next to the profile instructions so I copied that file over to my profile and could access the functions but now obviously don't have the themes and expected folders.
I am going to try and install through Powershell Prerelease stuff.

@JanDeDobbeleer
Copy link
Owner

Correct, Scoop only installs the binaries. Although the setup instructions do explain what to alter when installing with Scoop, if you're not proficient in Powershell and only want to go for a Powershell integration, the module is preferred.

@firlus
Copy link
Author

firlus commented Dec 14, 2020

Wow, that was quick, thanks for the help!
Your fix worked, it was basically what I also tried to do to fix the problem.

Thanks for your great tool, I will try to look into creating an own segment since I am trying to learn Go right now and I really love this project!

@mendoncaftw
Copy link

mendoncaftw commented Dec 14, 2020

Well, you also helped me because following all the instructions about Powershell without mixing in scoop also worked for me.
Thank you @JanDeDobbeleer for your help and amazing work. 👍🏻

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🐛 bug Something isn't working powershell
Projects
None yet
Development

No branches or pull requests

3 participants