[go: up one dir, main page]

Skip to content

Multiplatform netstandard 2.0 C# console progress bar, with support for single or multithreaded progress updates.

License

Notifications You must be signed in to change notification settings

goblinfactory/progress-bar

Repository files navigation

(Goblinfactory.Konsole) progress-bar

  • C# (dotnet standard) console progress bar with support for single or multithreaded progress updates.

Install-Package Goblinfactory.ProgressBar

Screenshot of progressbar demo using the double line progressbar, ProgressBarTwoLine.

InstallProgressBar

OSX Notes and limitations

  • Tested on OSX, and currently only works if there is no screen scrolling.
    • i.e. if you create progressbars and the screen scrolls when creating the progressbar, then the progressbars will not work correctly at present.

ProgressBar Usage

    using Goblinfactory.ProgressBar;

           . . .

            var pb = new ProgressBar(50);
            pb.Refresh(0, "connecting to server to download 50 files sychronously.");
            Console.ReadLine();

            pb.Refresh(25, "downloading file number 25");
            Console.ReadLine();
            pb.Refresh(50, "finished.");

produces the following output

Item 0     of 50   . (0  %)
connecting to server to download 50 files sychronously.

(press enter)

Item 25    of 50   . (50%) ######################################
downloading file number 25

(press enter again)

Item 50    of 50   . (100%) ############################################################################
finished.

example of showing status update for parallel tasks

This example creates 10 seperate console progress bars, each being updated on a seperate thread. (This code generates the output visible in the animated gif.)

            // demo; take the first 10 directories that have files from c:\windows, and then pretends to process (list) them.
            // processing of each directory happens on a different thread, to simulate multiple background tasks,
            // e.g. file downloading.
            // ==============================================================================================================
            var dirs = Directory.GetDirectories(@"c:\windows").Where(d=> Directory.GetFiles(d).Count()>0).Take(10);

            var tasks = new List<Task>();
            var bars = new List<ProgressBar>();
            foreach (var d in dirs)
            {
                var dir = new DirectoryInfo(d);
                var files = dir.GetFiles().Take(100).Select(f=>f.FullName).ToArray();
                if (files.Count()==0) continue;
                var bar = new ProgressBar(files.Count());
                bars.Add(bar);
                bar.Refresh(0, d);
                tasks.Add(new Task(() => ProcessFiles(d, files, bar)));
            }
            Console.WriteLine("ready press enter.");
            Console.ReadLine();

            foreach (var t in tasks) t.Start();
            Task.WaitAll(tasks.ToArray());
            Console.WriteLine("done.");
            Console.ReadLine();

        }

        public static void ProcessFiles(string directory, string[] files, ProgressBar bar)
        {
            var cnt = files.Count();
            foreach (var file in files)
            {
                bar.Next(new FileInfo(file).Name);
                Thread.Sleep(150);
            }
        }

Still Todo

  • support screen scrolling.
  • finish documentation for ProgressBarSlim and ProgressBar.
  • Include tests
  • more manual testing
  • Some cleanup work to be done on resetting the cursor position when lots of threads updating progressbars
  • I think we need to move all the lock objects to a shared static ConsoleLocker class, as the current locking can bleed in some very high (lots of threads using both ProgressBar, ProgressBarSlim as well as ThreadSafeWriter.)

Mac Screenshots

Multi threaded test using the default 1 line slim ProgressBar on mac ternimal.

Screenshot below is the output from the [QuickTest sample project] (QuickTest/Program.cs).

multi-threaded test in mac terminal

Useful links for future work

https://docs.microsoft.com/en-us/windows/console/console-screen-buffersdoub

About

Multiplatform netstandard 2.0 C# console progress bar, with support for single or multithreaded progress updates.

Topics

Resources

License

Stars

Watchers

Forks

Sponsor this project

Packages

No packages published

Languages