From 79c70b399884f8b7ed027916171d0a04234e5192 Mon Sep 17 00:00:00 2001 From: Den Delimarsky <1389609+dend@users.noreply.github.com> Date: Mon, 10 May 2021 17:55:11 -0700 Subject: [PATCH] Modify how settings are handled by Espresso --- src/modules/espresso/Espresso/Program.cs | 25 +++++++++++-------- .../ISettingsUtils.cs | 2 ++ .../SettingsUtils.cs | 6 +++++ 3 files changed, 22 insertions(+), 11 deletions(-) diff --git a/src/modules/espresso/Espresso/Program.cs b/src/modules/espresso/Espresso/Program.cs index 1d0dc6695..8579c00ba 100644 --- a/src/modules/espresso/Espresso/Program.cs +++ b/src/modules/espresso/Espresso/Program.cs @@ -52,12 +52,12 @@ namespace Espresso.Shell log.Debug($"OS: {Environment.OSVersion}"); log.Debug($"OS Build: {APIHelper.GetOperatingSystemBuild()}"); - var configOption = new Option( - aliases: new[] { "--config", "-c" }, - getDefaultValue: () => string.Empty, - description: "Pointer to a PowerToys configuration file that the tool will be watching for changes. All other options are disregarded if config is used.") + var configOption = new Option( + aliases: new[] { "--use-pt-config", "-c" }, + getDefaultValue: () => true, + description: "Specifies whether Espresso will be using the PowerToys configuration file for managing the state.") { - Argument = new Argument(() => string.Empty) + Argument = new Argument(() => true) { Arity = ArgumentArity.ZeroOrOne, }, @@ -114,7 +114,7 @@ namespace Espresso.Shell rootCommand.Description = appName; - rootCommand.Handler = CommandHandler.Create(HandleCommandLineArguments); + rootCommand.Handler = CommandHandler.Create(HandleCommandLineArguments); return rootCommand.InvokeAsync(args).Result; } @@ -127,25 +127,28 @@ namespace Espresso.Shell Environment.Exit(exitCode); } - private static void HandleCommandLineArguments(string config, bool displayOn, long timeLimit, int pid) + private static void HandleCommandLineArguments(bool usePtConfig, bool displayOn, long timeLimit, int pid) { - log.Info($"The value for --config is: {config}"); + log.Info($"The value for --use-pt-config is: {usePtConfig}"); log.Info($"The value for --display-on is: {displayOn}"); log.Info($"The value for --time-limit is: {timeLimit}"); log.Info($"The value for --pid is: {pid}"); - if (!string.IsNullOrWhiteSpace(config)) + if (usePtConfig) { // Configuration file is used, therefore we disregard any other command-line parameter // and instead watch for changes in the file. try { + var settingsPath = settingsUtils.GetSettingsFilePath(appName); + log.Info($"Reading configuration file: {settingsPath}"); + watcher = new FileSystemWatcher { - Path = Path.GetDirectoryName(config), + Path = Path.GetDirectoryName(settingsPath), EnableRaisingEvents = true, NotifyFilter = NotifyFilters.LastWrite, - Filter = Path.GetFileName(config) + Filter = Path.GetFileName(settingsPath) }; Observable.FromEventPattern( diff --git a/src/settings-ui/Microsoft.PowerToys.Settings.UI.Library/ISettingsUtils.cs b/src/settings-ui/Microsoft.PowerToys.Settings.UI.Library/ISettingsUtils.cs index ea7b615fc..7cfa33be6 100644 --- a/src/settings-ui/Microsoft.PowerToys.Settings.UI.Library/ISettingsUtils.cs +++ b/src/settings-ui/Microsoft.PowerToys.Settings.UI.Library/ISettingsUtils.cs @@ -19,5 +19,7 @@ namespace Microsoft.PowerToys.Settings.UI.Library bool SettingsExists(string powertoy = "", string fileName = "settings.json"); void DeleteSettings(string powertoy = ""); + + string GetSettingsFilePath(string powertoy = "", string fileName = "settings.json"); } } diff --git a/src/settings-ui/Microsoft.PowerToys.Settings.UI.Library/SettingsUtils.cs b/src/settings-ui/Microsoft.PowerToys.Settings.UI.Library/SettingsUtils.cs index eb40f04cf..926674ab0 100644 --- a/src/settings-ui/Microsoft.PowerToys.Settings.UI.Library/SettingsUtils.cs +++ b/src/settings-ui/Microsoft.PowerToys.Settings.UI.Library/SettingsUtils.cs @@ -135,5 +135,11 @@ namespace Microsoft.PowerToys.Settings.UI.Library #endif } } + + // Returns the file path to the settings file, that is exposed from the local ISettingsPath instance. + public string GetSettingsFilePath(string powertoy = "", string fileName = "settings.json") + { + return _settingsPath.GetSettingsPath(powertoy, fileName); + } } }