tags:

views:

98

answers:

3

Hi,

I am using 7zip in command line mode.

When the operation takes quite a long time, sometimes there is a process percentage displayed.

I wonder if we want to do this using C#/Java, what library to use?

Regards,

+3  A: 

no library. You just print to the console System.out.print (not println!), then send backspace characters to clear the line.

System.out("Progress 5");
System.out("\b\b\b\b\b\b\b\b\b\b");
System.out("Progress 10");

I haven't done this in awhile, but that should do it.

Kevin Day
+1 I did something like this the other day when fooling around with Mono/C# on the Mac, except I used a single `\r` to get straight back to the start of the line in one jump.
Daniel Earwicker
+5  A: 

You can print a "carriage return", also known as a '\r' to reset the "cursor" to the start of the line.

System.out.printf("Progress: %3d %% \r", percentComplete);

Now every time you print the line, you get sent back to the start so the next percent overwrites the previous one.

Chris Cooper
A: 

You could clear the console for kicks...oh and don't forget string.format has percentage support...

using System;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
          var perc = 0.0;
          while(perc <= 1.0)
          {

            Threading.Thread.Sleep(50); //simulate doing some work

            //EDIT:
            //Console.Clear();
            Console.Write(String.Format("{0:P}\r", perc)); //as per suggestion

            perc += 0.01;
          }
          Console.WriteLine("Press any key to exit");
          var exit = Console.ReadKey();
      }

  }

}

Jim Burger
The nice thing about overprinting one line instead of clearing the whole screen is (a) you don't lose the rest of the screen's content and (b) it doesn't flicker when it redraws (okay, that one is pretty obsessive...)
Daniel Earwicker
Good point, I learned something today :)
Jim Burger