Code cleanup

This commit is contained in:
Tig
2025-12-01 15:29:35 -07:00
parent a1cbb42bc1
commit 02b6aae484
6 changed files with 125 additions and 129 deletions

View File

@@ -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))

View File

@@ -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))

View File

@@ -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))

View File

@@ -9,10 +9,6 @@ namespace Terminal.Gui.Drivers;
/// </summary>
public class FakeComponentFactory : ComponentFactoryImpl<ConsoleKeyInfo>
{
private readonly FakeInput? _input;
private readonly IOutput? _output;
private readonly ISizeMonitor? _sizeMonitor;
/// <summary>
/// Creates a new FakeComponentFactory with optional output capture.
/// </summary>
@@ -26,12 +22,9 @@ public class FakeComponentFactory : ComponentFactoryImpl<ConsoleKeyInfo>
_sizeMonitor = sizeMonitor;
}
/// <inheritdoc/>
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;
/// <inheritdoc/>
public override IInput<ConsoleKeyInfo> CreateInput ()
@@ -40,7 +33,7 @@ public class FakeComponentFactory : ComponentFactoryImpl<ConsoleKeyInfo>
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<ConsoleKeyInfo>
{
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<ConsoleKeyInfo>
return fakeInput;
}
private static ConsoleKeyInfo ConvertKeyToConsoleKeyInfo (Input.Key key)
/// <inheritdoc/>
public override IInputProcessor CreateInputProcessor (ConcurrentQueue<ConsoleKeyInfo> inputBuffer) { return new FakeInputProcessor (inputBuffer); }
/// <inheritdoc/>
public override IOutput CreateOutput () { return _output ?? new FakeOutput (); }
/// <inheritdoc/>
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<ConsoleKeyInfo>
// 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<ConsoleKeyInfo>
return new (keyChar, consoleKey, key.IsShift, key.IsAlt, key.IsCtrl);
}
/// <inheritdoc/>
public override IInputProcessor CreateInputProcessor (ConcurrentQueue<ConsoleKeyInfo> inputBuffer) { return new FakeInputProcessor (inputBuffer); }
/// <inheritdoc/>
public override IOutput CreateOutput ()
{
return _output ?? new FakeOutput ();
}
}

View File

@@ -13,13 +13,13 @@ public class ExampleContext
/// Gets or sets the name of the driver to use (e.g., "FakeDriver", "DotnetDriver").
/// If <see langword="null"/>, the default driver for the platform is used.
/// </summary>
public string? DriverName { get; set; } = null;
public string? DriverName { get; set; }
/// <summary>
/// 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 <see cref="Input.Key.TryParse"/>.
/// </summary>
public List<string> KeysToInject { get; set; } = new ();
public List<string> KeysToInject { get; set; } = [];
/// <summary>
/// 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 <see cref="ExampleContext"/>
/// to example applications.
/// </summary>
public const string EnvironmentVariableName = "TERMGUI_TEST_CONTEXT";
public const string ENVIRONMENT_VARIABLE_NAME = "TERMGUI_TEST_CONTEXT";
/// <summary>
/// Serializes this context to a JSON string for passing via environment variables.

View File

@@ -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<ExampleMetrics> (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<string> () });
result = entryPoint.Invoke (null, [Array.Empty<string> ()]);
}
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<ExampleMetrics> (match.Groups [1].Value);
}
catch
{
return null;
}
}
}