** ConsoleInput** public class ConsoleInput { private string inputString = string.Empty; private readonly List inputHistory = new List(); internal readonly stri ..

[C#] 将状态信息放置在控制台最下面

本贴最后更新于 194 天前,其中的信息可能已经沧海桑田

** ConsoleInput**

public class ConsoleInput
    {
        private string inputString = string.Empty;
        private readonly List<string> inputHistory = new List<string>();
        internal readonly string[] StatusTextLeft = { string.Empty, string.Empty, string.Empty, string.Empty };
        internal readonly string[] StatusTextRight = { string.Empty, string.Empty, string.Empty, string.Empty };
        internal readonly ConsoleColor[] StatusTextLeftColor = { ConsoleColor.White, ConsoleColor.White, ConsoleColor.White, ConsoleColor.White };
        internal readonly ConsoleColor[] StatusTextRightColor = { ConsoleColor.White, ConsoleColor.White, ConsoleColor.White, ConsoleColor.White };
        public int LineWidth => Console.BufferWidth;
        public void ClearLine(int numLines)
        {
            Console.CursorLeft = 0;
            Console.Write(new string(' ', LineWidth * numLines));
            Console.CursorTop = Console.CursorTop - numLines;
            Console.CursorLeft = 0;
        }

        public void RedrawInputLine()
        {
            try
            {
                Console.CursorTop = Console.CursorTop + 1;
                for (int i = 0; i < StatusTextLeft.Length; i++)
                {
                    Console.CursorLeft = 0;
                    Console.ForegroundColor = StatusTextLeftColor[i];
                    Console.Write(StatusTextLeft[i].Substring(0, Math.Min(StatusTextLeft[i].Length, LineWidth - 1)));
                    Console.ForegroundColor = StatusTextRightColor[i];
                    Console.Write(StatusTextRight[i].PadRight(LineWidth));
                }
                Console.CursorTop = Console.CursorTop - StatusTextLeft.Length -1;
                Console.CursorLeft = 0;
                Console.BackgroundColor = ConsoleColor.Black;
                Console.ForegroundColor = ConsoleColor.Green;
                ClearLine(1);
                if (inputString.Length == 0)
                {
                    Console.ForegroundColor = ConsoleColor.Gray;
                    return;
                }
                Console.Write(inputString.Length >= LineWidth - 2 ? inputString.Substring(inputString.Length - (LineWidth - 2)) : inputString);
                Console.ForegroundColor = ConsoleColor.Gray;
            }
            catch (Exception e)
            {
                Console.WriteLine("RedrawInputLine: " + e);

            }
        }

        public void Update()
        {

            try
            {
                if (!Console.KeyAvailable)
                {
                    return;
                }
            }
            catch (Exception)
            {
                return;
            }
            ConsoleKeyInfo consoleKeyInfo = Console.ReadKey();
            if (consoleKeyInfo.Key == ConsoleKey.Enter)
            {
                Console.WriteLine(inputString);
                inputString = string.Empty;
                RedrawInputLine();
                return;
            }
            inputString = string.Concat(inputString, consoleKeyInfo.KeyChar);
            RedrawInputLine();
        }
    }

Main

private static ConsoleInput consoleInput;
        private static string GetStatusValue(string status) => status != null ? status ?? string.Empty : "";
        private static string GetStatusRight(int leftLength, string right) => leftLength >= right.Length ? string.Empty : right.Substring(leftLength);
        static void Main(string[] args)
        {
            consoleInput = new ConsoleInput();
            Console.WriteLine("load............ing...............");
            Console.WriteLine("ConsoleInput");
           int j=0;
            while (true)
            {
                j++;
                for (int i = 0; i < consoleInput.StatusTextLeft.Length; i++)
                {
                    consoleInput.StatusTextLeft[i] = GetStatusValue($"{j}{i}");//赋值
                }
                j++;
                for (int i = 0; i < consoleInput.StatusTextRight.Length; i++)
                {
                    consoleInput.StatusTextRight[i] = GetStatusRight(consoleInput.StatusTextLeft[i].Length, GetStatusValue($"{j}{i}").PadLeft(consoleInput.LineWidth - 1));//右边计算位置再赋值
                }
                consoleInput.Update();
            }
        }

bgif

  • B3log

    B3log 是一个开源组织,名字来源于“Bulletin Board Blog”缩写,目标是将独立博客与论坛结合,形成一种新的网络社区体验,详细请看 B3log 构思。目前 B3log 已经开源了多款产品:PipeSoloSymWide 等,欢迎大家加入,贡献开源。

    1891 引用 • 3630 回帖 • 590 关注
  • 笔记

    好记性不如烂笔头。

    171 引用 • 396 回帖
  • .NET
    14 引用 • 5 回帖 • 3 关注
回帖   
请输入回帖内容...