/
ConsoleImpl.cs
711 lines (576 loc) · 21.6 KB
/
ConsoleImpl.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
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
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
using System;
using System.Collections.Generic;
using System.Text;
using Cosmos.System;
using IL2CPU.API;
using IL2CPU.API.Attribs;
namespace Cosmos.System_Plugs.System
{
[Plug(Target = typeof (global::System.Console))]
public static class ConsoleImpl
{
private static ConsoleColor mForeground = ConsoleColor.White;
private static ConsoleColor mBackground = ConsoleColor.Black;
private static Encoding ConsoleInputEncoding = Encoding.ASCII;
private static Encoding ConsoleOutputEncoding = Encoding.ASCII;
private static readonly Cosmos.System.Console mFallbackConsole = new Cosmos.System.Console(null);
private static Cosmos.System.Console GetConsole()
{
return mFallbackConsole;
}
public static ConsoleColor get_BackgroundColor()
{
return mBackground;
}
public static void set_BackgroundColor(ConsoleColor value)
{
mBackground = value;
//Cosmos.HAL.Global.TextScreen.SetColors(mForeground, mBackground);
if (GetConsole() != null) GetConsole().Background = value;
}
public static int get_BufferHeight()
{
WriteLine("Not implemented: get_BufferHeight");
return -1;
}
public static void set_BufferHeight(int aHeight)
{
WriteLine("Not implemented: set_BufferHeight");
}
public static int get_BufferWidth()
{
WriteLine("Not implemented: get_BufferWidth");
return -1;
}
public static void set_BufferWidth(int aWidth)
{
WriteLine("Not implemented: set_BufferWidth");
}
public static bool get_CapsLock()
{
return Global.CapsLock;
}
public static int get_CursorLeft()
{
var xConsole = GetConsole();
if (xConsole == null)
{
// for now:
return 0;
}
return GetConsole().X;
}
public static void set_CursorLeft(int x)
{
var xConsole = GetConsole();
if (xConsole == null)
{
// for now:
return;
}
if (x < get_WindowWidth())
{
xConsole.X = x;
}
else
{
WriteLine("x must be lower than the console width!");
}
}
public static int get_CursorSize()
{
var xConsole = GetConsole();
if (xConsole == null)
{
// for now:
return 0;
}
return xConsole.CursorSize;
}
public static void set_CursorSize(int aSize)
{
var xConsole = GetConsole();
if (xConsole == null)
{
// for now:
return;
}
xConsole.CursorSize = aSize;
}
public static int get_CursorTop()
{
var xConsole = GetConsole();
if (xConsole == null)
{
// for now:
return 0;
}
return GetConsole().Y;
}
public static void set_CursorTop(int y)
{
var xConsole = GetConsole();
if (xConsole == null)
{
// for now:
return;
}
if (y < get_WindowHeight())
{
xConsole.Y = y;
}
else
{
WriteLine("y must be lower than the console height!");
}
}
public static bool get_CursorVisible()
{
var xConsole = GetConsole();
if (xConsole == null)
{
return false;
}
return GetConsole().CursorVisible;
}
public static void set_CursorVisible(bool value)
{
var xConsole = GetConsole();
if (xConsole == null)
{
// for now:
return;
}
xConsole.CursorVisible = value;
}
//public static TextWriter get_Error() {
// WriteLine("Not implemented: get_Error");
// return null;
//}
public static ConsoleColor get_ForegroundColor()
{
return mForeground;
}
public static void set_ForegroundColor(ConsoleColor value)
{
mForeground = value;
//Cosmos.HAL.Global.TextScreen.SetColors(mForeground, mBackground);
if (GetConsole() != null) GetConsole().Foreground = value;
}
//public static TextReader get_In()
//{
// WriteLine("Not implemented: get_In");
// return null;
//}
public static Encoding get_InputEncoding()
{
return ConsoleInputEncoding;
}
public static void set_InputEncoding(Encoding value)
{
ConsoleInputEncoding = value;
}
public static Encoding get_OutputEncoding()
{
return ConsoleOutputEncoding;
}
public static void set_OutputEncoding(Encoding value)
{
ConsoleOutputEncoding = value;
}
public static bool get_KeyAvailable()
{
return KeyboardManager.KeyAvailable;
}
public static int get_LargestWindowHeight()
{
WriteLine("Not implemented: get_LargestWindowHeight");
return -1;
}
public static int get_LargestWindowWidth()
{
WriteLine("Not implemented: get_LargestWindowWidth");
return -1;
}
public static bool get_NumberLock()
{
return Global.NumLock;
}
//public static TextWriter get_Out() {
// WriteLine("Not implemented: get_Out");
// return null;
//}
public static string get_Title()
{
WriteLine("Not implemented: get_Title");
return string.Empty;
}
public static void set_Title(string value)
{
WriteLine("Not implemented: set_Title");
}
public static bool get_TreatControlCAsInput()
{
WriteLine("Not implemented: get_TreatControlCAsInput");
return false;
}
public static void set_TreatControlCAsInput(bool value)
{
WriteLine("Not implemented: set_TreatControlCAsInput");
}
public static int get_WindowHeight()
{
var xConsole = GetConsole();
if (xConsole == null)
{
// for now:
return 25;
}
return GetConsole().Rows;
}
public static void set_WindowHeight(int value)
{
WriteLine("Not implemented: set_WindowHeight");
}
public static int get_WindowLeft()
{
WriteLine("Not implemented: get_WindowLeft");
return -1;
}
public static void set_WindowLeft(int value)
{
WriteLine("Not implemented: set_WindowLeft");
}
public static int get_WindowTop()
{
WriteLine("Not implemented: get_WindowTop");
return -1;
}
public static void set_WindowTop(int value)
{
WriteLine("Not implemented: set_WindowTop");
}
public static int get_WindowWidth()
{
var xConsole = GetConsole();
if (xConsole == null)
{
// for now:
return 85;
}
return GetConsole().Cols;
}
public static void set_WindowWidth(int value)
{
WriteLine("Not implemented: set_WindowWidth");
}
/// <summary>
/// The ArgumentOutOfRangeException check is now done at driver level in PCSpeaker - is it still needed here?
/// </summary>
/// <param name="aFrequency"></param>
/// <param name="aDuration"></param>
public static void Beep(int aFrequency, int aDuration)
{
if (aFrequency < 37 || aFrequency > 32767)
{
throw new ArgumentOutOfRangeException("Frequency must be between 37 and 32767Hz");
}
if (aDuration <= 0)
{
throw new ArgumentOutOfRangeException("Duration must be more than 0");
}
PCSpeaker.Beep((uint) aFrequency, (uint) aDuration);
}
/// <summary>
/// Beep() is pure CIL
/// Default implementation beeps for 200 milliseconds at 800 hertz
/// In Cosmos, these are Cosmos.System.Duration.Default and Cosmos.System.Notes.Default respectively,
/// and are used when there are no params
/// https://docs.microsoft.com/en-us/dotnet/api/system.console.beep?view=netcore-2.0
/// </summary>
public static void Beep()
{
PCSpeaker.Beep();
}
//TODO: Console uses TextWriter - intercept and plug it instead
public static void Clear()
{
var xConsole = GetConsole();
if (xConsole == null)
{
// for now:
return;
}
GetConsole().Clear();
}
// MoveBufferArea(int, int, int, int, int, int) is pure CIL
public static void MoveBufferArea(int sourceLeft, int sourceTop, int sourceWidth, int sourceHeight,
int targetLeft, int targetTop, Char sourceChar, ConsoleColor sourceForeColor, ConsoleColor sourceBackColor)
{
WriteLine("Not implemented: MoveBufferArea");
}
//public static Stream OpenStandardError() {
// WriteLine("Not implemented: OpenStandardError");
//}
//public static Stream OpenStandardError(int bufferSize) {
// WriteLine("Not implemented: OpenStandardError");
//}
//public static Stream OpenStandardInput(int bufferSize) {
// WriteLine("Not implemented: OpenStandardInput");
//}
//public static Stream OpenStandardInput() {
// WriteLine("Not implemented: OpenStandardInput");
//}
//public static Stream OpenStandardOutput(int bufferSize) {
// WriteLine("Not implemented: OpenStandardOutput");
//}
//public static Stream OpenStandardOutput() {
// WriteLine("Not implemented: OpenStandardOutput");
//}
public static int Read()
{
// TODO special cases, if needed, that returns -1
KeyEvent xResult;
if (KeyboardManager.TryReadKey(out xResult))
{
return xResult.KeyChar;
}
else
{
return -1;
}
}
public static ConsoleKeyInfo ReadKey()
{
return ReadKey(false);
}
// ReadKey() pure CIL
public static ConsoleKeyInfo ReadKey(bool intercept)
{
var key = KeyboardManager.ReadKey();
if (intercept == false && key.KeyChar != '\0')
{
Write(key.KeyChar);
}
//TODO: Plug HasFlag and use the next 3 lines instead of the 3 following lines
//bool xShift = key.Modifiers.HasFlag(ConsoleModifiers.Shift);
//bool xAlt = key.Modifiers.HasFlag(ConsoleModifiers.Alt);
//bool xControl = key.Modifiers.HasFlag(ConsoleModifiers.Control);
bool xShift = (key.Modifiers & ConsoleModifiers.Shift) == ConsoleModifiers.Shift;
bool xAlt = (key.Modifiers & ConsoleModifiers.Alt) == ConsoleModifiers.Alt;
bool xControl = (key.Modifiers & ConsoleModifiers.Control) == ConsoleModifiers.Control;
return new ConsoleKeyInfo(key.KeyChar, key.Key.ToConsoleKey(), xShift, xAlt, xControl);
}
public static String ReadLine()
{
var xConsole = GetConsole();
if (xConsole == null)
{
// for now:
return null;
}
List<char> chars = new List<char>(32);
KeyEvent current;
int currentCount = 0;
while ((current = KeyboardManager.ReadKey()).Key != ConsoleKeyEx.Enter)
{
if (current.Key == ConsoleKeyEx.NumEnter) break;
//Check for "special" keys
if (current.Key == ConsoleKeyEx.Backspace) // Backspace
{
if (currentCount > 0)
{
int curCharTemp = GetConsole().X;
chars.RemoveAt(currentCount - 1);
GetConsole().X = GetConsole().X - 1;
//Move characters to the left
for (int x = currentCount - 1; x < chars.Count; x++)
{
Write(chars[x]);
}
Write(' ');
GetConsole().X = curCharTemp - 1;
currentCount--;
}
continue;
}
else if (current.Key == ConsoleKeyEx.LeftArrow)
{
if (currentCount > 0)
{
GetConsole().X = GetConsole().X - 1;
currentCount--;
}
continue;
}
else if (current.Key == ConsoleKeyEx.RightArrow)
{
if (currentCount < chars.Count)
{
GetConsole().X = GetConsole().X + 1;
currentCount++;
}
continue;
}
if (current.KeyChar == '\0') continue;
//Write the character to the screen
if (currentCount == chars.Count)
{
chars.Add(current.KeyChar);
Write(current.KeyChar);
currentCount++;
}
else
{
//Insert the new character in the correct location
//For some reason, List.Insert() doesn't work properly
//so the character has to be inserted manually
List<char> temp = new List<char>();
for (int x = 0; x < chars.Count; x++)
{
if (x == currentCount)
{
temp.Add(current.KeyChar);
}
temp.Add(chars[x]);
}
chars = temp;
//Shift the characters to the right
for (int x = currentCount; x < chars.Count; x++)
{
Write(chars[x]);
}
GetConsole().X -= (chars.Count - currentCount) - 1;
currentCount++;
}
}
WriteLine();
char[] final = chars.ToArray();
return new string(final);
}
public static void ResetColor()
{
set_BackgroundColor(ConsoleColor.Black);
set_ForegroundColor(ConsoleColor.White);
}
public static void SetBufferSize(int width, int height)
{
WriteLine("Not implemented: SetBufferSize");
}
public static void SetCursorPosition(int left, int top)
{
set_CursorLeft(left);
set_CursorTop(top);
}
//public static void SetError(TextWriter newError) {
// WriteLine("Not implemented: SetError");
//}
//public static void SetIn(TextReader newIn) {
// WriteLine("Not implemented: SetIn");
//}
//public static void SetOut(TextWriter newOut) {
// WriteLine("Not implemented: SetOut");
//}
public static void SetWindowPosition(int left, int top)
{
WriteLine("Not implemented: SetWindowPosition");
}
public static void SetWindowSize(int width, int height)
{
WriteLine("Not implemented: SetWindowSize");
}
#region Write
public static void Write(bool aBool)
{
Write(aBool.ToString());
}
/*
* A .Net character can be effectevily more can one byte so calling the low level Console.Write() will be wrong as
* it accepts only bytes, we need to convert it using the specified OutputEncoding but to do this we have to convert
* it ToString first
*/
public static void Write(char aChar) => Write(aChar.ToString());
public static void Write(char[] aBuffer) => Write(aBuffer, 0, aBuffer.Length);
/* Decimal type is not working yet... */
//public static void Write(decimal aDecimal) => Write(aDecimal.ToString());
public static void Write(double aDouble) => Write(aDouble.ToString());
public static void Write(float aFloat) => Write(aFloat.ToString());
public static void Write(int aInt) => Write(aInt.ToString());
public static void Write(long aLong) => Write(aLong.ToString());
/* Correct behaviour printing null should not throw NRE or do nothing but should print an empty string */
public static void Write(object value) => Write((value ?? String.Empty));
public static void Write(string aText)
{
var xConsole = GetConsole();
if (xConsole == null)
{
// for now:
return;
}
byte[] aTextEncoded = ConsoleOutputEncoding.GetBytes(aText);
GetConsole().Write(aTextEncoded);
}
public static void Write(uint aInt) => Write(aInt.ToString());
public static void Write(ulong aLong) => Write(aLong.ToString());
public static void Write(string format, object arg0) => Write(String.Format(format, arg0));
public static void Write(string format, object arg0, object arg1) => Write(String.Format(format, arg0, arg1));
public static void Write(string format, object arg0, object arg1, object arg2) => Write(String.Format(format, arg0, arg1, arg2));
public static void Write(string format, object arg0, object arg1, object arg2, object arg3) => Write(String.Format(format, arg0, arg1, arg2, arg3));
public static void Write(string format, params object[] arg) => Write(String.Format(format, arg));
public static void Write(char[] aBuffer, int aIndex, int aCount)
{
if (aBuffer == null)
{
throw new ArgumentNullException("aBuffer");
}
if (aIndex < 0)
{
throw new ArgumentOutOfRangeException("aIndex");
}
if (aCount < 0)
{
throw new ArgumentOutOfRangeException("aCount");
}
if ((aBuffer.Length - aIndex) < aCount)
{
throw new ArgumentException();
}
for (int i = 0; i < aCount; i++)
{
Write(aBuffer[aIndex + i]);
}
}
//You'd expect this to be on System.Console wouldn't you? Well, it ain't so we just rely on Write(object value)
//public static void Write(byte aByte) {
// Write(aByte.ToString());
//}
#endregion
#region WriteLine
public static void WriteLine() => Write(Environment.NewLine);
public static void WriteLine(bool aBool) => WriteLine(aBool.ToString());
public static void WriteLine(char aChar) => WriteLine(aChar.ToString());
public static void WriteLine(char[] aBuffer) => WriteLine(new String(aBuffer));
/* Decimal type is not working yet... */
//public static void WriteLine(decimal aDecimal) => WriteLine(aDecimal.ToString());
public static void WriteLine(double aDouble) => WriteLine(aDouble.ToString());
public static void WriteLine(float aFloat) => WriteLine(aFloat.ToString());
public static void WriteLine(int aInt) => WriteLine(aInt.ToString());
public static void WriteLine(long aLong) => WriteLine(aLong.ToString());
/* Correct behaviour printing null should not throw NRE or do nothing but should print an empty line */
public static void WriteLine(object value) => Write((value ?? String.Empty) + Environment.NewLine);
public static void WriteLine(string aText) => Write(aText + Environment.NewLine);
public static void WriteLine(uint aInt) => WriteLine(aInt.ToString());
public static void WriteLine(ulong aLong) => WriteLine(aLong.ToString());
public static void WriteLine(string format, object arg0) => WriteLine(String.Format(format, arg0));
public static void WriteLine(string format, object arg0, object arg1) => WriteLine(String.Format(format, arg0, arg1));
public static void WriteLine(string format, object arg0, object arg1, object arg2) => WriteLine(String.Format(format, arg0, arg1, arg2));
public static void WriteLine(string format, object arg0, object arg1, object arg2, object arg3) => WriteLine(String.Format(format, arg0, arg1, arg2, arg3));
public static void WriteLine(string format, params object[] arg) => WriteLine(String.Format(format, arg));
public static void WriteLine(char[] aBuffer, int aIndex, int aCount)
{
Write(aBuffer, aIndex, aCount);
WriteLine();
}
#endregion
}
}