Skip to content

Commit

Permalink
Determine color usage dynamically
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
ekohl committed Sep 21, 2023
1 parent a3bfeeb commit f09afdc
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 7 deletions.
6 changes: 5 additions & 1 deletion lib/kafo/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down
14 changes: 8 additions & 6 deletions lib/kafo/kafo_configure.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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?
Expand Down Expand Up @@ -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}'?",
Expand Down

0 comments on commit f09afdc

Please sign in to comment.