From 0b50462985f2e128774d2b2f0f5dc2d56ab8dd23 Mon Sep 17 00:00:00 2001 From: byronap Date: Wed, 6 Feb 2019 19:26:03 -0500 Subject: [PATCH] v 2 adds async and ability to set the textwriter --- OutputConsole/OutputConsole.cs | 99 ++++++++++++++++-------------- OutputConsole/OutputConsole.csproj | 6 +- 2 files changed, 58 insertions(+), 47 deletions(-) diff --git a/OutputConsole/OutputConsole.cs b/OutputConsole/OutputConsole.cs index b5b4d7b..9a700b4 100644 --- a/OutputConsole/OutputConsole.cs +++ b/OutputConsole/OutputConsole.cs @@ -1,65 +1,74 @@ using System; using System.Collections.Concurrent; +using System.IO; using System.Runtime.InteropServices; using System.Threading; +using System.Threading.Tasks; -namespace Coinigy.Common +public static class OutputConsole { - public static class OutputConsole + public static TextWriter Writer { get; set; } + public static bool IsWriterConsole { get; set; } + + private static readonly BlockingCollection> MQueue = new BlockingCollection>(); + + static OutputConsole() { - private static readonly BlockingCollection> MQueue = new BlockingCollection>(); + Writer = Console.Out; + IsWriterConsole = true; - static OutputConsole() + try { - try - { - var thread = new Thread( - () => + var thread = new Thread( + () => + { + var isWindows = RuntimeInformation.IsOSPlatform(OSPlatform.Windows); + while (true) { - while (true) + var (message, foreColor, backColor) = MQueue.Take(); + try { - var item = MQueue.Take(); - try - { - if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) - { - if (item.Item2 != null) - Console.ForegroundColor = (ConsoleColor) item.Item2; - if (item.Item3 != null) - Console.BackgroundColor = (ConsoleColor) item.Item3; - } - } - catch + if (isWindows && IsWriterConsole) { - // ignore - } - - Console.WriteLine(item.Item1); - try - { - if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) - Console.ResetColor(); - } - catch - { - // ignore + if (foreColor != null) + Console.ForegroundColor = (ConsoleColor)foreColor; + if (backColor != null) + Console.BackgroundColor = (ConsoleColor)backColor; } } + catch + { + // ignore + } - // ReSharper disable once FunctionNeverReturns - }) - {IsBackground = true}; - thread.Start(); - } - catch (Exception) - { - // ignored - } + Writer.WriteLine(message); + try + { + if (isWindows && IsWriterConsole) + Console.ResetColor(); + } + catch + { + // ignore + } + } + }) + { IsBackground = true }; + thread.Start(); } - - public static void WriteLine(string value, ConsoleColor? foregroundColor = null, ConsoleColor? backgroundColor = null) + catch (Exception) { - MQueue.Add(new Tuple(value, foregroundColor, backgroundColor)); + // ignored } } + + public static void WriteLine(string value, ConsoleColor? foregroundColor = null, ConsoleColor? backgroundColor = null) + { + MQueue.Add(new Tuple(value, foregroundColor, backgroundColor)); + } + + public static Task WriteLineAsync(string value, ConsoleColor? foregroundColor = null, ConsoleColor? backgroundColor = null) + { + return Task.Run(() => { MQueue.Add(new Tuple(value, foregroundColor, backgroundColor)); }); + } } diff --git a/OutputConsole/OutputConsole.csproj b/OutputConsole/OutputConsole.csproj index e5828f2..08084b8 100644 --- a/OutputConsole/OutputConsole.csproj +++ b/OutputConsole/OutputConsole.csproj @@ -6,14 +6,16 @@ true Coinigy Inc. A cross platform console enhancement library for .NET NetStandard core. - Copyright 2017 2018 Coinigy Inc. + Copyright 2017 2019 Coinigy Inc. https://raw.githubusercontent.com/Coinigy/OutputConsole/master/LICENSE https://github.com/coinigy/OutputConsole https://raw.githubusercontent.com/Coinigy/OutputConsole/master/PWSIcon.ico https://github.com/coinigy/OutputConsole A cross platform console enhancement library for .NET NetStandard core. - console, output, formatting, color + console, output, formatting, color, textwriter, text, writer See git for list of changes. + + 2.0.0