Class: Canon::DiffFormatter::Theme::Resolver

Inherits:
Object
  • Object
show all
Defined in:
lib/canon/diff_formatter/theme.rb

Overview

Resolves the actual theme hash from configuration. Priority:

  1. ENV (highest)

  2. config.xml.diff.theme

  3. :dark default

Also supports:

  • Theme inheritance via config.xml.diff.theme_inheritance

  • Custom theme via config.xml.diff.custom_theme

Instance Method Summary collapse

Constructor Details

#initialize(config = nil) ⇒ Resolver

Initialize with a config object (optional)

Parameters:



788
789
790
# File 'lib/canon/diff_formatter/theme.rb', line 788

def initialize(config = nil)
  @config = config
end

Instance Method Details

#resolveHash

Resolve the theme hash to use for rendering

Returns:

  • (Hash)

    Complete theme hash



794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
# File 'lib/canon/diff_formatter/theme.rb', line 794

def resolve
  # Check ENV first
  env_theme = resolve_from_env
  return env_theme if env_theme

  # Check config theme_inheritance (custom theme with base + overrides)
  if @config.respond_to?(:xml) && @config.xml.diff.respond_to?(:theme_inheritance)
    inheritance = @config.xml.diff.theme_inheritance
    return resolve_inheritance_theme(inheritance) if inheritance
  end

  # Check config custom_theme (full custom theme hash)
  if @config.respond_to?(:xml) && @config.xml.diff.respond_to?(:custom_theme)
    custom = @config.xml.diff.custom_theme
    return custom if custom.is_a?(Hash) && !custom.empty?
  end

  # Check config theme name
  if @config.respond_to?(:xml) && @config.xml.diff.respond_to?(:theme)
    theme_name = @config.xml.diff.theme
    return Theme[theme_name] if Theme.include?(theme_name)
  end

  # Default to :dark
  Theme[:dark]
end

#theme_nameSymbol

Get theme by name from ENV or config

Returns:

  • (Symbol)

    Theme name



823
824
825
826
827
828
829
830
831
832
833
834
835
836
# File 'lib/canon/diff_formatter/theme.rb', line 823

def theme_name
  # ENV takes precedence
  env_name = ENV["CANON_DIFF_THEME"]&.to_sym
  return env_name if env_name && Theme.include?(env_name)

  # Check config
  if @config.respond_to?(:xml) && @config.xml.diff.respond_to?(:theme)
    theme_name = @config.xml.diff.theme
    return theme_name if Theme.include?(theme_name)
  end

  # Default
  :dark
end