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.

Examples:

Initialize and load config

Kolor::Config.init

Reload config manually

Kolor::Config.reload!

Access loaded themes

Kolor::Config.themes_from_config

Describe a theme

Kolor::Config.describe_theme(:success)

Constant Summary collapse

HOME_PATH =
ENV['HOME'] || ENV['USERPROFILE']
CONFIG_FILE_RB =
HOME_PATH ? File.expand_path("#{HOME_PATH}/.kolorrc.rb") : nil
CONFIG_FILE_ALIAS =
HOME_PATH ? File.expand_path("#{HOME_PATH}/.kolorrc") : nil

Class Method Summary collapse

Class Method Details

.config_exists?Boolean

Checks whether any supported config file exists.

Returns:

  • (Boolean)

    true if a config file is found, false otherwise



44
45
46
# File 'lib/kolor/internal/config.rb', line 44

def config_exists?
  !config_file_path.nil?
end

.create_default_configvoid

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.message if e.is_a?(LoadError)
  end
end

.describe_theme(name) ⇒ Hash{Symbol => Object}?

Returns the theme configuration as a hash.

Parameters:

  • name (Symbol, String)

    theme name

Returns:

  • (Hash{Symbol => Object}, nil)

    theme config or nil if not found



108
109
110
# File 'lib/kolor/internal/config.rb', line 108

def describe_theme(name)
  Kolor::Extra.get_theme(name.to_sym)
end

.initvoid

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_configvoid

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_configArray<Symbol>

Returns all theme names loaded from config.

Returns:

  • (Array<Symbol>)

    list of theme keys



115
116
117
# File 'lib/kolor/internal/config.rb', line 115

def themes_from_config
  Kolor::Extra.themes
end