-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConsoleIO.cs
1531 lines (1530 loc) · 90.3 KB
/
ConsoleIO.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
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using System; //~ for datatypes/namespaces, Console object, ConsoleColor, Convert object and exceptions (the basics)
using System.Threading; //~ for Sleep (time delay)
using System.Globalization; //~ for NumberFormatInfo (NumberFormatting)
using System.Collections.Generic; //~ for List<T>
using System.Runtime.InteropServices; //~ for DllImports (fullscreen)
namespace ConsoleIO{
//~ If you're reading this, don't forget to document your code with these → https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/language-specification/documentation-comments#recommended-tags
/// <summary>A lot of static methods for creating a console based text adventure (needs 64bit on win7+)</summary>
public static class Run{
/// <summary>
/// get or set the cursor height in percent [1 to 100]
/// <br/>see also:
/// <br/><seealso cref="CursorShow"/>
/// </summary>
/// <exception cref="ArgumentOutOfRangeException">[set] if the cursor size is not in range [1 to 100]</exception>
public static Int32 CursorSize{
get => Console.CursorSize;
set{
if(value is < 1 or > 100) throw new ArgumentOutOfRangeException(nameof(value), "must be in range [1 to 100]");
Console.CursorSize = value;
}
}
/// <summary>
/// Used as directions for:
/// <br/><seealso cref="Scroll"/>
/// <br/><seealso cref="ScrollAnim"/>
/// <br/><seealso cref="ScrollOnce"/>
/// </summary>
public enum ScrollDirection : byte{ up, down, left, right }
/// <summary>
/// [private] used by
/// <br/><seealso cref="ScrollAnim"/>
/// </summary>
/// <param name="dir">[Optional] The direction in wich to scroll - defaults to <see cref="ScrollDirection.up"></param>
/// <param name="dir">[Optional] Whether to move the cursor in scroll direction or not (only moves if possible) - defaults to true</param>
private static void ScrollOnce(ScrollDirection dir = ScrollDirection.up, bool cm = true){
switch(dir){
case ScrollDirection.up:
if(cm && CursorY > 1) CursorY--;
Console.MoveBufferArea(1, 2, InnerWidth, InnerHeight - 1, 1, 1, ' ', Console.ForegroundColor, Console.BackgroundColor);
break;
case ScrollDirection.down:
if(cm && CursorY < InnerHeight) CursorY++;
Console.MoveBufferArea(1, 1, InnerWidth, InnerHeight - 1, 1, 2, ' ', Console.ForegroundColor, Console.BackgroundColor);
break;
case ScrollDirection.left:
if(cm && CursorX > 1) CursorX--;
Console.MoveBufferArea(2, 1, InnerWidth - 1, InnerHeight, 1, 1, ' ', Console.ForegroundColor, Console.BackgroundColor);
break;
case ScrollDirection.right:
if(cm && CursorX < InnerWidth) CursorX++;
Console.MoveBufferArea(1, 1, InnerWidth - 1, InnerHeight, 2, 1, ' ', Console.ForegroundColor, Console.BackgroundColor);
break;
}
}
/// <summary>scrolls the content inside borders by <paramref name="amount"/> in <see cref="direction"/> with <paramref name="ms"/> delay</summary>
/// <param name="amount">[Optional] how moch rows to scroll - [1 to (<see cref="InnerHeight"/> - 1)] - defaults to 1</param>
/// <param name="ms">[Optional] time in milliseconds for delay - must be a positive integer - defaults to 10</param>
/// <param name="cursorMove">[Optional] if true moves the cursor along with the scrolling - defaults to true</param>
/// <param name="direction">[Optional] in which direction to scroll - defaults to <see cref="ScrollDirection.up"/></param>
/// <exception cref="ArgumentOutOfRangeException">if <paramref name="amount"/> is not a positive integer</exception>
/// <exception cref="ArgumentOutOfRangeException">if <paramref name="ms"/> is not a positive integer</exception>
/// <exception cref="ArgumentOutOfRangeException">if <paramref name="cursorMove"/> is true but can not move the cursor <paramref name="amount"/> in <paramref name="direction"/></exception>
public static void ScrollAnim(int amount = 1, int ms = 10, bool cursorMove = true, ScrollDirection direction = ScrollDirection.up){
if(amount < 0) throw new ArgumentOutOfRangeException(nameof(amount), "must be a positive integer");
if(ms < 0) throw new ArgumentOutOfRangeException(nameof(ms), "must be a positive integer");
try{
if(ms == 0){
Scroll(amount, cursorMove, direction);
return;
}
for(; amount > 0; amount--){
ScrollOnce(direction, cursorMove);
Wait(ms);
}
}catch(ArgumentOutOfRangeException){ throw new ArgumentOutOfRangeException(nameof(cursorMove), "can not move the cursor that much with [cursorMove] on"); }
}
/// <summary>scrolls the content inside borders by <paramref name="amount"/> in <see cref="direction"/></summary>
/// <param name="amount">[Optional] how many rows to scroll - [1 to (<see cref="InnerHeight"/> - 1)] - defaults to 1</param>
/// <param name="cursorMove">[Optional] if true moves the cursor along with the scrolling - defaults to true</param>
/// <param name="direction">[Optional] in which direction to scroll - defaults to <see cref="ScrollDirection.up"/></param>
/// <exception cref="ArgumentOutOfRangeException">if <paramref name="amount"/> is not a positive integer</exception>
/// <exception cref="ArgumentOutOfRangeException">if <paramref name="cursorMove"/> is true but can not move the cursor <paramref name="amount"/> in <paramref name="direction"/></exception>
public static void Scroll(int amount = 1, bool cursorMove = true, ScrollDirection direction = ScrollDirection.up){
if(amount < 0) throw new ArgumentOutOfRangeException(nameof(amount), "must be a positive integer");
switch(direction){
case ScrollDirection.up:
if(cursorMove){
int _check = CursorY - amount;
if(_check < 1) throw new ArgumentOutOfRangeException(nameof(cursorMove), "can not move the cursor that much with [cursorMove] on");
else CursorY = _check;
} Console.MoveBufferArea(1, 1 + amount, InnerWidth, InnerHeight - amount, 1, 1, ' ', Console.ForegroundColor, Console.BackgroundColor);
break;
case ScrollDirection.down:
if(cursorMove){
int _check = CursorY + amount;
if(_check > InnerHeight) throw new ArgumentOutOfRangeException(nameof(cursorMove), "can not move the cursor that much with [cursorMove] on");
else CursorY = _check;
} Console.MoveBufferArea(1, 1, InnerWidth, InnerHeight - amount, 1, 1 + amount, ' ', Console.ForegroundColor, Console.BackgroundColor);
break;
case ScrollDirection.left:
if(cursorMove){
int _check = CursorX - amount;
if(_check < 1) throw new ArgumentOutOfRangeException(nameof(cursorMove), "can not move the cursor that much with [cursorMove] on");
else CursorX = _check;
} Console.MoveBufferArea(1 + amount, 1, InnerWidth - amount, InnerHeight, 1, 1, ' ', Console.ForegroundColor, Console.BackgroundColor);
break;
case ScrollDirection.right:
if(cursorMove){
int _check = CursorX + amount;
if(_check > InnerWidth) throw new ArgumentOutOfRangeException(nameof(cursorMove), "can not move the cursor that much with [cursorMove] on");
else CursorX = _check;
} Console.MoveBufferArea(1, 1, InnerWidth - amount, InnerHeight, 1 + amount, 1, ' ', Console.ForegroundColor, Console.BackgroundColor);
break;
}
}
/// <summary>plays a sound with the given frequency and duration (at system volume!)</summary>
/// <param name="freq">[Optional] the frequancy of the tone in Hertz - [37 to 32'767] - defaults to 800</param>
/// <param name="time">[Optional] the duration of the tone in milliseconds - must be a positive integer - defaults to 200</param>
/// <param name="wait">[Optional] if true waits for the tone to end - defaults to true</param>
/// <exception cref="freq">if <paramref name="freq"/> is not in range [37 to 32'767]</exception>
/// <exception cref="time">if <paramref name="time"/> is not a positive integer</exception>
public static void PlayTone(Int32 freq = 800, Int32 time = 200, bool wait = true){
if(freq is < 37 or > 32_767) throw new ArgumentOutOfRangeException(nameof(freq), "must be in range [37 to 32'767]");
if(time < 0) throw new ArgumentOutOfRangeException(nameof(time), "must be a positive integer");
if(time == 0) return;
//~ no volume option - better options with [new System.Media.SoundPlayer(@"c:\mywavfile.wav").Play();] but this needs a soundfile ~ or byte stream !
Console.Beep(freq, time);
if(wait) Wait(time);
}
/// <summary>
/// get or set the cursor position from the left [1 to innerWidth]
/// <br/>see also:
/// <br/><seealso cref="CursorY"/>
/// <br/><seealso cref="SetCursorPos"/>
/// <br/><seealso cref="InnerWidth"/>
/// <br/><seealso cref="InnerHeight"/>
/// <br/><seealso cref="SetConsoleSize"/>
/// <br/><seealso cref="LargestWidth"/>
/// <br/><seealso cref="LargestHeight"/>
/// <br/><seealso cref="SetFullscreen"/>
/// <br/><seealso cref="RevertFullscreen"/>
/// </summary>
/// <exception cref="ArgumentOutOfRangeException">[set] if the cursors X position is not in range [1 to <see cref="InnerWidth"/>]</exception>
public static Int32 CursorX{
get => Console.CursorLeft - Console.WindowLeft;
set{
if(value < 1 || value > InnerWidth) throw new ArgumentOutOfRangeException(nameof(value), "must be in range [1 to innerWidth]");
Console.CursorLeft = Console.WindowLeft + value;
}
}
/// <summary>
/// get or set the cursor position from the top [1 to innerHeight]
/// <br/>see also:
/// <br/><seealso cref="CursorX"/>
/// <br/><seealso cref="SetCursorPos"/>
/// <br/><seealso cref="InnerWidth"/>
/// <br/><seealso cref="InnerHeight"/>
/// <br/><seealso cref="SetConsoleSize"/>
/// <br/><seealso cref="LargestWidth"/>
/// <br/><seealso cref="LargestHeight"/>
/// <br/><seealso cref="SetFullscreen"/>
/// <br/><seealso cref="RevertFullscreen"/>
/// </summary>
/// <exception cref="ArgumentOutOfRangeException">[set] if the cursors Y position is not in range [1 to <see cref="InnerHeight"/>]</exception>
public static Int32 CursorY{
get => Console.CursorTop - Console.WindowTop;
set{
if(value < 1 || value > InnerHeight) throw new ArgumentOutOfRangeException(nameof(value), "must be in range [1 to innerHeight]");
Console.CursorTop = Console.WindowTop + value;
}
}
/// <summary>
/// set the position of the cursor in the console
/// <br/>see also:
/// <br/><seealso cref="CursorX"/>
/// <br/><seealso cref="CursorY"/>
/// <br/><seealso cref="InnerWidth"/>
/// <br/><seealso cref="InnerHeight"/>
/// <br/><seealso cref="SetConsoleSize"/>
/// <br/><seealso cref="LargestWidth"/>
/// <br/><seealso cref="LargestHeight"/>
/// <br/><seealso cref="SetFullscreen"/>
/// <br/><seealso cref="RevertFullscreen"/>
/// </summary>
/// <param name="col">column index (from left) - [1 to <see cref="GetInnerWidth"/>]</param>
/// <param name="row">row index (from top) - [1 to <see cref="GetInnerHeight"/>]</param>
/// <exception cref="ArgumentOutOfRangeException">if <paramref name="col"/> is not in range [1 to <see cref="GetInnerWidth"/>]</exception>
/// <exception cref="ArgumentOutOfRangeException">if <paramref name="row"/> is not in range [1 to <see cref="GetInnerHeight"/>]</exception>
public static void SetCursorPos(Int32 col, Int32 row){
if(col < 1 || col > InnerWidth) throw new ArgumentOutOfRangeException(nameof(col), "must be in range [1 to innerWidth]");
if(row < 1 || row > InnerHeight) throw new ArgumentOutOfRangeException(nameof(row), "must be in range [1 to innerHeight]");
Console.SetCursorPosition(col, row);
}
/// <summary>
/// get the maximum width the console could be set at
/// <br/>for <see cref="SetConsoleSize"/>
/// <br/>see also:
/// <br/><seealso cref="LargestHeight"/>
/// <br/><seealso cref="InnerWidth"/>
/// <br/><seealso cref="InnerHeight"/>
/// <br/><seealso cref="SetConsoleSize"/>
/// <br/><seealso cref="CursorX"/>
/// <br/><seealso cref="CursorY"/>
/// <br/><seealso cref="SetCursorPos"/>
/// <br/><seealso cref="SetFullscreen"/>
/// <br/><seealso cref="RevertFullscreen"/>
/// </summary>
public static Int32 LargestWidth => Console.LargestWindowWidth;
/// <summary>
/// get the maximum height the console could be set at
/// <br/>for <see cref="SetConsoleSize"/>
/// <br/>see also:
/// <br/><seealso cref="LargestWidth"/>
/// <br/><seealso cref="InnerWidth"/>
/// <br/><seealso cref="InnerHeight"/>
/// <br/><seealso cref="SetConsoleSize"/>
/// <br/><seealso cref="CursorX"/>
/// <br/><seealso cref="CursorY"/>
/// <br/><seealso cref="SetCursorPos"/>
/// <br/><seealso cref="SetFullscreen"/>
/// <br/><seealso cref="RevertFullscreen"/>
/// </summary>
public static Int32 LargestHeight => Console.LargestWindowHeight;
/// <summary>
/// get or set the console width (including the 1 width border on each side)
/// <br/><i>the border is overwritten with spaces and the cursor is reset to [0,0]</i>
/// <br/>see also:
/// <br/><seealso cref="InnerHeight"/>
/// <br/><seealso cref="SetConsoleSize"/>
/// <br/><seealso cref="DrawBorder"/>
/// <br/><seealso cref="LargestWidth"/>
/// <br/><seealso cref="LargestHeight"/>
/// <br/><seealso cref="CursorX"/>
/// <br/><seealso cref="CursorY"/>
/// <br/><seealso cref="SetCursorPos"/>
/// <br/><seealso cref="SetFullscreen"/>
/// <br/><seealso cref="RevertFullscreen"/>
/// </summary>
/// <exception cref="ArgumentOutOfRangeException">[set] if the window width is not in range [3 to <see cref="LargestWidth"/>]</exception>
public static Int32 InnerWidth{
get => Console.WindowWidth;
set{
if(value < 3 || value > LargestWidth) throw new ArgumentOutOfRangeException(nameof(value), "must be in range [3 to LargestWidth]");
Console.WindowWidth = value;
DrawBorder(' ');
SetCursorPos(0, 0);
}
}
/// <summary>
/// get or set the console height (including the 1 width border on each side)
/// <br/><i>the border is overwritten with spaces and the cursor is reset to [0,0]</i>
/// <br/>see also:
/// <br/><seealso cref="InnerWidth"/>
/// <br/><seealso cref="SetConsoleSize"/>
/// <br/><seealso cref="DrawBorder"/>
/// <br/><seealso cref="LargestWidth"/>
/// <br/><seealso cref="LargestHeight"/>
/// <br/><seealso cref="CursorX"/>
/// <br/><seealso cref="CursorY"/>
/// <br/><seealso cref="SetCursorPos"/>
/// <br/><seealso cref="SetFullscreen"/>
/// <br/><seealso cref="RevertFullscreen"/>
/// </summary>
/// <exception cref="ArgumentOutOfRangeException">[set] if the window height is not in range [3 to <see cref="LargestHeight"/>]</exception>
public static Int32 InnerHeight{
get => Console.WindowHeight;
set{
if(value < 3 || value > LargestHeight) throw new ArgumentOutOfRangeException(nameof(value), "must be in range [3 to LargestHeight]");
Console.WindowHeight = value;
DrawBorder(' ');
SetCursorPos(0, 0);
}
}
/// <summary>
/// sets the size of the console window
/// <br/><i>the border is overwritten with spaces and the cursor is reset to [0,0]</i>
/// <br/>see also:
/// <br/><seealso cref="InnerWidth"/>
/// <br/><seealso cref="InnerHeight"/>
/// <br/><seealso cref="LargestWidth"/>
/// <br/><seealso cref="LargestHeight"/>
/// <br/><seealso cref="DrawBorder"/>
/// <br/><seealso cref="CursorX"/>
/// <br/><seealso cref="CursorY"/>
/// <br/><seealso cref="SetFullscreen"/>
/// <br/><seealso cref="RevertFullscreen"/>
/// </summary>
/// <param name="width">the new width of the console - [3 to <see cref="LargestWidth"/>]</param>
/// <param name="height">the new height of the console - [3 to <see cref="LargestHeight"/>]</param>
/// <exception cref="ArgumentOutOfRangeException">if <paramref name="width"/> is not in range [3 to <see cref="LargestWidth"/>]</exception>
/// <exception cref="ArgumentOutOfRangeException">if <paramref name="height"/> is not in range [3 to <see cref="LargestHeight"/>]</exception>
public static void SetConsoleSize(Int32 width, Int32 height){
if(width < 3 || width > LargestWidth) throw new ArgumentOutOfRangeException(nameof(width), "must be in range [3 to LargestWidth]");
if(height < 3 || height > LargestHeight) throw new ArgumentOutOfRangeException(nameof(height), "must be in range [3 to LargestHeight]");
Console.SetWindowSize(width, height);
DrawBorder(' ');
SetCursorPos(0, 0);
}
/// <summary>
/// waits for input and returns it
/// <br/>custom input mode, can read [A-Z], [0-9], [Numpad0-9], space, '_', delete(left/right), move cursor(left/right/start/end) and tabulator(auto 1-4 spaces)
/// <br/>input ends either if <see cref="MaxInputSize"/> is reached or if ESC or Enter is pressed
/// <br/>see also:
/// <br/><seealso cref="GetNum"/>
/// <br/><seealso cref="GetIntNum"/>
/// <br/><seealso cref="GetSingleNum"/>
/// <br/><seealso cref="GetChar"/>
/// <br/><seealso cref="ClearInput"/>
/// </summary>
/// <param name="MaxInputSize">maximum characters allowet to enter - defaults to 10'000</param>
/// <return>the string inputed</return>
public static string GetInput(ushort MaxInputSize = 10_000){
int _c = CursorX,
_r = CursorY,
_i = 0;
string _s = "";
bool _end = false;
ConsoleKeyInfo _last;
do{
ClearInput();
CursorShow = true;
_last = Console.ReadKey(true);
CursorShow = false;
switch(_last.Key){
case ConsoleKey.Backspace:
if(_i > 0){
Text(new string(' ', _s.Length), true, _c, _r);
_s = _s.Substring(0, _i - 1) + _s.Substring(_i);
_i--;
}
break;
case ConsoleKey.Delete:
if(_i < _s.Length){
Text(new string(' ', _s.Length), true, _c, _r);
_s = _s.Substring(0, _i) + _s.Substring(_i + 1);
}
break;
case ConsoleKey.LeftArrow:
if(_i > 0) _i--;
break;
case ConsoleKey.RightArrow:
if(_i < _s.Length) _i++;
break;
case ConsoleKey.Home:
case ConsoleKey.UpArrow:
_i = 0;
break;
case ConsoleKey.End:
case ConsoleKey.DownArrow:
_i = _s.Length;
break;
case ConsoleKey.Tab:
int _tmp_t = 4 - (((_c + _i) % InnerWidth + 1) % 4);
_s = _s.Substring(0, _i) + new String((char)ConsoleKey.Spacebar, _tmp_t) + _s.Substring(_i);
_i += _tmp_t;
break;
case ConsoleKey.Enter:
case ConsoleKey.Escape:
_end = true;
break;
default:
if(
(_last.Key >= ConsoleKey.A && _last.Key <= ConsoleKey.Z)
|| (_last.Key >= ConsoleKey.D0 && _last.Key <= ConsoleKey.D9)
|| (_last.Key >= ConsoleKey.NumPad0 && _last.Key <= ConsoleKey.NumPad9)
|| _last.Key == ConsoleKey.Spacebar
|| _last.KeyChar == '_'
|| _last.KeyChar == '-'
|| _last.KeyChar == '.'
|| _last.KeyChar == ','
|| _last.KeyChar == '\''
){
_s = _s.Substring(0, _i) + _last.KeyChar + _s.Substring(_i);
_i++;
}
break;
}
SetColor(Colors.Yellow);
Text(_s, true, _c, _r);
SetColor();
CursorX = (((_c - 1) + _i) % InnerWidth) + 1;
CursorY = (_r + (int)(((_c - 1) + _i) / InnerWidth));
}while(!_end && _s.Length < MaxInputSize);
ClearInput();
return _s;
}
/// <summary>
/// waits for keypress and returns it
/// <br/>see also:
/// <br/><seealso cref="GetNum"/>
/// <br/><seealso cref="GetIntNum"/>
/// <br/><seealso cref="GetSingleNum"/>
/// <br/><seealso cref="GetInput"/>
/// <br/><seealso cref="ClearInput"/>
/// </summary>
/// <param name="hide">if true does not show pressed char in console - defaults to false → shows char in console</param>
/// <return>the key pressed</return>
public static char GetChar(bool hide = false){
ConsoleKeyInfo _tmp_c = Console.ReadKey(true);
if(!hide && _tmp_c.Key != ConsoleKey.Enter) Text("" + _tmp_c.KeyChar);
return _tmp_c.KeyChar;
}
/// <summary>
/// waits for input, converts it and then returns the number
/// <br/>see also:
/// <br/><seealso cref="GetNum"/>
/// <br/><seealso cref="GetIntNum"/>
/// <br/><seealso cref="GetChar"/>
/// <br/><seealso cref="GetInput"/>
/// <br/><seealso cref="ClearInput"/>
/// </summary>
/// <param name="hide">if true does not show pressed char in console - defaults to false → shows char in console (also on error)</param>
/// <return>the number</return>
/// <exception cref="FormatException">if the input is not a number</exception>
public static byte GetSingleNum(bool hide = false){
char _tmp = GetChar(hide);
if(_tmp < '0' || _tmp > '9') throw new FormatException("input is not a number");
return (byte)(_tmp - '0');
}
/// <summary>
/// waits for input, converts it and then returns the number
/// <br/>see also:
/// <br/><seealso cref="GetNum"/>
/// <br/><seealso cref="GetSingleNum"/>
/// <br/><seealso cref="GetChar"/>
/// <br/><seealso cref="GetInput"/>
/// <br/><seealso cref="ClearInput"/>
/// </summary>
/// <return>the number</return>
/// <exception cref="FormatException">if the input is in wrong format</exception>
/// <exception cref="OverflowException">if the input is too long to be converted</exception>
public static int GetIntNum() => Convert.ToInt32(GetInput());
/// <summary>
/// waits for input, converts it and then returns the number
/// <br/>see also:
/// <br/><seealso cref="GetIntNum"/>
/// <br/><seealso cref="GetSingleNum"/>
/// <br/><seealso cref="GetChar"/>
/// <br/><seealso cref="GetInput"/>
/// <br/><seealso cref="ClearInput"/>
/// </summary>
/// <return>the number</return>
/// <exception cref="FormatException">if the input is in wrong format</exception>
/// <exception cref="OverflowException">if the input is too long to be converted</exception>
public static double GetNum() => Convert.ToDouble(GetInput());
/// <summary>
/// clears the input stream
/// <br/>see also:
/// <br/><seealso cref="GetNum"/>
/// <br/><seealso cref="GetIntNum"/>
/// <br/><seealso cref="GetSingleNum"/>
/// <br/><seealso cref="GetChar"/>
/// <br/><seealso cref="GetInput"/>
/// </summary>
public static void ClearInput(){ while(Console.KeyAvailable)GetChar(true); }
/// <summary>
/// [private] imported function to set window size (maximize/minimize/restore/hidden/...)
/// <br/>used for <see cref="SetFullscreen"/> and <see cref="RevertFullscreen"/>
/// </summary>
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
//~ https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-showwindow#parameters
private const int W_MAXIMIZE = 3;
private const int W_RESTORE = 9;
/// <summary>
/// [private] imported function to get the console window process handle
/// <br/>used for <see cref="ShowWindow"/>'s first parameter
/// </summary>
[DllImport("kernel32.dll", ExactSpelling = true)]
private static extern IntPtr GetConsoleWindow();
/// <summary>
/// make window fullscreen
/// <br/>see also:
/// <br/><seealso cref="RevertFullscreen"/>
/// <br/><seealso cref="SetConsoleSize"/>
/// </summary>
public static void SetFullscreen() => ShowWindow(GetConsoleWindow(), W_MAXIMIZE);
/// <summary>
/// turn fullscreen off and restores the original window position and size
/// <br/>see also:
/// <br/><seealso cref="SetFullscreen"/>
/// <br/><seealso cref="SetConsoleSize"/>
/// </summary>
public static void RevertFullscreen() => ShowWindow(GetConsoleWindow(), W_RESTORE);
/// <summary>
/// get or set visibility of the console cursor
/// <br/>see also:
/// <br/><seealso cref="CursorSize"/>
/// </summary>
public static bool CursorShow{
get => Console.CursorVisible;
set => Console.CursorVisible = value;
}
/// <summary>set the title of the console window</summary>
/// <param name="title">title</param>
/// <exception cref="ArgumentOutOfRangeException">if <paramref name="title"/> is longer than 24'500 characters</exception>
public static void SetTitle(string title){
if(title.Length > 24_500) throw new ArgumentOutOfRangeException(nameof(title), "must not be more than 24'500 characters long");
Console.Title = title;
}
/// <summary>used as Colors for: <seealso cref="SetColor"/></summary>
public enum Colors : byte{
Black = ConsoleColor.Black, // 0
DarkBlue = ConsoleColor.DarkBlue, // 1
DarkGreen = ConsoleColor.DarkGreen, // 2
DarkCyan = ConsoleColor.DarkCyan, // 3
DarkRed = ConsoleColor.DarkRed, // 4
DarkMagenta = ConsoleColor.DarkMagenta, // 5
DarkYellow = ConsoleColor.DarkYellow, // 6
Gray = ConsoleColor.Gray, // 7
DarkGray = ConsoleColor.DarkGray, // 8
Blue = ConsoleColor.Blue, // 9
Green = ConsoleColor.Green, // A (10)
Cyan = ConsoleColor.Cyan, // B (11)
Red = ConsoleColor.Red, // C (12)
Magenta = ConsoleColor.Magenta, // D (13)
Yellow = ConsoleColor.Yellow, // E (14)
White = ConsoleColor.White // F (15)
}
/// <summary>
/// get or set the console background color
/// <br/>see also:
/// <br/><seealso cref="ForegroundColor"/>
/// <br/><seealso cref="Colors"/>
/// <br/><seealso cref="SetColor"/>
/// <br/><seealso cref="ResetColor"/>
/// </summary>
public static Colors BackgroundColor{
get => (Colors)Console.BackgroundColor;
set => Console.BackgroundColor = (ConsoleColor)value;
}
/// <summary>
/// get or set the console foreground color
/// <br/>see also:
/// <br/><seealso cref="BackgroundColor"/>
/// <br/><seealso cref="Colors"/>
/// <br/><seealso cref="SetColor"/>
/// <br/><seealso cref="ResetColor"/>
/// </summary>
public static Colors ForegroundColor{
get => (Colors)Console.ForegroundColor;
set => Console.ForegroundColor = (ConsoleColor)value;
}
/// <summary>
/// set the fore- and background color for the console
/// <br/>see also:
/// <br/><seealso cref="ForegroundColor"/>
/// <br/><seealso cref="BackgroundColor"/>
/// <br/><seealso cref="Colors"/>
/// <br/><seealso cref="ResetColor"/>
/// </summary>
/// <param name="foregroundColor">foreground color - defaults to <see cref="Colors.Green"/></param>
/// <param name="backgroundColor">background color - defaults to <see cref="Colors.Black"/></param>
public static void SetColor(Colors foregroundColor = Colors.Green, Colors backgroundColor = Colors.Black){
ForegroundColor = foregroundColor;
BackgroundColor = backgroundColor;
}
/// <summary>
/// resets the console color to original values
/// <br/>see also:
/// <br/><seealso cref="ForegroundColor"/>
/// <br/><seealso cref="BackgroundColor"/>
/// <br/><seealso cref="Colors"/>
/// <br/><seealso cref="SetColor"/><br/>
/// </summary>
public static void ResetColor() => Console.ResetColor();
/// <summary>[private] cursor save slot list (each int[2])</summary>
private static List<int[]> cursorSave = new List<int[]>(4); //~ 4 empty slots
/// <summary>
/// saves current cursor position in cursor save slot list
/// <br/>see also:
/// <br/><seealso cref="LoadCursor"/>
/// <br/><seealso cref="ClearSavedCursors"/>
/// </summary>
/// <param name="index">save slot index - overrides - defaults to -1 → auto add to end</param>
/// <return>the index of the save slot used</return>
/// <exception cref="ArgumentOutOfRangeException">if <paramref name="index"/> is not an available save slot</exception>
public static int SaveCursor(int index = -1){
if(index == -1 || index == cursorSave.Count){
cursorSave.Add(new int[2]{ CursorX, CursorY });
return cursorSave.Count - 1;
}else if(index < 0 || index >= cursorSave.Count) throw new ArgumentOutOfRangeException(nameof(index), "must be an index of a possible cursor save slot");
cursorSave[index] = new int[2]{ CursorX, CursorY };
return index;
}
/// <summary>
/// loads cursor save slot
/// <br/>see also:
/// <br/><seealso cref="SaveCursor"/>
/// <br/><seealso cref="ClearSavedCursors"/>
/// </summary>
/// <param name="index">save slot index</param>
/// <exception cref="ArgumentOutOfRangeException">if <paramref name="index"/> is not an existing save slot</exception>
public static void LoadCursor(int index){
if(index < 0 || index >= cursorSave.Count) throw new ArgumentOutOfRangeException(nameof(index), "must be an index of an existing cursor save slot");
CursorX = cursorSave[index][0];
CursorY = cursorSave[index][1];
}
/// <summary>
/// clear all cursor save slots
/// <br/>see also:
/// <br/><seealso cref="SaveCursor"/>
/// <br/><seealso cref="LoadCursor"/>
/// </summary>
public static void ClearSavedCursors() => cursorSave.Clear();
/// <summary>holds the program for the given delay in milliseconds</summary>
/// <param name="ms">time in milliseconds for delay - must be positive integer</param>
/// <exception cref="ArgumentOutOfRangeException">if <paramref name="ms"/> is not a positive integer</exception>
public static void Wait(int ms){
if(ms < 0) throw new ArgumentOutOfRangeException(nameof(ms), "must be a positive integer");
if(ms > 0) Thread.Sleep(ms);
}
/// <summary>
/// draws the border in the console with custom chars
/// <br/>custom [left / top / bottom / right] bars and [top-left / bottom-left / top-right / bottom-right] corners in that order
/// </summary>
/// <param name="left">char for the left (vertical) bar of the console excluding the corners</param>
/// <param name="top">char for the top (horizontal) bar of the console excluding the corners</param>
/// <param name="bottom">char for the bottom (horizontal) bar of the console excluding the corners</param>
/// <param name="right">char for the right (vertical) bar of the console excluding the corners</param>
/// <param name="topleft">char for the topleft corner of the console</param>
/// <param name="bottomleft">char for the bottomleft corner of the console</param>
/// <param name="topright">char for the topright corner of the console</param>
/// <param name="bottomright">char for the bottomright corner of the console</param>
/// <exception cref="ArgumentException">if any parameter has '\r' or '\n' as value</exception>
public static void DrawBorder(char left, char top, char bottom, char right, char topLeft, char bottomLeft, char topRight, char bottomRight){
if(left is '\r' or '\n') throw new ArgumentException(@"must not be '\r' or '\n'", nameof(left));
if(top is '\r' or '\n') throw new ArgumentException(@"must not be '\r' or '\n'", nameof(top));
if(bottom is '\r' or '\n') throw new ArgumentException(@"must not be '\r' or '\n'", nameof(bottom));
if(right is '\r' or '\n') throw new ArgumentException(@"must not be '\r' or '\n'", nameof(right));
if(topLeft is '\r' or '\n') throw new ArgumentException(@"must not be '\r' or '\n'", nameof(topLeft));
if(bottomLeft is '\r' or '\n') throw new ArgumentException(@"must not be '\r' or '\n'", nameof(bottomLeft));
if(topRight is '\r' or '\n') throw new ArgumentException(@"must not be '\r' or '\n'", nameof(topRight));
if(bottomRight is '\r' or '\n') throw new ArgumentException(@"must not be '\r' or '\n'", nameof(bottomRight));
Console.MoveBufferArea(InnerWidth + 1, 0, 1, 1, 0, 0, topRight, (ConsoleColor)ForegroundColor, (ConsoleColor)BackgroundColor);
Console.MoveBufferArea(0, InnerHeight + 1, 1, 1, 0, 0, bottomLeft, (ConsoleColor)ForegroundColor, (ConsoleColor)BackgroundColor);
Console.MoveBufferArea(InnerWidth + 1, InnerHeight + 1, 1, 1, 0, 0, bottomRight, (ConsoleColor)ForegroundColor, (ConsoleColor)BackgroundColor);
Console.MoveBufferArea(1, InnerHeight + 1, InnerWidth, 1, 1, 0, bottom, (ConsoleColor)ForegroundColor, (ConsoleColor)BackgroundColor);
SetCursorPos(1, 0);
Console.Write(new String(top, InnerWidth));
Console.MoveBufferArea(InnerWidth + 1, 1, 1, InnerHeight, 0, 1, right, (ConsoleColor)ForegroundColor, (ConsoleColor)BackgroundColor);
for(int i = 1; i <= InnerHeight; i++){
SetCursorPos(0, i);
Console.Write(left);
}
SetCursorPos(0, 0);
Console.Write(topLeft);
SetCursorPos(1, 1);
}
/// <summary>
/// draws the border in the console with custom chars
/// <br/>custom [horizontal / vertical] bars and [top-left / bottom-left / top-right / bottom-right] corners in that order
/// </summary>s
/// <param name="horizontal">char for the horizontal bars (top / bottom) of the console excluding the corners</param>
/// <param name="vertical">char for the vertical bars (left / right) of the console excluding the corners</param>
/// <param name="topleft">char for the topleft corner of the console</param>
/// <param name="bottomleft">char for the bottomleft corner of the console</param>
/// <param name="topright">char for the topright corner of the console</param>
/// <param name="bottomright">char for the bottomright corner of the console</param>
/// <exception cref="ArgumentException">if any parameter has '\r' or '\n' as value</exception>
public static void DrawBorder(char horizontal, char vertical, char topLeft, char bottomLeft, char topRight, char bottomRight){
if(horizontal is '\r' or '\n') throw new ArgumentException(@"must not be '\r' or '\n'", nameof(horizontal));
if(vertical is '\r' or '\n') throw new ArgumentException(@"must not be '\r' or '\n'", nameof(vertical));
if(topLeft is '\r' or '\n') throw new ArgumentException(@"must not be '\r' or '\n'", nameof(topLeft));
if(bottomLeft is '\r' or '\n') throw new ArgumentException(@"must not be '\r' or '\n'", nameof(bottomLeft));
if(topRight is '\r' or '\n') throw new ArgumentException(@"must not be '\r' or '\n'", nameof(topRight));
if(bottomRight is '\r' or '\n') throw new ArgumentException(@"must not be '\r' or '\n'", nameof(bottomRight));
DrawBorder(vertical, horizontal, horizontal, vertical, topLeft, bottomLeft, topRight, bottomRight);
}
/// <summary>
/// draws the border in the console with custom chars
/// <br/>custom [left / top / bottom / right] bars and corners in that order
/// </summary>
/// <param name="left">char for the left (vertical) bar of the console excluding the corners</param>
/// <param name="top">char for the top (horizontal) bar of the console excluding the corners</param>
/// <param name="bottom">char for the bottom (horizontal) bar of the console excluding the corners</param>
/// <param name="right">char for the right (vertical) bar of the console excluding the corners</param>
/// <param name="corners">char for all corners of the console</param>
/// <exception cref="ArgumentException">if any parameter has '\r', '\n', or '\t' as value</exception>
public static void DrawBorder(char left, char top, char bottom, char right, char corners){
if(left is '\r' or '\n' or '\t')throw new ArgumentException(@"must not be '\r', '\n', or '\t'", nameof(left));
if(top is '\r' or '\n' or '\t')throw new ArgumentException(@"must not be '\r', '\n', or '\t'", nameof(top));
if(bottom is '\r' or '\n' or '\t')throw new ArgumentException(@"must not be '\r', '\n', or '\t'", nameof(bottom));
if(right is '\r' or '\n' or '\t')throw new ArgumentException(@"must not be '\r', '\n', or '\t'", nameof(right));
if(corners is '\r' or '\n' or '\t')throw new ArgumentException(@"must not be '\r', '\n', or '\t'", nameof(corners));
DrawBorder(left, top, bottom, right, corners, corners, corners, corners);
}
/// <summary>
/// draws the border in the console with custom chars
/// <br/>custom horizontal/vertical-bars and corners in that order
/// </summary>
/// <param name="horizontal">char for the horizontal bars (top / bottom) of the console excluding the corners</param>
/// <param name="vertical">char for the vertical bars (left / right) of the console excluding the corners</param>
/// <param name="corners">char for all corners of the console</param>
/// <exception cref="ArgumentException">if any parameter has '\r' or '\n' as value</exception>
public static void DrawBorder(char horizontal, char vertical, char corners){
if(horizontal is '\r' or '\n') throw new ArgumentException(@"must not be '\r' or '\n'", nameof(horizontal));
if(vertical is '\r' or '\n') throw new ArgumentException(@"must not be '\r' or '\n'", nameof(vertical));
if(corners is '\r' or '\n') throw new ArgumentException(@"must not be '\r' or '\n'", nameof(corners));
DrawBorder(vertical, horizontal, horizontal, vertical, corners, corners, corners, corners);
}
/// <summary>
/// draws the border in the console with custom chars
/// <br/>custom bars and corners in that order
/// </summary>
/// <param name="left">char for all bars of the console excluding the corners</param>
/// <param name="corners">char for all corners of the console</param>
/// <exception cref="ArgumentException">if any parameter has '\r' or '\n' as value</exception>
public static void DrawBorder(char bars, char corners){
if(bars is '\r' or '\n') throw new ArgumentException(@"must not be '\r' or '\n'", nameof(bars));
if(corners is '\r' or '\n') throw new ArgumentException(@"must not be '\r' or '\n'", nameof(corners));
DrawBorder(bars, bars, bars, bars, corners, corners, corners, corners);
}
/// <summary>draws the border in the console with custom char</summary>
/// <param name="frame">char for the border of the console</param>
/// <exception cref="ArgumentException">if <paramref name="frame"/> has '\r' or '\n' as value</exception>
public static void DrawBorder(char frame){
if(frame is '\r' or '\n') throw new ArgumentException(@"must not be '\r' or '\n'", nameof(frame));
DrawBorder(frame, frame, frame, frame, frame, frame, frame, frame);
}
/// <summary>
/// prints text animated without finishing line break, auto bound to inner border
/// <br/>can read '\n', '\r', '\t' and '\b'
/// <br/>'\0' does not print anything nor move the cursor but still delays
/// <br/>clears input before and after printing and if <see cref="interrupt"/> is pressed during printing sets <see cref="ms"/> to 0 and returns true
/// <br/>see also:
/// <br/><seealso cref="AnimHor"/>
/// <br/><seealso cref="Text"/>
/// </summary>
/// <param name="outpt">output string</param>
/// <param name="ms">time in milliseconds to wait each character while printing - must be a positive integer - defaults to 20</param>
/// <param name="interrupt">if this character is pressed during printing sets <see cref="ms"/> to 0 - defaults to ' '</param>
/// <param name="scrolling">if it should scroll up if the text is to long for screen height - defaults to true</param>
/// <param name="col">column index (left) where to start printing - [1 to <see cref="InnerWidth"/>] - defaults to 0 → use current</param>
/// <param name="row">row index (top) where to start printing - [1 to <see cref="InnerHeight"/>] - defaults to 0 → use current</param>
/// <return>true if printing was interrupted by pressing the <see cref="interrupt"/> key or <see cref="ms"/> is 0</return>
/// <exception cref="ArgumentOutOfRangeException">if <paramref name="ms"/> is not a positive integer</exception>
/// <exception cref="ArgumentOutOfRangeException">if <paramref name="col"/> is not in range [1 to <see cref="InnerWidth"/>]</exception>
/// <exception cref="ArgumentOutOfRangeException">if <paramref name="row"/> is not in range [1 to <see cref="InnerHeight"/>]</exception>
public static bool Anim(string outpt, int ms = 20, char interrupt = ' ', bool scrolling = true, int col = 0, int row = 0){
if(ms < 0) throw new ArgumentOutOfRangeException(nameof(ms), "must be a positive integer");
if(col == 0) col = CursorX;
if(row == 0) row = CursorY;
if(col < 1 || col > InnerWidth) throw new ArgumentOutOfRangeException(nameof(col), "must be in range [1 to innerWidth]");
if(row < 1 || row > InnerHeight) throw new ArgumentOutOfRangeException(nameof(row), "must be in range [1 to innerHeight]");
SetCursorPos(col, row);
ClearInput();
//// Array.ForEach<string>(outpt.Split('\n'), (string line) => {});
foreach(char key in outpt) switch(key){
case '\n':
//// SetCursorPos(CursorX, (CursorY + 1) % InnerHeight);
if(CursorY >= InnerHeight){
if(scrolling) ScrollAnim((InnerHeight - CursorY) + 1);
else SetCursorPos(CursorX, 1);
}else SetCursorPos(CursorX, CursorY + 1);
break;
case '\r':
SetCursorPos(1,CursorY);
break;
case '\t':
int _tmp_c = CursorX + (4 - ((CursorX + 1) % 4));
if(_tmp_c > InnerWidth) SetCursorPos(_tmp_c - InnerWidth, CursorY + 1);
else SetCursorPos(_tmp_c, CursorY);
break;
case '\b':
int _tmp_b = CursorX - 1;
if(_tmp_b < 1) SetCursorPos(InnerWidth, CursorY - 1);
else SetCursorPos(_tmp_b, CursorY);
break;
default:
if(key != '\0'){
if(CursorX >= InnerWidth){
Console.Write(key);
if(CursorY >= InnerHeight){
if(scrolling) ScrollAnim((InnerHeight - CursorY) + 1);
else SetCursorPos(1, 1);
}else SetCursorPos(1, CursorY + 1);
}else Console.Write(key);
}
if(ms > 0){
Wait(ms);
if(Console.KeyAvailable)
if(GetChar(true) == interrupt) ms = 0;
}
break;
}
ClearInput();
return ms == 0;
}
/// <summary>
/// prints text animated from top to bottom without finishing line break, auto bound to inner border
/// <br/>auto converts '\n', '\r', '\t' and '\b' to be on the vertical axis
/// <br/>'\0' does not print anything nor move the cursor but still delays
/// <br/>clears input before and after printing and if <see cref="interrupt"/> is pressed during printing sets <see cref="ms"/> to 0 and returns true
/// <br/>see also:
/// <br/><seealso cref="Anim"/>
/// <br/><seealso cref="Text"/>
/// <br/><seealso cref="TextHor"/>
/// </summary>
/// <param name="outpt">output string</param>
/// <param name="ms">time in milliseconds to wait each character while printing - must be a positive integer - defaults to 20</param>
/// <param name="interrupt">if this character is pressed during printing sets <see cref="ms"/> to 0 - defaults to ' '</param>
/// <param name="scrolling">if it should scroll up if the text is to long for screen height - defaults to true</param>
/// <param name="col">column index (left) where to start printing - [1 to <see cref="InnerWidth"/>] - defaults to 0 → use current</param>
/// <param name="row">row index (top) where to start printing - [1 to <see cref="InnerHeight"/>] - defaults to 0 → use current</param>
/// <return>true if printing was interrupted by pressing the <see cref="interrupt"/> key or <see cref="ms"/> is 0</return>
/// <exception cref="ArgumentOutOfRangeException">if <paramref name="ms"/> is not a positive integer</exception>
/// <exception cref="ArgumentOutOfRangeException">if <paramref name="col"/> is not in range [1 to <see cref="InnerWidth"/>]</exception>
/// <exception cref="ArgumentOutOfRangeException">if <paramref name="row"/> is not in range [1 to <see cref="InnerHeight"/>]</exception>
public static bool AnimHor(string outpt, int ms = 20, char interrupt = ' ', bool scrolling = true, int col = 0, int row = 0){
if(ms < 0) throw new ArgumentOutOfRangeException(nameof(ms), "must be a positive integer");
if(col == 0)col = CursorX;
if(row == 0)row = CursorY;
if(col < 1 || col > InnerWidth) throw new ArgumentOutOfRangeException(nameof(col), "must be in range [1 to innerWidth]");
if(row < 1 || row > InnerHeight) throw new ArgumentOutOfRangeException(nameof(row), "must be in range [1 to innerHeight]");
SetCursorPos(col, row);
ClearInput();
foreach(char key in outpt) switch(key){
case '\n':
if(CursorX >= InnerWidth){
if(scrolling) ScrollAnim((InnerWidth - CursorX) + 1);
else SetCursorPos(1, CursorY);
}else SetCursorPos(CursorX + 1, CursorY);
break;
case '\r':
SetCursorPos(CursorX, 1);
break;
case '\t':
int _tmp_r = CursorY + (4 - ((CursorY + 1) % 4));
if(_tmp_r > InnerHeight) SetCursorPos(CursorX + 1, _tmp_r - InnerHeight);
else SetCursorPos(CursorX, _tmp_r);
break;
case '\b':
int _tmp_b = CursorY - 1;
if(_tmp_b < 1) SetCursorPos(CursorX - 1, InnerHeight);
else SetCursorPos(CursorX, _tmp_b);
break;
default:
if(key != '\0'){
if(CursorY >= InnerHeight){
Console.Write(key);
if(CursorX >= InnerWidth){
if(scrolling) ScrollAnim((InnerWidth - CursorX) + 1);
else SetCursorPos(1, 1);
}else SetCursorPos(CursorX, 1);
}else{
Console.Write(key);
SetCursorPos(CursorX - 1, CursorY + 1);
}
}
if(ms > 0){
Wait(ms);
if(Console.KeyAvailable)
if(GetChar(true) == interrupt) ms = 0;
}
break;
}
ClearInput();
return ms == 0;
}
/// <summary>
/// prints text without finishing line break, auto bound to inner border
/// <br/>see also:
/// <br/><seealso cref="Anim"/>
/// <br/><seealso cref="TextHor"/>
/// <br/><seealso cref="AnimHor"/>
/// </summary>
/// <param name="outpt">output string</param>
/// <param name="scrolling">if it should scroll up if the text is to long for screen height - defaults to true</param>
/// <param name="col">column index (left) where to start printing - [1 to <see cref="InnerWidth"/>] - defaults to 0 → use current</param>
/// <param name="row">row index (top) where to start printing - [1 to <see cref="InnerHeight"/>] - defaults to 0 → use current</param>
/// <exception cref="ArgumentOutOfRangeException">if <paramref name="col"/> is not in range [1 to <see cref="InnerWidth"/>]</exception>
/// <exception cref="ArgumentOutOfRangeException">if <paramref name="row"/> is not in range [1 to <see cref="InnerHeight"/>]</exception>
public static void Text(string outpt, bool scrolling = true, int col = 0, int row = 0){
if(col == 0) col = CursorX;
if(row == 0) row = CursorY;
if(col < 1 || col > InnerWidth) throw new ArgumentOutOfRangeException(nameof(col), "must be in range [1 to innerWidth]");
if(row < 1 || row > InnerHeight) throw new ArgumentOutOfRangeException(nameof(row), "must be in range [1 to innerHeight]");
Anim(outpt, 0, ' ', scrolling, col, row);
}
/// <summary>
/// prints text from top to bottom without finishing line break, auto bound to inner border<
/// <br/>auto converts '\n' and '\r' to be on the vertical axis
/// <br/>see also:
/// <br/><seealso cref="AnimHor"/>
/// <br/><seealso cref="Text"/>
/// <br/><seealso cref="Anim"/>
/// </summary>
/// <param name="outpt">output string</param>
/// <param name="scrolling">if it should scroll up if the text is to long for screen height - defaults to true</param>
/// <param name="col">column index (left) where to start printing - [1 to <see cref="InnerWidth"/>] - defaults to 0 → use current</param>
/// <param name="row">row index (top) where to start printing - [1 to <see cref="InnerHeight"/>] - defaults to 0 → use current</param>
/// <exception cref="ArgumentOutOfRangeException">if <paramref name="col"/> is not in range [1 to <see cref="InnerWidth"/>]</exception>
/// <exception cref="ArgumentOutOfRangeException">if <paramref name="row"/> is not in range [1 to <see cref="InnerHeight"/>]</exception>
public static void TextHor(string outpt, bool scrolling = true, int col = 0, int row = 0){
if(col == 0) col = CursorX;
if(row == 0) row = CursorY;
if(col < 1 || col > InnerWidth) throw new ArgumentOutOfRangeException(nameof(col), "must be in range [1 to innerWidth]");
if(row < 1 || row > InnerHeight) throw new ArgumentOutOfRangeException(nameof(row), "must be in range [1 to innerHeight]");
AnimHor(outpt, 0, ' ', scrolling, col, row);
}
/// <summary>
/// clears a single column of the console by printing <paramref name="ch"/> repeadedly, excluding borders
/// <br/>if on last column set the cursor to [1, 1], else to the start of next column, afterwards
/// <br/>see also:
/// <br/><seealso cref="ClearRow"/>
/// <br/><seealso cref="ClearAll"/>
/// <br/><seealso cref="ClearAllAnim"/>
/// <br/><seealso cref="ClearConsoleAll"/>
/// </summary>
/// <param name="col">column index (top) of line to clear - [1 to <see cref="InnerWidth"/>]</param>
/// <param name="ch">char to clear the line with - defaults to ' '</param>
/// <exception cref="ArgumentOutOfRangeException">if <paramref name="col"/> is not in range [1 to <see cref="InnerWidth"/>]</exception>
/// <exception cref="ArgumentException">if <paramref name="ch"/> is '\r' or '\n'</exception>
public static void ClearCol(Int32 col, char ch = ' '){
if(col < 1 || col > InnerWidth) throw new ArgumentOutOfRangeException(nameof(col), "must be in range [1 to innerWidth]");
if(ch is '\r' or '\n') throw new ArgumentException(@"must not be '\r' or '\n'",nameof(ch));
for(int i = 1; i <= InnerHeight; i++){
SetCursorPos(col, i);
Console.Write(ch);
}
if(col == InnerWidth) SetCursorPos(1, 1);
else SetCursorPos(col + 1, 1);
}
/// <summary>
/// clears a single row/line of the console by printing <paramref name="ch"/> repeatedly, excluding borders
/// <br/>if on last row/line set the cursor to [1, 1], else to the start of next row/line, afterwards
/// <br/>see also:
/// <br/><seealso cref="ClearCol"/>
/// <br/><seealso cref="ClearAll"/>
/// <br/><seealso cref="ClearAllAnim"/>
/// <br/><seealso cref="ClearConsoleAll"/>
/// </summary>
/// <param name="row">row index (left) of line to clear - [1 to <see cref="InnerHeight"/>]</param>
/// <param name="ch">char to clear the line with - defaults to ' '</param>
/// <exception cref="ArgumentOutOfRangeException">if <paramref name="row"/> is not in range [1 to <see cref="InnerHeight"/>]</exception>
/// <exception cref="ArgumentException">if <paramref name="ch"/> is '\r' or '\n'</exception>
public static void ClearRow(Int32 row, char ch = ' '){
if(row < 1 || row > InnerHeight) throw new ArgumentOutOfRangeException(nameof(row), "must be in range [1 to innerHeight]");
if(ch is '\r' or '\n') throw new ArgumentException(@"must not be '\r' or '\n'",nameof(ch));
SetCursorPos(1, row);
Console.Write(new string(ch, InnerWidth));
if(row == InnerHeight) SetCursorPos(1, 1);
else SetCursorPos(1, row + 1);
}
/// <summary>used as directions for <see cref="ClearAllAnim"/></summary>
[Flags]
public enum AnimDir : byte{
left = 1,
up = 2,
right = 4,
down = 8,
row = left | right, // 5 (0101)
col = up | down, // 10 (1010)
}
// TODO → ClearAllAnim → add diagonal-lines ~
/// <summary>
/// like <see cref="ClearAll"/> but animated in horizontal/vertical line
/// <br/>sets the cursor to [1, 1] afterwards
/// <br/>see also:
/// <br/><seealso cref="ClearAll"/>
/// <br/><seealso cref="ClearRow"/>
/// <br/><seealso cref="ClearCol"/>
/// </summary>
/// <param name="ch">char to clear the console with - defaults to ' '</param>
/// <param name="ms">duration to wait in milliseconds - must be a positive integer - defaults to 20</param>
/// <param name="timing">where to wait, one of [<see cref="AnimDir.col"/> | <see cref="AnimDir.row"/>] - defaults to <see cref="AnimDir.row"/></param>
/// <param name="dir">direction in wich to print, one of [ <see cref="AnimDir.left"/> | <see cref="AnimDir.right"/> | <see cref="AnimDir.up"/> | <see cref="AnimDir.down"/> ] - defaults to <see cref="AnimDir.down"/></param>
/// <exception cref="ArgumentException">if <paramref name="ch"/> is '\r' or '\n'</exception>
/// <exception cref="ArgumentOutOfRangeException">if <paramref name="ms"/> is not a positive integer</exception>
/// <exception cref="ArgumentException">if <paramref name="timing"/> is not one of [<see cref="AnimDir.col"/> | <see cref="AnimDir.row"/>]</exception>
/// <exception cref="ArgumentException">if <paramref name="dir"/> is not one of [ <see cref="AnimDir.left"/> | <see cref="AnimDir.right"/> | <see cref="AnimDir.up"/> | <see cref="AnimDir.down"/> ]</exception>
/// <exception cref="InvalidOperationException">if the combination of <paramref name="timing"/> and <paramref name="dir"/> is not [<see cref="AnimDir.col"/> & [<see cref="AnimDir.left"/> | <see cref="AnimDir.right"/>]] or [<see cref="AnimDir.row"/> & [<see cref="AnimDir.up"/> | <see cref="AnimDir.down"/>]]</exception>
public static void ClearAllAnim(char ch = ' ', int ms = 20, AnimDir timing = AnimDir.row, AnimDir dir = AnimDir.down){
if(ch is '\r' or '\n') throw new ArgumentException(@"must not be '\n' or '\r'", nameof(ch));
if(ms < 0) throw new ArgumentOutOfRangeException(nameof(ms), "must be a positive integer");
switch(timing){
case AnimDir.row:
case AnimDir.col:
break;
default: throw new ArgumentException("value must be one of AnimDir[ col | row ]", nameof(timing));
}
switch(dir){
case AnimDir.up:
case AnimDir.down:
case AnimDir.left:
case AnimDir.right:
break;
default: throw new ArgumentException("value must be one of AnimDir[ left | right | up | down ]", nameof(dir));
}
switch(timing | dir){
case AnimDir.col | AnimDir.left: for(int i = InnerWidth; i > 0; i--){ ClearCol(i, ch); Wait(ms); } break;
case AnimDir.col | AnimDir.right: for(int i = 1; i <= InnerWidth; i++){ ClearCol(i, ch); Wait(ms); } break;
case AnimDir.row | AnimDir.up: for(int i = InnerHeight; i > 0; i--){ ClearRow(i, ch); Wait(ms); } break;
case AnimDir.row | AnimDir.down: for(int i = 1; i <= InnerHeight; i++){ ClearRow(i, ch); Wait(ms); } break;
default: throw new InvalidOperationException("combination of [timing | dir] is not AnimDir[col & left/right] or AnimDir[row & up/down]");
}
SetCursorPos(1, 1);