如何修改控制台文本的previous行? [英] How to modify the previous line of console text?

查看:132
本文介绍了如何修改控制台文本的previous行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想实现的东西是这样的:

I'd like to achieve something like this:

Time consuming operation...OK
Another time consuming operation...
And another one, but it completed, so...OK

我显示3行文字,每一个与它可以结束迟早一个线程相关的。但是,如果第二个以后完成比第三个,我会得到这样的:

I displayed 3 line of text, each one related with a thread which can end sooner or later. But if the second one complete later than the third one, I'll get something like this:

Time consuming operation...OK
Another time consuming operation...
And another one, but it completed, so...OKOK

这当然是不能接受的。我知道如何回去当前行,但有很长的路要走UP?我发誓,我哪儿见过它,虽然它可能是一个Linux控制台:)

Which is of course unacceptable. I know how to go back in current line, but is there a way to go UP? I'd swear I've seen it somewhere, though it could be a Linux console :)

算了。看得远文件管理器!它可以在Windows控制台,它工作即使在PowerShell中!如何让这样的事情?而最酷的部分是它在退出后恢复控制台状态。所以,也许我应该问 - 如何直接访问控制台缓冲区?我想我需要一些本地的code这样的伎俩,但也许有另一种方式?我想在每次更新结算控制台,但这似乎是大材小用。或者,也许这是不是?它会闪烁?

Forget it. See Far File Manager! It works in Windows console, it works even in PowerShell! How to make something like this? And the coolest part is it restores console state after exiting. So maybe I should ask - how to access console buffer directly? I assume I'll need some native code to do the trick, but maybe there's another way? I thought of clearing console with each update, but this seems like overkill. Or maybe it isn't? Will it blink?

下面是与演示完整的解决方案:

Here's complete solution with demo:

using System;
using System.Collections.Generic;
using System.Threading;

namespace PowerConsole {

    internal class Containers {

        internal struct Container {
            public int Id;
            public int X;
            public int Y;
            public string Content;
        }

        public static List<Container> Items = new List<Container>();

        private static int Identity = 0;

        public static int Add(string text) {
            var c = new Container();
            c.Id = Identity++;
            c.X = Console.CursorLeft;
            c.Y = Console.CursorTop;
            c.Content = text;
            Console.Write(text);
            Items.Add(c);
            return c.Id;
        }

        public static void Remove(int id) {
            Items.RemoveAt(id);
        }

        public static void Replace(int id, string text) {
            int x = Console.CursorLeft, y = Console.CursorTop;
            Container c = Items[id];
            Console.MoveBufferArea(
                c.X + c.Content.Length, c.Y,
                Console.BufferWidth - c.X - text.Length, 1,
                c.X + text.Length, c.Y
            );
            Console.CursorLeft = c.X;
            Console.CursorTop = c.Y;
            Console.Write(text);
            c.Content = text;
            Console.CursorLeft = x;
            Console.CursorTop = y;
        }

        public static void Clear() {
            Items.Clear();
            Identity = 0;
        }
    }

    internal class Program {
        private static List<Thread> Threads = new List<Thread>();

        private static void Main(string[] args) {
            Console.WriteLine("So we have some threads:\r\n");
            int i, id;
            Random r = new Random();
            for (i = 0; i < 10; i++) {
                Console.Write("Starting thread " + i + "...[");
                id = Containers.Add("?");
                Console.WriteLine("]");
                Thread t = new Thread((object data) => {
                    Thread.Sleep(r.Next(5000) + 100);
                    Console.ForegroundColor = ConsoleColor.Green;
                    Containers.Replace((int)data, "DONE");
                    Console.ResetColor();
                });
                Threads.Add(t);
            }
            Console.WriteLine("\n\"But will it blend?\"...");
            Console.ReadKey(true);
            i = 0;
            Threads.ForEach(t => t.Start(i++));
            Threads.ForEach(t => t.Join());
            Console.WriteLine("\r\nVoila.");
            Console.ReadKey(true);
        }
    }
}

顺便说一句,好的基本线程教程...

BTW, nice basic threading tutorial...

推荐答案

您可以不管你想要移动光标:<一href=\"http://msdn.microsoft.com/en-us/library/system.console.setcursorposition.aspx\">Console.SetCursorPosition或者使用<一个href=\"http://msdn.microsoft.com/en-us/library/system.console.cursortop.aspx\">Console.CursorTop.

You can move cursor wherever you want: Console.SetCursorPosition or use Console.CursorTop.

Console.SetCursorPosition(0, Console.CursorTop -1);
Console.WriteLine("Over previous line!!!");

这篇关于如何修改控制台文本的previous行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