private static void IndeterminateSample() { Console.WriteLine("\nThis progress bar shows indeterminate progress and removes itself when complete"); var bar = new CliProgressBar("Please wait"); int secs = 5; bar.RenderAndPollIndeterminate(() => { return secs-- > 0; }, TimeSpan.FromSeconds(1)); bar.Wipe(); }
private static void DeterminateSample() { Console.WriteLine("This progress bar shows determinate progress and stays visible after the operation is complete"); var bar = new CliProgressBar("My operation is {%} complete"); var start = DateTime.Now; bar.RenderAndPollDeterminate(() => { var delta = DateTime.Now - start; bar.Progress = Math.Min(delta.TotalSeconds / 4, 1); if (delta > TimeSpan.FromSeconds(3)) { bar.Message = "The operation was cancelled".ToConsoleString(); bar.FillColor = ConsoleColor.Red; throw new OperationCanceledException(); } } , TimeSpan.FromMilliseconds(100)); }
public void TestProgressBarDeterminateSanity() { var bar = new CliProgressBar("", 10); bar.Progress = .5; bar.Render(); Assert.AreEqual(6, bar.renderedMessage.Length); for(int i = 0; i < bar.renderedMessage.Length; i++) { if(i < 3) { // the first three characters should be filled since the progress is .5 Assert.AreEqual(bar.renderedMessage[i].ForegroundColor, bar.MessageFillColor); Assert.AreEqual(bar.renderedMessage[i].BackgroundColor, bar.FillColor); } else { // the final three characters should NOT BE FILLED since the progress is .5 Assert.AreEqual(bar.renderedMessage[i].ForegroundColor, ConsoleString.DefaultForegroundColor); Assert.AreEqual(bar.renderedMessage[i].BackgroundColor, ConsoleString.DefaultBackgroundColor); } } }