views:

3151

answers:

7

When building a Windows Console App in C#, is it possible to write to the console without having to extend a current line or go to a new line? For example, if I want to show a percentage representing how close a process is to completion, I'd just like to update the value on the same line as the cursor, and not have to put each percentage on a new line.

Can this be done with a "standard" C# console app?

+21  A: 

You can use Console.SetCursorPosition to set the position of the cursor and then write at the current position.

Here is an example showing a simple "spinner":

static void Main(string[] args)
{
    ConsoleSpinner spin = new ConsoleSpinner();
    Console.Write("Working....");
    while (true) 
    {
        spin.Turn();
    }
}

public class ConsoleSpinner
{
    int counter;
    public ConsoleSpiner()
    {
        counter = 0;
    }
    public void Turn()
    {
        counter++;        
        switch (counter % 4)
        {
            case 0: Console.Write("/"); break;
            case 1: Console.Write("-"); break;
            case 2: Console.Write("\\"); break;
            case 3: Console.Write("|"); break;
        }
        Console.SetCursorPosition(Console.CursorLeft - 1, Console.CursorTop);
    }
}

Note that you will have to make sure to overwrite any existing output with new output or blanks.

Update: As it has been critisized that the example moves the cursor only back by one character, I will add this for clarification: Using SetCursorPosition you may set the cursor to any position in the console window.

Console.SetCursorPosition(0, Console.CursorTop);

will set the cursor to the beginning of the current line (or you can use Console.CursorLeft = 0 directly).

0xA3
The problem might be solved using \r, but using `SetCursorPosition` (or `CursorLeft`) allows for more flexibility, e.g. not writing at the beginning of the line, moving up in the window, etc so it is a more general approach that can be used to e.g. output custom progress bars or ASCII graphic.
0xA3
Overly complicated, yes. Greater flexibility, yes. Answer the question, not exactly. Great resource though for as you say, custom progress bar, etc...
Nate Bross
+1 for being verbose and going above and beyond the call of duty. Good stuff thanks.
Copas
+1 for showing a different way of doing it. Everyone else showed \r, and if the OP is simply updating a percentage, with this he can just update the value without having to re-write the whole line.The OP never actually said he wanted to move to the start of the line, just that he wanted to update something on the same line as the cursor.
Andy
Please also note that in the example provided a \r followed by some output would overwrite the text already written in that line ("Working..."). That may not be wanted.
0xA3
Removed downvote, divo added code to show how to move it back to the start of the line.
Malfist
The added flexibility of SetCursorPosition comes at the cost of a little bit of speed and a noticeable cursor flicker if the loop is long enough for the user to notice. See my test comment below.
Iceman
+19  A: 

If you print only "\r" to the console the cursor goes back to the begining of the current line and then you can rewrite it. This should do the trick:

for(int i = 0; i < 100; ++i)
{
    Console.Write("\r{0}%   ", i);
}

Notice the few spaces after the number to make sure that whatever was there before is erased.
Also notice the use of Write() instead of WriteLine() since you don't want to add an "\n" at the end of the line.

shoosh
A: 

\r is used for this scenarios. \r represents a carriage return which means the cursor returns to the start of the line. That's why windows uses \n\r as it's new line marker. \n moves you down a line, and \r returns you to the start of the line.

Malfist
+1  A: 

Explicitly using a Carrage Return (\r) at the beginning of the line rather than (implicitly or explicitly) using a New Line (\n) at the end should get what you want. For example:

void demoPercentDone() {
    for(int i = 0; i < 100; i++) {
        System.Console.Write( "\rProcessing {0}%...", i );
        System.Threading.Thread.Sleep( 1000 );
    }
    System.Console.WriteLine();    
}
James Hugard
-1, Question asks for C#, I rewrite it in C# and you change it back to F#
Malfist
It looks like an editing conflict rather than him changing your C# back to F#. His change was a minute after yours, and focussed on the sprintf.
Andy
ah, removed the downvote then.
Malfist
We need one more edit to get rid of the reference to F#
Andy
Thanks for the edit. I tend to use F# interactive mode to test things and figured the important parts were the BCL calls, which are the same in C#.
James Hugard
+9  A: 

