From 90e92ef61f0808688abf638ee5cb941215c1586f Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Sat, 14 Jan 2017 15:05:38 -0500 Subject: fix error when the console doesn't support colour (#206) --- .../InteractiveInstaller.cs | 58 ++++++++++++++++------ 1 file changed, 42 insertions(+), 16 deletions(-) (limited to 'src/StardewModdingAPI.Installer') diff --git a/src/StardewModdingAPI.Installer/InteractiveInstaller.cs b/src/StardewModdingAPI.Installer/InteractiveInstaller.cs index 5f89caf2..d7279cf7 100644 --- a/src/StardewModdingAPI.Installer/InteractiveInstaller.cs +++ b/src/StardewModdingAPI.Installer/InteractiveInstaller.cs @@ -77,6 +77,9 @@ namespace StardewModdingApi.Installer "StardewModdingAPI-settings.json" // 1.0-1.4 }; + /// Whether the current console supports color formatting. + private static readonly bool ConsoleSupportsColor = InteractiveInstaller.GetConsoleSupportsColor(); + /********* ** Public methods @@ -253,18 +256,18 @@ namespace StardewModdingApi.Installer /**** ** exit ****/ - Console.ForegroundColor = ConsoleColor.DarkGreen; - Console.WriteLine("Done!"); + this.PrintColor("Done!", ConsoleColor.DarkGreen); if (platform == Platform.Windows) { - Console.WriteLine(action == ScriptAction.Install - ? "Don't forget to launch StardewModdingAPI.exe instead of the normal game executable. See the readme.txt for details." - : "If you manually changed shortcuts or Steam to launch SMAPI, don't forget to change those back." + this.PrintColor( + action == ScriptAction.Install + ? "Don't forget to launch StardewModdingAPI.exe instead of the normal game executable. See the readme.txt for details." + : "If you manually changed shortcuts or Steam to launch SMAPI, don't forget to change those back.", + ConsoleColor.DarkGreen ); } else if (action == ScriptAction.Install) - Console.WriteLine("You can launch the game the same way as before to play with mods."); - Console.ResetColor(); + this.PrintColor("You can launch the game the same way as before to play with mods.", ConsoleColor.DarkGreen); Console.ReadKey(); } @@ -287,6 +290,20 @@ namespace StardewModdingApi.Installer } } + /// Test whether the current console supports color formatting. + private static bool GetConsoleSupportsColor() + { + try + { + Console.ResetColor(); + return true; + } + catch (Exception) + { + return false; + } + } + #if SMAPI_FOR_WINDOWS /// Get the value of a key in the Windows registry. /// The full path of the registry key relative to HKLM. @@ -306,30 +323,39 @@ namespace StardewModdingApi.Installer /// The text to print. private void PrintDebug(string text) { - Console.ForegroundColor = ConsoleColor.DarkGray; - Console.WriteLine(text); - Console.ResetColor(); + this.PrintColor(text, ConsoleColor.DarkGray); } /// Print a warning message. /// The text to print. private void PrintWarning(string text) { - Console.ForegroundColor = ConsoleColor.DarkYellow; - Console.WriteLine(text); - Console.ResetColor(); + this.PrintColor(text, ConsoleColor.DarkYellow); } /// Print an error and pause the console if needed. /// The error text. private void ExitError(string error) { - Console.ForegroundColor = ConsoleColor.Red; - Console.WriteLine(error); - Console.ResetColor(); + this.PrintColor(error, ConsoleColor.Red); Console.ReadLine(); } + /// 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); + } + /// Interactively ask the user to choose a value. /// The message to print. /// The allowed options (not case sensitive). -- cgit From f957af71d1533e6b3635c7f4ac31807367a99195 Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Sat, 14 Jan 2017 15:53:28 -0500 Subject: fix console color support check (#206) --- src/StardewModdingAPI.Installer/InteractiveInstaller.cs | 4 ++-- src/StardewModdingAPI/Framework/Monitor.cs | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) (limited to 'src/StardewModdingAPI.Installer') diff --git a/src/StardewModdingAPI.Installer/InteractiveInstaller.cs b/src/StardewModdingAPI.Installer/InteractiveInstaller.cs index d7279cf7..ef813eb3 100644 --- a/src/StardewModdingAPI.Installer/InteractiveInstaller.cs +++ b/src/StardewModdingAPI.Installer/InteractiveInstaller.cs @@ -295,12 +295,12 @@ namespace StardewModdingApi.Installer { try { - Console.ResetColor(); + Console.ForegroundColor = Console.ForegroundColor; return true; } catch (Exception) { - return false; + return false; // Mono bug } } diff --git a/src/StardewModdingAPI/Framework/Monitor.cs b/src/StardewModdingAPI/Framework/Monitor.cs index b492e5c7..39b567d8 100644 --- a/src/StardewModdingAPI/Framework/Monitor.cs +++ b/src/StardewModdingAPI/Framework/Monitor.cs @@ -133,12 +133,12 @@ namespace StardewModdingAPI.Framework { try { - Console.ResetColor(); + Console.ForegroundColor = Console.ForegroundColor; return true; } catch (Exception) { - return false; + return false; // Mono bug } } } -- cgit