From 75a731b6567ea65a2fe03f82565df7afb62205aa Mon Sep 17 00:00:00 2001 From: Marvin Huber Date: Wed, 2 Dec 2020 19:33:03 +0100 Subject: [PATCH] Refactore console extensions (#822) * use of language feature * removed intend if * replaced ifs with switch * Update ConsoleExtensions.cs Co-authored-by: John Luo --- src/Microsoft.Tye.Core/ConsoleExtensions.cs | 36 ++++++++------------- 1 file changed, 14 insertions(+), 22 deletions(-) diff --git a/src/Microsoft.Tye.Core/ConsoleExtensions.cs b/src/Microsoft.Tye.Core/ConsoleExtensions.cs index d3a6eef8..642efd3a 100644 --- a/src/Microsoft.Tye.Core/ConsoleExtensions.cs +++ b/src/Microsoft.Tye.Core/ConsoleExtensions.cs @@ -14,18 +14,20 @@ namespace System.CommandLine { get { - if (_isConsoleRedirectionCheckSupported == null) + if (_isConsoleRedirectionCheckSupported != null) { - try - { - var check = Console.IsOutputRedirected; - _isConsoleRedirectionCheckSupported = true; - } + return _isConsoleRedirectionCheckSupported.Value; + } + + try + { + var check = Console.IsOutputRedirected; + _isConsoleRedirectionCheckSupported = true; + } - catch (PlatformNotSupportedException) - { - _isConsoleRedirectionCheckSupported = false; - } + catch (PlatformNotSupportedException) + { + _isConsoleRedirectionCheckSupported = false; } return _isConsoleRedirectionCheckSupported.Value; @@ -39,12 +41,7 @@ namespace System.CommandLine ((dynamic)console).ForegroundColor = color; } - if (IsConsoleRedirectionCheckSupported && - !Console.IsOutputRedirected) - { - Console.ForegroundColor = color; - } - else if (IsConsoleRedirectionCheckSupported) + if (IsConsoleRedirectionCheckSupported) { Console.ForegroundColor = color; } @@ -57,12 +54,7 @@ namespace System.CommandLine ((dynamic)console).ForegroundColor = ConsoleColor.Red; } - if (IsConsoleRedirectionCheckSupported && - !Console.IsOutputRedirected) - { - Console.ResetColor(); - } - else if (IsConsoleRedirectionCheckSupported) + if (IsConsoleRedirectionCheckSupported) { Console.ResetColor(); }