Refactor, new features
This commit is contained in:
parent
b634166051
commit
c2228c53b9
|
@ -8,8 +8,12 @@ namespace ProgressBarLib
|
||||||
{
|
{
|
||||||
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 int _lastCurr;
|
||||||
|
private volatile int _lastTotal;
|
||||||
|
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;
|
||||||
|
|
||||||
public ProgressBar()
|
public ProgressBar()
|
||||||
{
|
{
|
||||||
|
@ -23,41 +27,47 @@ namespace ProgressBarLib
|
||||||
lock (_lock)
|
lock (_lock)
|
||||||
if (_msgList.Count > 0)
|
if (_msgList.Count > 0)
|
||||||
{
|
{
|
||||||
|
ClearCurrentConsoleLine();
|
||||||
foreach (var msg in _msgList)
|
foreach (var msg in _msgList)
|
||||||
{
|
{
|
||||||
Console.WriteLine(msg);
|
Console.WriteLine(msg.Length < Console.WindowWidth
|
||||||
|
? msg
|
||||||
|
: msg.Substring(0, Console.WindowWidth - 1));
|
||||||
}
|
}
|
||||||
|
|
||||||
_msgList.Clear();
|
_msgList.Clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
ClearCurrentConsoleLine();
|
ClearCurrentConsoleLine();
|
||||||
Console.Write("\r" + _mainMsg);
|
Console.Write("\r" + _mainMsg);
|
||||||
Thread.Sleep(500);
|
Thread.Sleep(500);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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();
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
ClearCurrentConsoleLine();
|
||||||
|
Console.Write("\r" + _mainMsg);
|
||||||
|
}
|
||||||
|
|
||||||
|
_hasStopped = true;
|
||||||
}).Start();
|
}).Start();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void UpdateMain(int curr, int total, string msg)
|
public void UpdateMain(int curr, int total, string msg)
|
||||||
{
|
{
|
||||||
var outMsg = $"({curr}/{total}) ";
|
UpdateMainAdv(curr, total, curr.ToString(), msg);
|
||||||
var progPart = MakeProgressBar(curr, total);
|
|
||||||
var msgMaxLength = Console.WindowWidth - outMsg.Length - progPart.Length;
|
|
||||||
|
|
||||||
if (msgMaxLength < 0)
|
|
||||||
{
|
|
||||||
_mainMsg = "Increase terminal width";
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (msg.Length < msgMaxLength)
|
|
||||||
for (var i = (msgMaxLength - msg.Length); i > 0; i--)
|
|
||||||
msg += " ";
|
|
||||||
else
|
|
||||||
msg = msg.Substring(0, msgMaxLength);
|
|
||||||
|
|
||||||
outMsg += msg;
|
|
||||||
outMsg += progPart;
|
|
||||||
_mainMsg = outMsg;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void UpdateMainAdv(int curr, int total, string custCurr, string msg)
|
public void UpdateMainAdv(int curr, int total, string custCurr, string msg)
|
||||||
|
@ -80,18 +90,30 @@ namespace ProgressBarLib
|
||||||
|
|
||||||
outMsg += msg;
|
outMsg += msg;
|
||||||
outMsg += progPart;
|
outMsg += progPart;
|
||||||
|
|
||||||
|
_lastCurr = curr;
|
||||||
|
_lastMsg = msg;
|
||||||
|
_lastTotal = total;
|
||||||
|
|
||||||
_mainMsg = outMsg;
|
_mainMsg = outMsg;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void Tick()
|
||||||
|
{
|
||||||
|
_lastCurr++;
|
||||||
|
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 Stop()
|
||||||
{
|
{
|
||||||
_shouldStop = true;
|
_shouldStop = true;
|
||||||
|
while (!_hasStopped) ;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static string MakeProgressBar(int curr, int total)
|
private static string MakeProgressBar(int curr, int total)
|
||||||
|
@ -114,10 +136,10 @@ namespace ProgressBarLib
|
||||||
|
|
||||||
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));
|
||||||
Console.SetCursorPosition(0, currentLineCursor);
|
Console.SetCursorPosition(0, currentLineCursor);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Reference in a new issue