Skip to content

Commit ef263f6

Browse files
Copilottig
andcommitted
Remove cruft and implement SetDelay keystroke command
- Removed test context detection cruft from examples - Examples now use Application.Create(example: true) directly - Updated ExampleDemoKeyStrokesAttribute to support "SetDelay:nnn" command - Removed DelayMs parameter from attribute - Default delay between keys is 100ms - SetDelay command changes delay for subsequent keys in sequence - Metadata moved after using statements (before code) - All examples cleaned up and building successfully This addresses @tig's feedback for cleaner examples and better delay control. Co-authored-by: tig <[email protected]>
1 parent 401db78 commit ef263f6

File tree

7 files changed

+66
-79
lines changed

7 files changed

+66
-79
lines changed

Examples/Example/Example.cs

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,34 +6,22 @@
66

77
using Terminal.Gui.App;
88
using Terminal.Gui.Configuration;
9-
using Terminal.Gui.Examples;
109
using Terminal.Gui.ViewBase;
1110
using Terminal.Gui.Views;
1211

13-
[assembly: ExampleMetadata ("Simple Example", "A basic login form demonstrating Terminal.Gui fundamentals")]
14-
[assembly: ExampleCategory ("Getting Started")]
15-
[assembly: ExampleDemoKeyStrokes (KeyStrokes = ["a", "d", "m", "i", "n", "Tab", "p", "a", "s", "s", "w", "o", "r", "d", "Enter"], DelayMs = 500, Order = 1)]
16-
[assembly: ExampleDemoKeyStrokes (KeyStrokes = ["Enter"], DelayMs = 500, Order = 2)]
17-
[assembly: ExampleDemoKeyStrokes (KeyStrokes = ["Esc"], DelayMs = 100, Order = 3)]
12+
// Example metadata
13+
[assembly: Terminal.Gui.Examples.ExampleMetadata ("Simple Example", "A basic login form demonstrating Terminal.Gui fundamentals")]
14+
[assembly: Terminal.Gui.Examples.ExampleCategory ("Getting Started")]
15+
[assembly: Terminal.Gui.Examples.ExampleDemoKeyStrokes (KeyStrokes = ["SetDelay:500", "a", "d", "m", "i", "n", "Tab", "p", "a", "s", "s", "w", "o", "r", "d", "Enter"], Order = 1)]
16+
[assembly: Terminal.Gui.Examples.ExampleDemoKeyStrokes (KeyStrokes = ["SetDelay:500", "Enter"], Order = 2)]
17+
[assembly: Terminal.Gui.Examples.ExampleDemoKeyStrokes (KeyStrokes = ["SetDelay:100", "Esc"], 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-
// Check for test context to determine driver
24-
string? contextJson = Environment.GetEnvironmentVariable (ExampleContext.ENVIRONMENT_VARIABLE_NAME);
25-
string? driverName = null;
26-
var isExample = false;
27-
28-
if (!string.IsNullOrEmpty (contextJson))
29-
{
30-
ExampleContext? context = ExampleContext.FromJson (contextJson);
31-
driverName = context?.DriverName;
32-
isExample = true;
33-
}
34-
35-
IApplication app = Application.Create (example: isExample);
36-
app.Init (driverName);
23+
IApplication app = Application.Create (example: true);
24+
app.Init ();
3725
app.Run<ExampleWindow> ();
3826

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

Examples/FluentExample/Program.cs

Lines changed: 8 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,31 +3,18 @@
33

44
using Terminal.Gui.App;
55
using Terminal.Gui.Drawing;
6-
using Terminal.Gui.Examples;
76
using Terminal.Gui.ViewBase;
87
using Terminal.Gui.Views;
98

10-
[assembly: ExampleMetadata ("Fluent API Example", "Demonstrates the fluent IApplication API with IRunnable pattern")]
11-
[assembly: ExampleCategory ("API Patterns")]
12-
[assembly: ExampleCategory ("Controls")]
13-
[assembly: ExampleDemoKeyStrokes (KeyStrokes = ["CursorDown", "CursorDown", "CursorRight", "Enter"], Order = 1)]
14-
[assembly: ExampleDemoKeyStrokes (KeyStrokes = ["Esc"], DelayMs = 100, Order = 2)]
9+
// Example metadata
10+
[assembly: Terminal.Gui.Examples.ExampleMetadata ("Fluent API Example", "Demonstrates the fluent IApplication API with IRunnable pattern")]
11+
[assembly: Terminal.Gui.Examples.ExampleCategory ("API Patterns")]
12+
[assembly: Terminal.Gui.Examples.ExampleCategory ("Controls")]
13+
[assembly: Terminal.Gui.Examples.ExampleDemoKeyStrokes (KeyStrokes = ["CursorDown", "CursorDown", "CursorRight", "Enter"], Order = 1)]
14+
[assembly: Terminal.Gui.Examples.ExampleDemoKeyStrokes (KeyStrokes = ["SetDelay:100", "Esc"], Order = 2)]
1515

16-
17-
// Check for test context to determine driver
18-
string? contextJson = Environment.GetEnvironmentVariable (ExampleContext.ENVIRONMENT_VARIABLE_NAME);
19-
string? driverName = null;
20-
var isExample = false;
21-
22-
if (!string.IsNullOrEmpty (contextJson))
23-
{
24-
ExampleContext? context = ExampleContext.FromJson (contextJson);
25-
driverName = context?.DriverName;
26-
isExample = true;
27-
}
28-
29-
IApplication? app = Application.Create (example: isExample)
30-
.Init (driverName)
16+
IApplication? app = Application.Create (example: true)
17+
.Init ()
3118
.Run<ColorPickerView> ();
3219

3320
// Run the application with fluent API - automatically creates, runs, and disposes the runnable

Examples/RunnableWrapperExample/Program.cs

Lines changed: 12 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3,33 +3,21 @@
33

44
using Terminal.Gui.App;
55
using Terminal.Gui.Drawing;
6-
using Terminal.Gui.Examples;
76
using Terminal.Gui.ViewBase;
87
using Terminal.Gui.Views;
98

10-
[assembly: ExampleMetadata ("Runnable Wrapper Example", "Shows how to wrap any View to make it runnable without implementing IRunnable")]
11-
[assembly: ExampleCategory ("API Patterns")]
12-
[assembly: ExampleCategory ("Views")]
13-
[assembly: ExampleDemoKeyStrokes (KeyStrokes = ["t", "e", "s", "t", "Esc"], Order = 1)]
14-
[assembly: ExampleDemoKeyStrokes (KeyStrokes = ["Enter", "Esc"], DelayMs = 100, Order = 2)]
15-
[assembly: ExampleDemoKeyStrokes (KeyStrokes = ["Enter", "Esc"], DelayMs = 100, Order = 3)]
16-
[assembly: ExampleDemoKeyStrokes (KeyStrokes = ["Enter", "Esc"], DelayMs = 100, Order = 4)]
17-
[assembly: ExampleDemoKeyStrokes (KeyStrokes = ["Enter", "Esc"], DelayMs = 100, Order = 5)]
18-
19-
// Check for test context to determine driver
20-
string? contextJson = Environment.GetEnvironmentVariable (ExampleContext.ENVIRONMENT_VARIABLE_NAME);
21-
string? driverName = null;
22-
var isExample = false;
23-
24-
if (!string.IsNullOrEmpty (contextJson))
25-
{
26-
ExampleContext? context = ExampleContext.FromJson (contextJson);
27-
driverName = context?.DriverName;
28-
isExample = true;
29-
}
30-
31-
IApplication app = Application.Create (example: isExample);
32-
app.Init (driverName);
9+
// Example metadata
10+
[assembly: Terminal.Gui.Examples.ExampleMetadata ("Runnable Wrapper Example", "Shows how to wrap any View to make it runnable without implementing IRunnable")]
11+
[assembly: Terminal.Gui.Examples.ExampleCategory ("API Patterns")]
12+
[assembly: Terminal.Gui.Examples.ExampleCategory ("Views")]
13+
[assembly: Terminal.Gui.Examples.ExampleDemoKeyStrokes (KeyStrokes = ["t", "e", "s", "t", "Esc"], Order = 1)]
14+
[assembly: Terminal.Gui.Examples.ExampleDemoKeyStrokes (KeyStrokes = ["SetDelay:100", "Enter", "Esc"], Order = 2)]
15+
[assembly: Terminal.Gui.Examples.ExampleDemoKeyStrokes (KeyStrokes = ["SetDelay:100", "Enter", "Esc"], Order = 3)]
16+
[assembly: Terminal.Gui.Examples.ExampleDemoKeyStrokes (KeyStrokes = ["SetDelay:100", "Enter", "Esc"], Order = 4)]
17+
[assembly: Terminal.Gui.Examples.ExampleDemoKeyStrokes (KeyStrokes = ["SetDelay:100", "Enter", "Esc"], Order = 5)]
18+
19+
IApplication app = Application.Create (example: true);
20+
app.Init ();
3321

3422
// Example 1: Use extension method with result extraction
3523
var textField = new TextField { Width = 40, Text = "Default text" };

Terminal.Gui/App/ApplicationImpl.Lifecycle.cs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -452,15 +452,38 @@ private void SendDemoKeys ()
452452
// Sort by Order and collect all keystrokes
453453
var sortedSequences = demoKeyAttributes.OrderBy<Terminal.Gui.Examples.ExampleDemoKeyStrokesAttribute, int> (a => a.Order);
454454

455+
// Default delay between keys is 100ms
456+
int currentDelay = 100;
457+
455458
foreach (var attr in sortedSequences)
456459
{
457460
// Handle KeyStrokes array
458461
if (attr.KeyStrokes is { Length: > 0 })
459462
{
460463
foreach (string keyStr in attr.KeyStrokes)
461464
{
465+
// Check for SetDelay command
466+
if (keyStr.StartsWith ("SetDelay:", StringComparison.OrdinalIgnoreCase))
467+
{
468+
string delayValue = keyStr.Substring ("SetDelay:".Length);
469+
470+
if (int.TryParse (delayValue, out int newDelay))
471+
{
472+
currentDelay = newDelay;
473+
}
474+
475+
continue;
476+
}
477+
478+
// Regular key
462479
if (Input.Key.TryParse (keyStr, out Input.Key? key) && key is { })
463480
{
481+
// Apply delay before sending key
482+
if (currentDelay > 0)
483+
{
484+
System.Threading.Thread.Sleep (currentDelay);
485+
}
486+
464487
Keyboard?.RaiseKeyDownEvent (key);
465488
}
466489
}
@@ -473,6 +496,12 @@ private void SendDemoKeys ()
473496
{
474497
for (var i = 0; i < attr.RepeatCount; i++)
475498
{
499+
// Apply delay before sending key
500+
if (currentDelay > 0)
501+
{
502+
System.Threading.Thread.Sleep (currentDelay);
503+
}
504+
476505
Keyboard?.RaiseKeyDownEvent (key);
477506
}
478507
}

Terminal.Gui/Examples/DemoKeyStrokeSequence.cs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,10 @@ public class DemoKeyStrokeSequence
77
{
88
/// <summary>
99
/// Gets or sets the array of keystroke names to inject.
10+
/// Can include special "SetDelay:nnn" commands to change the delay between keys.
1011
/// </summary>
1112
public string [] KeyStrokes { get; set; } = [];
1213

13-
/// <summary>
14-
/// Gets or sets the delay in milliseconds before injecting these keystrokes.
15-
/// </summary>
16-
public int DelayMs { get; set; } = 0;
17-
1814
/// <summary>
1915
/// Gets or sets the order in which this sequence should be executed.
2016
/// </summary>

Terminal.Gui/Examples/ExampleDemoKeyStrokesAttribute.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,24 @@ namespace Terminal.Gui.Examples;
99
/// Multiple instances of this attribute can be applied to a single assembly to define a sequence
1010
/// of keystroke injections. The <see cref="Order"/> property controls the execution sequence.
1111
/// </para>
12+
/// <para>
13+
/// Keystrokes can include special "SetDelay:nnn" entries to change the delay between subsequent keys.
14+
/// The default delay is 100ms. For example: KeyStrokes = ["SetDelay:500", "Enter", "SetDelay:100", "Tab"]
15+
/// </para>
1216
/// </remarks>
1317
/// <example>
1418
/// <code>
15-
/// [assembly: ExampleDemoKeyStrokes(RepeatKey = "CursorDown", RepeatCount = 5, Order = 1, DelayMs = 100)]
16-
/// [assembly: ExampleDemoKeyStrokes(KeyStrokes = new[] { "Enter" }, Order = 2, DelayMs = 200)]
19+
/// [assembly: ExampleDemoKeyStrokes(RepeatKey = "CursorDown", RepeatCount = 5, Order = 1)]
20+
/// [assembly: ExampleDemoKeyStrokes(KeyStrokes = ["SetDelay:500", "Enter", "SetDelay:100", "Esc"], Order = 2)]
1721
/// </code>
1822
/// </example>
1923
[AttributeUsage (AttributeTargets.Assembly, AllowMultiple = true)]
2024
public class ExampleDemoKeyStrokesAttribute : System.Attribute
2125
{
2226
/// <summary>
2327
/// Gets or sets an array of keystroke names to inject.
24-
/// Each string should be a valid key name that can be parsed by <see cref="Input.Key.TryParse"/>.
28+
/// Each string should be a valid key name that can be parsed by <see cref="Input.Key.TryParse"/>,
29+
/// or a special "SetDelay:nnn" command to change the delay between subsequent keys.
2530
/// </summary>
2631
public string []? KeyStrokes { get; set; }
2732

@@ -37,11 +42,6 @@ public class ExampleDemoKeyStrokesAttribute : System.Attribute
3742
/// </summary>
3843
public int RepeatCount { get; set; } = 1;
3944

40-
/// <summary>
41-
/// Gets or sets the delay in milliseconds before injecting these keystrokes.
42-
/// </summary>
43-
public int DelayMs { get; set; } = 0;
44-
4545
/// <summary>
4646
/// Gets or sets the order in which this keystroke sequence should be executed
4747
/// relative to other <see cref="ExampleDemoKeyStrokesAttribute"/> instances.

Terminal.Gui/Examples/ExampleDiscovery.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,6 @@ private static List<DemoKeyStrokeSequence> ParseDemoKeyStrokes (Assembly assembl
110110
new ()
111111
{
112112
KeyStrokes = keys.ToArray (),
113-
DelayMs = attr.DelayMs,
114113
Order = attr.Order
115114
});
116115
}

0 commit comments

Comments
 (0)