You can use the \b (backspace) escape sequence to backup a particular number of characters on the current line. This just moves the current location, it does not remove the characters.

For example:

string line="";

for(int i=0; i<100; i++)
{
    string backup=new string('\b',line.Length);
    Console.Write(backup);
    line=string.Format("{0}%",i);
    Console.Write(line);
}

Here, line is the percentage line to write to the console. The trick is to generate the correct number of \b characters for the previous output.

The advantage of this over the \r approach is that if works even if your percentage output is not at the beginning of the line.

Sean
+1, this turns out to be the fastest method presented (see my test comment below)
Iceman
+6  A: 

So far we have three competing alternatives for how to do this:

Console.Write("\r{0}   ", value); //option 1: carriage return
Console.Write("\b\b\b\b\b{0}", value); //option 2: backspace
{                                           //option 3 in two parts:
  Console.SetCursorPosition(0, Console.CursorTop); //move cursor
  Console.Write(value);                            //rewrite
}

I've always used Console.CursorLeft = 0, a variation on the third option, so I decided to do some tests. Here's the code I used:

public static void CursorTest()
{
  int testsize = 1000000;
  Console.WriteLine("Testing cursor position");
  Stopwatch sw = new Stopwatch();
  sw.Start();
  for (int i = 0; i < testsize; i++)
  {
    Console.Write("\rCounting: {0}     ", i);
  }
  sw.Stop();
  Console.WriteLine("\nTime using \\r: {0}", sw.ElapsedMilliseconds);
  sw.Reset();
  sw.Start();
  int top = Console.CursorTop;
  for (int i = 0; i < testsize; i++)
  {
    Console.SetCursorPosition(0, top);        
    Console.Write("Counting: {0}     ", i);
  }
  sw.Stop();
  Console.WriteLine("\nTime using CursorLeft: {0}", sw.ElapsedMilliseconds);
  sw.Reset();
  sw.Start();
  Console.Write("Counting:          ");
  for (int i = 0; i < testsize; i++)
  {        
    Console.Write("\b\b\b\b\b\b\b\b{0,8}", i);
  }
  sw.Stop();
  Console.WriteLine("\nTime using \\b: {0}", sw.ElapsedMilliseconds);
}

On my machine, I get the following results:

  • Backspaces: 25.0 seconds
  • Carriage Returns: 28.7 seconds
  • SetCursorPosition: 49.7 seconds

Additionally, SetCursorPosition caused noticeable flicker that I didn't observe with either of the alternatives. So, the moral is to use backspaces or carriage returns when possible, and thanks for teaching me a faster way to do this, SO!


Update: In the comments, Joel suggests that SetCursorPosition is constant with respect to the distance moved while the other methods are linear. Further testing confirms that this is the case, however constant time and slow is still slow. In my tests, writing a long string of backspaces to the console is faster than SetCursorPosition until somewhere around 60 characters. So backspace is faster for replacing portions of the line shorter than 60 characters (or so), and it doesn't flicker, so I'm going to stand by my initial endorsement of \b over \r and SetCursorPosition.

Iceman
The efficiency of the operation in question really shouldn't matter. It should all occur too fast for the user to notice. Unnecessary microptimisation is bad.
Malfist
@Malfist: Depending on the length of the loop, the user may or may not notice. As I added in the edit above (before I saw your comment), SetCursorPosition introduced flicker and takes almost twice as long as the other options.
Iceman
I agree that it is a micro-optimisation (running it a million times and taking 50 seconds is still a very small amount of time), +1 for the results, and it could definitely be very useful to know.
Andy
@Andy: It's almost definitely a micro-optimization in this case, but could make a difference in some situations. The big difference was the flicker introduced by SetCursorPosition, which would be a reason to avoid it even if the times were equal, in my opinion.
Iceman
The benchmark is fundamentally flawed. It's possible that SetCursorPosition() time is the same no matter how far the cursor moves, while the other options vary by how many characters the console has to process.
Joel Coehoorn
This is a very nice sum up of the different options available. However, I also see flickering when using \r. With \b there is obviously no flickering because the fix text ("Counting:") is not rewritten. You will also get flickering if you add additional \b and rewrite the fix text as it is happening with \b and SetCursorPosition. Concerning Joel's remark: Joel is basically right, however \r will still outperform SetCursorPosition on very long lines, but the difference gets less.
0xA3
+4  A: 

