PSReadLine icon indicating copy to clipboard operation
PSReadLine copied to clipboard

Windows PSReadline stores and restores multiple bits of console state before/after commands

Open DHowett-MSFT opened this issue 6 years ago • 10 comments

Environment data

PSReadline version: 2.0.0-beta3
Windows

This applies to all extant versions of Powershell.

Steps to reproduce or exception report

There are a number of user preferences that are PSReadline stores and restores across command executions; it is arguable as to whether it is PSReadline's job to do so.

  • [ ] the codepage
    • this is the cause of font flickering across commands in the windows console (still!)
    • mitigation: none; i'm curious as to why it changes the codepage to begin with?
  • [ ] the index of the currently-set foreground and background colors
    • when using a modern terminal (or the terminal options in Windows 19H1), this causes colored lines to streak across the window because the "default colors" are not representable through color indices.
    • mitigation: CSI 0 m will reset the color to whatever the user requested as defaults
  • ~[ ] the height of the legacy cursor (microsoft/terminal#1145)~ <update: this is not a PSReadLine issue, see microsoft/terminal#4124>
    • ~mitigation: use the standard VT sequences for cursor shaping~
  • [ ] the cursor position, in some limited cases.
    • it would be better to (and this is probably a breaking change) conform to the *NIX model where you're not easily allowed to know where the cursor is

Most of these things happen here.

https://github.com/PowerShell/PSReadLine/blob/f2f553156063a8635b9d4882a878a828365cdd99/PSReadLine/ReadLine.cs#L436-L452

DHowett-MSFT avatar Jul 14 '19 00:07 DHowett-MSFT

codepage

Setting the codepage was the easy way to fix rendering issues with CJK characters. It seemed to work well.

If there is a more appropriate fix, details would be appreciated.

background colors

Try beta4, I think this was fixed.

height of cursor

Should be an easy fix here.

cursor position

You'll need to provide a repro.

lzybkr avatar Jul 16 '19 00:07 lzybkr

I investigated the font flickering issue on CJK locale machines. Here is what I found:

  1. CJK locale machines doesn't use the Raster font by default. So the fix in #771 doesn't help.

    • For Chinese-simplified OS, the code page is 936 (Chinese Simplified (GB2312)). The corresponding default font is 新宋体 (Font Family: 54)
    • For Chinese-traditional OS, the code page is 950 (Chinese Traditional (Big5)). The corresponding default font is 細明體 (Font Family: 54)
    • For Japanese OS, the code page is 932 (Japanese (Shift-JIS)). The corresponding default font is MS ゴシック (Font Family: 54)
    • Note Font Family: 54 is not that special. Consolas and Lucida Sans Typewriter also have Font Family 54, and it could be the same for many other fonts too.
  2. The font flickers because: when setting OutputEncoding to utf8, several more fonts will become available in the console host (Properties -> Font) comparing to when using the default OutputEncoding. For example, on the Chinese-simplified OS, Consolas, Courier New and Lucida Console become available after setting Console.OutputEncoding = Encoding.UTF8.

    • when the user chooses one of the newly available font, the font will be restored back to the default font when Console.OutputEncoding is restored to the initial default encoding, because the chosen font is unavailable when using the default encoding (default code page).
    • when the user chooses a font that's available for both the default and utf8 code pages, the font flickering won't happen because the font won't be restored.

I'm not sure how this should be fixed. For a workaround, we can check whether the initial encoding is one of the default CJK encoding's, and if so not change the output encoding. However, it seems setting code page to utf8 was to fix rendering issues with CJK characters, and not changing the output encoding sounds contradict to that. @lzybkr Could you please explain more about why PSReadLine changes the codepage to begin with?

daxian-dbw avatar Jan 04 '20 01:01 daxian-dbw

@daxian-dbw - I'm not sure there's much more to say about why. I didn't do a lot of investigation - I was just guessing at a possible fix - but the change seemed to help according to some users.

lzybkr avatar Jan 04 '20 17:01 lzybkr

@lzybkr Can you please point me to some of the issues that got fixed or mitigated by changing/restoring the code page? That would be helpful.

daxian-dbw avatar Jan 05 '20 06:01 daxian-dbw

There may be other issues, but https://github.com/PowerShell/PSReadLine/issues/289 is one I think.

lzybkr avatar Jan 06 '20 04:01 lzybkr

the height of the legacy cursor (microsoft/terminal#1145)

I want to update that microsoft/terminal#1145 is not a PSReadLine issue, but a Windows Terminal or .NET Core issue. The cursor shape is reverted when hiding the cursor and then showing it by setting Console.CursorVisible. https://github.com/microsoft/terminal/issues/4124 is logged for that. Related issue in PSReadLine repo is https://github.com/PowerShell/PSReadLine/issues/903

daxian-dbw avatar Jan 06 '20 19:01 daxian-dbw

For Windows users with CJK as default display language, there may be a workaround to the font flickering issue: unchecking the Unicode UTF-8 option in the language settings.

daxian-dbw avatar Mar 20 '20 15:03 daxian-dbw

Hi, Mates, I have to say this issue has existed for too looooong time since powershell-7 release, even the latest stable release V-7.1.3, I have to fix it by workaround, really a big surprise!

ToMakeSense avatar Mar 29 '21 02:03 ToMakeSense

What should we do for use vertical line always?

mammadkoma avatar Jan 21 '22 12:01 mammadkoma

Storing and restoring OutputEncoding also causes the chcp command to seemingly not work as expected. See the video record in https://github.com/PowerShell/PSReadLine/issues/4027.

daxian-dbw avatar May 31 '24 18:05 daxian-dbw