【问题标题】:Beautify console window?美化控制台窗口?
【发布时间】:2020-01-18 11:20:45
【问题描述】:

我不确定如何表达问题本身,但我的意思是让控制台看起来像这样:

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

>

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

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

我不确定这是否可以实现,但如果可以,请告诉我如何实现!谢谢。

编辑:如果输入光标不可能一直向下,那么只需将其前缀为> 也可以。可悲的是,我也不知道如何实现。

【问题讨论】:

  • xplatform?你是说 Xamarin 吗?
  • 跨平台。我应该把它改为。
  • 啊,有道理。我把它读作前平台????

标签: c# .net-core


【解决方案1】:

所以我找到的答案是:

How can I put a string character before Console.ReadLine() Console application

Can Console.Clear be used to only clear a line instead of whole console?

基本上就是这一点:

// Prefix with >
                Console.Write("> ", Color.Crimson);

                var readInput = Console.ReadLine();

// Don't add a new line if input is empty
                if (string.IsNullOrWhiteSpace(readInput)) {
                    Console.SetCursorPosition(0, Console.CursorTop);
                    Console.Write(new string(' ', Console.WindowWidth)); 
                    Console.SetCursorPosition(0, Console.CursorTop - 1);
                    continue;
                }

【讨论】:

    【解决方案2】:

    完全有可能

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

    当我构建一个基于控制台的解决方案并且我想要漂亮的格式时,这就是我正在使用的。他们的网站很好,因为它演示了它可以做什么。但是,您可以在任何给定时间显示最大数量的不同颜色。这个限制是 16 种不同的颜色。

    这个项目还允许你使用FIGlet字体,即红色'FourFort'的大标题。其中一些已经包含在内,但您可以在 Internet 上找到更多内容并可以创建自己的内容。

    http://colorfulconsole.com/

    许可证类型是 MIT,你可以在 Github 上找到它们 https://github.com/tomakita/Colorful.Console/blob/master/LICENSE.md

    我在 Windows 和 Linux 的 net core 中编译没有任何问题

    另一部分,如我所见,是覆盖一些控制台行为。

    您可以使用 Console.Write 写入控制台并输出该字符,以便它预先添加所有内容。

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

    的错觉

    更新

    我写了一个概念证明来描述我提出的第二个想法。任何“静态”内容都必须添加到“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();
            }
    
        }
    }
    

    我不想向这个答案发送垃圾邮件,但是这个概念证明的第三种变体可以实现捕获每一行而不是一行中的每个字符。

    【讨论】:

    • Colorful.Console 问题不大,但谢谢!
    • @Yucked 这个评论是为了你自己在下面的答案,但我没有足够的意义;请记住,如果您计划在终端 GUI 而不是从 ssh(tty 终端)运行程序,您可能会在 Linux 系统上的 Console.SetCursorPosition 上遭受性能损失。此问题不适用于 Microsoft 或 Apple 操作系统。检查此以获取详细信息github.com/dotnet/corefx/issues/32174
    • 嘿,它似乎已修复github.com/dotnet/corefx/pull/36049
    猜你喜欢
    • 2014-03-29
    • 2014-06-24
    • 1970-01-01
    • 2018-04-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多