Skip to content
This repository has been archived by the owner on Nov 20, 2018. It is now read-only.

Simplify console output and fix color output on CMD #268

Merged
merged 1 commit into from
Mar 13, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions shared/IConsole.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,7 @@ public interface IConsole
bool IsInputRedirected { get; }
bool IsOutputRedirected { get; }
bool IsErrorRedirected { get; }
ConsoleColor ForegroundColor { get; set; }
void ResetColor();
}
}
7 changes: 7 additions & 0 deletions shared/PhysicalConsole.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,12 @@ private PhysicalConsole()
public bool IsInputRedirected => Console.IsInputRedirected;
public bool IsOutputRedirected => Console.IsOutputRedirected;
public bool IsErrorRedirected => Console.IsErrorRedirected;
public ConsoleColor ForegroundColor
{
get => Console.ForegroundColor;
set => Console.ForegroundColor = value;
}

public void ResetColor() => Console.ResetColor();
}
}
57 changes: 0 additions & 57 deletions shared/Reporting/ColorFormatter.cs

This file was deleted.

26 changes: 0 additions & 26 deletions shared/Reporting/CompositeFormatter.cs

This file was deleted.

24 changes: 0 additions & 24 deletions shared/Reporting/ConditionalFormatter.cs

This file was deleted.

72 changes: 72 additions & 0 deletions shared/Reporting/ConsoleReporter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
using System.IO;

namespace Microsoft.Extensions.Tools.Internal
{
public class ConsoleReporter : IReporter
{
private object _writeLock = new object();

public ConsoleReporter(IConsole console)
: this(console, verbose: false, quiet: false)
{ }

public ConsoleReporter(IConsole console, bool verbose, bool quiet)
{
Ensure.NotNull(console, nameof(console));

Console = console;
IsVerbose = verbose;
IsQuiet = quiet;
}

protected IConsole Console { get; }
public bool IsVerbose { get; set; }
public bool IsQuiet { get; set; }

protected virtual void WriteLine(TextWriter writer, string message, ConsoleColor? color)
{
lock (_writeLock)
{
if (color.HasValue)
{
Console.ForegroundColor = color.Value;
}

writer.WriteLine(message);

if (color.HasValue)
{
Console.ResetColor();
}
}
}

public virtual void Error(string message)
=> WriteLine(Console.Error, message, ConsoleColor.Red);
public virtual void Warn(string message)
=> WriteLine(Console.Out, message, ConsoleColor.Yellow);

public virtual void Output(string message)
{
if (IsQuiet)
{
return;
}
WriteLine(Console.Out, message, color: null);
}

public virtual void Verbose(string message)
{
if (!IsVerbose)
{
return;
}

WriteLine(Console.Out, message, ConsoleColor.DarkGray);
}
}
}
14 changes: 0 additions & 14 deletions shared/Reporting/DefaultFormatter.cs

This file was deleted.

46 changes: 0 additions & 46 deletions shared/Reporting/FormatterBuilder.cs

This file was deleted.

62 changes: 0 additions & 62 deletions shared/Reporting/FormattingReporter.cs

This file was deleted.

10 changes: 0 additions & 10 deletions shared/Reporting/IFormatter.cs

This file was deleted.

20 changes: 0 additions & 20 deletions shared/Reporting/PrefixFormatter.cs

This file was deleted.

Loading