Skip to content

Instantly share code, notes, and snippets.

@t3knoid
Created February 11, 2022 14:32
Show Gist options
  • Save t3knoid/f49de1d5cde1cc042003d88aa49474a3 to your computer and use it in GitHub Desktop.
Save t3knoid/f49de1d5cde1cc042003d88aa49474a3 to your computer and use it in GitHub Desktop.

Revisions

  1. t3knoid created this gist Feb 11, 2022.
    34 changes: 34 additions & 0 deletions ConsoleProgressBar.cs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,34 @@
    # Copied from https://www.codeproject.com/Tips/5255878/A-Console-Progress-Bar-in-Csharp

    using System;

    namespace CU
    {
    static class ConsoleUtility
    {
    const char _block = '■';
    const string _back = "\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b";
    const string _twirl = "-\\|/";
    public static void WriteProgressBar(int percent, bool update = false)
    {
    if(update)
    Console.Write(_back);
    Console.Write("[");
    var p = (int)((percent / 10f)+.5f);
    for (var i = 0;i<10;++i)
    {
    if (i >= p)
    Console.Write(' ');
    else
    Console.Write(_block);
    }
    Console.Write("] {0,3:##0}%", percent);
    }
    public static void WriteProgress(int progress, bool update = false)
    {
    if (update)
    Console.Write("\b");
    Console.Write(_twirl[progress % _twirl.Length]);
    }
    }
    }