Repository navigation
Expand file tree
/
Copy pathInlineCLI.cs
More file actions
237 lines (189 loc) · 8.31 KB
/
Copy pathInlineCLI.cs
File metadata and controls
237 lines (189 loc) · 8.31 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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
// A simple Terminal.Gui example demonstrating Inline mode rendering.
//
// This example shows how to use AppModel.Inline to render UI inline within
// the primary (scrollback) terminal buffer, similar to how Claude Code CLI
// and GitHub Copilot CLI render their UI.
//
// The application renders below the current shell prompt without switching
// to the alternate screen buffer. On exit, the rendered content stays in
// scrollback history.
using System.Collections.ObjectModel;
using System.Drawing;
using Terminal.Gui.App;
using Terminal.Gui.Drawing;
using Terminal.Gui.ViewBase;
using Terminal.Gui.Views;
// ReSharper disable AccessToModifiedClosure
// Set Inline mode BEFORE Init
Application.AppModel = AppModel.Inline;
//Application.ForceInlinePosition = new Point (0, 3);
IApplication app = Application.Create ().Init ();
var smokeTest = args.Length > 0 && args [0] == "--smoke-test";
if (smokeTest)
{
using CancellationTokenSource cts = new (TimeSpan.FromSeconds (2));
await app.RunAsync<InlinePromptView> (cts.Token);
Console.WriteLine ("Smoke test passed.");
app.Dispose ();
return;
}
app.Run<InlinePromptView> ();
app.Dispose ();
/// <summary>
/// A simple inline prompt view that demonstrates the inline rendering mode.
/// Uses <c>Height = Dim.Auto(minimumContentSize: 10)</c> so the view sizes itself by content
/// with a minimum height. The framework automatically resizes <c>Screen</c> to the rows
/// available below the cursor and offsets rendering so the view appears at the cursor position.
/// </summary>
public sealed class InlinePromptView : Window
{
private readonly Shortcut? _cursorShortcut;
private readonly Shortcut? _driverShortcut;
private readonly Shortcut? _frameShortcut;
private readonly Shortcut? _screenShortcut;
public InlinePromptView ()
{
Title = "Inline CLI Demo";
Border.Thickness = new Thickness (0, 3, 0, 0);
Border.LineStyle = LineStyle.Rounded;
Arrangement = ViewArrangement.TopResizable;
Width = Dim.Fill ();
Height = Dim.Auto ();
StatusBar statusBar = new () { AlignmentModes = AlignmentModes.IgnoreFirstOrLast, SchemeName = SchemeName };
Shortcut itemCountShortcut = new ()
{ Title = "No items", MouseHighlightStates = MouseState.None, Enabled = false };
_cursorShortcut = new Shortcut { Text = "Cursor", MouseHighlightStates = MouseState.None, Enabled = false };
_driverShortcut = new Shortcut { Text = "Driver", MouseHighlightStates = MouseState.None, Enabled = false };
_screenShortcut = new Shortcut { Text = "Screen", MouseHighlightStates = MouseState.None, Enabled = false };
_frameShortcut = new Shortcut { Text = "Frame", MouseHighlightStates = MouseState.None, Enabled = false };
Logo logo = new () { X = Pos.Center () };
Label infoLabel = new ()
{
Text = "Demonstrates Inline Application Mode.\nType a message and press Enter. Press Esc to exit.",
Y = Pos.Bottom (logo),
TextAlignment = Alignment.Center,
Width = Dim.Fill (),
Enabled = false
};
ObservableCollection<string> items = [];
// Declared early so the local function can capture them.
ListView<string> outputList = null!;
View inputIndicator = null!;
outputList = new ListView<string>
{
Y = Pos.Bottom (infoLabel),
Width = Dim.Fill (),
Height = Dim.Auto (minimumContentDim: 1, maximumContentDim: Dim.Func (_ => GetMaxListHeight ())),
BorderStyle = LineStyle.Dotted
};
outputList.Border.Thickness = new Thickness (0, 0, 0, 1);
outputList.SubViewsLaidOut += (_, _) =>
{
if (outputList.Index is not null)
{
outputList.Viewport = outputList.Viewport with { Y = outputList.Index.Value };
}
};
outputList.GettingAttributeForRole += (sender, args) =>
{
View? view = sender as View;
if (args.Role != VisualRole.Active)
{
return;
}
args.Result = view?.GetAttributeForRole (VisualRole.Normal);
args.Handled = true;
};
outputList.SetSource (items);
inputIndicator = new View
{
Text = $"{Glyphs.RightArrow}",
Y = Pos.Bottom (outputList),
Width = 2,
Height = 2,
Enabled = false,
BorderStyle = LineStyle.Dotted
};
inputIndicator.Border.Thickness = new Thickness (0, 0, 0, 1);
TextField inputField = new ()
{
X = Pos.Right (inputIndicator), Y = Pos.Top (inputIndicator), Width = Dim.Fill (),
BorderStyle = inputIndicator.BorderStyle
};
inputField.Border.Thickness = new Thickness (0, 0, 0, 1);
inputField.Accepted += (_, _) =>
{
var text = $"{Glyphs.BlackCircle} {inputField.Text}";
items.Add (text);
outputList.MoveEnd ();
inputField.Text = string.Empty;
itemCountShortcut.Title = $"{items.Count}";
};
statusBar.Add (_cursorShortcut, _driverShortcut, _screenShortcut, _frameShortcut, itemCountShortcut);
Add (logo, infoLabel, outputList, inputIndicator, inputField, statusBar);
inputField.SetFocus ();
_cursorShortcut?.Title = $"{App?.Driver?.InlinePosition.Y}";
_driverShortcut?.Title = $"{Format (App?.Driver?.Screen)}";
_screenShortcut?.Title = $"{Format (App?.Screen)}";
_frameShortcut?.Title = $"{Format (Frame)}";
return;
// Returns the maximum content height for the outputList so it doesn't grow beyond what the
// terminal can display. Subtracts all sibling and adornment heights from App.Screen.Height.
int GetMaxListHeight ()
{
var screenHeight = App?.Driver?.Screen.Height ?? 100;
// Adornments of the containing InlinePromptView (this Window)
var windowAdornments = (Border?.Thickness.Vertical ?? 0) + (Margin?.Thickness.Vertical ?? 0) +
(Padding?.Thickness.Vertical ?? 0);
// Heights of sibling views (everything except the list itself)
var siblingHeight = (logo?.Frame.Height ?? 0)
+ (infoLabel?.Frame.Height ?? 0)
+ (inputIndicator?.Frame.Height ?? 0)
+ (statusBar?.Frame.Height ?? 0);
// The list's own adornment overhead (border bottom = 1)
var listAdornments = (outputList.Border?.Thickness.Vertical ?? 0)
+ (outputList.Margin?.Thickness.Vertical ?? 0)
+ (outputList.Padding?.Thickness.Vertical ?? 0);
var maxContent = screenHeight - windowAdornments - siblingHeight - listAdornments;
return Math.Max (1, maxContent);
}
}
/// <inheritdoc />
protected override void OnIsRunningChanged (bool newIsRunning)
{
base.OnIsRunningChanged (newIsRunning);
if (newIsRunning)
{
App?.ScreenChanged += AppOnScreenChanged;
App?.Driver?.SizeChanged += DriverOnSizeChanged;
}
else
{
App?.ScreenChanged -= AppOnScreenChanged;
App?.Driver?.SizeChanged -= DriverOnSizeChanged;
}
}
private void AppOnScreenChanged (object? sender, EventArgs<Rectangle> e)
{
_screenShortcut?.Title = $"{Format (e.Value)}";
}
private void DriverOnSizeChanged (object? sender, SizeChangedEventArgs e)
{
_driverShortcut?.Title = $"{Format (new Rectangle (new Point (0, 0), e.Size!.Value))}";
}
/// <inheritdoc />
protected override void OnFrameChanged (in Rectangle frame)
{
base.OnFrameChanged (in frame);
_cursorShortcut?.Title = $"{App?.Driver?.InlinePosition.Y}";
_driverShortcut?.Title = $"{Format (App?.Driver?.Screen)}";
_screenShortcut?.Title = $"{Format (App?.Screen)}";
_frameShortcut?.Title = $"{Format (Frame)}";
}
private static string Format (Rectangle? rect)
{
return rect is null
? $"({Glyphs.Null})"
: $"({rect.Value.X},{rect.Value.Y},{rect.Value.Width},{rect.Value.Height})";
}
}