Skip to content

Commit

Permalink
Server Console Interaction Improvements (#89)
Browse files Browse the repository at this point in the history
* Improve CPU usage by delay inside ReadCommand

* Remove backspaced characters from console display
  • Loading branch information
d10sfan authored Dec 27, 2023
1 parent 9b96c79 commit 2a3e4bb
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
10 changes: 7 additions & 3 deletions src/Rasa.Utils/Commands/CommandProcessor.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;

namespace Rasa.Commands
{
public static class CommandProcessor
{
private static readonly Dictionary<string, Action<string[]>> Commands = new Dictionary<string, Action<string[]>>();

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;

Expand All @@ -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<string> ReadCommand(CancellationToken stopToken)
{
var command = string.Empty;
while (!stopToken.IsCancellationRequested)
Expand All @@ -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;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Rasa.Utils/Hosting/RasaHost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Expand Down

0 comments on commit 2a3e4bb

Please sign in to comment.