question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Syntax error prompt does not work when prompt function is defined in profile or launch command and uses Write-Host

See original GitHub issue

Environment

# Tested in 3 environments

PS version: 5.1.18362.628
PSReadline version: 2.0.1
os: 10.0.18362.1 (WinBuild.160101.0800)
PS file version: 10.0.18362.1 (WinBuild.160101.0800)
HostName: ConsoleHost (Windows Terminal)
BufferWidth: 229
BufferHeight: 55

PS version: 7.0.1
PSReadline version: 2.0.0
os: 10.0.18362.1 (WinBuild.160101.0800)
PS file version: 7.0.1.0
HostName: ConsoleHost (Windows Terminal)
BufferWidth: 229
BufferHeight: 55

PS version: 7.0.0
PSReadline version: 2.0.0
os: Linux 4.4.0-18362-Microsoft #476-Microsoft Fri Nov 01 16:53:00 PST 2019 x86_64 x86_64 x86_64 GNU/Linux
PS file version: 7.0.0.0
HostName: ConsoleHost
BufferWidth: 229
BufferHeight: 55

Exception report

None

Steps to reproduce

Write a prompt function that uses Write-Host to construct the prompt. Place the prompt function in your PowerShell profile or in the -Command option script block when launching PowerShell. Doing so will cause PSReadLine’s syntax error prompt notification color to not trigger.

# C:\users\<username>\Documents\WindowsPowerShell\profile.ps1

# Prompt function that uses Write-Host to construct prompt
function prompt {
    Write-Host 'myprompt' -NoNewLine
    '> ' # Need to also return string so PowerShell doesn't auto add "PS>"
}

Expected behavior

PS C:\> powershell.exe -NoLogo
PS C:\> function prompt { Write-Host 'myprompt' -NoNewLine; '> ' }
myprompt> } # syntax error prompt works

image

Actual behavior

PS C:\> powershell.exe -NoExit -Command { function prompt { Write-Host 'myprompt' -NoNewLine; '> ' } }
myprompt> } # last char of prompt does not change color with syntax error

image

All Examples

image

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:8 (7 by maintainers)

github_iconTop GitHub Comments

4reactions
lzybkrcommented, May 21, 2020

Add the following to your profile - I think it should fix it:

Set-PSReadLineOption -PromptText '> '

Before v2, PSReadLine relied on screen scraping to implement the error prompt coloring. With the move to a more portable PSReadLine, screen scraping was no longer an option.

If you have a “pure” prompt, PSReadLine can infer what text needs to change to properly change the color, but if your prompt is not pure, e.g. calls Write-Host, then you need to help PSReadLine out with this configuration option.

Note that if your prompt is extra fancy (like mine):

image

You can also specify the error coloring precisely by passing 2 strings instead of one - the first being the normal text, the second being the error text, so mine looks like:

$esc = [char]0x1b
$pc = [char]0xe0b0
$fg = "0"
$nbg = "8;2;95;158;160"
$ebg = "1"

Set-PSReadLineOption -PromptText (
    "$esc[4${nbg};3${fg}mPS$esc[4${fg};3${nbg}m$pc",
    "$esc[4${ebg};3${fg}mPS$esc[4${fg};3${ebg}m$pc"
)

Note how I use the background color instead of the foreground color.

1reaction
lzybkrcommented, May 22, 2020

@DecoyJoe - yeah, I see the same thing with the ? but hadn’t investigated. It looks like you need to set the output encoding to UTF8:

[Console]::OutputEncoding = [System.Text.Encoding]::UTF8

I thought PSReadLine was doing that for you, but maybe that changed. @daxian-dbw?

Read more comments on GitHub >

github_iconTop Results From Across the Web

about Prompts - PowerShell
The default prompt appears only when the Prompt function generates an error or doesn't return an object. The default PowerShell prompt is: Copy....
Read more >
it wont run because of a syntax error and i can't find it. sorry ...
it wont run because of a syntax error and i can't find it. sorry for ass code im new. (net script 2) :...
Read more >
Write-Host (Microsoft.PowerShell.Utility)
Starting in Windows PowerShell 5.0, Write-Host is a wrapper for Write-Information This allows you to use Write-Host to emit output to the information...
Read more >
Windows PowerShell: changing the command prompt
Just put the function prompt in your PowerShell profile ( notepad $PROFILE ), e.g.: function prompt {"PS: $(get-date)>"}. or colored:
Read more >
PowerShell prompt to continue execution of code
Another simple solution would be to use: Read-Host -Prompt "Press any key to continue or CTRL+C to quit" | Out-Null.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found