-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathVirtualTerminal.cs
More file actions
232 lines (199 loc) · 7.42 KB
/
VirtualTerminal.cs
File metadata and controls
232 lines (199 loc) · 7.42 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
using System;
using System.Collections.Generic;
using System.IO;
using Roslyn.Compilers;
using Roslyn.Compilers.CSharp;
namespace RemoteCSharpShell
{
public class VirtualTerminal
{
private readonly TextReader @in;
private readonly TextWriter @out;
private readonly List<string> history;
private ConsoleColor foregroundColor;
public ConsoleColor ForegroundColor
{
get { return this.foregroundColor; }
set
{
if (value != this.foregroundColor)
{
this.@out.Write("\x1b[{0}m", ColorToAnsi(value));
this.foregroundColor = value;
}
}
}
public VirtualTerminal(TextReader @in, TextWriter @out)
{
this.@in = @in;
this.@out = @out;
this.history = new List<string>();
}
public string ReadLine(string prompt)
{
string line = "";
int inTextPosition = 0;
int selectedHistoryEntry = 0;
this.@out.Write(prompt);
while (true)
{
var p = this.@in.Read();
switch ((ConsoleKey)p)
{
case ConsoleKey.Escape:
switch (HandleEscape())
{
case EscapeCode.MoveLeft:
inTextPosition--;
break;
case EscapeCode.MoveRight:
inTextPosition++;
break;
case EscapeCode.Up:
selectedHistoryEntry = Bound(1, selectedHistoryEntry + 1, this.history.Count);
line = this.history[this.history.Count - selectedHistoryEntry];
break;
case EscapeCode.Down:
selectedHistoryEntry = Bound(1, selectedHistoryEntry - 1, this.history.Count);
line = this.history[this.history.Count - selectedHistoryEntry];
break;
}
break;
case ConsoleKey.Enter:
NewLine();
return line;
case ConsoleKey.F16: //backspace
if (inTextPosition > 0)
{
line = line.Remove(inTextPosition - 1, 1);
inTextPosition--;
}
break;
default:
line = line.Insert(inTextPosition, ((char)p).ToString());
inTextPosition++;
break;
}
inTextPosition = Bound(0, inTextPosition, line.Length);
this.@out.Write("\x1b[s\x1b[0G\x1b[K");
this.@out.Write(prompt);
WriteCode(line);
this.@out.Write("\x1b[{0}G", prompt.Length + (char)inTextPosition + 1);
}
}
public void WriteCode(string line)
{
var options = new ParseOptions(
CompatibilityMode.None,
LanguageVersion.CSharp4,
true,
SourceCodeKind.Interactive,
default(ReadOnlyArray<string>));
var tree = SyntaxTree.ParseText(line, options: options);
var root = tree.GetRoot();
foreach (var item in root.DescendantTokens())
{
this.ForegroundColor = ConsoleColor.White;
this.@out.Write(item.LeadingTrivia);
if (item.IsKeyword() || item.IsReservedKeyword())
{
this.ForegroundColor = ConsoleColor.Yellow;
this.@out.Write(item);
}
else if (item.Kind == SyntaxKind.StringLiteralToken)
{
this.ForegroundColor = ConsoleColor.Green;
this.@out.Write(item);
}
else if (item.Kind == SyntaxKind.NumericLiteralToken)
{
this.ForegroundColor = ConsoleColor.Green;
this.@out.Write(item);
}
else
{
this.@out.Write(item);
}
this.@out.Write(item.TrailingTrivia);
}
}
private int Bound(int lower, int value, int upper)
{
return Math.Max(lower, Math.Min(value, upper));
}
private void NewLine()
{
this.@out.WriteLine();
}
private EscapeCode HandleEscape()
{
var secondChar = this.@in.Read();
if (secondChar == '[')
{
var actionCode = this.@in.Read();
switch ((ConsoleKey)actionCode)
{
case ConsoleKey.D:
return EscapeCode.MoveLeft;
case ConsoleKey.C:
return EscapeCode.MoveRight;
case ConsoleKey.A:
return EscapeCode.Up;
case ConsoleKey.B:
return EscapeCode.Down;
default:
return EscapeCode.Unknown;
}
}
return EscapeCode.Unknown;
}
public void Write(string text)
{
this.@out.Write(text);
}
public void RecordHistoryLine(string line)
{
this.history.Add(line);
}
private enum EscapeCode
{
MoveLeft,
MoveRight,
Unknown,
Up,
Down,
}
public void WriteLine()
{
this.@out.WriteLine();
}
public void WriteLine(string value)
{
this.@out.WriteLine(value);
}
public static string ColorToAnsi(ConsoleColor value)
{
switch (value)
{
case ConsoleColor.Black: return "0;30";
case ConsoleColor.White: return "1;37";
case ConsoleColor.DarkBlue: return "0;34";
case ConsoleColor.DarkGreen: return "0;32";
case ConsoleColor.DarkCyan: return "0;36";
case ConsoleColor.DarkRed: return "0;31";
case ConsoleColor.DarkMagenta: return "0;35";
case ConsoleColor.DarkYellow: return "0;33";
case ConsoleColor.DarkGray: return "0;33";
case ConsoleColor.Blue: return "1;34";
case ConsoleColor.Green: return "1;32";
case ConsoleColor.Cyan: return "1;36";
case ConsoleColor.Red: return "1;31";
case ConsoleColor.Magenta: return "1;35";
case ConsoleColor.Yellow: return "1;33";
case ConsoleColor.Gray: return "1;37";
default:
throw new ArgumentOutOfRangeException("value");
}
}
}
}