From 4e49ce8547c926f13589807a1ac70a3406a1cf01 Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Sat, 11 Aug 2018 23:19:21 -0400 Subject: let Linux/Mac players choose the console scheme when installing (#554) --- src/SMAPI.Installer/InteractiveInstaller.cs | 94 +++++++++++++++++++++++++++-- 1 file changed, 88 insertions(+), 6 deletions(-) (limited to 'src') diff --git a/src/SMAPI.Installer/InteractiveInstaller.cs b/src/SMAPI.Installer/InteractiveInstaller.cs index b686e5bc..bb953865 100644 --- a/src/SMAPI.Installer/InteractiveInstaller.cs +++ b/src/SMAPI.Installer/InteractiveInstaller.cs @@ -136,7 +136,7 @@ namespace StardewModdingApi.Installer } /// Handles writing color-coded text to the console. - private readonly ColorfulConsoleWriter ConsoleWriter; + private ColorfulConsoleWriter ConsoleWriter; /********* @@ -245,7 +245,55 @@ namespace StardewModdingApi.Installer /********* - ** Step 2: find game folder + ** Step 2: choose a theme (can't auto-detect on Linux/Mac) + *********/ + MonitorColorScheme scheme = MonitorColorScheme.AutoDetect; + if (platform == Platform.Linux || platform == Platform.Mac) + { + /**** + ** print header + ****/ + this.PrintPlain("Hi there! I'll help you install or remove SMAPI. Just a few questions first."); + this.PrintPlain("----------------------------------------------------------------------------"); + Console.WriteLine(); + + /**** + ** show theme selector + ****/ + // get theme writers + var lightBackgroundWriter = new ColorfulConsoleWriter(EnvironmentUtility.DetectPlatform(), MonitorColorScheme.LightBackground); + var darkDarkgroundWriter = new ColorfulConsoleWriter(EnvironmentUtility.DetectPlatform(), MonitorColorScheme.DarkBackground); + + // print question + this.PrintPlain("Which text looks more readable?"); + Console.WriteLine(); + Console.Write(" [1] "); + lightBackgroundWriter.WriteLine("Dark text on light background", ConsoleLogLevel.Info); + Console.Write(" [2] "); + darkDarkgroundWriter.WriteLine("Light text on dark background", ConsoleLogLevel.Info); + Console.WriteLine(); + + // handle choice + string choice = this.InteractivelyChoose("Type 1 or 2, then press enter.", new[] { "1", "2" }); + switch (choice) + { + case "1": + scheme = MonitorColorScheme.LightBackground; + this.ConsoleWriter = lightBackgroundWriter; + break; + case "2": + scheme = MonitorColorScheme.DarkBackground; + this.ConsoleWriter = darkDarkgroundWriter; + break; + default: + throw new InvalidOperationException($"Unexpected action key '{choice}'."); + } + } + Console.Clear(); + + + /********* + ** Step 3: find game folder *********/ InstallerPaths paths; { @@ -253,6 +301,7 @@ namespace StardewModdingApi.Installer ** print header ****/ this.PrintInfo("Hi there! I'll help you install or remove SMAPI. Just a few questions first."); + this.PrintDebug($"Color scheme: {this.GetDisplayText(scheme)}"); this.PrintDebug("----------------------------------------------------------------------------"); Console.WriteLine(); @@ -279,7 +328,7 @@ namespace StardewModdingApi.Installer /********* - ** Step 3: validate assumptions + ** Step 4: validate assumptions *********/ { if (!paths.PackageDir.Exists) @@ -302,7 +351,7 @@ namespace StardewModdingApi.Installer /********* - ** Step 4: ask what to do + ** Step 5: ask what to do *********/ ScriptAction action; { @@ -311,6 +360,7 @@ namespace StardewModdingApi.Installer ****/ this.PrintInfo("Hi there! I'll help you install or remove SMAPI. Just one question first."); this.PrintDebug($"Game path: {paths.GamePath}"); + this.PrintDebug($"Color scheme: {this.GetDisplayText(scheme)}"); this.PrintDebug("----------------------------------------------------------------------------"); Console.WriteLine(); @@ -347,7 +397,7 @@ namespace StardewModdingApi.Installer /********* - ** Step 5: apply + ** Step 6: apply *********/ { /**** @@ -355,6 +405,7 @@ namespace StardewModdingApi.Installer ****/ this.PrintInfo($"That's all I need! I'll {action.ToString().ToLower()} SMAPI now."); this.PrintDebug($"Game path: {paths.GamePath}"); + this.PrintDebug($"Color scheme: {this.GetDisplayText(scheme)}"); this.PrintDebug("----------------------------------------------------------------------------"); Console.WriteLine(); @@ -460,6 +511,16 @@ namespace StardewModdingApi.Installer foreach (FileInfo sourceFile in sourceDir.EnumerateFiles().Where(this.ShouldCopyFile)) sourceFile.CopyTo(Path.Combine(targetDir.FullName, sourceFile.Name)); } + + // set SMAPI's color scheme if defined + if (scheme != MonitorColorScheme.AutoDetect) + { + string configPath = Path.Combine(paths.GamePath, "StardewModdingAPI.config.json"); + string text = File + .ReadAllText(configPath) + .Replace(@"""ColorScheme"": ""AutoDetect""", $@"""ColorScheme"": ""{scheme}"""); + File.WriteAllText(configPath, text); + } } // remove obsolete appdata mods @@ -471,7 +532,7 @@ namespace StardewModdingApi.Installer /********* - ** Step 6: final instructions + ** Step 7: final instructions *********/ if (platform == Platform.Windows) { @@ -510,6 +571,23 @@ namespace StardewModdingApi.Installer return str; } + /// Get the display text for a color scheme. + /// The color scheme. + private string GetDisplayText(MonitorColorScheme scheme) + { + switch (scheme) + { + case MonitorColorScheme.AutoDetect: + return "auto-detect"; + case MonitorColorScheme.DarkBackground: + return "light text on dark background"; + case MonitorColorScheme.LightBackground: + return "dark text on light background"; + default: + return scheme.ToString(); + } + } + /// Get the value of a key in the Windows HKLM registry. /// The full path of the registry key relative to HKLM. /// The name of the value. @@ -536,6 +614,10 @@ namespace StardewModdingApi.Installer return (string)openKey.GetValue(name); } + /// Print a message without formatting. + /// The text to print. + private void PrintPlain(string text) => Console.WriteLine(text); + /// Print a debug message. /// The text to print. private void PrintDebug(string text) => this.ConsoleWriter.WriteLine(text, ConsoleLogLevel.Debug); -- cgit