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

Inconsistent behavior between executing directly from the command line and from inside a KeyHandler. #3826

Closed
3 tasks done
LittleNewton opened this issue Oct 7, 2023 · 3 comments
Labels
Resolution-By Design The behavior is by design.

Comments

@LittleNewton
Copy link

LittleNewton commented Oct 7, 2023

Prerequisites

  • Write a descriptive title.
  • Make sure you are able to repro it on the latest released version
  • Search the existing issues, especially the pinned issues.

Exception report

The code in question is as follows:

 powershell
Set-PSReadLineKeyHandler -Chord Ctrl+k `
                         -BriefDescription ClearHost `
                         -LongDescription "Clear the console host screen" `
                         -ScriptBlock {
    Clear-Host
}

I intend for the action to clear all command history upon pressing Ctrl+k. However, it should retain the prompt to avoid an unpleasant visual and lack of command indication. When executing Clear-Host directly from the command line, it operates as intended. Yet, when invoking the same through the aforementioned KeyHandler, it wipes the prompt as well, leaving the screen entirely blank.

Screenshot

After pressing Ctrl+k
image

Using Clear-Host

image

image

Environment data

PS Version: 7.3.7
PS HostName: ConsoleHost (Windows Terminal)
PSReadLine Version: 2.3.4
PSReadLine EditMode: Windows
OS: 10.0.19041.3324 (WinBuild.160101.0800)
BufferWidth: 135
BufferHeight: 45

Steps to reproduce

  1. Install oh-my-posh using winget.
winget install JanDeDobbeleer.OhMyPosh -s winget
  1. Using paradox theme.

How to set the theme? As following,

  • $PS code $PROFILE
  • Add a new line: oh-my-posh init pwsh --config $env:POSH_THEMES_PATH\paradox.omp.json | Invoke-Expression
  1. Add KeyHandler
Set-PSReadLineKeyHandler -Chord Ctrl+k `
                         -BriefDescription ClearHost `
                         -LongDescription "Clear the console host screen" `
                         -ScriptBlock {
    Clear-Host
  1. Try Ctrl+k and Clear-Host

Just compare the different behavior.

Expected behavior

I wish Ctrl k keyHandler has the same behavior as type Clear-Host.

Actual behavior

Ctrl k will clear all characters in the terminal.

@LittleNewton
Copy link
Author

After pressing Ctrl+k
image

Using Clear-Host

image

image

@microsoft-github-policy-service microsoft-github-policy-service bot added the Needs-Triage 🔍 It's a new issue that core contributor team needs to triage. label Oct 7, 2023
@daxian-dbw
Copy link
Member

This is by design. If you want to have a custom key binding that runs clear-host, then your key binding handler needs to take care of writing out the prompt message. PSReadLine has a default key binding Ctrl+l for clearing screen, you can use that instead of having your own custom binding for it.

PS:1> Get-PSReadLineKeyHandler -Chord "ctrl+l"

Miscellaneous functions
=======================

Key    Function    Description
---    --------    -----------
Ctrl+l ClearScreen Clear the screen and redraw the current line at the top of the screen

@microsoft-github-policy-service microsoft-github-policy-service bot removed the Needs-Triage 🔍 It's a new issue that core contributor team needs to triage. label Oct 9, 2023
@daxian-dbw daxian-dbw added the Resolution-By Design The behavior is by design. label Oct 9, 2023
@thegraffix
Copy link

thegraffix commented Jul 25, 2024

FWIW, one behavioral difference (that's important to some, but not all) between Clear-Host and the PSReadLineKeyHandler function ClearScreen is that Clear-Host clears the scrollback buffer. The ClearScreen function executes a carriage return everytime it's executed, resulting in a console that looks like this after doing CTRL+L 3x and scrolling up:

image

It would be cool if the ClearScreen function took into account the OS/environment (e.g. Windows/conhost has always cleared the scrollback buffer, whereas Linux/bash traditionally has not).



But this works just as well:

$args = @{
    BriefDescription = 'ClearHost'
    Chord = 'Ctrl+k'
    Description = 'Clear the console host screen'
    ScriptBlock = {
        [Microsoft.PowerShell.PSConsoleReadLine]::RevertLine()
        [Microsoft.PowerShell.PSConsoleReadLine]::AcceptLine()
        Clear-Host
    }
}
Set-PSReadLineKeyHandler @args

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Resolution-By Design The behavior is by design.
Projects
None yet
Development

No branches or pull requests

3 participants