From f09afdcb446eb80fc0c1cfb488f3a3c495d7570a Mon Sep 17 00:00:00 2001 From: Ewoud Kohl van Wijngaarden Date: Fri, 23 Oct 2020 16:01:20 +0200 Subject: [PATCH] Determine color usage dynamically This changes the color usage to be determined dynamically. Prior to this the config was always read. That meant that if the scenario was ever written to disk (using save_configuration), it stored the value of colors_possible? and uses that indefinitely. After this change, the value of colors will remain nil (determined at runtime) until the user uses --[no-]colors, which is then persisted. --- lib/kafo/configuration.rb | 6 +++++- lib/kafo/kafo_configure.rb | 14 ++++++++------ 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/lib/kafo/configuration.rb b/lib/kafo/configuration.rb index 411362a4..9456a81f 100644 --- a/lib/kafo/configuration.rb +++ b/lib/kafo/configuration.rb @@ -26,7 +26,7 @@ class Configuration ScenarioOption::ANSWER_FILE => './config/answers.yaml', ScenarioOption::INSTALLER_DIR => '.', ScenarioOption::MODULE_DIRS => ['./modules'], - ScenarioOption::COLORS => Kafo::ColorScheme.colors_possible?, + ScenarioOption::COLORS => nil, ScenarioOption::COLOR_OF_BACKGROUND => :dark, ScenarioOption::HOOK_DIRS => [], ScenarioOption::CHECK_DIRS => nil, @@ -107,6 +107,10 @@ def app end end + def use_colors? + app.fetch(ScenarioOption::COLORS) { Kafo::ColorScheme.colors_possible? } + end + def get_custom(key) custom_storage[key.to_sym] end diff --git a/lib/kafo/kafo_configure.rb b/lib/kafo/kafo_configure.rb index e6ad0489..763c6ed4 100644 --- a/lib/kafo/kafo_configure.rb +++ b/lib/kafo/kafo_configure.rb @@ -77,12 +77,14 @@ def help(*args) def use_colors? if config - colors = config.app[:colors] + config.use_colors? + elsif ARGV.include?('--no-colors') + false + elsif ARGV.include?('--colors') + true else - colors = ARGV.include?('--no-colors') ? false : nil - colors = ARGV.include?('--colors') ? true : nil if colors.nil? + Kafo::ColorScheme.colors_possible? end - colors end def preset_color_scheme @@ -190,7 +192,7 @@ def execute parse_cli_arguments if !config.app[:verbose] - @progress_bar = config.app[:colors] ? ProgressBars::Colored.new : ProgressBars::BlackWhite.new + @progress_bar = config.use_colors? ? ProgressBars::Colored.new : ProgressBars::BlackWhite.new end unless skip_checks_i_know_better? @@ -347,7 +349,7 @@ def terminal_log_levels_message def set_app_options app_option ['--[no-]colors'], :flag, 'Use color output on STDOUT', - :default => config.app[:colors], :advanced => true + :default => config.use_colors?, :advanced => true app_option ['--color-of-background'], 'COLOR', 'Your terminal background is :bright or :dark', :default => config.app[:color_of_background], :advanced => true app_option ['--dont-save-answers'], :flag, "Skip saving answers to '#{self.class.config.answer_file}'?",