Added simple, modular command parsing

This commit is contained in:
Vico 2026-04-04 10:08:24 +02:00
parent e41332cf85
commit 20158802dd
11 changed files with 164 additions and 10 deletions

18
commands/ClearCommand.cs Normal file
View File

@ -0,0 +1,18 @@
using System;
using YourProject.Commands;
namespace YourProject.Commands
{
public class ClearCommand : Command
{
public override string Name => "clear";
public override string Description => "Clears all text from the console output";
public override string Usage => "clear";
public override void Execute(string[] args, Action<string> outputCallback)
{
// Send a special marker that the console UI will recognize
outputCallback("CLEAR_CONSOLE");
}
}
}

View File

@ -0,0 +1 @@
uid://clober5sjosj3

12
commands/Command.cs Normal file
View File

@ -0,0 +1,12 @@
using System;
namespace YourProject.Commands // Add namespace
{
public abstract class Command
{
public abstract string Name { get; }
public abstract string Description { get; }
public abstract string Usage { get; }
public abstract void Execute(string[] args, Action<string> outputCallback);
}
}

1
commands/Command.cs.uid Normal file
View File

@ -0,0 +1 @@
uid://d20rwntnb4hf7

View File

@ -0,0 +1,58 @@
using System;
using System.Collections.Generic;
using System.Linq;
using YourProject.Commands;
namespace YourProject.Commands
{
public static class ConsoleManager
{
private static Dictionary<string, Command> commands = new Dictionary<string, Command>(StringComparer.OrdinalIgnoreCase);
static ConsoleManager()
{
// Register all commands
RegisterCommand(new HelpCommand());
RegisterCommand(new ClearCommand());
}
public static void RegisterCommand(Command command)
{
commands[command.Name] = command;
}
public static Command GetCommand(string name)
{
return commands.ContainsKey(name) ? commands[name] : null;
}
public static IEnumerable<Command> GetAllCommands()
{
return commands.Values;
}
public static bool Execute(string input, Action<string> outputCallback)
{
string[] parts = input.Trim().Split(' ', StringSplitOptions.RemoveEmptyEntries);
if (parts.Length == 0) return false;
string commandName = parts[0];
string[] args = parts.Skip(1).ToArray();
if (commands.ContainsKey(commandName))
{
try
{
commands[commandName].Execute(args, outputCallback);
}
catch (Exception e)
{
outputCallback($"Error: {e.Message}");
}
return true;
}
return false;
}
}
}

View File

@ -0,0 +1 @@
uid://bwl1i4sf5eysn

37
commands/HelpCommand.cs Normal file
View File

@ -0,0 +1,37 @@
using System;
using YourProject.Commands; // Reference the namespace
namespace YourProject.Commands
{
public class HelpCommand : Command
{
public override string Name => "help";
public override string Description => "Shows available commands";
public override string Usage => "help [command]";
public override void Execute(string[] args, Action<string> outputCallback)
{
if (args.Length > 0)
{
var command = ConsoleManager.GetCommand(args[0]);
if (command != null)
{
outputCallback($"{command.Name} - {command.Description}");
outputCallback($"Usage: {command.Usage}");
}
else
{
outputCallback($"Unknown command: {args[0]}");
}
}
else
{
outputCallback("Available commands:");
foreach (var cmd in ConsoleManager.GetAllCommands())
{
outputCallback($" {cmd.Name,-10} - {cmd.Description}");
}
}
}
}
}

View File

@ -0,0 +1 @@
uid://hungsjp7fpvo

0
commands/TimeCommand.cs Normal file
View File

View File

@ -0,0 +1 @@
uid://t8o6quogxufd

View File

@ -1,5 +1,6 @@
using Godot;
using System;
using YourProject.Commands; // Add this using statement
public partial class ConsoleUI : CanvasLayer
{
@ -10,27 +11,50 @@ public partial class ConsoleUI : CanvasLayer
{
consoleOutput = GetNode<Label>("ConsoleOutput");
consoleInput = GetNode<LineEdit>("ConsoleInput");
consoleInput.TextSubmitted += OnTextSubmitted;
consoleInput.GrabFocus();
Callable.From(() => consoleInput.GrabFocus()).CallDeferred();
}
private void OnTextSubmitted(string submittedText)
{
if (!string.IsNullOrWhiteSpace(submittedText))
if (string.IsNullOrWhiteSpace(submittedText))
{
string currentOutput = consoleOutput.Text;
if (!string.IsNullOrEmpty(currentOutput))
consoleInput.Clear();
return;
}
AddToConsole($"> {submittedText}");
bool commandExecuted = ConsoleManager.Execute(submittedText, (output) =>
{
if (output == "CLEAR_CONSOLE")
{
consoleOutput.Text = currentOutput + "\n" + submittedText;
consoleOutput.Text = "";
}
else
{
consoleOutput.Text = submittedText;
AddToConsole(output);
}
consoleInput.Clear();
});
if (!commandExecuted)
{
AddToConsole($"Unknown command. Type 'help' for available commands.");
}
consoleInput.Clear();
Callable.From(() => consoleInput.GrabFocus()).CallDeferred();
}
private void AddToConsole(string text)
{
if (string.IsNullOrEmpty(consoleOutput.Text))
{
consoleOutput.Text = text;
}
else
{
consoleOutput.Text += "\n" + text;
}
}
}