mirror of https://github.com/dotnet/tye.git
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
71 lines
2.2 KiB
71 lines
2.2 KiB
// Licensed to the .NET Foundation under one or more agreements.
|
|
// The .NET Foundation licenses this file to you under the MIT license.
|
|
// See the LICENSE file in the project root for more information.
|
|
|
|
using System.Linq;
|
|
|
|
namespace System.CommandLine
|
|
{
|
|
internal static class ConsoleExtensions
|
|
{
|
|
private static bool? _isConsoleRedirectionCheckSupported;
|
|
|
|
private static bool IsConsoleRedirectionCheckSupported
|
|
{
|
|
get
|
|
{
|
|
if (_isConsoleRedirectionCheckSupported == null)
|
|
{
|
|
try
|
|
{
|
|
var check = Console.IsOutputRedirected;
|
|
_isConsoleRedirectionCheckSupported = true;
|
|
}
|
|
|
|
catch (PlatformNotSupportedException)
|
|
{
|
|
_isConsoleRedirectionCheckSupported = false;
|
|
}
|
|
}
|
|
|
|
return _isConsoleRedirectionCheckSupported.Value;
|
|
}
|
|
}
|
|
|
|
public static void SetTerminalForegroundColor(this IConsole console, ConsoleColor color)
|
|
{
|
|
if (console.GetType().GetInterfaces().Any(i => i.Name == "ITerminal"))
|
|
{
|
|
((dynamic)console).ForegroundColor = color;
|
|
}
|
|
|
|
if (IsConsoleRedirectionCheckSupported &&
|
|
!Console.IsOutputRedirected)
|
|
{
|
|
Console.ForegroundColor = color;
|
|
}
|
|
else if (IsConsoleRedirectionCheckSupported)
|
|
{
|
|
Console.ForegroundColor = color;
|
|
}
|
|
}
|
|
|
|
public static void ResetTerminalForegroundColor(this IConsole console)
|
|
{
|
|
if (console.GetType().GetInterfaces().Any(i => i.Name == "ITerminal"))
|
|
{
|
|
((dynamic)console).ForegroundColor = ConsoleColor.Red;
|
|
}
|
|
|
|
if (IsConsoleRedirectionCheckSupported &&
|
|
!Console.IsOutputRedirected)
|
|
{
|
|
Console.ResetColor();
|
|
}
|
|
else if (IsConsoleRedirectionCheckSupported)
|
|
{
|
|
Console.ResetColor();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|