summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorJesse Plamondon-Willard <github@jplamondonw.com>2017-01-14 15:05:38 -0500
committerJesse Plamondon-Willard <github@jplamondonw.com>2017-01-14 15:05:38 -0500
commit90e92ef61f0808688abf638ee5cb941215c1586f (patch)
tree09e0b6734e867a61d31e934b7d40436a7dd970ca /src
parent2d824b34e478bd53a9cfae85332669755b326d84 (diff)
downloadSMAPI-90e92ef61f0808688abf638ee5cb941215c1586f.tar.gz
SMAPI-90e92ef61f0808688abf638ee5cb941215c1586f.tar.bz2
SMAPI-90e92ef61f0808688abf638ee5cb941215c1586f.zip
fix error when the console doesn't support colour (#206)
Diffstat (limited to 'src')
-rw-r--r--src/StardewModdingAPI.Installer/InteractiveInstaller.cs58
-rw-r--r--src/StardewModdingAPI/Framework/Monitor.cs28
-rw-r--r--src/StardewModdingAPI/LogWriter.cs11
3 files changed, 75 insertions, 22 deletions
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
};
+ /// <summary>Whether the current console supports color formatting.</summary>
+ 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
}
}
+ /// <summary>Test whether the current console supports color formatting.</summary>
+ private static bool GetConsoleSupportsColor()
+ {
+ try
+ {
+ Console.ResetColor();
+ return true;
+ }
+ catch (Exception)
+ {
+ return false;
+ }
+ }
+
#if SMAPI_FOR_WINDOWS
/// <summary>Get the value of a key in the Windows registry.</summary>
/// <param name="key">The full path of the registry key relative to HKLM.</param>
@@ -306,30 +323,39 @@ namespace StardewModdingApi.Installer
/// <param name="text">The text to print.</param>
private void PrintDebug(string text)
{
- Console.ForegroundColor = ConsoleColor.DarkGray;
- Console.WriteLine(text);
- Console.ResetColor();
+ this.PrintColor(text, ConsoleColor.DarkGray);
}
/// <summary>Print a warning message.</summary>
/// <param name="text">The text to print.</param>
private void PrintWarning(string text)
{
- Console.ForegroundColor = ConsoleColor.DarkYellow;
- Console.WriteLine(text);
- Console.ResetColor();
+ this.PrintColor(text, ConsoleColor.DarkYellow);
}
/// <summary>Print an error and pause the console if needed.</summary>
/// <param name="error">The error text.</param>
private void ExitError(string error)
{
- Console.ForegroundColor = ConsoleColor.Red;
- Console.WriteLine(error);
- Console.ResetColor();
+ this.PrintColor(error, ConsoleColor.Red);
Console.ReadLine();
}
+ /// <summary>Print a message to the console.</summary>
+ /// <param name="text">The message text.</param>
+ /// <param name="color">The text foreground color.</param>
+ private void PrintColor(string text, ConsoleColor color)
+ {
+ if (InteractiveInstaller.ConsoleSupportsColor)
+ {
+ Console.ForegroundColor = color;
+ Console.WriteLine(text);
+ Console.ResetColor();
+ }
+ else
+ Console.WriteLine(text);
+ }
+
/// <summary>Interactively ask the user to choose a value.</summary>
/// <param name="message">The message to print.</param>
/// <param name="options">The allowed options (not case sensitive).</param>
diff --git a/src/StardewModdingAPI/Framework/Monitor.cs b/src/StardewModdingAPI/Framework/Monitor.cs
index 0989bb7e..b492e5c7 100644
--- a/src/StardewModdingAPI/Framework/Monitor.cs
+++ b/src/StardewModdingAPI/Framework/Monitor.cs
@@ -34,6 +34,9 @@ namespace StardewModdingAPI.Framework
/*********
** Accessors
*********/
+ /// <summary>Whether the current console supports color codes.</summary>
+ internal static readonly bool ConsoleSupportsColor = Monitor.GetConsoleSupportsColor();
+
/// <summary>Whether to show trace messages in the console.</summary>
internal bool ShowTraceInConsole { get; set; }
@@ -113,11 +116,30 @@ namespace StardewModdingAPI.Framework
// log
if (this.WriteToConsole && (this.ShowTraceInConsole || level != LogLevel.Trace))
{
- Console.ForegroundColor = color;
- Console.WriteLine(message);
- Console.ResetColor();
+ if (Monitor.ConsoleSupportsColor)
+ {
+ Console.ForegroundColor = color;
+ Console.WriteLine(message);
+ Console.ResetColor();
+ }
+ else
+ Console.WriteLine(message);
}
this.LogFile.WriteLine(message);
}
+
+ /// <summary>Test whether the current console supports color formatting.</summary>
+ private static bool GetConsoleSupportsColor()
+ {
+ try
+ {
+ Console.ResetColor();
+ return true;
+ }
+ catch (Exception)
+ {
+ return false;
+ }
+ }
}
} \ No newline at end of file
diff --git a/src/StardewModdingAPI/LogWriter.cs b/src/StardewModdingAPI/LogWriter.cs
index 058fa649..9c2ef515 100644
--- a/src/StardewModdingAPI/LogWriter.cs
+++ b/src/StardewModdingAPI/LogWriter.cs
@@ -42,9 +42,14 @@ namespace StardewModdingAPI
string output = $"[{message.LogTime}] {message.Message}";
if (message.PrintConsole)
{
- Console.ForegroundColor = message.Colour;
- Console.WriteLine(message);
- Console.ForegroundColor = ConsoleColor.Gray;
+ if (Monitor.ConsoleSupportsColor)
+ {
+ Console.ForegroundColor = message.Colour;
+ Console.WriteLine(message);
+ Console.ResetColor();
+ }
+ else
+ Console.WriteLine(message);
}
this.LogFile.WriteLine(output);
}