Skip to content

Commit 215d766

Browse files
committed
Tweaks
1 parent f2a4736 commit 215d766

File tree

7 files changed

+61
-36
lines changed

7 files changed

+61
-36
lines changed

Examples/Example/Example.cs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,14 @@
1212

1313
[assembly: ExampleMetadata ("Simple Example", "A basic login form demonstrating Terminal.Gui fundamentals")]
1414
[assembly: ExampleCategory ("Getting Started")]
15-
[assembly: ExampleDemoKeyStrokes (KeyStrokes = ["a", "d", "m", "i", "n", "Tab", "p", "a", "s", "s", "w", "o", "r", "d", "Enter"], Order = 1)]
15+
[assembly: ExampleDemoKeyStrokes (KeyStrokes = ["a", "d", "m", "i", "n", "Tab", "p", "a", "s", "s", "w", "o", "r", "d", "Enter"], DelayMs = 500, Order = 1)]
1616
[assembly: ExampleDemoKeyStrokes (KeyStrokes = ["Enter"], DelayMs = 500, Order = 2)]
1717
[assembly: ExampleDemoKeyStrokes (KeyStrokes = ["Esc"], DelayMs = 100, Order = 3)]
1818

1919
// Override the default configuration for the application to use the Light theme
2020
ConfigurationManager.RuntimeConfig = """{ "Theme": "Light" }""";
2121
ConfigurationManager.Enable (ConfigLocations.All);
2222

23-
// Setup automatic key injection for testing
24-
ExampleContextInjector.SetupAutomaticInjection ();
25-
2623
// Check for test context to determine driver
2724
string? contextJson = Environment.GetEnvironmentVariable (ExampleContext.ENVIRONMENT_VARIABLE_NAME);
2825
string? driverName = null;
@@ -33,8 +30,12 @@
3330
driverName = context?.DriverName;
3431
}
3532

36-
IApplication app = Application.Create ().Init (driverName);
33+
IApplication app = Application.Create ();
34+
35+
// Setup automatic key injection for testing
36+
ExampleContextInjector.SetupAutomaticInjection (app);
3737

38+
app.Init (driverName);
3839
app.Run<ExampleWindow> ();
3940

4041
// Dispose the app to clean up and enable Console.WriteLine below

Examples/ExampleRunner/Program.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,11 @@
6565
// Create context for running the example
6666
ExampleContext context = new ()
6767
{
68-
DriverName = "FakeDriver",
69-
KeysToInject = ["Esc"], // Just press Esc to quit each example
68+
KeysToInject = example.DemoKeyStrokes.OrderBy (ks => ks.Order)
69+
.SelectMany (ks => ks.KeyStrokes)
70+
.ToList (),
7071
TimeoutMs = 5000,
71-
Mode = ExecutionMode.OutOfProcess
72+
Mode = ExecutionMode.InProcess
7273
};
7374

7475
try

Examples/FluentExample/Program.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@
1313
[assembly: ExampleDemoKeyStrokes (KeyStrokes = ["CursorDown", "CursorDown", "CursorRight", "Enter"], Order = 1)]
1414
[assembly: ExampleDemoKeyStrokes (KeyStrokes = ["Esc"], DelayMs = 100, Order = 2)]
1515

16-
// Setup automatic key injection for testing
17-
ExampleContextInjector.SetupAutomaticInjection ();
1816

1917
// Check for test context to determine driver
2018
string? contextJson = Environment.GetEnvironmentVariable (ExampleContext.ENVIRONMENT_VARIABLE_NAME);
@@ -30,6 +28,9 @@
3028
.Init (driverName)
3129
.Run<ColorPickerView> ();
3230

31+
// Setup automatic key injection for testing
32+
ExampleContextInjector.SetupAutomaticInjection (app);
33+
3334
// Run the application with fluent API - automatically creates, runs, and disposes the runnable
3435
Color? result = app.GetResult () as Color?;
3536

Examples/RunnableWrapperExample/Program.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,6 @@
1616
[assembly: ExampleDemoKeyStrokes (KeyStrokes = ["Enter", "Esc"], DelayMs = 100, Order = 4)]
1717
[assembly: ExampleDemoKeyStrokes (KeyStrokes = ["Enter", "Esc"], DelayMs = 100, Order = 5)]
1818

19-
// Setup automatic key injection for testing
20-
ExampleContextInjector.SetupAutomaticInjection ();
21-
2219
// Check for test context to determine driver
2320
string? contextJson = Environment.GetEnvironmentVariable (ExampleContext.ENVIRONMENT_VARIABLE_NAME);
2421
string? driverName = null;
@@ -30,6 +27,10 @@
3027
}
3128

3229
IApplication app = Application.Create ();
30+
31+
// Setup automatic key injection for testing
32+
ExampleContextInjector.SetupAutomaticInjection (app);
33+
3334
app.Init (driverName);
3435

3536
// Example 1: Use extension method with result extraction

