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

Corrupted prompt after certain commands #352

Closed
2 tasks done
peter-dolkens opened this issue Jan 17, 2021 · 23 comments
Closed
2 tasks done

Corrupted prompt after certain commands #352

peter-dolkens opened this issue Jan 17, 2021 · 23 comments
Labels
🐛 bug Something isn't working powershell

Comments

@peter-dolkens
Copy link

peter-dolkens commented Jan 17, 2021

Prerequisites

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

Description

When running grep, the prompt immediately afterwards is corrupted - not sure if there's other commands with similar issues yet.

Environment

  • Oh my Posh version: v3.75.0
  • Theme: custom - see below
  • Operating System: Win10 19042.746
  • Shell: Powershell 7.1.0
  • Terminal: Console Host , Windows Terminal 1.4.3243.0
  • Grep: GNU grep 3.1 (bundled with git 2.29.2.windows.2)

Steps to Reproduce

  1. Run echo hi | grep hi
  2. Hear audible bell, and observe corrupted prompt

Expected behavior: [What you expected to happen]

  1. Uncorrupted prompt

Actual behavior: [What actually happened]

image

Notes

  • I also tested without the special characters in my config file (as they only work in Windows Terminal) - same issue

Theme

{
    "final_space": false,
    "console_title": true,
    "console_title_style": "template",
    "console_title_template": "[{{.Env.Host}}] {{.Path}}",
    "blocks": [
        {
            "type": "prompt",
            "alignment": "left",
            "horizontal_offset": 0,
            "vertical_offset": 0,
            "segments": [
            

                {
                    "type": "exit",
                    "style": "plain",
                    "foreground": "#16c50c",
                    
                    "properties": {
                        "display_exit_code": false,
                        "always_enabled": true,
                        "error_color": "#e74856",
                        "prefix": "\u279C"
                    }
                },
                
                {
                    "type": "path",
                    "style": "plain",
                    "foreground": "#61d6d6",
                    "properties": {
                        "style": "folder"
                    }
                },
                
                {
                  "type": "git",
                  "style": "plain",
                  "foreground": "#e74856",
                  
                  "properties": {
                  
                  "prefix": "<#3b78ff>git:(</>",
                  "postfix": "<#3b78ff>) </>",
                    "local_changes_icon": "\u2717",
                    "display_status": false,
                    "display_status_detail": false,
                    "display_stash_count": false,
                    "display_upstream_icon": false,
                    "working_color": "#e74856",
                    "branch_icon": ""
                  }
                }
            ]
        }
    ]
}
@JanDeDobbeleer
Copy link
Owner

It is known that the git tooling in Windows can cause the entire terminal rendering to act weird due to overriding certain variables. I'll check, but can't promise this is something that can be fixed within oh-my-posh.

@lnu
Copy link
Contributor

lnu commented Jan 17, 2021

I did the same test with starship and it works. Maybe we can check what they do on their side with the powershell prompt.

@JanDeDobbeleer
Copy link
Owner

JanDeDobbeleer commented Jan 17, 2021

I did the same test with starship

@lnu could you also reproduce the issue for OMP?

@lnu
Copy link
Contributor

lnu commented Jan 17, 2021

And..... if I take the powershell bootstrap from starship and sets omp it works

image

@JanDeDobbeleer
Copy link
Owner

JanDeDobbeleer commented Jan 17, 2021

With all due respect, this works with the out of the box version of OMP on my Windows machine as well, albeit PowerShell 5:

image

@lnu
Copy link
Contributor

lnu commented Jan 17, 2021

powershell core 7.1.1 here in windows terminal

@lnu
Copy link
Contributor

lnu commented Jan 17, 2021

Only added $omp and $config and changed the starship calls.

function global:prompt {
   $omp="D:\dev\temp\oh-my-posh3\src\oh-my-posh3.exe"
   $config="D:\tools\ohmyposh\jandedobbeleer_custom.omp.json"
   $origDollarQuestion = $global:?
   $origLastExitCode = $global:LASTEXITCODE

   $out = $null
   # @ makes sure the result is an array even if single or no values are returned
   $jobs = @(Get-Job | Where-Object { $_.State -eq 'Running' }).Count

   $env:PWD = $PWD
   $current_directory = (Convert-Path -LiteralPath $PWD)
   
   # Whe start from the premise that the command executed correctly, which covers also the fresh console.
   $lastExitCodeForPrompt = 0

   # Save old output encoding and set it to UTF-8
   $origOutputEncoding = [Console]::OutputEncoding
   [Console]::OutputEncoding = [System.Text.Encoding]::UTF8
   if ($lastCmd = Get-History -Count 1) {
       # In case we have a False on the Dollar hook, we know there's an error.
       if (-not $origDollarQuestion) {
           # We retrieve the InvocationInfo from the most recent error.
           $lastCmdletError = try { Get-Error |  Where-Object { $_ -ne $null } | Select-Object -expand InvocationInfo } catch { $null }
           # We check if the las command executed matches the line that caused the last error , in which case we know
           # it was an internal Powershell command, otherwise, there MUST be an error code.
           $lastExitCodeForPrompt = if ($null -ne $lastCmdletError -and $lastCmd.CommandLine -eq $lastCmdletError.Line) { 1 } else { $origLastExitCode }
       }

       $duration = [math]::Round(($lastCmd.EndExecutionTime - $lastCmd.StartExecutionTime).TotalMilliseconds)
       # & ensures the path is interpreted as something to execute
       $out = @(&starship prompt "--path=$current_directory" --status=$lastExitCodeForPrompt --jobs=$jobs --cmd-duration=$duration)
       $out = @(&$omp "--config=$config" --error=$lastExitCodeForPrompt "--pwd=$current_directory" "--pswd=$current_directory" --execution-time=$duration)
   } else {
       #$out = @(&starship prompt "--path=$current_directory" --status=$lastExitCodeForPrompt --jobs=$jobs)
       $out = @(&$omp "--config=$config" --error=$lastExitCodeForPrompt "--pwd=$current_directory" "--pswd=$current_directory")
   }
   # Restore old output encoding
   [Console]::OutputEncoding = $origOutputEncoding

   # Convert stdout (array of lines) to expected return type string
   # `n is an escaped newline
   $out -join "`n"

   # Propagate the original $LASTEXITCODE from before the prompt function was invoked.
   $global:LASTEXITCODE = $origLastExitCode

   # Propagate the original $? automatic variable value from before the prompt function was invoked.
   #
   # $? is a read-only or constant variable so we can't directly override it.
   # In order to propagate up its original boolean value we will take an action
   # which will produce the desired value.
   #
   # This has to be the very last thing that happens in the prompt function
   # since every PowerShell command sets the $? variable.
   if ($global:? -ne $origDollarQuestion) {
       if ($origDollarQuestion) {
            # Simple command which will execute successfully and set $? = True without any other side affects.
           1+1
       } else {
           # Write-Error will set $? to False.
           # ErrorAction Ignore will prevent the error from being added to the $Error collection.
           Write-Error '' -ErrorAction 'Ignore'
       }
   }
}

