C# 美化控制台窗口?

C# 美化控制台窗口?,c#,.net-core,C#,.net Core,我不知道该如何表达这个问题本身,但我的意思是让console看起来像这样: “this”将使输入光标一直向下,并以字符串或字符作为前缀 > 如果Console.ReadLine()为空,则不向下移动光标。 它需要是一个xplatform应用程序,我不能使用任何本机windows dll加载和类似的东西来实现这一点(假设windows提供类似的东西) 我不确定这是否可以实现,但如果可以,请让我知道如何实现!多谢各位 编辑:如果输入光标不可能一直向下,那么只在其前面加上也可以。遗憾的是,

我不知道该如何表达这个问题本身,但我的意思是让console看起来像这样:

“this”将使输入光标一直向下,并以字符串或字符作为前缀

>
如果Console.ReadLine()为空,则不向下移动光标。

它需要是一个xplatform应用程序,我不能使用任何本机windows dll加载和类似的东西来实现这一点(假设windows提供类似的东西)

我不确定这是否可以实现,但如果可以,请让我知道如何实现!多谢各位

编辑:如果输入光标不可能一直向下,那么只在其前面加上
也可以。遗憾的是,我不知道如何实现这两个目标。

这完全有可能

彩色字体文本和大字体标题可以通过“彩色控制台”实现,我强烈推荐它

当我构建一个基于控制台的解决方案时,我希望使用好的格式,这就是我正在使用的。他们的网站很好,因为它演示了它能做什么。然而,在任何给定的时间,你都可以在画布上显示最大数量的不同颜色。该限制为16种不同颜色

该项目还允许您使用FIGlet字体,即红色“FourFort”的大标题。有些已经包括在内,但你可以在互联网上找到更多,并可以创建自己的

许可证类型是MIT,您可以在Github上找到它们

我在NetCore中编译Windows和Linux,没有任何问题

在我看来,另一部分是覆盖一些控制台行为

您可以使用console写入控制台。写入并输出该字符,使其在所有内容之前

您可以一次捕获输入的一个字符,在一个循环中,该循环只有在输入特定序列(如“回车”)时才会中断。如果您在捕获后重新提取输入,然后将其附加到上一次迭代中保留的内容中,您可能会产生前缀“>”的错觉

更新

我写了一份概念证明,描述了我提出的第二个想法。任何“静态”内容都必须添加到“historyInput”变量中。 代码如下:

using System;

namespace BeautifyConsoleSO
{
    class Program
    {
        static void Main(string[] args)
        {
            char inputPrefix = '>';
            bool flag = false;
            string historyInput = string.Empty;
            string currentInput = string.Empty;

            historyInput += "Hello World!";

            Console.WriteLine(historyInput);
            Console.Write(inputPrefix);

            while(flag != true)
            {
                ConsoleKeyInfo input = Console.ReadKey();

                switch(input.Key)
                {
                    case ConsoleKey.Spacebar:
                        currentInput += ' ';
                        break;
                    case ConsoleKey.Enter:
                        historyInput += Environment.NewLine;
                        historyInput += currentInput;
                        currentInput = string.Empty;
                        break;
                    case ConsoleKey.Backspace:
                        if(currentInput.Length > 0)
                        {
                            if (!currentInput[currentInput.Length - 1].Equals(' '))
                            {
                                currentInput = currentInput.Remove(currentInput.Length - 1);
                            }
                            else
                            {
                                currentInput = currentInput.Remove(currentInput.Length - 2);
                            }
                        }
                        break;
                    default:
                        currentInput += input.KeyChar;
                        break;
                }

                Console.Clear();
                Console.WriteLine(historyInput);
                Console.Write("{0}{1}", inputPrefix, currentInput);

            }

            Console.ReadLine();
        }

    }
}
然而,它并不完美;它有一些闪烁的效果。如果我有更好的想法,我会再次更新

更新2

这是一个变体;Console.Clear()生成闪烁。这一条限制刷新,用新行填充相当于控制台高度的内容。为了更好地演示,我还在光标前缀中添加了计数

using System;
using System.Linq;

namespace BeautifyConsoleSO
{
    class Program
    {
        static void Main(string[] args)
        {
            char inputPrefix = '>';
            bool flag = false;
            int clearConsoleRefreshSpeed = 100;
            int clearConsoleTick = 0;
            string historyInput = string.Empty;
            string currentInput = string.Empty;

            string fix = string.Concat(Enumerable.Repeat(Environment.NewLine, Console.WindowHeight));
            Console.WriteLine(fix);

            historyInput += "Hello World!";

            Console.WriteLine(historyInput);
            Console.Write(inputPrefix);

            while(flag != true)
            {
                ConsoleKeyInfo input = Console.ReadKey();

                switch(input.Key)
                {
                    case ConsoleKey.Spacebar:
                        currentInput += ' ';
                        break;
                    case ConsoleKey.Enter:
                        historyInput += Environment.NewLine;
                        historyInput += currentInput;
                        currentInput = string.Empty;
                        break;
                    case ConsoleKey.Backspace:
                        if(currentInput.Length > 0)
                        {
                            if (!currentInput[currentInput.Length - 1].Equals(' '))
                            {
                                currentInput = currentInput.Remove(currentInput.Length - 1);
                            }
                            else
                            {
                                currentInput = currentInput.Remove(currentInput.Length - 2);
                            }
                        }
                        break;
                    default:
                        currentInput += input.KeyChar;
                        break;
                }

                // attempt to fix flickering associated with Console.Clear()
                Console.WriteLine(fix);

                Console.WriteLine(historyInput);
                Console.Write("{0}{1}", clearConsoleTick + " " +  inputPrefix, currentInput);

                clearConsoleTick++;

                if(clearConsoleTick % clearConsoleRefreshSpeed == 0)
                {
                    Console.Clear();
                    Console.WriteLine(fix);
                }
            }

            Console.ReadLine();
        }

    }
}

我不想滥发这个答案,但是可以通过捕捉每一行而不是一行中的每一个字符来实现这个概念验证的第三种变体。

因此我找到的答案是:

基本上这一点:

//前缀为>
控制台。写(“>”,颜色。深红色);
var readInput=Console.ReadLine();
//如果输入为空,则不要添加新行
if(string.IsNullOrWhiteSpace(readInput)){
Console.SetCursorPosition(0,Console.CursorTop);
Write(新字符串(“”,Console.WindowWidth));
Console.SetCursorPosition(0,Console.CursorTop-1);
继续;
}

xplatform?你是说Xamarin?跨平台。我应该换个说法。啊,这是有道理的。我读到它是前平台的彩色。控制台并没有那个么大的问题,但谢谢你们@Yucke这个评论是为了你自己的回答,但我没有足够的观点;请记住,如果您计划在终端GUI中而不是从ssh(tty终端)运行程序,您可能会在Linux系统上的Console.SetCursorPosition上受到性能惩罚。此问题不适用于Microsoft或Apple操作系统。检查这个细节嘿,好像是修好了