Question

I'm working on a CLI application that deals with uploading of files, and would like to have a combination of appended and overwritten output. Ideally it would look something like this:

c:\>upload
file1.dat       100%
file2.dat       100%
file3.dat        59%, 36.4k/s

I'd like just the last row to periodically update the percent complete and current speed. I know that I can use SetCursorPosition to write output to any part of the console, but it appears there is no GetCursorPosition and the absolute position of the most recently printed filename will vary. I also wonder how all this will affect redirected output, but handling that correctly isn't critical for this app.

EDIT: Looks like the Console.CursorLeft / Console.CursorTop etc. will give me the current cursor position. I looked right at em too! Oh well. Free accepted answer if anybody wants it.

Was it helpful?

Solution

Have you considered using the Console.CursorLeft / Console.CursorTop to get the current cursor position ;-)

OTHER TIPS

This is quite simple to do. The \r character moves the cursor to the beginning of the current line.

for(int i = 0; i <= 100; i++)
{
   printf("Progress: %02d \r", i);
   fflush(stdout);
   Sleep(200);
}

printf("\n");

It's not a platform limitation; I know that curses is a good tool for doing just this sort of stuff, and there's a version on Windows. (Nethack is a great example of just the sort of control that you need, and the Windows console version is quite entertaining as a bonus.) I don't know if there's anything for that level of control on .NET, though. Perhaps a compilation in managed code of curses might do what you want?

The simplest solution that came from old DOS times, is to write enough backspaces (\b) into console to clear last percent output, and then print the new one.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top