Terminal.Gui/Examples/ExampleContextInjector.cs

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,13 @@ public static class ExampleContextInjector
1313
/// Sets up automatic key injection if a test context is present in the environment.
1414
/// Call this method before calling <see cref="Application.Init"/> or <see cref="IApplication.Init"/>.
1515
/// </summary>
16+
/// <param name="app"></param>
1617
/// <remarks>
1718
/// This method is safe to call multiple times - it will only set up injection once.
1819
/// The actual key injection happens after the application is initialized, via the
1920
/// <see cref="Application.InitializedChanged"/> event.
2021
/// </remarks>
21-
public static void SetupAutomaticInjection ()
22+
public static void SetupAutomaticInjection (IApplication? app)
2223
{
2324
if (_initialized)
2425
{
@@ -43,19 +44,15 @@ public static void SetupAutomaticInjection ()
4344
}
4445

4546
// Subscribe to InitializedChanged to inject keys after initialization
46-
Application.InitializedChanged += OnInitializedChanged;
47+
app.SessionBegun += AppOnSessionBegun;
4748

4849
return;
4950

50-
void OnInitializedChanged (object? sender, EventArgs<bool> e)
51+
void AppOnSessionBegun (object? sender, SessionTokenEventArgs e)
5152
{
52-
if (!e.Value)
53-
{
54-
return;
55-
}
5653

5754
// Application has been initialized, inject the keys
58-
if (Application.Driver is null)
55+
if (app.Driver is null)
5956
{
6057
return;
6158
}
@@ -64,12 +61,12 @@ void OnInitializedChanged (object? sender, EventArgs<bool> e)
6461
{
6562
if (Input.Key.TryParse (keyStr, out Input.Key? key) && key is { })
6663
{
67-
Application.Driver.EnqueueKeyEvent (key);
64+
app.Keyboard.RaiseKeyDownEvent (key);
6865
}
6966
}
7067

7168
// Unsubscribe after injecting keys once
72-
Application.InitializedChanged -= OnInitializedChanged;
69+
app.SessionBegun -= AppOnSessionBegun;
7370
}
7471
}
7572
}

Terminal.Gui/Examples/ExampleRunner.cs

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -69,29 +69,47 @@ private static ExampleResult RunInProcess (ExampleInfo example, ExampleContext c
6969
}
7070

7171
ParameterInfo [] parameters = entryPoint.GetParameters ();
72-
object? result = null;
7372

74-
if (parameters.Length == 0)
73+
Task executionTask = Task.Run (() =>
7574
{
76-
result = entryPoint.Invoke (null, null);
77-
}
78-
else if (parameters.Length == 1 && parameters [0].ParameterType == typeof (string []))
79-
{
80-
result = entryPoint.Invoke (null, [Array.Empty<string> ()]);
81-
}
82-
else
75+
object? result = null;
76+
77+
if (parameters.Length == 0)
78+
{
79+
result = entryPoint.Invoke (null, null);
80+
}
81+
else if (parameters.Length == 1 && parameters [0].ParameterType == typeof (string []))
82+
{
83+
result = entryPoint.Invoke (null, [Array.Empty<string> ()]);
84+
}
85+
else
86+
{
87+
throw new InvalidOperationException ("Entry point has unsupported signature");
88+
}
89+
90+
// If entry point returns Task, wait for it
91+
if (result is Task task)
92+
{
93+
task.GetAwaiter ().GetResult ();
94+
}
95+
});
96+
97+
bool completed = executionTask.Wait (context.TimeoutMs);
98+
99+
if (!completed)
83100
{
101+
// reset terminal
102+
Console.Clear ();
84103
return new ()
85104
{
86105
Success = false,
87-
ErrorMessage = "Entry point has unsupported signature"
106+
TimedOut = true
88107
};
89108
}
90109

91-
// If entry point returns Task, wait for it
92-
if (result is Task task)
110+
if (executionTask.Exception is { })
93111
{
94-
task.GetAwaiter ().GetResult ();
112+
throw executionTask.Exception.GetBaseException ();
95113
}
96114

97115
return new ()

Terminal.sln

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FluentExample", "Examples\F
127127
EndProject
128128
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "RunnableWrapperExample", "Examples\RunnableWrapperExample\RunnableWrapperExample.csproj", "{26FDEE3C-9D1F-79A6-F48F-D0944C7F09F8}"
129129
EndProject
130+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ExampleRunner", "Examples\ExampleRunner\ExampleRunner.csproj", "{2CB35142-AAD4-D424-61D3-88F9C94AD62A}"
131+
EndProject
130132
Global
131133
GlobalSection(SolutionConfigurationPlatforms) = preSolution
132134
Debug|Any CPU = Debug|Any CPU
@@ -209,6 +211,10 @@ Global
209211
{26FDEE3C-9D1F-79A6-F48F-D0944C7F09F8}.Debug|Any CPU.Build.0 = Debug|Any CPU
210212
{26FDEE3C-9D1F-79A6-F48F-D0944C7F09F8}.Release|Any CPU.ActiveCfg = Release|Any CPU
211213
{26FDEE3C-9D1F-79A6-F48F-D0944C7F09F8}.Release|Any CPU.Build.0 = Release|Any CPU
214+
{2CB35142-AAD4-D424-61D3-88F9C94AD62A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
215+
{2CB35142-AAD4-D424-61D3-88F9C94AD62A}.Debug|Any CPU.Build.0 = Debug|Any CPU
216+
{2CB35142-AAD4-D424-61D3-88F9C94AD62A}.Release|Any CPU.ActiveCfg = Release|Any CPU
217+
{2CB35142-AAD4-D424-61D3-88F9C94AD62A}.Release|Any CPU.Build.0 = Release|Any CPU
212218
EndGlobalSection
213219
GlobalSection(SolutionProperties) = preSolution
214220
HideSolutionNode = FALSE

0 commit comments

Comments
 (0)