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

View file

@ -2,147 +2,152 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.Threading; using System.Threading;
namespace ProgressBarLib namespace ProgressBarLib {
{ public class ProgressBar {
public class ProgressBar private volatile bool _shouldStop;
{ private volatile List<string> _msgList = new List<string>();
private volatile bool _shouldStop; private volatile List<string> _errList = new List<string>();
private volatile List<string> _msgList = new List<string>(); private volatile int _lastCurr;
private volatile int _lastCurr; private volatile int _lastTotal;
private volatile int _lastTotal; private volatile string _lastMsg = "";
private volatile string _lastMsg = ""; private volatile string _mainMsg = "";
private volatile string _mainMsg = ""; private readonly object _lock = new object();
private readonly object _lock = new object(); private volatile bool _hasStopped;
private volatile bool _hasStopped;
public ProgressBar() public ProgressBar() {
{ new Thread(() => {
Console.WriteLine(); Thread.CurrentThread.IsBackground = true;
while (!_shouldStop) {
lock (_lock) {
if (_msgList.Count > 0) {
ClearCurrentConsoleLine();
foreach (var msg in _msgList) {
Console.WriteLine(msg.Length < Console.WindowWidth
? msg
: msg.Substring(0, Console.WindowWidth - 1));
}
new Thread(() => _msgList.Clear();
{ }
Thread.CurrentThread.IsBackground = true;
while (!_shouldStop)
{
lock (_lock)
if (_msgList.Count > 0)
{
ClearCurrentConsoleLine();
foreach (var msg in _msgList)
{
Console.WriteLine(msg.Length < Console.WindowWidth
? msg
: msg.Substring(0, Console.WindowWidth - 1));
}
_msgList.Clear(); if (_errList.Count > 0) {
} ClearCurrentConsoleLine();
foreach (var err in _errList) {
Console.WriteLine("ERROR: " + err);
}
ClearCurrentConsoleLine(); _errList.Clear();
Console.Write("\r" + _mainMsg); }
Thread.Sleep(500); }
}
if (_msgList.Count > 0) ClearCurrentConsoleLine();
{ Console.Write("\r" + _mainMsg);
ClearCurrentConsoleLine(); Thread.Sleep(500);
foreach (var msg in _msgList) }
{
Console.WriteLine(msg.Length < Console.WindowWidth
? msg
: msg.Substring(0, Console.WindowWidth - 1));
}
_msgList.Clear(); if (_errList.Count > 0) {
} ClearCurrentConsoleLine();
else foreach (var err in _errList) {
{ Console.WriteLine("ERROR: " + err);
ClearCurrentConsoleLine(); }
Console.Write("\r" + _mainMsg);
}
_hasStopped = true; _errList.Clear();
}).Start(); }
}
public void UpdateMain(int curr, int total, string msg) if (_msgList.Count > 0) {
{ ClearCurrentConsoleLine();
var currstr = curr.ToString(); foreach (var msg in _msgList) {
for (var i = total.ToString().Length - curr.ToString().Length; i > 0; i--) Console.WriteLine(msg.Length < Console.WindowWidth
currstr = " " + currstr; ? msg
UpdateMainAdv(curr, total, currstr, msg); : msg.Substring(0, Console.WindowWidth - 1));
} }
public void UpdateMainAdv(int curr, int total, string custCurr, string msg) _msgList.Clear();
{ }
var outMsg = $"({custCurr}/{total}) "; else {
var progPart = MakeProgressBar(curr, total); ClearCurrentConsoleLine();
var msgMaxLength = Console.WindowWidth - outMsg.Length - progPart.Length; Console.Write("\r" + _mainMsg);
}
if (msgMaxLength < 0) _hasStopped = true;
{ }).Start();
_mainMsg = "Increase terminal width"; }
return;
}
if (msg.Length < msgMaxLength) public void UpdateMain(int curr, int total, string msg) {
for (var i = (msgMaxLength - msg.Length); i > 0; i--) var currstr = curr.ToString();
msg += " "; for (var i = total.ToString().Length - curr.ToString().Length; i > 0; i--)
else currstr = " " + currstr;
msg = msg.Substring(0, msgMaxLength); UpdateMainAdv(curr, total, currstr, msg);
}
outMsg += msg; public void UpdateMainAdv(int curr, int total, string custCurr, string msg) {
outMsg += progPart; var outMsg = $"({custCurr}/{total}) ";
var progPart = MakeProgressBar(curr, total);
var msgMaxLength = Console.WindowWidth - outMsg.Length - progPart.Length;
_lastCurr = curr; if (msgMaxLength < 0) {
_lastMsg = msg; _mainMsg = "Increase terminal width";
_lastTotal = total; return;
}
_mainMsg = outMsg; if (msg.Length < msgMaxLength)
} for (var i = (msgMaxLength - msg.Length); i > 0; i--)
msg += " ";
else
msg = msg.Substring(0, msgMaxLength);
public void Tick() outMsg += msg;
{ outMsg += progPart;
_lastCurr++;
UpdateMain(_lastCurr, _lastTotal, _lastMsg);
}
public void PushMsg(string msg) _lastCurr = curr;
{ _lastMsg = msg;
lock (_lock) _lastTotal = total;
_msgList.Add(msg);
}
public void Stop() _mainMsg = outMsg;
{ }
_shouldStop = true;
while (!_hasStopped) ;
}
private static string MakeProgressBar(int curr, int total) public void Tick() {
{ _lastCurr++;
const string fullChar = "="; UpdateMain(_lastCurr, _lastTotal, _lastMsg);
const string blankChar = "-"; }
var progStr = "["; public void PushMsg(string msg) {
lock (_lock)
_msgList.Add(msg);
}
var fullCharCount = (int) (15d / total * curr); public void PushErr(string err) {
var blankCharCount = 15 - fullCharCount; lock (_lock)
_errList.Add(err);
}
for (var i = fullCharCount; i > 0; i--) progStr += fullChar; public void Stop() {
for (var i = blankCharCount; i > 0; i--) progStr += blankChar; _shouldStop = true;
while (!_hasStopped) ;
}
progStr += "]"; private static string MakeProgressBar(int curr, int total) {
const string fullChar = "=";
const string blankChar = "-";
return progStr; var progStr = "[";
}
private static void ClearCurrentConsoleLine() var fullCharCount = (int) (15d / total * curr);
{ var blankCharCount = 15 - fullCharCount;
var currentLineCursor = Console.CursorTop;
Console.SetCursorPosition(0, Console.CursorTop); for (var i = fullCharCount; i > 0; i--) progStr += fullChar;
Console.Write(new string(' ', Console.WindowWidth)); for (var i = blankCharCount; i > 0; i--) progStr += blankChar;
Console.SetCursorPosition(0, currentLineCursor);
} progStr += "]";
}
return progStr;
}
private static void ClearCurrentConsoleLine() {
var currentLineCursor = Console.CursorTop;
Console.SetCursorPosition(0, Console.CursorTop);
Console.Write(new string(' ', Console.WindowWidth));
Console.SetCursorPosition(0, currentLineCursor);
}
}
} }