-
-
Notifications
You must be signed in to change notification settings - Fork 531
Expand file tree
/
Copy pathConsoleViewModel.cs
More file actions
629 lines (538 loc) · 20.7 KB
/
ConsoleViewModel.cs
File metadata and controls
629 lines (538 loc) · 20.7 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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
using System.ComponentModel;
using System.Diagnostics;
using System.Threading.Tasks.Dataflow;
using System.Web;
using Avalonia.Threading;
using AvaloniaEdit;
using AvaloniaEdit.Document;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using Nito.AsyncEx;
using Nito.AsyncEx.Synchronous;
using NLog;
using StabilityMatrix.Core.Extensions;
using StabilityMatrix.Core.Processes;
namespace StabilityMatrix.Avalonia.ViewModels;
public partial class ConsoleViewModel : ObservableObject, IDisposable, IAsyncDisposable
{
private static readonly Logger Logger = LogManager.GetCurrentClassLogger();
private bool isDisposed;
// Queue for console updates
private BufferBlock<ProcessOutput> buffer = new();
// Task that updates the console (runs on UI thread)
private Task? updateTask;
// Cancellation token source for updateTask
private CancellationTokenSource? updateCts;
public int MaxLines { get; set; } = -1;
public bool IsUpdatesRunning => updateTask?.IsCompleted == false;
[ObservableProperty]
private TextDocument document = new();
/// <summary>
/// Current offset for write operations.
/// </summary>
private int writeCursor;
/// <summary>
/// Lock for accessing <see cref="writeCursor"/>
/// </summary>
private readonly AsyncLock writeCursorLock = new();
/// <summary>
/// Timeout for acquiring locks on <see cref="writeCursor"/>
/// </summary>
// ReSharper disable once MemberCanBePrivate.Global
public TimeSpan WriteCursorLockTimeout { get; init; } = TimeSpan.FromMilliseconds(100);
/// <summary>
/// Gets a cancellation token using the cursor lock timeout
/// </summary>
private CancellationToken WriteCursorLockTimeoutToken =>
new CancellationTokenSource(WriteCursorLockTimeout).Token;
/// <summary>
/// Event invoked when an ApcMessage of type Input is received.
/// </summary>
public event EventHandler<ApcMessage>? ApcInput;
/// <summary>
/// Starts update task for processing Post messages.
/// </summary>
/// <exception cref="InvalidOperationException">If update task is already running</exception>
public void StartUpdates()
{
if (updateTask is not null)
{
throw new InvalidOperationException("Update task is already running");
}
updateCts = new CancellationTokenSource();
updateTask = Dispatcher.UIThread.InvokeAsync(ConsoleUpdateLoop, DispatcherPriority.Send);
}
/// <summary>
/// Cancels the update task and waits for it to complete.
/// </summary>
public async Task StopUpdatesAsync()
{
Logger.Trace($"Stopping console updates, current buffer items: {buffer.Count}");
// First complete the buffer
buffer.Complete();
// Wait for buffer to complete, max 3 seconds
var completionCts = new CancellationTokenSource(3000);
try
{
await buffer.Completion.WaitAsync(completionCts.Token);
}
catch (TaskCanceledException e)
{
// We can still continue since this just means we lose
// some remaining output
Logger.Warn("Buffer completion timed out: " + e.Message);
}
// Cancel update task
updateCts?.Cancel();
updateCts = null;
// Wait for update task
if (updateTask is not null)
{
await updateTask;
updateTask = null;
}
Logger.Trace($"Stopped console updates with {buffer.Count} buffer items remaining");
}
/// <summary>
/// Clears the console and sets a new buffer.
/// This also resets the write cursor to 0.
/// </summary>
public async Task Clear()
{
// Clear document
Document.Text = string.Empty;
// Reset write cursor
await ResetWriteCursor();
// Clear buffer and create new one
buffer.Complete();
buffer = new BufferBlock<ProcessOutput>();
}
/// <summary>
/// Resets the write cursor to be equal to the document length.
/// </summary>
public async Task ResetWriteCursor()
{
using (await writeCursorLock.LockAsync(WriteCursorLockTimeoutToken))
{
Logger.ConditionalTrace($"Reset cursor to end: ({writeCursor} -> {Document.TextLength})");
writeCursor = Document.TextLength;
}
DebugPrintDocument();
}
[RelayCommand]
private async Task CopySelection(TextEditor textEditor)
{
await App.Clipboard.SetTextAsync(textEditor.SelectedText);
}
[RelayCommand]
private void SelectAll(TextEditor textEditor)
{
textEditor.SelectAll();
}
[Localizable(false)]
[RelayCommand]
private void SearchWithGoogle(TextEditor textEditor)
{
var url = $"https://google.com/search?q={HttpUtility.UrlEncode(textEditor.SelectedText)}";
ProcessRunner.OpenUrl(url);
}
[Localizable(false)]
[RelayCommand]
private void SearchWithChatGpt(TextEditor textEditor)
{
var url = $"https://chatgpt.com/?q={HttpUtility.UrlEncode(textEditor.SelectedText)}";
ProcessRunner.OpenUrl(url);
}
private async Task ConsoleUpdateLoop()
{
// This must be run in the UI thread
Dispatcher.UIThread.VerifyAccess();
// Get cancellation token
var ct =
updateCts?.Token ?? throw new InvalidOperationException("Update cancellation token must be set");
try
{
while (!ct.IsCancellationRequested)
{
ProcessOutput output;
try
{
output = await buffer.ReceiveAsync(ct);
}
catch (InvalidOperationException e)
{
// Thrown when buffer is completed, convert to OperationCanceledException
throw new OperationCanceledException("Update buffer completed", e);
}
var outputType = output.IsStdErr ? "stderr" : "stdout";
Logger.ConditionalTrace(
$"Processing: [{outputType}] (Text = {output.Text.ToRepr()}, "
+ $"Raw = {output.RawText?.ToRepr()}, "
+ $"CarriageReturn = {output.CarriageReturn}, "
+ $"CursorUp = {output.CursorUp}, "
+ $"AnsiCommand = {output.AnsiCommand})"
);
// Link the cancellation token to the write cursor lock timeout
var linkedCt = CancellationTokenSource
.CreateLinkedTokenSource(ct, WriteCursorLockTimeoutToken)
.Token;
using (await writeCursorLock.LockAsync(linkedCt))
{
ConsoleUpdateOne(output);
}
}
}
catch (OperationCanceledException e)
{
Logger.Debug($"Console update loop canceled: {e.Message}");
}
catch (Exception e)
{
// Log other errors and continue here to not crash the UI thread
Logger.Error(e, $"Unexpected error in console update loop: {e.GetType().Name} {e.Message}");
}
}
/// <summary>
/// Handle one instance of ProcessOutput.
/// Calls to this function must be synchronized with <see cref="writeCursorLock"/>
/// </summary>
/// <remarks>Not checked, but must be run in the UI thread.</remarks>
private void ConsoleUpdateOne(ProcessOutput output)
{
Debug.Assert(Dispatcher.UIThread.CheckAccess());
// Check for Apc messages
if (output.ApcMessage is not null)
{
// Handle Apc message, for now just input audit events
var message = output.ApcMessage.Value;
if (message.Type == ApcType.Input)
{
ApcInput?.Invoke(this, message);
}
// Ignore further processing
return;
}
// If we have a carriage return,
// start current write at the beginning of the current line
if (output.CarriageReturn > 0)
{
var currentLine = Document.GetLineByOffset(writeCursor);
// Get the start of current line as new write cursor
var lineStartOffset = currentLine.Offset;
// See if we need to move the cursor
if (lineStartOffset == writeCursor)
{
Logger.ConditionalTrace(
$"Cursor already at start for carriage return "
+ $"(offset = {lineStartOffset}, line = {currentLine.LineNumber})"
);
}
else
{
// Also remove everything on current line
// We'll temporarily do this for now to fix progress
var lineEndOffset = currentLine.EndOffset;
var lineLength = lineEndOffset - lineStartOffset;
Document.Remove(lineStartOffset, lineLength);
Logger.ConditionalTrace(
$"Moving cursor to start for carriage return " + $"({writeCursor} -> {lineStartOffset})"
);
writeCursor = lineStartOffset;
}
}
// Write new text
if (!string.IsNullOrEmpty(output.Text))
{
DirectWriteLinesToConsole(output.Text);
}
// Handle cursor movements
if (output.CursorUp > 0)
{
// Get the line and column of the current cursor
var currentLocation = Document.GetLocation(writeCursor);
if (currentLocation.Line == 1)
{
// We are already on the first line, ignore
Logger.ConditionalTrace($"Cursor up: Already on first line");
}
else
{
// We want to move up one line
var targetLocation = new TextLocation(currentLocation.Line - 1, currentLocation.Column);
var targetOffset = Document.GetOffset(targetLocation);
// Update cursor to target offset
Logger.ConditionalTrace(
$"Cursor up: Moving (line {currentLocation.Line}, {writeCursor})"
+ $" -> (line {targetLocation.Line}, {targetOffset})"
);
writeCursor = targetOffset;
}
}
// Handle erase commands, different to cursor move as they don't move the cursor
// We'll insert blank spaces instead
if (output.AnsiCommand.HasFlag(AnsiCommand.EraseLine))
{
// Get the current line, we'll insert spaces from start to end
var currentLine = Document.GetLineByOffset(writeCursor);
// Must be smaller than total lines
currentLine =
currentLine.LineNumber < Document.LineCount
? currentLine
: Document.GetLineByNumber(Document.LineCount - 1);
// Make some spaces to insert
var spaces = new string(' ', currentLine.Length);
// Insert the text
Logger.ConditionalTrace(
$"Erasing line {currentLine.LineNumber}: (length = {currentLine.Length})"
);
using (Document.RunUpdate())
{
Document.Replace(currentLine.Offset, currentLine.Length, spaces);
}
}
DebugPrintDocument();
}
/// <summary>
/// Write text potentially containing line breaks to the console.
/// <remarks>This call will hold a upgradeable read lock</remarks>
/// </summary>
private void DirectWriteLinesToConsole(string text)
{
// When our cursor is not at end, newlines should be interpreted as commands to
// move cursor forward to the next linebreak instead of inserting a newline.
// If text contains no newlines, we can just call DirectWriteToConsole
// Also if cursor is equal to document length
if (!text.Contains(Environment.NewLine) || writeCursor == Document.TextLength)
{
DirectWriteToConsole(text);
return;
}
// Otherwise we need to handle how linebreaks are treated
// Split text into lines
var lines = text.Split(Environment.NewLine).ToList();
foreach (var lineText in lines.SkipLast(1))
{
// Insert text
DirectWriteToConsole(lineText);
// Set cursor to start of next line, if we're not already there
var currentLine = Document.GetLineByOffset(writeCursor);
// If next line is available, move cursor to start of next line
if (currentLine.LineNumber < Document.LineCount)
{
var nextLine = Document.GetLineByNumber(currentLine.LineNumber + 1);
Logger.ConditionalTrace(
$"Moving cursor to start of next line " + $"({writeCursor} -> {nextLine.Offset})"
);
writeCursor = nextLine.Offset;
}
else
{
// Otherwise move to end of current line, and direct insert a newline
var lineEndOffset = currentLine.EndOffset;
Logger.ConditionalTrace(
$"Moving cursor to end of current line " + $"({writeCursor} -> {lineEndOffset})"
);
writeCursor = lineEndOffset;
DirectWriteToConsole(Environment.NewLine);
}
}
}
/// <summary>
/// Write text to the console, does not handle newlines.
/// This should probably only be used by <see cref="DirectWriteLinesToConsole"/>
/// <remarks>This call will hold a upgradeable read lock</remarks>
/// </summary>
private void DirectWriteToConsole(string text)
{
CheckMaxLines();
using (Document.RunUpdate())
{
// Need to replace text first if cursor lower than document length
var replaceLength = Math.Min(Document.TextLength - writeCursor, text.Length);
if (replaceLength > 0)
{
var newText = text[..replaceLength];
Logger.ConditionalTrace(
$"Replacing: (cursor = {writeCursor}, length = {replaceLength}, "
+ $"text = {Document.GetText(writeCursor, replaceLength).ToRepr()} "
+ $"-> {newText.ToRepr()})"
);
Document.Replace(writeCursor, replaceLength, newText);
writeCursor += replaceLength;
}
// If we replaced less than content.Length, we need to insert the rest
var remainingLength = text.Length - replaceLength;
if (remainingLength > 0)
{
var textToInsert = text[replaceLength..];
Logger.ConditionalTrace(
$"Inserting: (cursor = {writeCursor}, " + $"text = {textToInsert.ToRepr()})"
);
Document.Insert(writeCursor, textToInsert);
writeCursor += textToInsert.Length;
}
}
}
private void CheckMaxLines()
{
// Ignore limit if MaxLines is negative
if (MaxLines < 0)
return;
if (Document.LineCount <= MaxLines)
return;
// Minimum lines to remove
const int removeLinesBatchSize = 1;
using (Document.RunUpdate())
{
var currentLines = Document.LineCount;
var linesExceeded = currentLines - MaxLines;
var linesToRemove = Math.Min(currentLines, Math.Max(linesExceeded, removeLinesBatchSize));
Logger.ConditionalTrace(
"Exceeded max lines ({Current} > {Max}), removing {Remove} lines",
currentLines,
MaxLines,
linesToRemove
);
// Remove lines from the start
var firstLine = Document.GetLineByNumber(1);
var lastLine = Document.GetLineByNumber(linesToRemove);
var removeStart = firstLine.Offset;
// If a next line exists, use the start offset of that instead in case of weird newlines
var removeEnd = lastLine.EndOffset;
if (lastLine.NextLine is not null)
{
removeEnd = lastLine.NextLine.Offset;
}
var removeLength = removeEnd - removeStart;
Logger.ConditionalTrace(
"Removing {LinesExceeded} lines from start: ({RemoveStart} -> {RemoveEnd})",
linesExceeded,
removeStart,
removeEnd
);
Document.Remove(removeStart, removeLength);
// Update cursor position
writeCursor -= removeLength;
}
}
/// <summary>
/// Debug function to print the current document to the console.
/// Includes formatted cursor position.
/// </summary>
[Conditional("DEBUG")]
private void DebugPrintDocument()
{
if (!Logger.IsTraceEnabled)
return;
var text = Document.Text;
// Add a number for each line
// Add an arrow line for where the cursor is, for example (cursor on offset 3):
//
// 1 | This is the first line
// ~~~~~~~^ (3)
// 2 | This is the second line
//
var lines = text.Split(Environment.NewLine).ToList();
var numberPadding = lines.Count.ToString().Length;
for (var i = 0; i < lines.Count; i++)
{
lines[i] = $"{(i + 1).ToString().PadLeft(numberPadding)} | {lines[i]}";
}
var cursorLine = Document.GetLineByOffset(writeCursor);
var cursorLineOffset = writeCursor - cursorLine.Offset;
// Need to account for padding + line number + space + cursor line offset
var linePadding = numberPadding + 3 + cursorLineOffset;
var cursorLineArrow = new string('~', linePadding) + $"^ ({writeCursor})";
// If more than line count, append to end
if (cursorLine.LineNumber >= lines.Count)
{
lines.Add(cursorLineArrow);
}
else
{
lines.Insert(cursorLine.LineNumber, cursorLineArrow);
}
var textWithCursor = string.Join(Environment.NewLine, lines);
Logger.ConditionalTrace("[Current Document]");
Logger.ConditionalTrace(textWithCursor);
}
/// <summary>
/// Posts an update to the console
/// <remarks>Safe to call on non-UI threads</remarks>
/// </summary>
public void Post(ProcessOutput output)
{
// If update task is running, send to buffer
if (updateTask != null)
{
buffer.Post(output);
return;
}
// Otherwise, use manual update one
Logger.Debug("Synchronous post update to console: {@Output}", output);
Dispatcher.UIThread.Post(() => ConsoleUpdateOne(output));
}
/// <summary>
/// Posts an update to the console.
/// Helper for calling Post(ProcessOutput) with strings
/// </summary>
public void Post(string text)
{
Post(new ProcessOutput { Text = text });
}
/// <summary>
/// Posts an update to the console.
/// Equivalent to Post(text + Environment.NewLine)
/// </summary>
public void PostLine(string text)
{
Post(new ProcessOutput { Text = text + Environment.NewLine });
}
public void Dispose()
{
if (isDisposed)
return;
updateCts?.Cancel();
updateCts?.Dispose();
updateCts = null;
buffer.Complete();
if (updateTask is not null)
{
Logger.Debug("Shutting down console update task");
try
{
updateTask.WaitWithoutException(new CancellationTokenSource(1000).Token);
updateTask.Dispose();
updateTask = null;
}
catch (OperationCanceledException)
{
Logger.Warn("During shutdown - Console update task cancellation timed out");
}
catch (InvalidOperationException e)
{
Logger.Warn(e, "During shutdown - Console update task dispose failed");
}
}
isDisposed = true;
GC.SuppressFinalize(this);
}
public async ValueTask DisposeAsync()
{
if (isDisposed)
return;
updateCts?.Cancel();
updateCts?.Dispose();
updateCts = null;
if (updateTask is not null)
{
Logger.Debug("Waiting for console update task shutdown...");
await updateTask;
updateTask.Dispose();
updateTask = null;
Logger.Debug("Console update task shutdown complete");
}
isDisposed = true;
GC.SuppressFinalize(this);
}
}