I just had to play with the divo's ConsoleSpinner class. Mine is nowhere near as concise, but it just didn't sit well with me that users of that class have to write their own while(true) loop. I'm shooting for an experience more like this:

static void Main(string[] args)
{
    Console.Write("Working....");
    ConsoleSpinner spin = new ConsoleSpinner();
    spin.Start();

    // Do some work...

    spin.Stop(); 
}

And I realized it with the code below. Since I don't want my Start() method to block, I don't want the user to have to worry about writing a while(spinFlag) -like loop, and I want to allow multiple spinners at the same time I had to spawn a separate thread to handle the spinning. And that means the code has to be a lot more complicated.

Also, I haven't done that much multi-threading so it's possible (likely even) that I've left a subtle bug or three in there. But it seems to work pretty well so far:

public class ConsoleSpinner : IDisposable
{       
    public ConsoleSpinner()
    {
        CursorLeft = Console.CursorLeft;
        CursorTop = Console.CursorTop;  
    }

    public ConsoleSpinner(bool start)
        : this()
    {
        if (start) Start();
    }

    public void Start()
    {
        // prevent two conflicting Start() calls ot the same instance
        lock (instanceLocker) 
        {
            if (!running )
            {
                running = true;
                turner = new Thread(Turn);
                turner.Start();
            }
        }
    }

    public void StartHere()
    {
        SetPosition();
        Start();
    }

    public void Stop()
    {
        lock (instanceLocker)
        {
            if (!running) return;

            running = false;
            if (! turner.Join(250))
                turner.Abort();
        }
    }

    public void SetPosition()
    {
        SetPosition(Console.CursorLeft, Console.CursorTop);
    }

    public void SetPosition(int left, int top)
    {
        bool wasRunning;
        //prevent other start/stops during move
        lock (instanceLocker)
        {
            wasRunning = running;
            Stop();

            CursorLeft = left;
            CursorTop = top;

            if (wasRunning) Start();
        } 
    }

    public bool IsSpinning { get { return running;} }

    /* ---  PRIVATE --- */

    private int counter=-1;
    private Thread turner; 
    private bool running = false;
    private int rate = 100;
    private int CursorLeft;
    private int CursorTop;
    private Object instanceLocker = new Object();
    private static Object console = new Object();

    private void Turn()
    {
        while (running)
        {
            counter++;

            // prevent two instances from overlapping cursor position updates
            // weird things can still happen if the main ui thread moves the cursor during an update and context switch
            lock (console)
            {                  
                int OldLeft = Console.CursorLeft;
                int OldTop = Console.CursorTop;
                Console.SetCursorPosition(CursorLeft, CursorTop);

                switch (counter)
                {
                    case 0: Console.Write("/"); break;
                    case 1: Console.Write("-"); break;
                    case 2: Console.Write("\\"); break;
                    case 3: Console.Write("|"); counter = -1; break;
                }
                Console.SetCursorPosition(OldLeft, OldTop);
            }

            Thread.Sleep(rate);
        }
        lock (console)
        {   // clean up
            int OldLeft = Console.CursorLeft;
            int OldTop = Console.CursorTop;
            Console.SetCursorPosition(CursorLeft, CursorTop);
            Console.Write(' ');
            Console.SetCursorPosition(OldLeft, OldTop);
        }
    }

    public void Dispose()
    {
        Stop();
    }
}
Joel Coehoorn
Nice modification, though the sample code is not mine. It's taken from Brad Abrams' blog (see the link in my answer). I think it's just been written as a simple sample demonstrating SetCursorPosition. Btw, I'm definitely surprised (in a positive way) about the discussion started about what I thought was just a simple sample. That's why I love this site :-)
0xA3