Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
ConsoleOut.cs
1using System;
2using System.IO;
3using System.Text;
4using System.Threading.Tasks;
7
9{
14 public delegate void CustomWriter(TextWriter Output);
15
20 public delegate Task CustomAsyncWriter(TextWriter Output);
21
26 public static class ConsoleOut
27 {
28 private static ConsoleColor foregroundColor = System.Console.ForegroundColor;
29 private static ConsoleColor backgroundColor = System.Console.BackgroundColor;
30
34 public static Encoding Encoding => System.Console.Out.Encoding;
35
39 public static IFormatProvider FormatProvider => System.Console.Out.FormatProvider;
40
44 public static string NewLine => System.Console.Out.NewLine;
45
49 public static bool HasConsole
50 {
51 get
52 {
53 return !(
54 System.Console.IsOutputRedirected ||
55 System.Console.IsErrorRedirected ||
56 System.Console.IsInputRedirected ||
57 !Environment.UserInteractive);
58 }
59 }
60
64 public static int WindowWidth => System.Console.WindowWidth;
65
69 public static int WindowLeft => System.Console.WindowLeft;
70
74 public static int WindowTop => System.Console.WindowTop;
75
79 public static int WindowHeight => System.Console.WindowHeight;
80
84 public static int CursorLeft => System.Console.CursorLeft;
85
89 public static int CursorTop => System.Console.CursorTop;
90
94 public static int CursorSize => System.Console.CursorSize;
95
99 public static ConsoleColor ForegroundColor
100 {
101 get => foregroundColor;
102 set
103 {
104 foregroundColor = value;
106 }
107 }
108
112 public static ConsoleColor BackgroundColor
113 {
114 get => backgroundColor;
115 set
116 {
117 backgroundColor = value;
119 }
120 }
121
126 public static void Write(string value)
127 {
128 if (!string.IsNullOrEmpty(value))
130 }
131
136 public static void Write(CustomWriter Writer)
137 {
138 Write(Writer, null, null);
139 }
140
147 public static void Write(CustomWriter Writer, ConsoleColor? ForegroundColor, ConsoleColor? BackgroundColor)
148 {
150 }
151
156 public static void Write(ulong value)
157 {
158 Write(value.ToString());
159 }
160
165 public static void Write(uint value)
166 {
167 Write(value.ToString());
168 }
169
175 public static void Write(string format, params object[] arg)
176 {
177 Write(string.Format(format, arg));
178 }
179
187 public static void Write(string format, object arg0, object arg1, object arg2)
188 {
189 Write(string.Format(format, arg0, arg1, arg2));
190 }
191
198 public static void Write(string format, object arg0, object arg1)
199 {
200 Write(string.Format(format, arg0, arg1));
201 }
202
208 public static void Write(string format, object arg0)
209 {
210 Write(string.Format(format, arg0));
211 }
212
217 public static void Write(float value)
218 {
219 Write(value.ToString());
220 }
221
226 public static void Write(long value)
227 {
228 Write(value.ToString());
229 }
230
235 public static void Write(int value)
236 {
237 Write(value.ToString());
238 }
239
244 public static void Write(double value)
245 {
246 Write(value.ToString());
247 }
248
253 public static void Write(decimal value)
254 {
255 Write(value.ToString());
256 }
257
264 public static void Write(char[] buffer, int index, int count)
265 {
266 Write(new string(buffer, index, count));
267 }
268
273 public static void Write(char[] buffer)
274 {
275 Write(new string(buffer));
276 }
277
282 public static void Write(char value)
283 {
284 Write(new string(value, 1));
285 }
286
291 public static void Write(bool value)
292 {
293 Write(value.ToString());
294 }
295
300 public static void Write(object value)
301 {
302 if (!(value is null))
303 Write(value.ToString());
304 }
305
310 public static async Task WriteAsync(string value)
311 {
312 if (!string.IsNullOrEmpty(value))
313 {
314 WorkItem Item = new ConsoleOutWriteString(value.ToString());
315
316 if (!await ConsoleWorker.Forward(Item))
317 return;
318
319 await Item.Wait();
320 }
321 }
322
329 public static Task WriteAsync(char[] buffer, int index, int count)
330 {
331 return WriteAsync(new string(buffer, index, count));
332 }
333
338 public static Task WriteAsync(char[] buffer)
339 {
340 return WriteAsync(new string(buffer));
341 }
342
347 public static Task WriteAsync(char value)
348 {
349 return WriteAsync(new string(value, 1));
350 }
351
357 {
358 return WriteAsync(Writer, null, null);
359 }
360
367 public static async Task WriteAsync(CustomAsyncWriter Writer, ConsoleColor? ForegroundColor, ConsoleColor? BackgroundColor)
368 {
370
371 if (!await ConsoleWorker.Forward(Item))
372 return;
373
374 await Item.Wait();
375 }
376
380 public static void WriteLine()
381 {
382 WriteLine(string.Empty);
383 }
384
389 public static void WriteLine(string value)
390 {
392 }
393
398 public static void WriteLine(ulong value)
399 {
400 WriteLine(value.ToString());
401 }
402
407 public static void WriteLine(uint value)
408 {
409 WriteLine(value.ToString());
410 }
411
417 public static void WriteLine(string format, params object[] arg)
418 {
419 WriteLine(string.Format(format, arg));
420 }
421
429 public static void WriteLine(string format, object arg0, object arg1, object arg2)
430 {
431 WriteLine(string.Format(format, arg0, arg1, arg2));
432 }
433
440 public static void WriteLine(string format, object arg0, object arg1)
441 {
442 WriteLine(string.Format(format, arg0, arg1));
443 }
444
449 public static void WriteLine(float value)
450 {
451 WriteLine(value.ToString());
452 }
453
459 public static void WriteLine(string format, object arg0)
460 {
461 WriteLine(string.Format(format, arg0));
462 }
463
468 public static void WriteLine(long value)
469 {
470 WriteLine(value.ToString());
471 }
472
477 public static void WriteLine(int value)
478 {
479 WriteLine(value.ToString());
480 }
481
486 public static void WriteLine(double value)
487 {
488 WriteLine(value.ToString());
489 }
490
495 public static void WriteLine(decimal value)
496 {
497 WriteLine(value.ToString());
498 }
499
506 public static void WriteLine(char[] buffer, int index, int count)
507 {
508 WriteLine(new string(buffer, index, count));
509 }
510
515 public static void WriteLine(char[] buffer)
516 {
517 WriteLine(new string(buffer));
518 }
519
524 public static void WriteLine(char value)
525 {
526 WriteLine(new string(value, 1));
527 }
528
533 public static void WriteLine(object value)
534 {
535 if (!(value is null))
536 WriteLine(value.ToString());
537 }
538
543 public static void WriteLine(bool value)
544 {
545 WriteLine(value.ToString());
546 }
547
552 public static async Task WriteLineAsync(string value)
553 {
554 WorkItem Item = new ConsoleOutWriteLineString(value ?? string.Empty);
555
556 if (!await ConsoleWorker.Forward(Item))
557 return;
558
559 await Item.Wait();
560 }
561
565 public static Task WriteLineAsync()
566 {
567 return WriteLineAsync(string.Empty);
568 }
569
574 public static Task WriteLineAsync(char value)
575 {
576 return WriteLineAsync(new string(value, 1));
577 }
578
583 public static Task WriteLineAsync(char[] buffer)
584 {
585 return WriteLineAsync(new string(buffer));
586 }
587
594 public static Task WriteLineAsync(char[] buffer, int index, int count)
595 {
596 return WriteLineAsync(new string(buffer, index, count));
597 }
598
602 public static void Beep()
603 {
605 }
606
611 public static void Flush()
612 {
613 Flush(false);
614 }
615
621 public static void Flush(bool Terminate)
622 {
623 FlushAsync(Terminate).Wait();
624 }
625
631 public static Task FlushAsync()
632 {
633 return FlushAsync(false);
634 }
635
641 public static async Task FlushAsync(bool Terminate)
642 {
644 {
645 TaskCompletionSource<bool> Result = new TaskCompletionSource<bool>();
646 if (await ConsoleWorker.Forward(new ConsoleFlush(Terminate, Result)))
647 await Result.Task;
648 }
649 }
650
654 public static TextWriter Writer
655 {
656 get => new ConsoleOutTextWriter();
657 }
658
662 private class ConsoleOutTextWriter : TextWriter
663 {
664 public ConsoleOutTextWriter()
665 {
666 }
667
668 public override Encoding Encoding => ConsoleOut.Encoding;
669 public override void Flush() => ConsoleOut.Flush();
670 public override Task FlushAsync() => ConsoleOut.FlushAsync();
671 public override void Write(char value) => ConsoleOut.Write(value);
672 public override void Write(ulong value) => ConsoleOut.Write(value);
673 public override void Write(uint value) => ConsoleOut.Write(value);
674 public override void Write(string format, params object[] arg) => ConsoleOut.Write(format, arg);
675 public override void Write(string format, object arg0, object arg1, object arg2) => ConsoleOut.Write(format, arg0, arg1, arg2);
676 public override void Write(string format, object arg0, object arg1) => ConsoleOut.Write(format, arg0, arg1);
677 public override void Write(string format, object arg0) => ConsoleOut.Write(format, arg0);
678 public override void Write(string value) => ConsoleOut.Write(value);
679 public override void Write(float value) => ConsoleOut.Write(value);
680 public override void Write(long value) => ConsoleOut.Write(value);
681 public override void Write(int value) => ConsoleOut.Write(value);
682 public override void Write(double value) => ConsoleOut.Write(value);
683 public override void Write(decimal value) => ConsoleOut.Write(value);
684 public override void Write(char[] buffer, int index, int count) => ConsoleOut.Write(buffer, index, count);
685 public override void Write(char[] buffer) => ConsoleOut.Write(buffer);
686 public override void Write(bool value) => ConsoleOut.Write(value);
687 public override void Write(object value) => ConsoleOut.Write(value);
688 public override Task WriteAsync(string value) => ConsoleOut.WriteAsync(value);
689 public override Task WriteAsync(char[] buffer, int index, int count) => ConsoleOut.WriteAsync(buffer, index, count);
690 public override Task WriteAsync(char value) => ConsoleOut.WriteAsync(value);
691 public override void WriteLine() => ConsoleOut.WriteLine();
692 public override void WriteLine(ulong value) => ConsoleOut.WriteLine(value);
693 public override void WriteLine(uint value) => ConsoleOut.WriteLine(value);
694 public override void WriteLine(string format, params object[] arg) => ConsoleOut.WriteLine(format, arg);
695 public override void WriteLine(string format, object arg0, object arg1, object arg2) => ConsoleOut.WriteLine(format, arg0, arg1, arg2);
696 public override void WriteLine(string format, object arg0, object arg1) => ConsoleOut.WriteLine(format, arg0, arg1);
697 public override void WriteLine(string value) => ConsoleOut.WriteLine(value);
698 public override void WriteLine(float value) => ConsoleOut.WriteLine(value);
699 public override void WriteLine(string format, object arg0) => ConsoleOut.WriteLine(format, arg0);
700 public override void WriteLine(long value) => ConsoleOut.WriteLine(value);
701 public override void WriteLine(int value) => ConsoleOut.WriteLine(value);
702 public override void WriteLine(double value) => ConsoleOut.WriteLine(value);
703 public override void WriteLine(decimal value) => ConsoleOut.WriteLine(value);
704 public override void WriteLine(char[] buffer, int index, int count) => ConsoleOut.WriteLine(buffer, index, count);
705 public override void WriteLine(char[] buffer) => ConsoleOut.WriteLine(buffer);
706 public override void WriteLine(char value) => ConsoleOut.WriteLine(value);
707 public override void WriteLine(object value) => ConsoleOut.WriteLine(value);
708 public override void WriteLine(bool value) => ConsoleOut.WriteLine(value);
709 public override Task WriteLineAsync() => ConsoleOut.WriteLineAsync();
710 public override Task WriteLineAsync(char value) => ConsoleOut.WriteLineAsync(value);
711 public override Task WriteLineAsync(char[] buffer, int index, int count) => ConsoleOut.WriteLineAsync(buffer, index, count);
712 public override Task WriteLineAsync(string value) => ConsoleOut.WriteLineAsync(value);
713 }
714 }
715}
Serializes output to System.Console.Out, and assures modules are not dead-locked in case the Console ...
Definition: ConsoleOut.cs:27
static void Write(long value)
Queues a value to be written to the console output.
Definition: ConsoleOut.cs:226
static void WriteLine(string format, object arg0, object arg1)
Queues a value to be written to the console output.
Definition: ConsoleOut.cs:440
static void Write(string format, params object[] arg)
Queues a value to be written to the console output.
Definition: ConsoleOut.cs:175
static async Task FlushAsync(bool Terminate)
Asynchronously clears all buffers for the current writer and causes any buffered data to be written t...
Definition: ConsoleOut.cs:641
static bool HasConsole
Checks if the console is available.
Definition: ConsoleOut.cs:50
static Encoding Encoding
The character encoding in which the output is written.
Definition: ConsoleOut.cs:34
static void Write(CustomWriter Writer, ConsoleColor? ForegroundColor, ConsoleColor? BackgroundColor)
Queues a custom writer to the console output.
Definition: ConsoleOut.cs:147
static int CursorTop
Top position of cursor.
Definition: ConsoleOut.cs:89
static void WriteLine(bool value)
Queues a value to be written to the console output.
Definition: ConsoleOut.cs:543
static void WriteLine(decimal value)
Queues a value to be written to the console output.
Definition: ConsoleOut.cs:495
static void Write(uint value)
Queues a value to be written to the console output.
Definition: ConsoleOut.cs:165
static void Write(bool value)
Queues a value to be written to the console output.
Definition: ConsoleOut.cs:291
static int WindowHeight
Height of window.
Definition: ConsoleOut.cs:79
static void Write(string format, object arg0)
Queues a value to be written to the console output.
Definition: ConsoleOut.cs:208
static void WriteLine(char[] buffer, int index, int count)
Queues a value to be written to the console output.
Definition: ConsoleOut.cs:506
static int CursorSize
Size of cursor.
Definition: ConsoleOut.cs:94
static Task WriteLineAsync()
Queues a value to be written to the console output.
Definition: ConsoleOut.cs:565
static ConsoleColor BackgroundColor
Console background color.
Definition: ConsoleOut.cs:113
static void Write(string value)
Queues a value to be written to the console output.
Definition: ConsoleOut.cs:126
static void WriteLine(double value)
Queues a value to be written to the console output.
Definition: ConsoleOut.cs:486
static Task FlushAsync()
Asynchronously clears all buffers for the current writer and causes any buffered data to be written t...
Definition: ConsoleOut.cs:631
static void Flush(bool Terminate)
Clears all buffers for the current writer and causes any buffered data to be written to the underlyin...
Definition: ConsoleOut.cs:621
static void Write(object value)
Queues a value to be written to the console output.
Definition: ConsoleOut.cs:300
static TextWriter Writer
Provides a TextWriter instance, that writes to ConsoleOut.
Definition: ConsoleOut.cs:655
static void WriteLine(string value)
Queues a value to be written to the console output.
Definition: ConsoleOut.cs:389
static int WindowLeft
Left edge of window.
Definition: ConsoleOut.cs:69
static void WriteLine(int value)
Queues a value to be written to the console output.
Definition: ConsoleOut.cs:477
static async Task WriteAsync(string value)
Queues a value to be written to the console output.
Definition: ConsoleOut.cs:310
static async Task WriteAsync(CustomAsyncWriter Writer, ConsoleColor? ForegroundColor, ConsoleColor? BackgroundColor)
Queues a custom writer to the console output.
Definition: ConsoleOut.cs:367
static void Write(double value)
Queues a value to be written to the console output.
Definition: ConsoleOut.cs:244
static void WriteLine(char value)
Queues a value to be written to the console output.
Definition: ConsoleOut.cs:524
static void Write(char value)
Queues a value to be written to the console output.
Definition: ConsoleOut.cs:282
static int CursorLeft
Left position of cursor.
Definition: ConsoleOut.cs:84
static void Write(ulong value)
Queues a value to be written to the console output.
Definition: ConsoleOut.cs:156
static void Write(int value)
Queues a value to be written to the console output.
Definition: ConsoleOut.cs:235
static void WriteLine(ulong value)
Queues a value to be written to the console output.
Definition: ConsoleOut.cs:398
static void Write(string format, object arg0, object arg1, object arg2)
Queues a value to be written to the console output.
Definition: ConsoleOut.cs:187
static ConsoleColor ForegroundColor
Console foreground color.
Definition: ConsoleOut.cs:100
static Task WriteLineAsync(char[] buffer)
Queues a value to be written to the console output.
Definition: ConsoleOut.cs:583
static void WriteLine()
Queues a value to be written to the console output.
Definition: ConsoleOut.cs:380
static Task WriteAsync(char value)
Queues a value to be written to the console output.
Definition: ConsoleOut.cs:347
static void WriteLine(object value)
Queues a value to be written to the console output.
Definition: ConsoleOut.cs:533
static void WriteLine(string format, params object[] arg)
Queues a value to be written to the console output.
Definition: ConsoleOut.cs:417
static void WriteLine(string format, object arg0, object arg1, object arg2)
Queues a value to be written to the console output.
Definition: ConsoleOut.cs:429
static string NewLine
The line terminator string for the current TextWriter.
Definition: ConsoleOut.cs:44
static Task WriteAsync(CustomAsyncWriter Writer)
Queues a custom writer to the console output.
Definition: ConsoleOut.cs:356
static Task WriteAsync(char[] buffer)
Queues a value to be written to the console output.
Definition: ConsoleOut.cs:338
static int WindowTop
Top edge of window.
Definition: ConsoleOut.cs:74
static void Write(decimal value)
Queues a value to be written to the console output.
Definition: ConsoleOut.cs:253
static void Write(float value)
Queues a value to be written to the console output.
Definition: ConsoleOut.cs:217
static void Write(CustomWriter Writer)
Queues a custom writer to the console output.
Definition: ConsoleOut.cs:136
static void Flush()
Clears all buffers for the current writer and causes any buffered data to be written to the underlyin...
Definition: ConsoleOut.cs:611
static void Beep()
Emits a Beep sounds.
Definition: ConsoleOut.cs:602
static void WriteLine(long value)
Queues a value to be written to the console output.
Definition: ConsoleOut.cs:468
static int WindowWidth
Width of window.
Definition: ConsoleOut.cs:64
static void WriteLine(char[] buffer)
Queues a value to be written to the console output.
Definition: ConsoleOut.cs:515
static void WriteLine(float value)
Queues a value to be written to the console output.
Definition: ConsoleOut.cs:449
static void WriteLine(string format, object arg0)
Queues a value to be written to the console output.
Definition: ConsoleOut.cs:459
static Task WriteLineAsync(char value)
Queues a value to be written to the console output.
Definition: ConsoleOut.cs:574
static async Task WriteLineAsync(string value)
Queues a value to be written to the console output.
Definition: ConsoleOut.cs:552
static void Write(char[] buffer, int index, int count)
Queues a value to be written to the console output.
Definition: ConsoleOut.cs:264
static void Write(char[] buffer)
Queues a value to be written to the console output.
Definition: ConsoleOut.cs:273
static Task WriteLineAsync(char[] buffer, int index, int count)
Queues a value to be written to the console output.
Definition: ConsoleOut.cs:594
static void WriteLine(uint value)
Queues a value to be written to the console output.
Definition: ConsoleOut.cs:407
static IFormatProvider FormatProvider
Gets an object that controls formatting.
Definition: ConsoleOut.cs:39
static Task WriteAsync(char[] buffer, int index, int count)
Queues a value to be written to the console output.
Definition: ConsoleOut.cs:329
static void Write(string format, object arg0, object arg1)
Queues a value to be written to the console output.
Definition: ConsoleOut.cs:198
Used to signal end of queue has been reached.
Definition: ConsoleFlush.cs:11
Writes a string to the console, appending a newline at the end.
Processes console tasks, such as input and output, in a serialized asynchronous manner.
static Task< bool > Forward(WorkItem Work)
Forwards a work item for processing.
static bool Terminating
If the console worker is being terminated.
Represents an asynchronous operation to be performed.
Definition: WorkItem.cs:10
Task< bool > Wait()
Waits for the item to be processed.
Definition: WorkItem.cs:40
delegate void CustomWriter(TextWriter Output)
Delegate for custom writers.
delegate Task CustomAsyncWriter(TextWriter Output)
Delegate for asynchronous custom writers.