SEBWIN-320: Implemented progress animation for reset utility.

This commit is contained in:
dbuechel 2019-07-18 10:36:41 +02:00
parent 86a3e9ce3c
commit 2754cdcc56
10 changed files with 70 additions and 14 deletions

View file

@ -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<MainMenuOption> BuildMainMenu(Context context)
private IList<MainMenuOption> BuildMainMenu(ProcedureContext context)
{
return new List<MainMenuOption>
{

View file

@ -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)
{
}

View file

@ -14,7 +14,7 @@ namespace SafeExamBrowser.ResetUtility.Procedure
{
internal class Log : ProcedureStep
{
public Log(Context context) : base(context)
public Log(ProcedureContext context) : base(context)
{
}

View file

@ -12,7 +12,7 @@ namespace SafeExamBrowser.ResetUtility.Procedure
{
internal class MainMenu : ProcedureStep
{
public MainMenu(Context context) : base(context)
public MainMenu(ProcedureContext context) : base(context)
{
}

View file

@ -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<string, IFeatureConfigurationBackup> CreateBackup { get; set; }
internal ILogger Logger { get; set; }

View file

@ -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<MenuOption> 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<MenuOption> options)
{
var current = options.First(o => o.IsSelected);

View file

@ -10,7 +10,7 @@ namespace SafeExamBrowser.ResetUtility.Procedure
{
internal class Reset : ProcedureStep
{
public Reset(Context context) : base(context)
public Reset(ProcedureContext context) : base(context)
{
}

View file

@ -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.");
}

View file

@ -13,7 +13,7 @@ namespace SafeExamBrowser.ResetUtility.Procedure
{
internal class Version : ProcedureStep
{
public Version(Context context) : base(context)
public Version(ProcedureContext context) : base(context)
{
}

View file

@ -60,7 +60,7 @@
<Reference Include="Microsoft.CSharp" />
</ItemGroup>
<ItemGroup>
<Compile Include="Context.cs" />
<Compile Include="Procedure\ProcedureContext.cs" />
<Compile Include="CompositionRoot.cs" />
<Compile Include="NativeMethods.cs" />
<Compile Include="Procedure\Initialization.cs" />