Skip to content

Guidance on shell integration + oh my posh seems to be incomplete/incorrect #847

@TheAtomicShoebox

Description

@TheAtomicShoebox

Type of issue

Code doesn't work

Feedback

I'm not sure if this fits under "Code doesn't work," or "missing information," so I tagged it as the former.

The shell integration documentation page has a block of code for PWSH shell integration with an additional block for integrating with oh my posh. Using the second configuration works only half-way, rendering previous lines correctly, but not the current line

As a minor note, the oh my posh block is configured incorrectly as pwsh instead of ps1 and doesn't show syntax highlighting:

Image

Now the meat is this:
This line is omitted in the second block:

$Global:__LastHistoryId = -1

In my testing, adding or removing that line did not change whether oh my posh actually works

What is actually happening:

Image
The current line prompt is incorrect, though running $Global:__OriginalPrompt.Invoke() will render the correct powerline

Image
The previous line prompt is rendered correctly.

Marks are emitting correctly, and using the defined actions (ctrl-up, ctrl-down) works as expected.

What should be happening:

Image
The current line powerline should render as expected

I am using the most recent oh my posh, with all default configurations, and the "CaskaydiaCove NF" font accessible through oh-my-posh font install

My Microsoft.PowerShell_profile.ps1:

# Initialize Oh My Posh
oh-my-posh init pwsh | Invoke-Expression

# then stash away the prompt() that oh-my-posh sets
$Global:__OriginalPrompt = $function:Prompt

# $Global:__LastHistoryId = -1 # <== left out in the oh my posh section

function $Global:__Terminal-Get-LastExitCode {
    if ($? -eq $True) { return 0 }
    $LastHistoryEntry = $(Get-History -Count 1)
    $IsPowerShellError = $Error[0].InvocationInfo.HistoryId -eq $LastHistoryEntry.Id
    if ($IsPowerShellError) { return -1 }
    return $LastExitCode
}

function prompt {
    # First, emit a mark for the _end_ of the previous command.

    $gle = $(__Terminal-Get-LastExitCode);
    $LastHistoryEntry = $(Get-History -Count 1)
    # Skip finishing the command if the first command has not yet started
    if($Global:__LastHistoryId -ne 1) {
        if($LastHistoryEntry.Id -eq $Global:__LastHistoryId) {
            # Don't provide a command line or exit code if there was no history entry (e.g., ctrl+c, enter on no command)
            $out += "`e]133;D`a"
        } else {
            $out += "`e]133;D;$gle`a"
        }
    }

    $loc = $($executionContext.SessionState.Path.CurrentLocation);

    # Prompt started
    $out += "`e]133;A$([char]07)";

    # CWD
    $out += "`e]9;9;`"$loc`"$([char]07)";

    # (Add the original prompt here, does not work for current line)
    $out += $Global:__OriginalPrompt.Invoke();

    # Prompt ended, Command started
    $out += "`e]133;B$([char]07)";

    $Global:__LastHistoryId = $LastHistoryEntry.Id

    return $out
}

The version used in the 'what should be happening' screenshot:

# Initialize Oh My Posh
oh-my-posh init pwsh | Invoke-Expression

Page URL

https://learn.microsoft.com/en-us/windows/terminal/tutorials/shell-integration

Content source URL

https://github.com/MicrosoftDocs/terminal/blob/main/TerminalDocs/tutorials/shell-integration.md

Author

@zadjii-msft

Document Id

407ac403-c6a4-bc7b-b93e-ad9d6ee480a2

Metadata

Metadata

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions