-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
103 lines (81 loc) · 3.2 KB
/
Program.cs
File metadata and controls
103 lines (81 loc) · 3.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
// Inline ColorPicker — demonstrates using RunnableWrapper<ColorPicker, Color?> in inline mode.
//
// NOTE: See https://github.com/gui-cs/clet that turns every Terminal.Gui View into a CLI command
// NOTE: — typed inputs, a real file picker, a Markdown viewer — with consistent JSON output,
// NOTE: predictable exit codes, and full keyboard/mouse support. Works for humans and AI agents alike.
//
// Renders a ColorPicker inline in the terminal (primary buffer) without dialog buttons.
// If the user accepts (double-click), the selected color name is written to stdout.
// If the user cancels (Esc), nothing is output and exit code is 1.
//
// Usage:
// dotnet run --project Examples/InlineColorPicker
// dotnet run --project Examples/InlineColorPicker -- --initial "#FF0000"
// dotnet run --project Examples/InlineColorPicker -- --initial Red
// $color = dotnet run --project Examples/InlineColorPicker # capture in shell
using Terminal.Gui.App;
using Terminal.Gui.Drawing;
using Terminal.Gui.ViewBase;
using Terminal.Gui.Views;
// Check for smoke test mode
var smokeTest = args.Length > 0 && args [0] == "--smoke-test";
if (smokeTest)
{
Application.AppModel = AppModel.Inline;
IApplication smokeApp = Application.Create ().Init ();
using CancellationTokenSource cts = new (TimeSpan.FromSeconds (2));
RunnableWrapper<ColorPicker, Color?> smokeWrapper = new () { Title = "Smoke Test" };
await smokeApp.RunAsync (smokeWrapper, cts.Token);
Console.WriteLine ("Smoke test passed.");
smokeApp.Dispose ();
return 0;
}
// Parse command-line arguments
string? initialValue = null;
for (var i = 0; i < args.Length; i++)
{
if (args [i] is "--initial" or "-i")
{
if (i + 1 < args.Length)
{
initialValue = args [++i];
}
else
{
Console.Error.WriteLine ("Error: --initial requires a color value (e.g., \"#FF0000\" or \"Red\").");
return 1;
}
}
}
// Enable inline mode before Init
Application.AppModel = AppModel.Inline;
IApplication app = Application.Create ().Init ();
// Wrap ColorPicker in a RunnableWrapper — no dialog buttons, just the picker.
// ColorPicker raises Command.Accept on double-click.
RunnableWrapper<ColorPicker, Color?> wrapper = new ()
{ Title = "Select a Color (Double-click to accept, Esc to cancel)", ResultExtractor = cp => cp.Value };
// Enable color name display
wrapper.GetWrappedView ().Style.ShowColorName = true;
wrapper.GetWrappedView ().ApplyStyleChanges ();
// Apply initial value via IValue.TrySetValueFromString if provided
if (initialValue is not null)
{
if (!((IValue)wrapper.GetWrappedView ()).TrySetValueFromString (initialValue))
{
Console.Error.WriteLine ($"Error: '{initialValue}' is not a valid color (use e.g., \"#FF0000\" or \"Red\").");
app.Dispose ();
return 1;
}
}
// Run inline — blocks until user accepts or cancels
app.Run (wrapper);
Color? result = wrapper.Result;
app.Dispose ();
if (result is { } selectedColor)
{
StandardColorsNameResolver resolver = new ();
var output = resolver.TryNameColor (selectedColor, out var name) ? name : selectedColor.ToString ();
Console.WriteLine (output);
return 0;
}
return 1;