start implementing commands

This commit is contained in:
Florian Stinglmayr 2025-08-06 14:45:47 +02:00
parent 6292428d86
commit 0021319909
8 changed files with 98 additions and 5 deletions

@ -1 +1 @@
Subproject commit ed68876300b2b32b97c2dfc436f3fb98e7f1a45d
Subproject commit 26bf6f5e0268e815779458bbc8a8fe40c576c837

38
src/Commands/Command.cs Normal file
View File

@ -0,0 +1,38 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
namespace Edith.Commands;
public class Command {
public static List<ICommand> commands = new() {
new ExitCommand()
};
public static async Task<bool> Execute(string cmdline) {
Regex r = new("\\d");
List<string> args = new(r.Split(cmdline));
if (args.Count <= 0) {
return false;
}
string cmd = args[0];
args.RemoveAt(0);
ICommand? command = commands
.FirstOrDefault(x => x.Name == cmd || (x.Aliases != null && x.Aliases.Contains(cmd)))
;
if (command == null) {
return false;
}
await command.Execute(args.ToArray()).ConfigureAwait(false);
return true;
}
}

View File

@ -0,0 +1,14 @@
namespace Edith.Commands;
internal class ExitCommand : ICommand {
public string Name => "exit";
public string Description => "exits the program";
public string[]? Aliases => ["quit", "q"];
public async Task Execute(string[] args) {
Edith.Instance.Quit();
}
}

11
src/Commands/ICommand.cs Normal file
View File

@ -0,0 +1,11 @@
namespace Edith.Commands;
public interface ICommand {
string Name { get; }
string[]? Aliases { get; }
string Description { get; }
Task Execute(string[] args);
}

View File

@ -61,6 +61,7 @@ internal class ConstructionHelperPage : IPage {
if (depot == null) {
depot = new() { Station = station, Progress = e.ConstructionProgress };
colony.Depots.Add(depot);
tree.RefreshObject(colony);
} else {
depot.Progress = e.ConstructionProgress;
tree.RefreshObject(depot, false);

View File

@ -42,8 +42,10 @@ internal class Edith {
}
private async Task ProcessQueue() {
while (watcher != null) {
watcher.ProcessQueues();
while (!quit) {
if (watcher != null) {
watcher.ProcessQueues();
}
await Task.Delay(100);
}
}
@ -56,6 +58,7 @@ internal class Edith {
}
state = Application.Begin(window);
window.Running = true;
while (!quit) {
Application.RunLoop(state);
@ -65,6 +68,17 @@ internal class Edith {
Application.End(state);
}
public void Quit() {
if (window != null) {
quit = true;
window.Running = false;
}
if (watcher != null) {
watcher.Close();
watcher = null;
}
}
public async Task Run() {
try {
SetupJournal();
@ -80,8 +94,10 @@ internal class Edith {
Task gui = Task.Run(RunGUILoop);
Task queue = Task.Run(ProcessQueue);
await Task.WhenAll([gui, queue]);
await queue.ConfigureAwait(false);
await gui.ConfigureAwait(false);
window.Dispose();
window = null;
}
}

View File

@ -5,6 +5,7 @@ using Terminal.Gui.Input;
using Terminal.Gui.Drivers;
using Edith.Construction;
using Edith.Commands;
namespace Edith;
@ -36,6 +37,7 @@ internal class MainWindow : Window {
Caption = "> _",
Width = Dim.Fill()
};
command.KeyDown += OnCommandKeyDown;
commandView.Add(command);
Add(commandView);
@ -56,6 +58,17 @@ internal class MainWindow : Window {
}
}
private void ProcessCommand(string cmd) {
Task.Run(() => Commands.Command.Execute(cmd));
}
private void OnCommandKeyDown(object? sender, Key key) {
if (key == Key.Enter) {
ProcessCommand(command.Text);
command.Text = "";
}
}
public void ShowPage(string name) {
int page = pages
.IndexOf(x => string.Compare(x.Name, name, StringComparison.InvariantCultureIgnoreCase) == 0)

View File

@ -3,6 +3,6 @@
public class Program
{
public static async Task Main(string[] args) {
await Edith.Instance.Run();
await Edith.Instance.Run().ConfigureAwait(false);
}
}