From 7f58fb4aaf511aca6f96f1e902734ff535506c11 Mon Sep 17 00:00:00 2001 From: Stone_Red <56473591+Stone-Red-Code@users.noreply.github.com> Date: Wed, 4 Mar 2026 16:01:23 +0100 Subject: [PATCH] Add step debug mode and refactor command handling for run/debug --- YesNt.CodeEditor/Editor.cs | 11 +++- YesNt.CodeEditor/InputHandler.cs | 102 +++++++++++++++++++++++-------- 2 files changed, 85 insertions(+), 28 deletions(-) diff --git a/YesNt.CodeEditor/Editor.cs b/YesNt.CodeEditor/Editor.cs index f3f7698..f2b2973 100644 --- a/YesNt.CodeEditor/Editor.cs +++ b/YesNt.CodeEditor/Editor.cs @@ -20,6 +20,7 @@ internal class TextEditor public Point CursorPosition { get; } = new(0, 0); public Mode EditMode { get; set; } = Mode.Command; public string CurrentPath { get; set; } = string.Empty; + public bool IsStepDebugMode { get; set; } public TextEditor(string path) : this() { @@ -241,6 +242,14 @@ internal class TextEditor { Console.Write(output); } + + if (IsStepDebugMode && e is not null && !e.IsTask) + { + Console.ForegroundColor = ConsoleColor.DarkGray; + Console.WriteLine("[Step] Press any key for next line (Ctrl+C to stop)..."); + Console.ForegroundColor = ConsoleColor.Gray; + _ = Console.ReadKey(true); + } } } @@ -279,4 +288,4 @@ internal enum Mode Edit, Command, Debug -} \ No newline at end of file +} diff --git a/YesNt.CodeEditor/InputHandler.cs b/YesNt.CodeEditor/InputHandler.cs index 924e5b2..bf24a9e 100644 --- a/YesNt.CodeEditor/InputHandler.cs +++ b/YesNt.CodeEditor/InputHandler.cs @@ -225,36 +225,18 @@ internal class InputHandler(TextEditor textEditor) break; case "run": - if (textEditor.Save(input, true)) - { - textEditor.EditMode = Mode.Debug; - Console.Clear(); - Console.CursorVisible = true; - textEditor.YesNtInterpreter.Execute(textEditor.CurrentPath); - while (Console.KeyAvailable) - { - _ = Console.ReadKey(true); - } - _ = Console.ReadKey(); - WriteStatus(string.Empty); - textEditor.EditMode = Mode.Command; - } + ExecuteWithDebugScreen(input, false, false); break; case "debug": - if (textEditor.Save(input, true)) + if (TryParseDebugCommand(input, out bool stepMode, out string parsedPath)) { - textEditor.EditMode = Mode.Debug; - Console.Clear(); - Console.CursorVisible = true; - textEditor.YesNtInterpreter.Execute(textEditor.CurrentPath, true); - while (Console.KeyAvailable) - { - _ = Console.ReadKey(true); - } - _ = Console.ReadKey(); - WriteStatus(string.Empty); - textEditor.EditMode = Mode.Command; + string saveInput = string.IsNullOrWhiteSpace(parsedPath) ? "debug" : $"debug {parsedPath}"; + ExecuteWithDebugScreen(saveInput, true, stepMode); + } + else + { + WriteStatus("Invalid arguments!"); } break; @@ -309,4 +291,70 @@ internal class InputHandler(TextEditor textEditor) Console.SetCursorPosition(0, Console.WindowHeight - 1); Console.Write(input + new string(' ', Console.WindowWidth - input.Length - 1)); } -} \ No newline at end of file + + private static bool TryParseDebugCommand(string input, out bool stepMode, out string path) + { + stepMode = false; + path = string.Empty; + + string[] parts = input.Split(' ', StringSplitOptions.RemoveEmptyEntries); + if (parts.Length == 0 || !parts[0].Equals("debug", StringComparison.OrdinalIgnoreCase)) + { + return false; + } + + for (int i = 1; i < parts.Length; i++) + { + string token = parts[i]; + if (token.Equals("step", StringComparison.OrdinalIgnoreCase)) + { + if (stepMode) + { + return false; + } + + stepMode = true; + continue; + } + + if (string.IsNullOrWhiteSpace(path)) + { + path = token; + continue; + } + + return false; + } + + return true; + } + + private void ExecuteWithDebugScreen(string saveInput, bool debugMode, bool stepMode) + { + if (textEditor.Save(saveInput, true)) + { + textEditor.EditMode = Mode.Debug; + textEditor.IsStepDebugMode = stepMode; + Console.Clear(); + Console.CursorVisible = true; + + if (debugMode) + { + textEditor.YesNtInterpreter.Execute(textEditor.CurrentPath, true); + } + else + { + textEditor.YesNtInterpreter.Execute(textEditor.CurrentPath); + } + + while (Console.KeyAvailable) + { + _ = Console.ReadKey(true); + } + _ = Console.ReadKey(); + WriteStatus(string.Empty); + textEditor.IsStepDebugMode = false; + textEditor.EditMode = Mode.Command; + } + } +}