This commit is contained in:
Laura 2018-06-09 22:33:23 +02:00
parent 5c7972fc66
commit 589ffbaffc

View file

@ -2,12 +2,11 @@
using System.Collections.Generic;
using System.Threading;
namespace ProgressBarLib
{
public class ProgressBar
{
namespace ProgressBarLib {
public class ProgressBar {
private volatile bool _shouldStop;
private volatile List<string> _msgList = new List<string>();
private volatile List<string> _errList = new List<string>();
private volatile int _lastCurr;
private volatile int _lastTotal;
private volatile string _lastMsg = "";
@ -15,21 +14,14 @@ namespace ProgressBarLib
private readonly object _lock = new object();
private volatile bool _hasStopped;
public ProgressBar()
{
Console.WriteLine();
new Thread(() =>
{
public ProgressBar() {
new Thread(() => {
Thread.CurrentThread.IsBackground = true;
while (!_shouldStop)
{
lock (_lock)
if (_msgList.Count > 0)
{
while (!_shouldStop) {
lock (_lock) {
if (_msgList.Count > 0) {
ClearCurrentConsoleLine();
foreach (var msg in _msgList)
{
foreach (var msg in _msgList) {
Console.WriteLine(msg.Length < Console.WindowWidth
? msg
: msg.Substring(0, Console.WindowWidth - 1));
@ -38,16 +30,33 @@ namespace ProgressBarLib
_msgList.Clear();
}
if (_errList.Count > 0) {
ClearCurrentConsoleLine();
foreach (var err in _errList) {
Console.WriteLine("ERROR: " + err);
}
_errList.Clear();
}
}
ClearCurrentConsoleLine();
Console.Write("\r" + _mainMsg);
Thread.Sleep(500);
}
if (_msgList.Count > 0)
{
if (_errList.Count > 0) {
ClearCurrentConsoleLine();
foreach (var msg in _msgList)
{
foreach (var err in _errList) {
Console.WriteLine("ERROR: " + err);
}
_errList.Clear();
}
if (_msgList.Count > 0) {
ClearCurrentConsoleLine();
foreach (var msg in _msgList) {
Console.WriteLine(msg.Length < Console.WindowWidth
? msg
: msg.Substring(0, Console.WindowWidth - 1));
@ -55,8 +64,7 @@ namespace ProgressBarLib
_msgList.Clear();
}
else
{
else {
ClearCurrentConsoleLine();
Console.Write("\r" + _mainMsg);
}
@ -65,22 +73,19 @@ namespace ProgressBarLib
}).Start();
}
public void UpdateMain(int curr, int total, string msg)
{
public void UpdateMain(int curr, int total, string msg) {
var currstr = curr.ToString();
for (var i = total.ToString().Length - curr.ToString().Length; i > 0; i--)
currstr = " " + currstr;
UpdateMainAdv(curr, total, currstr, msg);
}
public void UpdateMainAdv(int curr, int total, string custCurr, string msg)
{
public void UpdateMainAdv(int curr, int total, string custCurr, string msg) {
var outMsg = $"({custCurr}/{total}) ";
var progPart = MakeProgressBar(curr, total);
var msgMaxLength = Console.WindowWidth - outMsg.Length - progPart.Length;
if (msgMaxLength < 0)
{
if (msgMaxLength < 0) {
_mainMsg = "Increase terminal width";
return;
}
@ -101,26 +106,27 @@ namespace ProgressBarLib
_mainMsg = outMsg;
}
public void Tick()
{
public void Tick() {
_lastCurr++;
UpdateMain(_lastCurr, _lastTotal, _lastMsg);
}
public void PushMsg(string msg)
{
public void PushMsg(string msg) {
lock (_lock)
_msgList.Add(msg);
}
public void Stop()
{
public void PushErr(string err) {
lock (_lock)
_errList.Add(err);
}
public void Stop() {
_shouldStop = true;
while (!_hasStopped) ;
}
private static string MakeProgressBar(int curr, int total)
{
private static string MakeProgressBar(int curr, int total) {
const string fullChar = "=";
const string blankChar = "-";
@ -137,8 +143,7 @@ namespace ProgressBarLib
return progStr;
}
private static void ClearCurrentConsoleLine()
{
private static void ClearCurrentConsoleLine() {
var currentLineCursor = Console.CursorTop;
Console.SetCursorPosition(0, Console.CursorTop);
Console.Write(new string(' ', Console.WindowWidth));