Skip to content

Commit

Permalink
v 2 adds async and ability to set the textwriter
Browse files Browse the repository at this point in the history
  • Loading branch information
byronap committed Feb 7, 2019
1 parent 693309e commit 0b50462
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 47 deletions.
99 changes: 54 additions & 45 deletions OutputConsole/OutputConsole.cs
Original file line number Diff line number Diff line change
@@ -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<Tuple<string, ConsoleColor?, ConsoleColor?>> MQueue = new BlockingCollection<Tuple<string, ConsoleColor?, ConsoleColor?>>();

static OutputConsole()
{
private static readonly BlockingCollection<Tuple<string, ConsoleColor?, ConsoleColor?>> MQueue = new BlockingCollection<Tuple<string, ConsoleColor?, ConsoleColor?>>();
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<string, ConsoleColor?, ConsoleColor?>(value, foregroundColor, backgroundColor));
// ignored
}
}

public static void WriteLine(string value, ConsoleColor? foregroundColor = null, ConsoleColor? backgroundColor = null)
{
MQueue.Add(new Tuple<string, ConsoleColor?, ConsoleColor?>(value, foregroundColor, backgroundColor));
}

public static Task WriteLineAsync(string value, ConsoleColor? foregroundColor = null, ConsoleColor? backgroundColor = null)
{
return Task.Run(() => { MQueue.Add(new Tuple<string, ConsoleColor?, ConsoleColor?>(value, foregroundColor, backgroundColor)); });
}
}
6 changes: 4 additions & 2 deletions OutputConsole/OutputConsole.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,16 @@
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Company>Coinigy Inc.</Company>
<Description>A cross platform console enhancement library for .NET NetStandard core.</Description>
<Copyright>Copyright 2017 2018 Coinigy Inc.</Copyright>
<Copyright>Copyright 2017 2019 Coinigy Inc.</Copyright>
<PackageLicenseUrl>https://raw.githubusercontent.com/Coinigy/OutputConsole/master/LICENSE</PackageLicenseUrl>
<PackageProjectUrl>https://github.com/coinigy/OutputConsole</PackageProjectUrl>
<PackageIconUrl>https://raw.githubusercontent.com/Coinigy/OutputConsole/master/PWSIcon.ico</PackageIconUrl>
<RepositoryUrl>https://github.com/coinigy/OutputConsole</RepositoryUrl>
<Title>A cross platform console enhancement library for .NET NetStandard core.</Title>
<PackageTags>console, output, formatting, color</PackageTags>
<PackageTags>console, output, formatting, color, textwriter, text, writer</PackageTags>
<PackageReleaseNotes>See git for list of changes.</PackageReleaseNotes>
<RootNamespace />
<Version>2.0.0</Version>
</PropertyGroup>

</Project>

0 comments on commit 0b50462

Please sign in to comment.