fix console color scheme for PowerShell, and make it configurable

This commit is contained in:
Jesse Plamondon-Willard 2018-04-13 23:00:49 -04:00
parent a3ade7a512
commit 504733dec7
7 changed files with 70 additions and 29 deletions

View File

@ -5,6 +5,7 @@
* Added support for Stardew Valley 1.3+; no longer compatible with earlier versions.
* Added `Context.IsMultiplayer` and `Context.IsMainPlayer` flags.
* Fixed SMAPI update checks not showing newer beta versions when using a beta version.
* Fixed console color scheme for PowerShell and added override option to `StardewModdingAPI.config.json`.
* For modders:
* Added code analysis to mod build config package to flag common issues as warnings.

View File

@ -0,0 +1,15 @@
namespace StardewModdingAPI.Framework.Models
{
/// <summary>A monitor color scheme to use.</summary>
internal enum MonitorColorScheme
{
/// <summary>Choose a color scheme automatically.</summary>
AutoDetect,
/// <summary>Use lighter text colors that look better on a black or dark background.</summary>
DarkBackground,
/// <summary>Use darker text colors that look better on a white or light background.</summary>
LightBackground
}
}

View File

@ -20,5 +20,8 @@ namespace StardewModdingAPI.Framework.Models
/// <summary>Whether SMAPI should log more information about the game context.</summary>
public bool VerboseLogging { get; set; }
/// <summary>The console color scheme to use.</summary>
public MonitorColorScheme ColorScheme { get; set; }
}
}

View File

@ -3,6 +3,7 @@ using System.Collections.Generic;
using System.Linq;
using System.Threading;
using StardewModdingAPI.Framework.Logging;
using StardewModdingAPI.Framework.Models;
namespace StardewModdingAPI.Framework
{
@ -25,7 +26,7 @@ namespace StardewModdingAPI.Framework
private static readonly int MaxLevelLength = (from level in Enum.GetValues(typeof(LogLevel)).Cast<LogLevel>() select level.ToString().Length).Max();
/// <summary>The console text color for each log level.</summary>
private static readonly IDictionary<LogLevel, ConsoleColor> Colors = Monitor.GetConsoleColorScheme();
private readonly IDictionary<LogLevel, ConsoleColor> Colors;
/// <summary>Propagates notification that SMAPI should exit.</summary>
private readonly CancellationTokenSource ExitTokenSource;
@ -58,13 +59,15 @@ namespace StardewModdingAPI.Framework
/// <param name="consoleManager">Manages access to the console output.</param>
/// <param name="logFile">The log file to which to write messages.</param>
/// <param name="exitTokenSource">Propagates notification that SMAPI should exit.</param>
public Monitor(string source, ConsoleInterceptionManager consoleManager, LogFileManager logFile, CancellationTokenSource exitTokenSource)
/// <param name="colorScheme">The console color scheme to use.</param>
public Monitor(string source, ConsoleInterceptionManager consoleManager, LogFileManager logFile, CancellationTokenSource exitTokenSource, MonitorColorScheme colorScheme)
{
// validate
if (string.IsNullOrWhiteSpace(source))
throw new ArgumentException("The log source cannot be empty.");
// initialise
this.Colors = Monitor.GetConsoleColorScheme(colorScheme);
this.Source = source;
this.LogFile = logFile ?? throw new ArgumentNullException(nameof(logFile), "The log file manager cannot be null.");
this.ConsoleManager = consoleManager;
@ -76,7 +79,7 @@ namespace StardewModdingAPI.Framework
/// <param name="level">The log severity level.</param>
public void Log(string message, LogLevel level = LogLevel.Debug)
{
this.LogImpl(this.Source, message, level, Monitor.Colors[level]);
this.LogImpl(this.Source, message, level, this.Colors[level]);
}
/// <summary>Immediately exit the game without saving. This should only be invoked when an irrecoverable fatal error happens that risks save corruption or game-breaking bugs.</summary>
@ -145,32 +148,41 @@ namespace StardewModdingAPI.Framework
}
/// <summary>Get the color scheme to use for the current console.</summary>
private static IDictionary<LogLevel, ConsoleColor> GetConsoleColorScheme()
/// <param name="colorScheme">The console color scheme to use.</param>
private static IDictionary<LogLevel, ConsoleColor> GetConsoleColorScheme(MonitorColorScheme colorScheme)
{
// scheme for dark console background
if (Monitor.IsDark(Console.BackgroundColor))
{
return new Dictionary<LogLevel, ConsoleColor>
{
[LogLevel.Trace] = ConsoleColor.DarkGray,
[LogLevel.Debug] = ConsoleColor.DarkGray,
[LogLevel.Info] = ConsoleColor.White,
[LogLevel.Warn] = ConsoleColor.Yellow,
[LogLevel.Error] = ConsoleColor.Red,
[LogLevel.Alert] = ConsoleColor.Magenta
};
}
// auto detect color scheme
if (colorScheme == MonitorColorScheme.AutoDetect)
colorScheme = Monitor.IsDark(Console.BackgroundColor) ? MonitorColorScheme.DarkBackground : MonitorColorScheme.LightBackground;
// scheme for light console background
return new Dictionary<LogLevel, ConsoleColor>
// get colors for scheme
switch (colorScheme)
{
[LogLevel.Trace] = ConsoleColor.DarkGray,
[LogLevel.Debug] = ConsoleColor.DarkGray,
[LogLevel.Info] = ConsoleColor.Black,
[LogLevel.Warn] = ConsoleColor.DarkYellow,
[LogLevel.Error] = ConsoleColor.Red,
[LogLevel.Alert] = ConsoleColor.DarkMagenta
};
case MonitorColorScheme.DarkBackground:
return new Dictionary<LogLevel, ConsoleColor>
{
[LogLevel.Trace] = ConsoleColor.DarkGray,
[LogLevel.Debug] = ConsoleColor.DarkGray,
[LogLevel.Info] = ConsoleColor.White,
[LogLevel.Warn] = ConsoleColor.Yellow,
[LogLevel.Error] = ConsoleColor.Red,
[LogLevel.Alert] = ConsoleColor.Magenta
};
case MonitorColorScheme.LightBackground:
return new Dictionary<LogLevel, ConsoleColor>
{
[LogLevel.Trace] = ConsoleColor.DarkGray,
[LogLevel.Debug] = ConsoleColor.DarkGray,
[LogLevel.Info] = ConsoleColor.Black,
[LogLevel.Warn] = ConsoleColor.DarkYellow,
[LogLevel.Error] = ConsoleColor.Red,
[LogLevel.Alert] = ConsoleColor.DarkMagenta
};
default:
throw new NotSupportedException($"Unknown color scheme '{colorScheme}'.");
}
}
/// <summary>Get whether a console color should be considered dark, which is subjectively defined as 'white looks better than black on this text'.</summary>
@ -182,6 +194,7 @@ namespace StardewModdingAPI.Framework
case ConsoleColor.Black:
case ConsoleColor.Blue:
case ConsoleColor.DarkBlue:
case ConsoleColor.DarkMagenta: // Powershell
case ConsoleColor.DarkRed:
case ConsoleColor.Red:
return true;

