From 02c02a55eeeb744108d6a8335f6203a95ea20626 Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Thu, 10 May 2018 00:47:20 -0400 Subject: generalise console color logic for reuse (#495) --- .../ConsoleWriting/ColorfulConsoleWriter.cs | 136 +++++++++++++++++++++ src/SMAPI.Internal/ConsoleWriting/LogLevel.cs | 27 ++++ .../ConsoleWriting/MonitorColorScheme.cs | 15 +++ 3 files changed, 178 insertions(+) create mode 100644 src/SMAPI.Internal/ConsoleWriting/ColorfulConsoleWriter.cs create mode 100644 src/SMAPI.Internal/ConsoleWriting/LogLevel.cs create mode 100644 src/SMAPI.Internal/ConsoleWriting/MonitorColorScheme.cs (limited to 'src/SMAPI.Internal/ConsoleWriting') diff --git a/src/SMAPI.Internal/ConsoleWriting/ColorfulConsoleWriter.cs b/src/SMAPI.Internal/ConsoleWriting/ColorfulConsoleWriter.cs new file mode 100644 index 00000000..5f8fe271 --- /dev/null +++ b/src/SMAPI.Internal/ConsoleWriting/ColorfulConsoleWriter.cs @@ -0,0 +1,136 @@ +using System; +using System.Collections.Generic; + +namespace StardewModdingAPI.Internal.ConsoleWriting +{ + /// Provides a wrapper for writing color-coded text to the console. + internal class ColorfulConsoleWriter + { + /********* + ** Properties + *********/ + /// The console text color for each log level. + private readonly IDictionary Colors; + + /// Whether the current console supports color formatting. + private readonly bool SupportsColor; + + + /********* + ** Public methods + *********/ + /// Construct an instance. + /// The target platform. + /// The console color scheme to use. + public ColorfulConsoleWriter(Platform platform, MonitorColorScheme colorScheme) + { + this.SupportsColor = this.TestColorSupport(); + this.Colors = this.GetConsoleColorScheme(platform, colorScheme); + } + + /// Write a message line to the log. + /// The message to log. + /// The log level. + public void WriteLine(string message, ConsoleLogLevel level) + { + if (this.SupportsColor) + { + if (level == ConsoleLogLevel.Critical) + { + Console.BackgroundColor = ConsoleColor.Red; + Console.ForegroundColor = ConsoleColor.White; + Console.WriteLine(message); + Console.ResetColor(); + } + else + { + Console.ForegroundColor = this.Colors[level]; + Console.WriteLine(message); + Console.ResetColor(); + } + } + else + Console.WriteLine(message); + } + + + /********* + ** Private methods + *********/ + /// Test whether the current console supports color formatting. + private bool TestColorSupport() + { + try + { + Console.ForegroundColor = Console.ForegroundColor; + return true; + } + catch (Exception) + { + return false; // Mono bug + } + } + + /// Get the color scheme to use for the current console. + /// The target platform. + /// The console color scheme to use. + private IDictionary GetConsoleColorScheme(Platform platform, MonitorColorScheme colorScheme) + { + // auto detect color scheme + if (colorScheme == MonitorColorScheme.AutoDetect) + { + colorScheme = platform == Platform.Mac + ? MonitorColorScheme.LightBackground // MacOS doesn't provide console background color info, but it's usually white. + : ColorfulConsoleWriter.IsDark(Console.BackgroundColor) ? MonitorColorScheme.DarkBackground : MonitorColorScheme.LightBackground; + } + + // get colors for scheme + switch (colorScheme) + { + case MonitorColorScheme.DarkBackground: + return new Dictionary + { + [ConsoleLogLevel.Trace] = ConsoleColor.DarkGray, + [ConsoleLogLevel.Debug] = ConsoleColor.DarkGray, + [ConsoleLogLevel.Info] = ConsoleColor.White, + [ConsoleLogLevel.Warn] = ConsoleColor.Yellow, + [ConsoleLogLevel.Error] = ConsoleColor.Red, + [ConsoleLogLevel.Alert] = ConsoleColor.Magenta + }; + + case MonitorColorScheme.LightBackground: + return new Dictionary + { + [ConsoleLogLevel.Trace] = ConsoleColor.DarkGray, + [ConsoleLogLevel.Debug] = ConsoleColor.DarkGray, + [ConsoleLogLevel.Info] = ConsoleColor.Black, + [ConsoleLogLevel.Warn] = ConsoleColor.DarkYellow, + [ConsoleLogLevel.Error] = ConsoleColor.Red, + [ConsoleLogLevel.Alert] = ConsoleColor.DarkMagenta + }; + + default: + throw new NotSupportedException($"Unknown color scheme '{colorScheme}'."); + } + } + + /// Get whether a console color should be considered dark, which is subjectively defined as 'white looks better than black on this text'. + /// The color to check. + private static bool IsDark(ConsoleColor color) + { + switch (color) + { + case ConsoleColor.Black: + case ConsoleColor.Blue: + case ConsoleColor.DarkBlue: + case ConsoleColor.DarkMagenta: // Powershell + case ConsoleColor.DarkRed: + case ConsoleColor.Red: + return true; + + default: + return false; + } + } + } +} diff --git a/src/SMAPI.Internal/ConsoleWriting/LogLevel.cs b/src/SMAPI.Internal/ConsoleWriting/LogLevel.cs new file mode 100644 index 00000000..85e69f51 --- /dev/null +++ b/src/SMAPI.Internal/ConsoleWriting/LogLevel.cs @@ -0,0 +1,27 @@ +namespace StardewModdingAPI.Internal.ConsoleWriting +{ + /// The log severity levels. + internal enum ConsoleLogLevel + { + /// Tracing info intended for developers. + Trace, + + /// Troubleshooting info that may be relevant to the player. + Debug, + + /// Info relevant to the player. This should be used judiciously. + Info, + + /// An issue the player should be aware of. This should be used rarely. + Warn, + + /// A message indicating something went wrong. + Error, + + /// Important information to highlight for the player when player action is needed (e.g. new version available). This should be used rarely to avoid alert fatigue. + Alert, + + /// A critical issue that generally signals an immediate end to the application. + Critical + } +} diff --git a/src/SMAPI.Internal/ConsoleWriting/MonitorColorScheme.cs b/src/SMAPI.Internal/ConsoleWriting/MonitorColorScheme.cs new file mode 100644 index 00000000..bccb56d7 --- /dev/null +++ b/src/SMAPI.Internal/ConsoleWriting/MonitorColorScheme.cs @@ -0,0 +1,15 @@ +namespace StardewModdingAPI.Internal.ConsoleWriting +{ + /// A monitor color scheme to use. + internal enum MonitorColorScheme + { + /// Choose a color scheme automatically. + AutoDetect, + + /// Use lighter text colors that look better on a black or dark background. + DarkBackground, + + /// Use darker text colors that look better on a white or light background. + LightBackground + } +} -- cgit From 995a6fcca4a64d414d93e442419b31c89a2ce20f Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Thu, 10 May 2018 00:49:29 -0400 Subject: use SMAPI's console color scheme logic in installer too (#495) --- src/SMAPI.Installer/InteractiveInstaller.cs | 78 +++++++--------------- .../ConsoleWriting/ColorfulConsoleWriter.cs | 6 +- src/SMAPI.Internal/ConsoleWriting/LogLevel.cs | 5 +- 3 files changed, 33 insertions(+), 56 deletions(-) (limited to 'src/SMAPI.Internal/ConsoleWriting') diff --git a/src/SMAPI.Installer/InteractiveInstaller.cs b/src/SMAPI.Installer/InteractiveInstaller.cs index b4ed2c92..5249355b 100644 --- a/src/SMAPI.Installer/InteractiveInstaller.cs +++ b/src/SMAPI.Installer/InteractiveInstaller.cs @@ -8,6 +8,7 @@ using System.Threading; using Microsoft.Win32; using StardewModdingApi.Installer.Enums; using StardewModdingAPI.Internal; +using StardewModdingAPI.Internal.ConsoleWriting; namespace StardewModdingApi.Installer { @@ -114,13 +115,19 @@ namespace StardewModdingApi.Installer yield return Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "StardewValley", "ErrorLogs"); // remove old log files } - /// Whether the current console supports color formatting. - private static readonly bool ConsoleSupportsColor = InteractiveInstaller.GetConsoleSupportsColor(); + /// Handles writing color-coded text to the console. + private readonly ColorfulConsoleWriter ConsoleWriter; /********* ** Public methods *********/ + /// Construct an instance. + public InteractiveInstaller() + { + this.ConsoleWriter = new ColorfulConsoleWriter(EnvironmentUtility.DetectPlatform(), MonitorColorScheme.AutoDetect); + } + /// Run the install or uninstall script. /// The command line arguments. /// @@ -366,7 +373,7 @@ namespace StardewModdingApi.Installer } // remove obsolete appdata mods - this.InteractivelyRemoveAppDataMods(platform, modsDir, packagedModsDir); + this.InteractivelyRemoveAppDataMods(modsDir, packagedModsDir); } Console.WriteLine(); Console.WriteLine(); @@ -378,20 +385,20 @@ namespace StardewModdingApi.Installer { if (action == ScriptAction.Install) { - this.PrintColor("SMAPI is installed! If you use Steam, set your launch options to enable achievements (see smapi.io/install):", ConsoleColor.DarkGreen); - this.PrintColor($" \"{Path.Combine(installDir.FullName, "StardewModdingAPI.exe")}\" %command%", ConsoleColor.DarkGreen); + this.PrintSuccess("SMAPI is installed! If you use Steam, set your launch options to enable achievements (see smapi.io/install):"); + this.PrintSuccess($" \"{Path.Combine(installDir.FullName, "StardewModdingAPI.exe")}\" %command%"); Console.WriteLine(); - this.PrintColor("If you don't use Steam, launch StardewModdingAPI.exe in your game folder to play with mods.", ConsoleColor.DarkGreen); + this.PrintSuccess("If you don't use Steam, launch StardewModdingAPI.exe in your game folder to play with mods."); } else - this.PrintColor("SMAPI is removed! If you configured Steam to launch SMAPI, don't forget to clear your launch options.", ConsoleColor.DarkGreen); + this.PrintSuccess("SMAPI is removed! If you configured Steam to launch SMAPI, don't forget to clear your launch options."); } else { - if (action == ScriptAction.Install) - this.PrintColor("SMAPI is installed! Launch the game the same way as before to play with mods.", ConsoleColor.DarkGreen); - else - this.PrintColor("SMAPI is removed! Launch the game the same way as before to play without mods.", ConsoleColor.DarkGreen); + this.PrintSuccess(action == ScriptAction.Install + ? "SMAPI is installed! Launch the game the same way as before to play with mods." + : "SMAPI is removed! Launch the game the same way as before to play without mods." + ); } Console.ReadKey(); @@ -401,20 +408,6 @@ namespace StardewModdingApi.Installer /********* ** Private methods *********/ - /// Test whether the current console supports color formatting. - private static bool GetConsoleSupportsColor() - { - try - { - Console.ForegroundColor = Console.ForegroundColor; - return true; - } - catch (Exception) - { - return false; // Mono bug - } - } - /// Get the value of a key in the Windows registry. /// The full path of the registry key relative to HKLM. /// The name of the value. @@ -430,39 +423,19 @@ namespace StardewModdingApi.Installer /// Print a debug message. /// The text to print. - private void PrintDebug(string text) - { - this.PrintColor(text, ConsoleColor.DarkGray); - } + private void PrintDebug(string text) => this.ConsoleWriter.WriteLine(text, ConsoleLogLevel.Debug); /// Print a warning message. /// The text to print. - private void PrintWarning(string text) - { - this.PrintColor(text, ConsoleColor.DarkYellow); - } + private void PrintWarning(string text) => this.ConsoleWriter.WriteLine(text, ConsoleLogLevel.Warn); /// Print a warning message. /// The text to print. - private void PrintError(string text) - { - this.PrintColor(text, ConsoleColor.Red); - } + private void PrintError(string text) => this.ConsoleWriter.WriteLine(text, ConsoleLogLevel.Error); - /// Print a message to the console. - /// The message text. - /// The text foreground color. - private void PrintColor(string text, ConsoleColor color) - { - if (InteractiveInstaller.ConsoleSupportsColor) - { - Console.ForegroundColor = color; - Console.WriteLine(text); - Console.ResetColor(); - } - else - Console.WriteLine(text); - } + /// Print a success message. + /// The text to print. + private void PrintSuccess(string text) => this.ConsoleWriter.WriteLine(text, ConsoleLogLevel.Success); /// Get whether the current system has .NET Framework 4.5 or later installed. This only applies on Windows. /// The current platform. @@ -671,10 +644,9 @@ namespace StardewModdingApi.Installer } /// Interactively move mods out of the appdata directory. - /// The current platform. /// The directory which should contain all mods. /// The installer directory containing packaged mods. - private void InteractivelyRemoveAppDataMods(Platform platform, DirectoryInfo properModsDir, DirectoryInfo packagedModsDir) + private void InteractivelyRemoveAppDataMods(DirectoryInfo properModsDir, DirectoryInfo packagedModsDir) { // get packaged mods to delete string[] packagedModNames = packagedModsDir.GetDirectories().Select(p => p.Name).ToArray(); diff --git a/src/SMAPI.Internal/ConsoleWriting/ColorfulConsoleWriter.cs b/src/SMAPI.Internal/ConsoleWriting/ColorfulConsoleWriter.cs index 5f8fe271..c04cf0e7 100644 --- a/src/SMAPI.Internal/ConsoleWriting/ColorfulConsoleWriter.cs +++ b/src/SMAPI.Internal/ConsoleWriting/ColorfulConsoleWriter.cs @@ -95,7 +95,8 @@ namespace StardewModdingAPI.Internal.ConsoleWriting [ConsoleLogLevel.Info] = ConsoleColor.White, [ConsoleLogLevel.Warn] = ConsoleColor.Yellow, [ConsoleLogLevel.Error] = ConsoleColor.Red, - [ConsoleLogLevel.Alert] = ConsoleColor.Magenta + [ConsoleLogLevel.Alert] = ConsoleColor.Magenta, + [ConsoleLogLevel.Success] = ConsoleColor.DarkGreen }; case MonitorColorScheme.LightBackground: @@ -106,7 +107,8 @@ namespace StardewModdingAPI.Internal.ConsoleWriting [ConsoleLogLevel.Info] = ConsoleColor.Black, [ConsoleLogLevel.Warn] = ConsoleColor.DarkYellow, [ConsoleLogLevel.Error] = ConsoleColor.Red, - [ConsoleLogLevel.Alert] = ConsoleColor.DarkMagenta + [ConsoleLogLevel.Alert] = ConsoleColor.DarkMagenta, + [ConsoleLogLevel.Success] = ConsoleColor.DarkGreen }; default: diff --git a/src/SMAPI.Internal/ConsoleWriting/LogLevel.cs b/src/SMAPI.Internal/ConsoleWriting/LogLevel.cs index 85e69f51..54564111 100644 --- a/src/SMAPI.Internal/ConsoleWriting/LogLevel.cs +++ b/src/SMAPI.Internal/ConsoleWriting/LogLevel.cs @@ -22,6 +22,9 @@ namespace StardewModdingAPI.Internal.ConsoleWriting Alert, /// A critical issue that generally signals an immediate end to the application. - Critical + Critical, + + /// A success message that generally signals a successful end to a task. + Success } } -- cgit