Terminal: 隐藏光标,然后使其在控制台窗口中再次可见后,光标形状将恢复为旧式

创建于 2020-01-06  ·  3评论  ·  资料来源: microsoft/terminal

环境

Windows build number: Microsoft Windows [Version 10.0.18363.535]
Windows Terminal version (if applicable): NA

Any other software? No

重现步骤

Properties->Terminal设置光标形状后,隐藏并显示光标,方法是将Console.CursorVisiblefalse ,然后true导致光标形状恢复为控制台主机中的旧样式。

以下简单的C#程序可以显示该问题:

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
            }
        }
    }
}

预期行为

设置Console.CursorVisible不应恢复光标形状。

实际行为

光标形状从Solid Box还原为旧样式( Underline ):
linux

相关问题

409和#1145和https://github.com/PowerShell/PSReadLine/issues/903

这两个问题是在PowerShell + PSReadLine上下文中报告的。 PSReadLine依赖Console.CursorVisible在渲染过程中隐藏和显示光标,因此光标形状将恢复。

Area-Settings Issue-Bug Needs-Tag-Fix Product-Conhost Resolution-Fix-Committed

最有用的评论

tada:此问题已在#5251中得到解决,Windows Terminal Preview v0.11.1121.0 。:tada:

方便的链接:

所有3条评论

好吧,哎呀。

如#409所述,问题在于调用SetConsoleCursorInfo设置光标大小,而设置光标大小会强制将光标形状恢复为旧样式(因为这是唯一适用于该大小的形状) )。

“修复”非常容易。 我们可以从SetCursorInformation方法中删除此行:
https://github.com/microsoft/terminal/blob/6f667f48ae109d4291cda1f8d4e977315c0dae8a/src/host/screenInfo.cpp#L1778

但这是公共API行为的变化,并且某些应用程序实际上可能依赖于该行为(尽管我认为不太可能)。

理想情况下, Console.CursorVisible属性应该使用仅设置可见性的API,但我不认为有这种事情。 您能做的最好的事情就是使用VT序列,但这显然不适用于旧版本的Windows。

tada:此问题已在#5251中得到解决,Windows Terminal Preview v0.11.1121.0 。:tada:

方便的链接:

此页面是否有帮助?
0 / 5 - 0 等级