Added simple, modular command parsing
This commit is contained in:
parent
e41332cf85
commit
20158802dd
18
commands/ClearCommand.cs
Normal file
18
commands/ClearCommand.cs
Normal 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");
|
||||
}
|
||||
}
|
||||
}
|
||||
1
commands/ClearCommand.cs.uid
Normal file
1
commands/ClearCommand.cs.uid
Normal file
@ -0,0 +1 @@
|
||||
uid://clober5sjosj3
|
||||
12
commands/Command.cs
Normal file
12
commands/Command.cs
Normal 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
1
commands/Command.cs.uid
Normal file
@ -0,0 +1 @@
|
||||
uid://d20rwntnb4hf7
|
||||
58
commands/ConsoleManager.cs
Normal file
58
commands/ConsoleManager.cs
Normal 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
1
commands/ConsoleManager.cs.uid
Normal file
1
commands/ConsoleManager.cs.uid
Normal file
@ -0,0 +1 @@
|
||||
uid://bwl1i4sf5eysn
|
||||
37
commands/HelpCommand.cs
Normal file
37
commands/HelpCommand.cs
Normal 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}");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
1
commands/HelpCommand.cs.uid
Normal file
1
commands/HelpCommand.cs.uid
Normal file
@ -0,0 +1 @@
|
||||
uid://hungsjp7fpvo
|
||||
0
commands/TimeCommand.cs
Normal file
0
commands/TimeCommand.cs
Normal file
1
commands/TimeCommand.cs.uid
Normal file
1
commands/TimeCommand.cs.uid
Normal file
@ -0,0 +1 @@
|
||||
uid://t8o6quogxufd
|
||||
@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user