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