From 00213199096c807f092cc60bbca81aed57111bd6 Mon Sep 17 00:00:00 2001 From: Florian Stinglmayr Date: Wed, 6 Aug 2025 14:45:47 +0200 Subject: [PATCH] start implementing commands --- EDPlayerJournal | 2 +- src/Commands/Command.cs | 38 ++++++++++++++++++++++ src/Commands/ExitCommand.cs | 14 ++++++++ src/Commands/ICommand.cs | 11 +++++++ src/Construction/ConstructionHelperPage.cs | 1 + src/Edith.cs | 22 +++++++++++-- src/MainWindow.cs | 13 ++++++++ src/Program.cs | 2 +- 8 files changed, 98 insertions(+), 5 deletions(-) create mode 100644 src/Commands/Command.cs create mode 100644 src/Commands/ExitCommand.cs create mode 100644 src/Commands/ICommand.cs diff --git a/EDPlayerJournal b/EDPlayerJournal index ed68876..26bf6f5 160000 --- a/EDPlayerJournal +++ b/EDPlayerJournal @@ -1 +1 @@ -Subproject commit ed68876300b2b32b97c2dfc436f3fb98e7f1a45d +Subproject commit 26bf6f5e0268e815779458bbc8a8fe40c576c837 diff --git a/src/Commands/Command.cs b/src/Commands/Command.cs new file mode 100644 index 0000000..12a60fe --- /dev/null +++ b/src/Commands/Command.cs @@ -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 commands = new() { + new ExitCommand() + }; + + public static async Task Execute(string cmdline) { + Regex r = new("\\d"); + List 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; + } +} + diff --git a/src/Commands/ExitCommand.cs b/src/Commands/ExitCommand.cs new file mode 100644 index 0000000..f37906e --- /dev/null +++ b/src/Commands/ExitCommand.cs @@ -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(); + } +} + diff --git a/src/Commands/ICommand.cs b/src/Commands/ICommand.cs new file mode 100644 index 0000000..cf4cb86 --- /dev/null +++ b/src/Commands/ICommand.cs @@ -0,0 +1,11 @@ +namespace Edith.Commands; + +public interface ICommand { + string Name { get; } + + string[]? Aliases { get; } + + string Description { get; } + + Task Execute(string[] args); +} diff --git a/src/Construction/ConstructionHelperPage.cs b/src/Construction/ConstructionHelperPage.cs index 38019b6..826299e 100644 --- a/src/Construction/ConstructionHelperPage.cs +++ b/src/Construction/ConstructionHelperPage.cs @@ -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); diff --git a/src/Edith.cs b/src/Edith.cs index 2e50cb5..c5a4f85 100644 --- a/src/Edith.cs +++ b/src/Edith.cs @@ -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; } } diff --git a/src/MainWindow.cs b/src/MainWindow.cs index 2c0dd1b..988121d 100644 --- a/src/MainWindow.cs +++ b/src/MainWindow.cs @@ -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) diff --git a/src/Program.cs b/src/Program.cs index 1ca886c..9313f40 100644 --- a/src/Program.cs +++ b/src/Program.cs @@ -3,6 +3,6 @@ public class Program { public static async Task Main(string[] args) { - await Edith.Instance.Run(); + await Edith.Instance.Run().ConfigureAwait(false); } } \ No newline at end of file