Module: Kolor::Config
- Defined in:
- lib/kolor/internal/config.rb
Overview
Kolor::Config handles configuration loading and initialization for Kolor.
It supports Ruby-based config files located in the user’s home directory:
- `.kolorrc.rb`
The config file can define themes using ‘Kolor::Extra.theme`. If no config is found, a default file is created automatically.
Constant Summary collapse
- HOME_PATH =
ENV['HOME'] || ENV['USERPROFILE']
- CONFIG_FILE_RB =
HOME_PATH ? File.("#{HOME_PATH}/.kolorrc.rb") : nil
- CONFIG_FILE_ALIAS =
HOME_PATH ? File.("#{HOME_PATH}/.kolorrc") : nil
Class Method Summary collapse
-
.config_exists? ⇒ Boolean
Checks whether any supported config file exists.
-
.create_default_config ⇒ void
Creates a default Ruby config file in the user’s home directory.
-
.describe_theme(name) ⇒ Hash{Symbol => Object}?
Returns the theme configuration as a hash.
-
.init ⇒ void
Initializes configuration.
-
.load_config ⇒ void
Loads configuration from disk.
-
.reload! ⇒ void
Reloads configuration from disk.
-
.themes_from_config ⇒ Array<Symbol>
Returns all theme names loaded from config.
Class Method Details
.config_exists? ⇒ Boolean
Checks whether any supported config file exists.
44 45 46 |
# File 'lib/kolor/internal/config.rb', line 44 def config_exists? !config_file_path.nil? end |
.create_default_config ⇒ void
This method returns an undefined value.
Creates a default Ruby config file in the user’s home directory. Skips creation if a config already exists. Logs warnings if HOME is missing or creation fails.
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/kolor/internal/config.rb', line 53 def create_default_config unless HOME_PATH Kolor::Logger.warn 'No home directory found' return end return unless find_existing_config_file.nil? begin if CONFIG_FILE_RB.nil? raise LoadError, 'Config path is invalid' end FileUtils.cp(default_config_path, CONFIG_FILE_RB) Kolor::Logger.warn "Created default configuration file at #{CONFIG_FILE_RB}" rescue StandardError, LoadError => e Kolor::Logger.warn "Failed to create default config file: #{e.message}" if e.is_a?(StandardError) Kolor::Logger.warn e. if e.is_a?(LoadError) end end |
.describe_theme(name) ⇒ Hash{Symbol => Object}?
Returns the theme configuration as a hash.
108 109 110 |
# File 'lib/kolor/internal/config.rb', line 108 def describe_theme(name) Kolor::Extra.get_theme(name.to_sym) end |
.init ⇒ void
This method returns an undefined value.
Initializes configuration. Creates a default config file if none exists, then loads it.
36 37 38 39 |
# File 'lib/kolor/internal/config.rb', line 36 def init create_default_config unless config_exists? load_config end |
.load_config ⇒ void
This method returns an undefined value.
Loads configuration from disk. Supports only Ruby-based config files (.rb or .kolorrc). Logs info and warnings during the process.
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/kolor/internal/config.rb', line 78 def load_config config_path = config_file_path Kolor::Logger.info "load_config called, config_path: #{config_path.inspect}" return unless config_path Kolor::Logger.info "Loading config from: #{config_path}" if config_path.end_with?('.rb') || config_path.end_with?('.kolorrc') Kolor::Logger.info "Detected Ruby config" load_ruby_config(config_path) else Kolor::Logger.warn "Unknown config file type: #{config_path}" end rescue StandardError => e Kolor::Logger.warn "Error loading config file #{config_path}: #{e.message}" end |
.reload! ⇒ void
This method returns an undefined value.
Reloads configuration from disk.
100 101 102 |
# File 'lib/kolor/internal/config.rb', line 100 def reload! load_config end |
.themes_from_config ⇒ Array<Symbol>
Returns all theme names loaded from config.
115 116 117 |
# File 'lib/kolor/internal/config.rb', line 115 def themes_from_config Kolor::Extra.themes end |