diff --git a/Examples/Example/Example.cs b/Examples/Example/Example.cs index f4738504d..7fab15af2 100644 --- a/Examples/Example/Example.cs +++ b/Examples/Example/Example.cs @@ -12,16 +12,16 @@ using Terminal.Gui.Views; [assembly: ExampleMetadata ("Simple Example", "A basic login form demonstrating Terminal.Gui fundamentals")] [assembly: ExampleCategory ("Getting Started")] -[assembly: ExampleDemoKeyStrokes (KeyStrokes = new [] { "a", "d", "m", "i", "n", "Tab", "p", "a", "s", "s", "w", "o", "r", "d", "Enter" }, Order = 1)] -[assembly: ExampleDemoKeyStrokes (KeyStrokes = new [] { "Enter" }, DelayMs = 500, Order = 2)] -[assembly: ExampleDemoKeyStrokes (KeyStrokes = new [] { "Esc" }, DelayMs = 100, Order = 3)] +[assembly: ExampleDemoKeyStrokes (KeyStrokes = ["a", "d", "m", "i", "n", "Tab", "p", "a", "s", "s", "w", "o", "r", "d", "Enter"], Order = 1)] +[assembly: ExampleDemoKeyStrokes (KeyStrokes = ["Enter"], DelayMs = 500, Order = 2)] +[assembly: ExampleDemoKeyStrokes (KeyStrokes = ["Esc"], DelayMs = 100, Order = 3)] // Override the default configuration for the application to use the Light theme ConfigurationManager.RuntimeConfig = """{ "Theme": "Light" }"""; ConfigurationManager.Enable (ConfigLocations.All); // Check for test context to determine driver -string? contextJson = Environment.GetEnvironmentVariable (ExampleContext.EnvironmentVariableName); +string? contextJson = Environment.GetEnvironmentVariable (ExampleContext.ENVIRONMENT_VARIABLE_NAME); string? driverName = null; if (!string.IsNullOrEmpty (contextJson)) diff --git a/Examples/FluentExample/Program.cs b/Examples/FluentExample/Program.cs index 52184adda..97e25c19a 100644 --- a/Examples/FluentExample/Program.cs +++ b/Examples/FluentExample/Program.cs @@ -10,11 +10,11 @@ using Terminal.Gui.Views; [assembly: ExampleMetadata ("Fluent API Example", "Demonstrates the fluent IApplication API with IRunnable pattern")] [assembly: ExampleCategory ("API Patterns")] [assembly: ExampleCategory ("Controls")] -[assembly: ExampleDemoKeyStrokes (KeyStrokes = new [] { "CursorDown", "CursorDown", "CursorRight", "Enter" }, Order = 1)] -[assembly: ExampleDemoKeyStrokes (KeyStrokes = new [] { "Esc" }, DelayMs = 100, Order = 2)] +[assembly: ExampleDemoKeyStrokes (KeyStrokes = ["CursorDown", "CursorDown", "CursorRight", "Enter"], Order = 1)] +[assembly: ExampleDemoKeyStrokes (KeyStrokes = ["Esc"], DelayMs = 100, Order = 2)] // Check for test context to determine driver -string? contextJson = Environment.GetEnvironmentVariable (ExampleContext.EnvironmentVariableName); +string? contextJson = Environment.GetEnvironmentVariable (ExampleContext.ENVIRONMENT_VARIABLE_NAME); string? driverName = null; if (!string.IsNullOrEmpty (contextJson)) diff --git a/Examples/RunnableWrapperExample/Program.cs b/Examples/RunnableWrapperExample/Program.cs index 9c16a26ef..aedf66e54 100644 --- a/Examples/RunnableWrapperExample/Program.cs +++ b/Examples/RunnableWrapperExample/Program.cs @@ -10,14 +10,14 @@ using Terminal.Gui.Views; [assembly: ExampleMetadata ("Runnable Wrapper Example", "Shows how to wrap any View to make it runnable without implementing IRunnable")] [assembly: ExampleCategory ("API Patterns")] [assembly: ExampleCategory ("Views")] -[assembly: ExampleDemoKeyStrokes (KeyStrokes = new [] { "t", "e", "s", "t", "Esc" }, Order = 1)] -[assembly: ExampleDemoKeyStrokes (KeyStrokes = new [] { "Enter", "Esc" }, DelayMs = 100, Order = 2)] -[assembly: ExampleDemoKeyStrokes (KeyStrokes = new [] { "Enter", "Esc" }, DelayMs = 100, Order = 3)] -[assembly: ExampleDemoKeyStrokes (KeyStrokes = new [] { "Enter", "Esc" }, DelayMs = 100, Order = 4)] -[assembly: ExampleDemoKeyStrokes (KeyStrokes = new [] { "Enter", "Esc" }, DelayMs = 100, Order = 5)] +[assembly: ExampleDemoKeyStrokes (KeyStrokes = ["t", "e", "s", "t", "Esc"], Order = 1)] +[assembly: ExampleDemoKeyStrokes (KeyStrokes = ["Enter", "Esc"], DelayMs = 100, Order = 2)] +[assembly: ExampleDemoKeyStrokes (KeyStrokes = ["Enter", "Esc"], DelayMs = 100, Order = 3)] +[assembly: ExampleDemoKeyStrokes (KeyStrokes = ["Enter", "Esc"], DelayMs = 100, Order = 4)] +[assembly: ExampleDemoKeyStrokes (KeyStrokes = ["Enter", "Esc"], DelayMs = 100, Order = 5)] // Check for test context to determine driver -string? contextJson = Environment.GetEnvironmentVariable (ExampleContext.EnvironmentVariableName); +string? contextJson = Environment.GetEnvironmentVariable (ExampleContext.ENVIRONMENT_VARIABLE_NAME); string? driverName = null; if (!string.IsNullOrEmpty (contextJson)) diff --git a/Terminal.Gui/Drivers/FakeDriver/FakeComponentFactory.cs b/Terminal.Gui/Drivers/FakeDriver/FakeComponentFactory.cs index 3700c2d03..77fc1acdd 100644 --- a/Terminal.Gui/Drivers/FakeDriver/FakeComponentFactory.cs +++ b/Terminal.Gui/Drivers/FakeDriver/FakeComponentFactory.cs @@ -9,10 +9,6 @@ namespace Terminal.Gui.Drivers; /// public class FakeComponentFactory : ComponentFactoryImpl { - private readonly FakeInput? _input; - private readonly IOutput? _output; - private readonly ISizeMonitor? _sizeMonitor; - /// /// Creates a new FakeComponentFactory with optional output capture. /// @@ -26,12 +22,9 @@ public class FakeComponentFactory : ComponentFactoryImpl _sizeMonitor = sizeMonitor; } - - /// - public override ISizeMonitor CreateSizeMonitor (IOutput consoleOutput, IOutputBuffer outputBuffer) - { - return _sizeMonitor ?? new SizeMonitorImpl (consoleOutput); - } + private readonly FakeInput? _input; + private readonly IOutput? _output; + private readonly ISizeMonitor? _sizeMonitor; /// public override IInput CreateInput () @@ -40,7 +33,7 @@ public class FakeComponentFactory : ComponentFactoryImpl FakeInput fakeInput = _input ?? new FakeInput (); // Check for test context in environment variable - string? contextJson = Environment.GetEnvironmentVariable (ExampleContext.EnvironmentVariableName); + string? contextJson = Environment.GetEnvironmentVariable (ExampleContext.ENVIRONMENT_VARIABLE_NAME); if (!string.IsNullOrEmpty (contextJson)) { @@ -50,7 +43,7 @@ public class FakeComponentFactory : ComponentFactoryImpl { foreach (string keyStr in context.KeysToInject) { - if (Input.Key.TryParse (keyStr, out Input.Key? key) && key is { }) + if (Key.TryParse (keyStr, out Key? key) && key is { }) { ConsoleKeyInfo consoleKeyInfo = ConvertKeyToConsoleKeyInfo (key); fakeInput.AddInput (consoleKeyInfo); @@ -62,7 +55,19 @@ public class FakeComponentFactory : ComponentFactoryImpl return fakeInput; } - private static ConsoleKeyInfo ConvertKeyToConsoleKeyInfo (Input.Key key) + /// + public override IInputProcessor CreateInputProcessor (ConcurrentQueue inputBuffer) { return new FakeInputProcessor (inputBuffer); } + + /// + public override IOutput CreateOutput () { return _output ?? new FakeOutput (); } + + /// + public override ISizeMonitor CreateSizeMonitor (IOutput consoleOutput, IOutputBuffer outputBuffer) + { + return _sizeMonitor ?? new SizeMonitorImpl (consoleOutput); + } + + private static ConsoleKeyInfo ConvertKeyToConsoleKeyInfo (Key key) { ConsoleModifiers modifiers = 0; @@ -86,71 +91,71 @@ public class FakeComponentFactory : ComponentFactoryImpl // Map KeyCode to ConsoleKey ConsoleKey consoleKey = baseKeyCode switch - { - KeyCode.A => ConsoleKey.A, - KeyCode.B => ConsoleKey.B, - KeyCode.C => ConsoleKey.C, - KeyCode.D => ConsoleKey.D, - KeyCode.E => ConsoleKey.E, - KeyCode.F => ConsoleKey.F, - KeyCode.G => ConsoleKey.G, - KeyCode.H => ConsoleKey.H, - KeyCode.I => ConsoleKey.I, - KeyCode.J => ConsoleKey.J, - KeyCode.K => ConsoleKey.K, - KeyCode.L => ConsoleKey.L, - KeyCode.M => ConsoleKey.M, - KeyCode.N => ConsoleKey.N, - KeyCode.O => ConsoleKey.O, - KeyCode.P => ConsoleKey.P, - KeyCode.Q => ConsoleKey.Q, - KeyCode.R => ConsoleKey.R, - KeyCode.S => ConsoleKey.S, - KeyCode.T => ConsoleKey.T, - KeyCode.U => ConsoleKey.U, - KeyCode.V => ConsoleKey.V, - KeyCode.W => ConsoleKey.W, - KeyCode.X => ConsoleKey.X, - KeyCode.Y => ConsoleKey.Y, - KeyCode.Z => ConsoleKey.Z, - KeyCode.D0 => ConsoleKey.D0, - KeyCode.D1 => ConsoleKey.D1, - KeyCode.D2 => ConsoleKey.D2, - KeyCode.D3 => ConsoleKey.D3, - KeyCode.D4 => ConsoleKey.D4, - KeyCode.D5 => ConsoleKey.D5, - KeyCode.D6 => ConsoleKey.D6, - KeyCode.D7 => ConsoleKey.D7, - KeyCode.D8 => ConsoleKey.D8, - KeyCode.D9 => ConsoleKey.D9, - KeyCode.Enter => ConsoleKey.Enter, - KeyCode.Esc => ConsoleKey.Escape, - KeyCode.Space => ConsoleKey.Spacebar, - KeyCode.Tab => ConsoleKey.Tab, - KeyCode.Backspace => ConsoleKey.Backspace, - KeyCode.Delete => ConsoleKey.Delete, - KeyCode.Home => ConsoleKey.Home, - KeyCode.End => ConsoleKey.End, - KeyCode.PageUp => ConsoleKey.PageUp, - KeyCode.PageDown => ConsoleKey.PageDown, - KeyCode.CursorUp => ConsoleKey.UpArrow, - KeyCode.CursorDown => ConsoleKey.DownArrow, - KeyCode.CursorLeft => ConsoleKey.LeftArrow, - KeyCode.CursorRight => ConsoleKey.RightArrow, - KeyCode.F1 => ConsoleKey.F1, - KeyCode.F2 => ConsoleKey.F2, - KeyCode.F3 => ConsoleKey.F3, - KeyCode.F4 => ConsoleKey.F4, - KeyCode.F5 => ConsoleKey.F5, - KeyCode.F6 => ConsoleKey.F6, - KeyCode.F7 => ConsoleKey.F7, - KeyCode.F8 => ConsoleKey.F8, - KeyCode.F9 => ConsoleKey.F9, - KeyCode.F10 => ConsoleKey.F10, - KeyCode.F11 => ConsoleKey.F11, - KeyCode.F12 => ConsoleKey.F12, - _ => (ConsoleKey)0 - }; + { + KeyCode.A => ConsoleKey.A, + KeyCode.B => ConsoleKey.B, + KeyCode.C => ConsoleKey.C, + KeyCode.D => ConsoleKey.D, + KeyCode.E => ConsoleKey.E, + KeyCode.F => ConsoleKey.F, + KeyCode.G => ConsoleKey.G, + KeyCode.H => ConsoleKey.H, + KeyCode.I => ConsoleKey.I, + KeyCode.J => ConsoleKey.J, + KeyCode.K => ConsoleKey.K, + KeyCode.L => ConsoleKey.L, + KeyCode.M => ConsoleKey.M, + KeyCode.N => ConsoleKey.N, + KeyCode.O => ConsoleKey.O, + KeyCode.P => ConsoleKey.P, + KeyCode.Q => ConsoleKey.Q, + KeyCode.R => ConsoleKey.R, + KeyCode.S => ConsoleKey.S, + KeyCode.T => ConsoleKey.T, + KeyCode.U => ConsoleKey.U, + KeyCode.V => ConsoleKey.V, + KeyCode.W => ConsoleKey.W, + KeyCode.X => ConsoleKey.X, + KeyCode.Y => ConsoleKey.Y, + KeyCode.Z => ConsoleKey.Z, + KeyCode.D0 => ConsoleKey.D0, + KeyCode.D1 => ConsoleKey.D1, + KeyCode.D2 => ConsoleKey.D2, + KeyCode.D3 => ConsoleKey.D3, + KeyCode.D4 => ConsoleKey.D4, + KeyCode.D5 => ConsoleKey.D5, + KeyCode.D6 => ConsoleKey.D6, + KeyCode.D7 => ConsoleKey.D7, + KeyCode.D8 => ConsoleKey.D8, + KeyCode.D9 => ConsoleKey.D9, + KeyCode.Enter => ConsoleKey.Enter, + KeyCode.Esc => ConsoleKey.Escape, + KeyCode.Space => ConsoleKey.Spacebar, + KeyCode.Tab => ConsoleKey.Tab, + KeyCode.Backspace => ConsoleKey.Backspace, + KeyCode.Delete => ConsoleKey.Delete, + KeyCode.Home => ConsoleKey.Home, + KeyCode.End => ConsoleKey.End, + KeyCode.PageUp => ConsoleKey.PageUp, + KeyCode.PageDown => ConsoleKey.PageDown, + KeyCode.CursorUp => ConsoleKey.UpArrow, + KeyCode.CursorDown => ConsoleKey.DownArrow, + KeyCode.CursorLeft => ConsoleKey.LeftArrow, + KeyCode.CursorRight => ConsoleKey.RightArrow, + KeyCode.F1 => ConsoleKey.F1, + KeyCode.F2 => ConsoleKey.F2, + KeyCode.F3 => ConsoleKey.F3, + KeyCode.F4 => ConsoleKey.F4, + KeyCode.F5 => ConsoleKey.F5, + KeyCode.F6 => ConsoleKey.F6, + KeyCode.F7 => ConsoleKey.F7, + KeyCode.F8 => ConsoleKey.F8, + KeyCode.F9 => ConsoleKey.F9, + KeyCode.F10 => ConsoleKey.F10, + KeyCode.F11 => ConsoleKey.F11, + KeyCode.F12 => ConsoleKey.F12, + _ => 0 + }; var keyChar = '\0'; Rune rune = key.AsRune; @@ -162,13 +167,4 @@ public class FakeComponentFactory : ComponentFactoryImpl return new (keyChar, consoleKey, key.IsShift, key.IsAlt, key.IsCtrl); } - - /// - public override IInputProcessor CreateInputProcessor (ConcurrentQueue inputBuffer) { return new FakeInputProcessor (inputBuffer); } - - /// - public override IOutput CreateOutput () - { - return _output ?? new FakeOutput (); - } } diff --git a/Terminal.Gui/Examples/ExampleContext.cs b/Terminal.Gui/Examples/ExampleContext.cs index 8a29909d1..68d9e6292 100644 --- a/Terminal.Gui/Examples/ExampleContext.cs +++ b/Terminal.Gui/Examples/ExampleContext.cs @@ -13,13 +13,13 @@ public class ExampleContext /// Gets or sets the name of the driver to use (e.g., "FakeDriver", "DotnetDriver"). /// If , the default driver for the platform is used. /// - public string? DriverName { get; set; } = null; + public string? DriverName { get; set; } /// /// Gets or sets the list of key names to inject into the example during execution. /// Each string should be a valid key name that can be parsed by . /// - public List KeysToInject { get; set; } = new (); + public List KeysToInject { get; set; } = []; /// /// Gets or sets the maximum time in milliseconds to allow the example to run before forcibly terminating it. @@ -46,7 +46,7 @@ public class ExampleContext /// The name of the environment variable used to pass the serialized /// to example applications. /// - public const string EnvironmentVariableName = "TERMGUI_TEST_CONTEXT"; + public const string ENVIRONMENT_VARIABLE_NAME = "TERMGUI_TEST_CONTEXT"; /// /// Serializes this context to a JSON string for passing via environment variables. diff --git a/Terminal.Gui/Examples/ExampleRunner.cs b/Terminal.Gui/Examples/ExampleRunner.cs index 5cea99b57..f088ce053 100644 --- a/Terminal.Gui/Examples/ExampleRunner.cs +++ b/Terminal.Gui/Examples/ExampleRunner.cs @@ -26,12 +26,32 @@ public static class ExampleRunner : RunOutOfProcess (example, context); } + private static ExampleMetrics? ExtractMetricsFromOutput (string output) + { + // Look for the metrics marker in the output + Match match = Regex.Match (output, @"###TERMGUI_METRICS:(.+?)###"); + + if (!match.Success) + { + return null; + } + + try + { + return JsonSerializer.Deserialize (match.Groups [1].Value); + } + catch + { + return null; + } + } + [RequiresUnreferencedCode ("Calls System.Reflection.Assembly.LoadFrom")] [RequiresDynamicCode ("Calls System.Reflection.Assembly.LoadFrom")] private static ExampleResult RunInProcess (ExampleInfo example, ExampleContext context) { Environment.SetEnvironmentVariable ( - ExampleContext.EnvironmentVariableName, + ExampleContext.ENVIRONMENT_VARIABLE_NAME, context.ToJson ()); try @@ -57,7 +77,7 @@ public static class ExampleRunner } else if (parameters.Length == 1 && parameters [0].ParameterType == typeof (string [])) { - result = entryPoint.Invoke (null, new object [] { Array.Empty () }); + result = entryPoint.Invoke (null, [Array.Empty ()]); } else { @@ -89,7 +109,7 @@ public static class ExampleRunner } finally { - Environment.SetEnvironmentVariable (ExampleContext.EnvironmentVariableName, null); + Environment.SetEnvironmentVariable (ExampleContext.ENVIRONMENT_VARIABLE_NAME, null); } } @@ -105,7 +125,7 @@ public static class ExampleRunner CreateNoWindow = true }; - psi.Environment [ExampleContext.EnvironmentVariableName] = context.ToJson (); + psi.Environment [ExampleContext.ENVIRONMENT_VARIABLE_NAME] = context.ToJson (); using Process? process = Process.Start (psi); @@ -126,8 +146,8 @@ public static class ExampleRunner { try { - const bool killEntireProcessTree = true; - process.Kill (killEntireProcessTree); + const bool KILL_ENTIRE_PROCESS_TREE = true; + process.Kill (KILL_ENTIRE_PROCESS_TREE); } catch { @@ -154,24 +174,4 @@ public static class ExampleRunner Metrics = metrics }; } - - private static ExampleMetrics? ExtractMetricsFromOutput (string output) - { - // Look for the metrics marker in the output - Match match = Regex.Match (output, @"###TERMGUI_METRICS:(.+?)###"); - - if (!match.Success) - { - return null; - } - - try - { - return JsonSerializer.Deserialize (match.Groups [1].Value); - } - catch - { - return null; - } - } }