From 2a3e4bb8f9f153ebf64805cbd420f855f850c78b Mon Sep 17 00:00:00 2001 From: d10sfan <4337981+d10sfan@users.noreply.github.com> Date: Wed, 27 Dec 2023 10:45:23 -0600 Subject: [PATCH] Server Console Interaction Improvements (#89) * Improve CPU usage by delay inside ReadCommand * Remove backspaced characters from console display --- src/Rasa.Utils/Commands/CommandProcessor.cs | 10 +++++++--- src/Rasa.Utils/Hosting/RasaHost.cs | 2 +- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/src/Rasa.Utils/Commands/CommandProcessor.cs b/src/Rasa.Utils/Commands/CommandProcessor.cs index 7bf2cab0..9c26d030 100644 --- a/src/Rasa.Utils/Commands/CommandProcessor.cs +++ b/src/Rasa.Utils/Commands/CommandProcessor.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.Threading; +using System.Threading.Tasks; namespace Rasa.Commands { @@ -8,9 +9,9 @@ public static class CommandProcessor { private static readonly Dictionary> Commands = new Dictionary>(); - public static void ProcessCommand(CancellationToken stopToken) + public static async Task ProcessCommand(CancellationToken stopToken) { - var command = ReadCommand(stopToken); + var command = await ReadCommand(stopToken); if (string.IsNullOrWhiteSpace(command)) return; @@ -27,7 +28,7 @@ public static void ProcessCommand(CancellationToken stopToken) Logger.WriteLog(LogType.Command, $"Invalid command: {command}"); } - private static string ReadCommand(CancellationToken stopToken) + private static async Task ReadCommand(CancellationToken stopToken) { var command = string.Empty; while (!stopToken.IsCancellationRequested) @@ -41,12 +42,15 @@ private static string ReadCommand(CancellationToken stopToken) return command; case ConsoleKey.Backspace: command = command.Substring(0, command.Length - 1); + Console.Write("\b \b"); break; default: command += key.KeyChar; break; } } + + await Task.Delay(25, stopToken); } return null; } diff --git a/src/Rasa.Utils/Hosting/RasaHost.cs b/src/Rasa.Utils/Hosting/RasaHost.cs index a42098a3..e5c20545 100644 --- a/src/Rasa.Utils/Hosting/RasaHost.cs +++ b/src/Rasa.Utils/Hosting/RasaHost.cs @@ -50,7 +50,7 @@ private async Task ProcessCommands(CancellationToken stoppingToken) while (_rasaServer.Running && !stoppingToken.IsCancellationRequested) { - CommandProcessor.ProcessCommand(stoppingToken); + await CommandProcessor.ProcessCommand(stoppingToken); await Task.Delay(25, stoppingToken); } }