From 1b5055dfaafc6dcf77b5262b8290e8ca2c8179ed Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Mon, 23 Sep 2019 17:09:35 -0400 Subject: make console colors configurable --- .../ConsoleWriting/ColorSchemeConfig.cs | 15 ++++ .../ConsoleWriting/ColorfulConsoleWriter.cs | 88 +++++++++++++--------- .../ConsoleWriting/ConsoleLogLevel.cs | 30 ++++++++ src/SMAPI.Internal/ConsoleWriting/LogLevel.cs | 30 -------- 4 files changed, 96 insertions(+), 67 deletions(-) create mode 100644 src/SMAPI.Internal/ConsoleWriting/ColorSchemeConfig.cs create mode 100644 src/SMAPI.Internal/ConsoleWriting/ConsoleLogLevel.cs delete mode 100644 src/SMAPI.Internal/ConsoleWriting/LogLevel.cs (limited to 'src/SMAPI.Internal/ConsoleWriting') diff --git a/src/SMAPI.Internal/ConsoleWriting/ColorSchemeConfig.cs b/src/SMAPI.Internal/ConsoleWriting/ColorSchemeConfig.cs new file mode 100644 index 00000000..001840bf --- /dev/null +++ b/src/SMAPI.Internal/ConsoleWriting/ColorSchemeConfig.cs @@ -0,0 +1,15 @@ +using System; +using System.Collections.Generic; + +namespace StardewModdingAPI.Internal.ConsoleWriting +{ + /// The console color scheme options. + internal class ColorSchemeConfig + { + /// The default color scheme ID to use, or to select one automatically. + public MonitorColorScheme UseScheme { get; set; } + + /// The available console color schemes. + public IDictionary> Schemes { get; set; } + } +} diff --git a/src/SMAPI.Internal/ConsoleWriting/ColorfulConsoleWriter.cs b/src/SMAPI.Internal/ConsoleWriting/ColorfulConsoleWriter.cs index db016bae..aefda9b6 100644 --- a/src/SMAPI.Internal/ConsoleWriting/ColorfulConsoleWriter.cs +++ b/src/SMAPI.Internal/ConsoleWriting/ColorfulConsoleWriter.cs @@ -22,11 +22,16 @@ namespace StardewModdingAPI.Internal.ConsoleWriting *********/ /// Construct an instance. /// The target platform. - /// The console color scheme to use. - public ColorfulConsoleWriter(Platform platform, MonitorColorScheme colorScheme) + public ColorfulConsoleWriter(Platform platform) + : this(platform, ColorfulConsoleWriter.GetDefaultColorSchemeConfig(MonitorColorScheme.AutoDetect)) { } + + /// Construct an instance. + /// The target platform. + /// The colors to use for text written to the SMAPI console. + public ColorfulConsoleWriter(Platform platform, ColorSchemeConfig colorConfig) { this.SupportsColor = this.TestColorSupport(); - this.Colors = this.GetConsoleColorScheme(platform, colorScheme); + this.Colors = this.GetConsoleColorScheme(platform, colorConfig); } /// Write a message line to the log. @@ -54,6 +59,40 @@ namespace StardewModdingAPI.Internal.ConsoleWriting Console.WriteLine(message); } + /// Get the default color scheme config for cases where it's not configurable (e.g. the installer). + /// The default color scheme ID to use, or to select one automatically. + /// The colors here should be kept in sync with the SMAPI config file. + public static ColorSchemeConfig GetDefaultColorSchemeConfig(MonitorColorScheme useScheme) + { + return new ColorSchemeConfig + { + UseScheme = useScheme, + Schemes = new Dictionary> + { + [MonitorColorScheme.DarkBackground] = 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, + [ConsoleLogLevel.Success] = ConsoleColor.DarkGreen + }, + [MonitorColorScheme.LightBackground] = 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, + [ConsoleLogLevel.Success] = ConsoleColor.DarkGreen + } + } + }; + } + /********* ** Private methods @@ -74,47 +113,22 @@ namespace StardewModdingAPI.Internal.ConsoleWriting /// 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) + /// The colors to use for text written to the SMAPI console. + private IDictionary GetConsoleColorScheme(Platform platform, ColorSchemeConfig colorConfig) { - // auto detect color scheme - if (colorScheme == MonitorColorScheme.AutoDetect) + // get color scheme ID + MonitorColorScheme schemeID = colorConfig.UseScheme; + if (schemeID == MonitorColorScheme.AutoDetect) { - colorScheme = platform == Platform.Mac + schemeID = 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, - [ConsoleLogLevel.Success] = ConsoleColor.DarkGreen - }; - - 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, - [ConsoleLogLevel.Success] = ConsoleColor.DarkGreen - }; - - default: - throw new NotSupportedException($"Unknown color scheme '{colorScheme}'."); - } + return colorConfig.Schemes.TryGetValue(schemeID, out IDictionary scheme) + ? scheme + : throw new NotSupportedException($"Unknown color scheme '{schemeID}'."); } /// Get whether a console color should be considered dark, which is subjectively defined as 'white looks better than black on this text'. diff --git a/src/SMAPI.Internal/ConsoleWriting/ConsoleLogLevel.cs b/src/SMAPI.Internal/ConsoleWriting/ConsoleLogLevel.cs new file mode 100644 index 00000000..54564111 --- /dev/null +++ b/src/SMAPI.Internal/ConsoleWriting/ConsoleLogLevel.cs @@ -0,0 +1,30 @@ +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, + + /// A success message that generally signals a successful end to a task. + Success + } +} diff --git a/src/SMAPI.Internal/ConsoleWriting/LogLevel.cs b/src/SMAPI.Internal/ConsoleWriting/LogLevel.cs deleted file mode 100644 index 54564111..00000000 --- a/src/SMAPI.Internal/ConsoleWriting/LogLevel.cs +++ /dev/null @@ -1,30 +0,0 @@ -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, - - /// A success message that generally signals a successful end to a task. - Success - } -} -- cgit