-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathApp.razor.cs
188 lines (159 loc) · 5.9 KB
/
App.razor.cs
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
using CUIFlavoredPortfolioSite.Services;
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.Web;
using Microsoft.JSInterop;
using Toolbelt.Blazor.HotKeys2;
using static Toolbelt.AnsiEscCode.Colorize;
namespace CUIFlavoredPortfolioSite;
public partial class App(PathUtility pathUtility) : IAsyncDisposable
{
public enum RuntimeModes
{
Release,
Debug
}
[Parameter]
public RuntimeModes RuntimeMode { get; set; }
#if RELEASE
= RuntimeModes.Release;
#else
= RuntimeModes.Debug;
#endif
private readonly PathUtility PathUtility = pathUtility;
private ElementReference CommandLineInput;
private string CommandLineInputText { get; set; } = "";
private CommandHistory CommandHistory { get; } = new CommandHistory();
private bool CommandProcessing = false;
private bool _Initialized = false;
private HotKeysContext _HotKeysContext;
protected override async Task OnInitializedAsync()
{
this._HotKeysContext = this.HotKeys.CreateContext().Add(ModCode.Ctrl, Code.C, () => this.OnCtrlC());
await Task.Delay(400);
if (this.RuntimeMode != RuntimeModes.Debug)
{
var todaysMMDD = DateTime.Now.Month * 100 + DateTime.Now.Day;
if (todaysMMDD is >= 101 and <= 115)
{
await this.TypeAndExecuteCommandAsync("new-year-greeting");
}
else
{
await this.TypeAndExecuteCommandAsync("banner");
}
await Task.Delay(400);
await this.TypeAndExecuteCommandAsync("profile");
}
this._Initialized = true;
this.StateHasChanged();
}
private async Task TypeAndExecuteCommandAsync(string text)
{
var r = new Random((int)(DateTime.Now.Ticks % int.MaxValue));
foreach (var c in text)
{
this.CommandLineInputText += c;
this.StateHasChanged();
await Task.Delay(r.Next(50, 200));
}
await Task.Delay(400);
await this.ExecuteCommandAsync();
}
private string GetTwitterShareButtonUrl()
{
const string description = "This is a CUI flavored portfolio site about @jsakamoto that he created using Blazor WebAssembly!";
const string url = "https://jsakamoto.github.io/";
return "https://twitter.com/intent/tweet" +
$"?text={Uri.EscapeDataString(description)}" +
$"&hashtags=Blazor" +
$"&url={Uri.EscapeDataString(url)}";
}
protected override async Task OnAfterRenderAsync(bool firstRender)
{
await this.CommandLineInput.FocusAsync();
await this.JS.InvokeVoidAsync("Helper.scrollIntoView", this.CommandLineInput);
}
private void OnCtrlC()
{
this._CommandCanceller?.Cancel();
}
private async Task OnKeyDownCommandLineInput(KeyboardEventArgs e)
{
if (!this._Initialized) return;
switch (e.Code)
{
case "Enter":
await this.ExecuteCommandAsync();
break;
case "KeyL":
if (e.CtrlKey) await this.ProcessCommandLineAsync("clear", noSaveHistory: true);
break;
case "ArrowUp":
this.RecallHistory(this.CommandHistory.TryGetPrevious(out var prevCommand), prevCommand);
break;
case "ArrowDown":
this.RecallHistory(this.CommandHistory.TryGetNext(out var nextCommand), nextCommand);
break;
case "Tab":
this.CommandLineInputText = this.CommandCompletion.Completion(this.CommandLineInputText);
this.StateHasChanged();
break;
default: break;
}
}
private void RecallHistory(bool found, string commandText)
{
if (!found) return;
this.CommandLineInputText = commandText;
this.StateHasChanged();
}
private async ValueTask ExecuteCommandAsync()
{
this.ConsoleHost.WriteLine($"{Green("jsakamoto")}:{Blue(this.PathUtility.GetCurrentDirectoryDisplayText())}$ {this.CommandLineInputText}");
await this.ProcessCommandLineAsync(this.CommandLineInputText);
this.CommandLineInputText = "";
this.StateHasChanged();
}
private CancellationTokenSource _CommandCanceller;
private async ValueTask ProcessCommandLineAsync(string commandLineInputText, bool noSaveHistory = false)
{
this._CommandCanceller?.Dispose();
this._CommandCanceller = new CancellationTokenSource();
if (!noSaveHistory) this.CommandHistory.Push(commandLineInputText);
if (commandLineInputText != "")
{
var commandArgs = commandLineInputText.Split(' ');
var commandName = commandArgs.First();
if (this.CommandSet.TryGetCommand(commandName, out var command))
{
try
{
this.CommandProcessing = true;
await command.InvokeAsync(this.ConsoleHost, commandArgs, this._CommandCanceller.Token);
}
catch (Exception e)
{
this.ConsoleHost.WriteLine(Red(e.ToString()));
}
finally
{
this.CommandProcessing = false;
if (this._CommandCanceller?.IsCancellationRequested == true)
{
this.ConsoleHost.WriteLine("^C");
}
}
}
else
{
this.ConsoleHost.WriteLine($"{commandName}: command not found. Please try out {Cyan("help")} command.");
}
this.ConsoleHost.WriteLine();
}
}
public async ValueTask DisposeAsync()
{
GC.SuppressFinalize(this);
if (this._HotKeysContext is not null) await this._HotKeysContext.DisposeAsync();
}
}