From 2754cdcc56f8b5b0078a8efb9669914d93c3979c Mon Sep 17 00:00:00 2001 From: dbuechel Date: Thu, 18 Jul 2019 10:36:41 +0200 Subject: [PATCH] SEBWIN-320: Implemented progress animation for reset utility. --- .../CompositionRoot.cs | 4 +- .../Procedure/Initialization.cs | 2 +- SafeExamBrowser.ResetUtility/Procedure/Log.cs | 2 +- .../Procedure/MainMenu.cs | 2 +- .../ProcedureContext.cs} | 5 +- .../Procedure/ProcedureStep.cs | 58 ++++++++++++++++++- .../Procedure/Reset.cs | 2 +- .../Procedure/Restore.cs | 5 +- .../Procedure/Version.cs | 2 +- .../SafeExamBrowser.ResetUtility.csproj | 2 +- 10 files changed, 70 insertions(+), 14 deletions(-) rename SafeExamBrowser.ResetUtility/{Context.cs => Procedure/ProcedureContext.cs} (86%) diff --git a/SafeExamBrowser.ResetUtility/CompositionRoot.cs b/SafeExamBrowser.ResetUtility/CompositionRoot.cs index bfdcb408..6dd88e43 100644 --- a/SafeExamBrowser.ResetUtility/CompositionRoot.cs +++ b/SafeExamBrowser.ResetUtility/CompositionRoot.cs @@ -26,7 +26,7 @@ namespace SafeExamBrowser.ResetUtility internal void BuildObjectGraph() { - var context = new Context(); + var context = new ProcedureContext(); InitializeLogging(); @@ -51,7 +51,7 @@ namespace SafeExamBrowser.ResetUtility logger?.Log($"# Application terminated at {DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff")}"); } - private IList BuildMainMenu(Context context) + private IList BuildMainMenu(ProcedureContext context) { return new List { diff --git a/SafeExamBrowser.ResetUtility/Procedure/Initialization.cs b/SafeExamBrowser.ResetUtility/Procedure/Initialization.cs index 75bfefad..892a5a96 100644 --- a/SafeExamBrowser.ResetUtility/Procedure/Initialization.cs +++ b/SafeExamBrowser.ResetUtility/Procedure/Initialization.cs @@ -16,7 +16,7 @@ namespace SafeExamBrowser.ResetUtility.Procedure { private static readonly Mutex mutex = new Mutex(true, "safe_exam_browser_reset_mutex"); - public Initialization(Context context) : base(context) + public Initialization(ProcedureContext context) : base(context) { } diff --git a/SafeExamBrowser.ResetUtility/Procedure/Log.cs b/SafeExamBrowser.ResetUtility/Procedure/Log.cs index 53431734..be75b5cf 100644 --- a/SafeExamBrowser.ResetUtility/Procedure/Log.cs +++ b/SafeExamBrowser.ResetUtility/Procedure/Log.cs @@ -14,7 +14,7 @@ namespace SafeExamBrowser.ResetUtility.Procedure { internal class Log : ProcedureStep { - public Log(Context context) : base(context) + public Log(ProcedureContext context) : base(context) { } diff --git a/SafeExamBrowser.ResetUtility/Procedure/MainMenu.cs b/SafeExamBrowser.ResetUtility/Procedure/MainMenu.cs index c8ea2938..3dce5052 100644 --- a/SafeExamBrowser.ResetUtility/Procedure/MainMenu.cs +++ b/SafeExamBrowser.ResetUtility/Procedure/MainMenu.cs @@ -12,7 +12,7 @@ namespace SafeExamBrowser.ResetUtility.Procedure { internal class MainMenu : ProcedureStep { - public MainMenu(Context context) : base(context) + public MainMenu(ProcedureContext context) : base(context) { } diff --git a/SafeExamBrowser.ResetUtility/Context.cs b/SafeExamBrowser.ResetUtility/Procedure/ProcedureContext.cs similarity index 86% rename from SafeExamBrowser.ResetUtility/Context.cs rename to SafeExamBrowser.ResetUtility/Procedure/ProcedureContext.cs index c18da8ce..4d8ba85d 100644 --- a/SafeExamBrowser.ResetUtility/Context.cs +++ b/SafeExamBrowser.ResetUtility/Procedure/ProcedureContext.cs @@ -10,11 +10,10 @@ using System; using System.Collections.Generic; using SafeExamBrowser.Contracts.Lockdown; using SafeExamBrowser.Contracts.Logging; -using SafeExamBrowser.ResetUtility.Procedure; -namespace SafeExamBrowser.ResetUtility +namespace SafeExamBrowser.ResetUtility.Procedure { - internal class Context + internal class ProcedureContext { internal Func CreateBackup { get; set; } internal ILogger Logger { get; set; } diff --git a/SafeExamBrowser.ResetUtility/Procedure/ProcedureStep.cs b/SafeExamBrowser.ResetUtility/Procedure/ProcedureStep.cs index b956fdf0..320af50f 100644 --- a/SafeExamBrowser.ResetUtility/Procedure/ProcedureStep.cs +++ b/SafeExamBrowser.ResetUtility/Procedure/ProcedureStep.cs @@ -9,18 +9,23 @@ using System; using System.Collections.Generic; using System.Linq; +using System.Threading; +using System.Threading.Tasks; using SafeExamBrowser.Contracts.Logging; namespace SafeExamBrowser.ResetUtility.Procedure { internal abstract class ProcedureStep { - protected Context Context { get; } + private CancellationTokenSource progressAnimationToken; + private Task progressAnimation; + + protected ProcedureContext Context { get; } protected ILogger Logger => Context.Logger; protected ConsoleColor BackgroundColor => ConsoleColor.White; protected ConsoleColor ForegroundColor => ConsoleColor.Black; - internal ProcedureStep(Context context) + internal ProcedureStep(ProcedureContext context) { Context = context; } @@ -87,6 +92,18 @@ namespace SafeExamBrowser.ResetUtility.Procedure Console.Write($"[{new String('■', (int) progress)}{new String('─', (int) remaining)}] {current * 100 / total}%"); } + protected void StartProgressAnimation() + { + progressAnimationToken = new CancellationTokenSource(); + progressAnimation = Task.Run(new Action(ProgressAnimation)); + } + + protected void StopProgressAnimation() + { + progressAnimationToken?.Cancel(); + progressAnimation?.Wait(); + } + private void PrintMenu(IList options, int left, int top, bool showInstructions) { Console.SetCursorPosition(left, top); @@ -109,6 +126,43 @@ namespace SafeExamBrowser.ResetUtility.Procedure } } + private void ProgressAnimation() + { + var length = 12; + var max = Console.BufferWidth - 8; + var min = 1; + var left = 1; + var operand = 1; + + Console.Write($"[{new String('■', length)}{new String('─', max - length)}]"); + + while (!progressAnimationToken.IsCancellationRequested) + { + Console.SetCursorPosition(left, Console.CursorTop); + Console.Write(new String('─', length)); + + if (left + length > max) + { + operand = -1; + } + else if (left <= min) + { + operand = 1; + } + + left += operand; + + Console.SetCursorPosition(left, Console.CursorTop); + Console.Write(new String('■', length)); + + Thread.Sleep(20); + } + + Console.SetCursorPosition(0, Console.CursorTop); + Console.WriteLine(new String(' ', Console.BufferWidth)); + Console.SetCursorPosition(0, Console.CursorTop - 2); + } + private void SelectNextOption(ConsoleKey key, IList options) { var current = options.First(o => o.IsSelected); diff --git a/SafeExamBrowser.ResetUtility/Procedure/Reset.cs b/SafeExamBrowser.ResetUtility/Procedure/Reset.cs index 9097a46d..3300b6f2 100644 --- a/SafeExamBrowser.ResetUtility/Procedure/Reset.cs +++ b/SafeExamBrowser.ResetUtility/Procedure/Reset.cs @@ -10,7 +10,7 @@ namespace SafeExamBrowser.ResetUtility.Procedure { internal class Reset : ProcedureStep { - public Reset(Context context) : base(context) + public Reset(ProcedureContext context) : base(context) { } diff --git a/SafeExamBrowser.ResetUtility/Procedure/Restore.cs b/SafeExamBrowser.ResetUtility/Procedure/Restore.cs index fdee1007..4adabc01 100644 --- a/SafeExamBrowser.ResetUtility/Procedure/Restore.cs +++ b/SafeExamBrowser.ResetUtility/Procedure/Restore.cs @@ -20,7 +20,7 @@ namespace SafeExamBrowser.ResetUtility.Procedure { private ProcedureStep next; - public Restore(Context context) : base(context) + public Restore(ProcedureContext context) : base(context) { next = new MainMenu(Context); } @@ -67,6 +67,7 @@ namespace SafeExamBrowser.ResetUtility.Procedure Logger.Info($"Found backup file '{filePath}' with {configurations.Count} items. Initiating restore procedure..."); Console.WriteLine($"Found backup file with {configurations.Count} items."); + Console.WriteLine(); Console.WriteLine("Initiating restore procedure..."); foreach (var configuration in configurations) @@ -106,7 +107,9 @@ namespace SafeExamBrowser.ResetUtility.Procedure Logger.Info("Starting system configuration update..."); Console.WriteLine(); Console.WriteLine("Performing system configuration update, please wait..."); + StartProgressAnimation(); Context.Update.Execute(); + StopProgressAnimation(); Logger.Info("Update completed."); Console.WriteLine("Update completed."); } diff --git a/SafeExamBrowser.ResetUtility/Procedure/Version.cs b/SafeExamBrowser.ResetUtility/Procedure/Version.cs index 4144f7bc..b0800e26 100644 --- a/SafeExamBrowser.ResetUtility/Procedure/Version.cs +++ b/SafeExamBrowser.ResetUtility/Procedure/Version.cs @@ -13,7 +13,7 @@ namespace SafeExamBrowser.ResetUtility.Procedure { internal class Version : ProcedureStep { - public Version(Context context) : base(context) + public Version(ProcedureContext context) : base(context) { } diff --git a/SafeExamBrowser.ResetUtility/SafeExamBrowser.ResetUtility.csproj b/SafeExamBrowser.ResetUtility/SafeExamBrowser.ResetUtility.csproj index 54830eb1..b12c3df3 100644 --- a/SafeExamBrowser.ResetUtility/SafeExamBrowser.ResetUtility.csproj +++ b/SafeExamBrowser.ResetUtility/SafeExamBrowser.ResetUtility.csproj @@ -60,7 +60,7 @@ - +