C# Progress bar Console App
Приведу для вас дорогой код, для тех, кто не хочет тратить 9 баксов для скачивания спец библиотеки.
static void Progressbar() {
var blackBlock = "█";
var middleBlock = "▓";
var ligthBlock = "▒";
int blockCount = 100;
int start = 11;
Console.ForegroundColor = ConsoleColor.Cyan;
var posY = Console.CursorTop;
//var LOADING = " Loading: [{0}{1}{2}]";
// Console.Write(LOADING);
Console.BackgroundColor = ConsoleColor.Blue;
string TEMPLATE = " Loading: [{0}{1}{2}] ";
Console.SetCursorPosition(start, posY);
for (int i = 0; i < 50; i++) {
/* Thread.Sleep(100);
TimeSpan.FromSeconds(100);
Console.Write("{0}", new string(' ', ligthBlock.Length));
Console.SetCursorPosition(Console.CursorLeft - 1, posY);
Console.Write("{0}", new string(' ', middleBlock.Length));
Thread.Sleep(100);
Console.BackgroundColor = ConsoleColor.DarkBlue;
Console.SetCursorPosition(Console.CursorLeft - 1, posY);
Console.Write("{0}", new string(' ', blackBlock.Length));
*/
Thread.Sleep(200);
TimeSpan.FromSeconds(200);
Console.BackgroundColor = ConsoleColor.Gray;
var output = string.Format(" Loading: [{0}{1}{2}] ",
new string(' ', ligthBlock.Length),
new string(' ', middleBlock.Length),
new string(' ', blackBlock.Length));
Console.Write(output);
}
Console.BackgroundColor = ConsoleColor.White;
Console.Write("] \n");
}
UPD:
static void Progressbar() {
int blockCount = 100;
int start = 11;
var posY = Console.CursorTop;
// //012345678911
string template = " Loading: [{0}{1}{2} ] {3}% {4}s";
Console.WriteLine(template);
int maxProgress = 50;
ConsoleProgressBar bar = new ConsoleProgressBar(11, posY, maxProgress);
long previous = -1;
long total = 1000000000;
for (long i = 0; i < total; i++) {
long progress = i * maxProgress / total;
if (progress != previous) {
bar.ShowProgress((int)progress);
previous = progress;
}
}
bar.ShowProgress(maxProgress);
Console.BackgroundColor = ConsoleColor.White;
}
}
public class ConsoleProgressBar {
public int Left { get; set; }
public int Top { get; set; }
public int Length { get; set; }
public ConsoleProgressBar(int left, int top, int length) {
Left = left;
Top = top;
Length = length;
}
public void ShowProgress(int progress) {
if (progress > Length || progress < 0)
throw new ArgumentException($"Invalid progress value, must be between 0 and {Length} but actual {progress}.");
int start = 0;
(int left, int top) = Console.GetCursorPosition();
Console.SetCursorPosition(Left, Top);
Thread.Sleep(100);
Console.Write($"{new string('█', progress)}" +
$"{new string('▓', progress)}" +
$"{new string('░', Length - progress)}" +
$"{new string(Convert.ToChar(start++), Length - progress)}" +
$"{new string(Convert.ToChar(Timer.ActiveCount), Length - progress)}");
Console.SetCursorPosition(left, top);
Thread.Sleep(100);
}
}
Почему не отображается ] , процент выполнения, время за которое сделано? Да и обратите внимание на то что в половине прогресс бара █ не дошёл до конца. между скобками во время загрузки тоже можно сделать свободно пространство если надо, то есть в ShowProgress указать второй аргумент который будет проверять надо ли пустое пространство между скобками и оно заполняется.
Что я не так делаю?
Ответы (1 шт):
Автор решения: aepot
→ Ссылка
Что-то такое хотели?
static class Program
{
static void Main(string[] args)
{
Progressbar();
Console.ReadKey();
}
static void Progressbar()
{
Console.ForegroundColor = ConsoleColor.Cyan;
var posY = Console.CursorTop;
string template = " Loading: ";
Console.WriteLine(template);
ConsoleProgressBar bar = new ConsoleProgressBar(template.Length, posY, 50);
long previous = -1;
long total = 1000000000;
for (long i = 0; i < total; i++)
{
long progress = i * 100 / total;
if (progress != previous)
{
bar.ShowProgress((int)progress);
previous = progress;
}
}
bar.ShowProgress(100);
Console.ResetColor();
}
}
public class ConsoleProgressBar
{
public int Left { get; }
public int Top { get; }
public int Length { get; }
public ConsoleProgressBar(int left, int top, int length)
{
Left = left;
Top = top;
Length = length;
}
public void ShowProgress(int progress)
{
if (progress > 100 || progress < 0)
throw new ArgumentException($"Invalid progress value, must be between 0 and 100 but actual {progress}.");
int p = progress * Length / 100;
(int left, int top) = Console.GetCursorPosition();
Console.SetCursorPosition(Left, Top);
Console.Write($"[{new string('█', p / 2)}" +
new string('▓', p / 2 + p % 2) +
$"{new string('░', Length - p)}] {progress}%");
Console.SetCursorPosition(left, top);
}
}
Переделал на задание прогресса в процентах.

