Psreadline: Cursor shape not preserved

Created on 24 Apr 2019  路  9Comments  路  Source: PowerShell/PSReadLine

PSReadLine resets the cursor shape, causing the user's setting to no longer be applied. If PSReadLine has a reason to change the cursor shape temporarily, it should restore the user's setting when it's done.
Migrated from Microsoft/console#409.
Duplicate: https://github.com/microsoft/terminal/issues/1145 -- it has detailed repro steps to show the problem.

Note

This is not related to the VI edit mode.
This only happens when setting cursor shape in the Terminal tab in console host.
When using Windows Terminal and set the cursor shape in Profile.json, the cursor doesn't get reset by PSReadLine.

Environment data

PS version: 7.0.0-rc.1
PSReadline version: 2.0.0-rc1
os: 10.0.18362.1 (WinBuild.160101.0800)
PS file version: 7.0.0.0
HostName: ConsoleHost
BufferWidth: 145
BufferHeight: 9001

Steps to reproduce or exception report

  1. Open pwsh in console host
  2. Right click on a console window and select Properties -> Terminal
  3. Set the Cursor Shape to anything other than "Use Legacy Style"
  4. Notice that the cursor shows up correctly but after hitting enter and typing a few characters, it returns to the default style
  5. Repeat step 1 and notice that your setting has been reverted to "Use Legacy Style"
Resolution-External

Most helpful comment

Noticed the same behavior. In my case, the cursor changes back to legacy shape as soon as I start typing.

All 9 comments

Noticed the same behavior. In my case, the cursor changes back to legacy shape as soon as I start typing.

Same thing happens to me. Typing immediately changes the cursor back to default shape. Removing the module using "Remove-Module PSReadline" fixes the issue.

This has been bugging me for a long time, I hope this is fixed soon.

I only see code that changes the cursor size in Vi mode with the ViModeIndicator=Cursor. Neither setting is a default.

Are folks seeing this problem with other modes?

Assuming so (based on the number of comments/thumbs up), I wonder if the cursor size is being changed in .Net somehow?

@lzybkr This only became an issue for me once I upgraded to Windows 10 1903.

I've seen this behavior in the default Windows mode and Vi mode.

I have this in the default editing mode as well, I tried to enable Vi mode to test but I was not sure if it worked.

Related to #964

I think this is probably related to PSReadLine resetting the OutputEncoding after every operation. Will look closer and report back.

OK, it turns out setting Console.CursorVisible to false and then back to true causes the cursor shape to be restored to the legacy style in console host.
I can reproduce it with a simple C# echo program:

using System;
using System.Text;

namespace cursorShape
{
    class Program
    {
        static void Main(string[] args)
        {
            const string prompt = "PROMP> ";
            StringBuilder sb = new StringBuilder();
            int top = Console.CursorTop;

            Console.OutputEncoding = Encoding.UTF8;
            Console.Write(prompt);

            while (true)
            {
                var key = Console.ReadKey();
                if (key.Key == ConsoleKey.Q)
                {
                    break;
                }

                sb.Append(key.KeyChar);

                Console.CursorVisible = false;   // Hide the cursor before rewriting
                Console.SetCursorPosition(0, top);
                Console.Write($"{prompt}{sb}");
                Console.SetCursorPosition(Console.CursorLeft, top);
                Console.CursorVisible = true;    // Show the cursor afterwards
            }
        }
    }
}

linux


Logged the issue https://github.com/microsoft/terminal/issues/4124 in Windows Terminal repo.
I will close this issue as an external issue.

Was this page helpful?
0 / 5 - 0 ratings