diff --git a/ProgressBarLib/ProgressBar.cs b/ProgressBarLib/ProgressBar.cs index 6666805..a58a59b 100644 --- a/ProgressBarLib/ProgressBar.cs +++ b/ProgressBarLib/ProgressBar.cs @@ -2,147 +2,152 @@ using System.Collections.Generic; using System.Threading; -namespace ProgressBarLib -{ - public class ProgressBar - { - private volatile bool _shouldStop; - private volatile List _msgList = new List(); - private volatile int _lastCurr; - private volatile int _lastTotal; - private volatile string _lastMsg = ""; - private volatile string _mainMsg = ""; - private readonly object _lock = new object(); - private volatile bool _hasStopped; +namespace ProgressBarLib { + public class ProgressBar { + private volatile bool _shouldStop; + private volatile List _msgList = new List(); + private volatile List _errList = new List(); + private volatile int _lastCurr; + private volatile int _lastTotal; + private volatile string _lastMsg = ""; + private volatile string _mainMsg = ""; + private readonly object _lock = new object(); + private volatile bool _hasStopped; - public ProgressBar() - { - Console.WriteLine(); + public ProgressBar() { + new Thread(() => { + 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(() => - { - 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(); + } - _msgList.Clear(); - } + if (_errList.Count > 0) { + ClearCurrentConsoleLine(); + foreach (var err in _errList) { + Console.WriteLine("ERROR: " + err); + } - ClearCurrentConsoleLine(); - Console.Write("\r" + _mainMsg); - Thread.Sleep(500); - } + _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)); - } + ClearCurrentConsoleLine(); + Console.Write("\r" + _mainMsg); + Thread.Sleep(500); + } - _msgList.Clear(); - } - else - { - ClearCurrentConsoleLine(); - Console.Write("\r" + _mainMsg); - } + if (_errList.Count > 0) { + ClearCurrentConsoleLine(); + foreach (var err in _errList) { + Console.WriteLine("ERROR: " + err); + } - _hasStopped = true; - }).Start(); - } + _errList.Clear(); + } - 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); - } + if (_msgList.Count > 0) { + ClearCurrentConsoleLine(); + foreach (var msg in _msgList) { + Console.WriteLine(msg.Length < Console.WindowWidth + ? msg + : msg.Substring(0, Console.WindowWidth - 1)); + } - 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; + _msgList.Clear(); + } + else { + ClearCurrentConsoleLine(); + Console.Write("\r" + _mainMsg); + } - if (msgMaxLength < 0) - { - _mainMsg = "Increase terminal width"; - return; - } + _hasStopped = true; + }).Start(); + } - if (msg.Length < msgMaxLength) - for (var i = (msgMaxLength - msg.Length); i > 0; i--) - msg += " "; - else - msg = msg.Substring(0, msgMaxLength); + 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); + } - outMsg += msg; - outMsg += progPart; + 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; - _lastCurr = curr; - _lastMsg = msg; - _lastTotal = total; + if (msgMaxLength < 0) { + _mainMsg = "Increase terminal width"; + 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() - { - _lastCurr++; - UpdateMain(_lastCurr, _lastTotal, _lastMsg); - } + outMsg += msg; + outMsg += progPart; - public void PushMsg(string msg) - { - lock (_lock) - _msgList.Add(msg); - } + _lastCurr = curr; + _lastMsg = msg; + _lastTotal = total; - public void Stop() - { - _shouldStop = true; - while (!_hasStopped) ; - } + _mainMsg = outMsg; + } - private static string MakeProgressBar(int curr, int total) - { - const string fullChar = "="; - const string blankChar = "-"; + public void Tick() { + _lastCurr++; + UpdateMain(_lastCurr, _lastTotal, _lastMsg); + } - var progStr = "["; + public void PushMsg(string msg) { + lock (_lock) + _msgList.Add(msg); + } - var fullCharCount = (int) (15d / total * curr); - var blankCharCount = 15 - fullCharCount; + public void PushErr(string err) { + lock (_lock) + _errList.Add(err); + } - for (var i = fullCharCount; i > 0; i--) progStr += fullChar; - for (var i = blankCharCount; i > 0; i--) progStr += blankChar; + public void Stop() { + _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 currentLineCursor = Console.CursorTop; - Console.SetCursorPosition(0, Console.CursorTop); - Console.Write(new string(' ', Console.WindowWidth)); - Console.SetCursorPosition(0, currentLineCursor); - } - } + var fullCharCount = (int) (15d / total * curr); + var blankCharCount = 15 - fullCharCount; + + for (var i = fullCharCount; i > 0; i--) progStr += fullChar; + for (var i = blankCharCount; i > 0; i--) progStr += blankChar; + + 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); + } + } } \ No newline at end of file