View File

@ -134,7 +134,7 @@ namespace StardewModdingAPI
// init basics
this.Settings = JsonConvert.DeserializeObject<SConfig>(File.ReadAllText(Constants.ApiConfigPath));
this.LogFile = new LogFileManager(logPath);
this.Monitor = new Monitor("SMAPI", this.ConsoleManager, this.LogFile, this.CancellationTokenSource)
this.Monitor = new Monitor("SMAPI", this.ConsoleManager, this.LogFile, this.CancellationTokenSource, this.Settings.ColorScheme)
{
WriteToConsole = writeToConsole,
ShowTraceInConsole = this.Settings.DeveloperMode,
@ -1149,7 +1149,7 @@ namespace StardewModdingAPI
/// <param name="name">The name of the module which will log messages with this instance.</param>
private Monitor GetSecondaryMonitor(string name)
{
return new Monitor(name, this.ConsoleManager, this.LogFile, this.CancellationTokenSource)
return new Monitor(name, this.ConsoleManager, this.LogFile, this.CancellationTokenSource, this.Settings.ColorScheme)
{
WriteToConsole = this.Monitor.WriteToConsole,
ShowTraceInConsole = this.Settings.DeveloperMode,

View File

@ -36,5 +36,13 @@ This file contains advanced configuration for SMAPI. You generally shouldn't cha
/**
* Whether SMAPI should log more information about the game context.
*/
"VerboseLogging": false
"VerboseLogging": false,
/**
* The console color theme to use. The possible values are:
* - AutoDetect: SMAPI will assume a light background on Mac, and detect the background color automatically on Linux or Mac.
* - LightBackground: use darker text colors that look better on a white or light background.
* - DarkBackground: use lighter text colors that look better on a black or dark background.
*/
"ColorScheme": "AutoDetect"
}

View File

@ -123,6 +123,7 @@
<Compile Include="Framework\ContentManagerShim.cs" />
<Compile Include="Framework\Exceptions\SAssemblyLoadFailedException.cs" />
<Compile Include="Framework\ModLoading\AssemblyLoadStatus.cs" />
<Compile Include="Framework\Models\MonitorColorScheme.cs" />
<Compile Include="Framework\Reflection\InterfaceProxyBuilder.cs" />
<Compile Include="Framework\Reflection\InterfaceProxyFactory.cs" />
<Compile Include="Framework\Serialisation\SmapiConverters\ManifestContentPackForConverter.cs" />