-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKeyboardHandler.cs
More file actions
197 lines (177 loc) · 9.21 KB
/
KeyboardHandler.cs
File metadata and controls
197 lines (177 loc) · 9.21 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
using NShell.Shell.Commands;
using NShell.Shell.History;
using Spectre.Console;
namespace NShell.Shell.Keyboard
{
public class KeyboardHandler
{
public static void Handler(HistoryManager history, ShellContext context, CommandParser parser)
{
ConsoleKeyInfo key;
ConsoleKeyInfo promptKey;
string inputBuffer = "";
history.ResetIndex();
while (true)
{
Environment.SetEnvironmentVariable("LS_COLORS", context.GetLsColors());
context.SetTheme(context.CurrentTheme);
AnsiConsole.Markup(context.GetPrompt());
inputBuffer = "";
history.ResetIndex();
while (true)
{
key = Console.ReadKey(intercept: true);
if (key.Key == ConsoleKey.Enter)
{
if (inputBuffer.Trim() != "")
{
history.Add(inputBuffer);
history.Save();
}
Console.WriteLine();
break;
}
if (key.Key == ConsoleKey.Backspace && inputBuffer.Length > 0)
{
inputBuffer = inputBuffer[..^1];
Console.Write("\b \b");
}
else if (key.Key == ConsoleKey.UpArrow)
{
var prev = history.GetPrevious();
if (prev != null)
{
Console.Write(new string('\b', inputBuffer.Length) + new string(' ', inputBuffer.Length) + new string('\b', inputBuffer.Length));
inputBuffer = prev;
Console.Write(inputBuffer);
}
}
else if (key.Key == ConsoleKey.DownArrow)
{
var next = history.GetNext();
Console.Write(new string('\b', inputBuffer.Length) + new string(' ', inputBuffer.Length) + new string('\b', inputBuffer.Length));
inputBuffer = next ?? "";
Console.Write(inputBuffer);
}
else if (key.Key == ConsoleKey.Tab)
{
if (inputBuffer.StartsWith("cd "))
{
string path = inputBuffer.Substring(3);
string currentDir = Directory.GetCurrentDirectory();
string fullPath = Path.Combine(currentDir, path);
if (Directory.Exists(fullPath))
{
inputBuffer += Path.DirectorySeparatorChar;
var directories = Directory.GetDirectories(fullPath);
if (directories.Length > 0)
{
string directoryToSuggest = directories[0];
Console.Write(new string('\b', inputBuffer.Length) + new string(' ', inputBuffer.Length) + new string('\b', inputBuffer.Length));
inputBuffer = "cd " + directoryToSuggest;
Console.Write(inputBuffer);
}
else
{
Console.Write(new string('\b', inputBuffer.Length) + new string(' ', inputBuffer.Length) + new string('\b', inputBuffer.Length));
Console.Write(inputBuffer);
}
}
else
{
Console.Write(new string('\b', inputBuffer.Length) + new string(' ', inputBuffer.Length) + new string('\b', inputBuffer.Length));
Console.Write(inputBuffer);
}
} else if (inputBuffer.Length > 0) {
HashSet<string> suggestCommands = new();
HashSet<string> allCommands = CommandParser.CustomCommands.Keys
.Concat(CommandParser.SystemCommands)
.ToHashSet();
foreach (var suggestion in allCommands)
{
if (suggestion.StartsWith(inputBuffer))
{
suggestCommands.Add(suggestion);
}
}
if (suggestCommands.Count > 15)
{
bool showFullList = false;
AnsiConsole.MarkupLine($"[[[yellow]*[/]]] - Do you want to list all [yellow]{suggestCommands.Count}[/] commands ? ([yellow]y[/]/[yellow]n[/]) >> ");
while (true)
{
promptKey = Console.ReadKey(intercept: true);
if (promptKey.Key == ConsoleKey.Y || promptKey.Key == ConsoleKey.N)
{
if (promptKey.Key == ConsoleKey.Y)
{
showFullList = true;
}
break;
}
}
if (showFullList)
{
var sortedCommands = suggestCommands.OrderBy(c => c.Length).ToArray();
int i = 0;
foreach (var cmd in sortedCommands)
{
if (i == 7)
{
AnsiConsole.MarkupLine("");
i = 0;
}
AnsiConsole.Markup($"{cmd} ");
i++;
}
inputBuffer = string.Empty;
AnsiConsole.MarkupLine("");
AnsiConsole.MarkupLine("[[[yellow]*[/]]] - Press enter to continue...");
}
}
else
{
var sortedCommands = suggestCommands.OrderBy(c => c.Length).ToArray();
List<string> commandsList = sortedCommands.ToList();
int currentIndex = 0;
if (!string.IsNullOrEmpty(inputBuffer))
{
for (int i = 0; i < commandsList.Count; i++)
{
if (commandsList[i].StartsWith(inputBuffer))
{
currentIndex = (i + 1) % commandsList.Count;
inputBuffer = commandsList[currentIndex];
break;
}
}
}
Console.SetCursorPosition(0, Console.CursorTop - 1);
Console.SetCursorPosition(0, Console.CursorTop);
Console.Write(new string(' ', Console.WindowWidth));
Console.SetCursorPosition(0, Console.CursorTop);
AnsiConsole.Markup(context.GetPrompt());
Console.Write(inputBuffer);
}
}
}
else if (!char.IsControl(key.KeyChar))
{
inputBuffer += key.KeyChar;
Console.Write(key.KeyChar);
}
}
if (string.IsNullOrWhiteSpace(inputBuffer)) continue;
history.Add(inputBuffer);
try
{
parser.TryExecute(inputBuffer, context);
}
catch (Exception ex)
{
AnsiConsole.MarkupLine($"[[[red]-[/]]] - Shell crash: [yellow]{ex.Message}[/]");
}
}
}
}
}