@JanDeDobbeleer
Copy link
Owner

@lnu @peter-dolkens so this is confirmed to only break in PSCore, for me that's 7.2:

image

There's a reason I went with the current invocation technique as I did use a starship like invocation which ruined the rendering on certain machines. This solution made it work everywhere. There's something in the output of that command which ruins the rendering, and the next ENTER fixes that once again. I'm in favor of figuring out what exactly is the difference and adding a one-liner fix to the current invocation rather than opening that can of worms again (I was happy I was able to close it 5 months ago).

@JanDeDobbeleer JanDeDobbeleer added 🐛 bug Something isn't working powershell labels Jan 17, 2021
@JanDeDobbeleer
Copy link
Owner

@lnu @peter-dolkens fixed it. I believe, at the time, I was battling combination of issues which led me down this path. I was able to directly invoke OMP now without the entire process bootstrap leading to a correctly displayed prompt in all circumstances. I'm going to test a little bit more but you should see a fix ASAP.

JanDeDobbeleer added a commit that referenced this issue Jan 17, 2021
@peter-dolkens
Copy link
Author

Great work - @JanDeDobbeleer!

Was a low-priority bug for me, but thought I had an easy enough repro for it to be worthwhile logging =D

@JanDeDobbeleer
Copy link
Owner

I think I tested everything, also simplified the module in the meantime. Hope I didn't break anything for someone else 🤞🏻

@kidchenko-jq-tw
Copy link

kidchenko-jq-tw commented Apr 2, 2021

After run any git command it also breaks for me :/

Terminal: Hyper

image

@JanDeDobbeleer
Copy link
Owner

@kidchenko-tw That's likely going to be a Hyper issue, allow me to check. Is everything up to date?

@JanDeDobbeleer
Copy link
Owner

@kidchenko-tw I can't reproduce this

@kidchenko-jq-tw
Copy link

Yes, everything is up to date, I will double check, I remember I already had this problem in the past... But can't remember the fix,

@JanDeDobbeleer
Copy link
Owner

@kidchenko-tw can you copy the prompt here? If you pipe the out of oh-my-posh with your config I can have a look. Maybe there's a character the ruins the ANSI sequences?

@kidchenko-jq-tw
Copy link

Oh my Posh version: 3.131.0
Operating System: Windows Server 2016 DataCenter
Shell: Powershell 5.1.14393.3866
Terminal: Hyper 3.0.2

@kidchenko-jq-tw
Copy link

Sorry, but I didn't understand, how can I pipe the out? Is this what you want?

My prompt before:
image

Copy: ~ 

My after:
image

?[38;2;255;255;255mD:\thoughtworks\test ?[0m?[38;2;194;194;6m  ≢ ?[0m?[38;2;181;181;13m  ?[0m?[K
?[K?[38;2;0;122;204m ?[0m?[K?[0m ?[K

@JanDeDobbeleer
Copy link
Owner

JanDeDobbeleer commented Apr 7, 2021

Aha git diff. That's going to be the culprit. Did you update git as well?

@kidchenko-jq-tw
Copy link

Just updated my git

git version 2.31.1.windows.1

Still same problem :/

@JanDeDobbeleer
Copy link
Owner

I'll try to reproduce this.

@JanDeDobbeleer
Copy link
Owner

@kidchenko-tw I just tried to reproduce this again, doesn't happen. Did you add a specific pager or something else in your git config that influences git diff?

@JanDeDobbeleer JanDeDobbeleer mentioned this issue Oct 5, 2021
1 task
Copy link

This issue has been automatically locked since there has not been any recent activity (i.e. last half year) after it was closed. It helps our maintainers focus on the active issues.
If you have found a problem that seems similar, please open a discussion first, complete the body with all the details necessary to reproduce, and mention this issue as reference.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Apr 22, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
🐛 bug Something isn't working powershell
Projects
None yet
Development

No branches or pull requests

4 participants