Module: Fatty::Themes::Resolver
- Defined in:
- lib/fatty/themes/resolver.rb
Overview
The Resolver module is responsible for transforming a raw theme loaded by the Loader into a fully-resolved "theme_spec".
Resolution includes:
- applying inheritance between themes
- applying inheritance between roles
- filling in default attributes
- synthesizing "composite" roles used by the alert panel
Composite alert roles (e.g., :alert_info, :alert_warn, etc.) combine the background and structural properties of the :alert role with the foreground and attributes of the semantic roles (:good, :info, :warn, :error).
The result of resolution is a Hash keyed by role names, where each value is a symbolic color specification (typically using color names and attrs). We refer to this Hash as a "theme_spec".
A theme_spec is not directly renderable. Before rendering, it must be "compiled" into a palette by Fatty::Colors::Palette.apply!. The palette maps each role to concrete rendering data (RGB values for truecolor or Curses color pairs, along with attributes).
Renderer classes consume only the compiled palette and never operate directly on the theme_spec. module Resolver
Constant Summary collapse
- DEFAULT_ROLE_SPECS =
{ region: { attrs: [:reverse] }, cursor: { attrs: [:reverse] }, input_suggestion: { attrs: [:dim] }, pager_status: { attrs: [:reverse] }, search_input: { attrs: [:reverse] }, match_current: { attrs: [:reverse] }, match_other: { attrs: [:underline] }, popup_counts: { attrs: [:bold] }, alert: { attrs: [:bold] }, markdown_table_cell: {}, markdown_underline: { attrs: [:underline] }, markdown_hrule: { attrs: [:dim] }, }.freeze
- ROLE_PARENTS =
{ input: :output, input_suggestion: :input, region: :output, cursor: :input, popup: :output, popup_frame: :popup, popup_input: :input, popup_selection: :region, popup_counts: :popup, search_input: :popup, match_current: :region, match_other: :region, status: :output, alert: :output, info: :output, good: :info, warn: :info, error: :warn, pager_status: :status, markdown_h1: :output, markdown_h2: :markdown_h1, markdown_h3: :markdown_h2, markdown_code: :output, markdown_code_gutter: :markdown_code, markdown_strong: :output, markdown_emphasis: :output, markdown_link: :output, markdown_url: :markdown_link, markdown_quote_gutter: :output, markdown_highlight: :output, markdown_table_header: :markdown_strong, markdown_table_cell: :output, markdown_underline: :output, markdown_hrule: :output, }.freeze
Class Method Summary collapse
- .deep_merge(parent, child) ⇒ Object
- .deep_merge_hash(parent, child) ⇒ Object
- .empty_theme ⇒ Object
- .merge_theme_chain(registry, name, stack:) ⇒ Object
- .resolve(registry, name) ⇒ Object
- .resolve_role(name, roles, resolved, stack:) ⇒ Object
- .resolve_role_inheritance(roles) ⇒ Object
Class Method Details
.deep_merge(parent, child) ⇒ Object
179 180 181 182 183 184 185 186 187 188 189 190 191 |
# File 'lib/fatty/themes/resolver.rb', line 179 def self.deep_merge(parent, child) out = parent.dup out[:roles] = deep_merge_hash(parent[:roles] || {}, child[:roles] || {}) child.each do |key, value| next if key == :roles out[key] = value end out end |
.deep_merge_hash(parent, child) ⇒ Object
193 194 195 196 197 198 199 200 201 |
# File 'lib/fatty/themes/resolver.rb', line 193 def self.deep_merge_hash(parent, child) parent.merge(child) do |_key, old_value, new_value| if old_value.is_a?(Hash) && new_value.is_a?(Hash) deep_merge_hash(old_value, new_value) else new_value end end end |
.empty_theme ⇒ Object
98 99 100 101 102 103 104 105 |
# File 'lib/fatty/themes/resolver.rb', line 98 def self.empty_theme { name: nil, inherit: nil, roles: {}, source: nil, } end |
.merge_theme_chain(registry, name, stack:) ⇒ Object
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 |
# File 'lib/fatty/themes/resolver.rb', line 115 def self.merge_theme_chain(registry, name, stack:) defn = registry.fetch(name) raise MissingThemeError, "Theme not found: #{name}" unless defn if stack.include?(name) cycle = (stack + [name]).join(" -> ") raise InheritanceCycleError, "Theme inheritance cycle: #{cycle}" end parent = if defn[:inherit] merge_theme_chain(registry, defn[:inherit], stack: stack + [name]) else empty_theme end merged = deep_merge(parent, defn) merged[:name] = name merged[:inherit] = defn[:inherit] merged[:source] = defn[:source] merged end |
.resolve(registry, name) ⇒ Object
107 108 109 110 111 112 113 |
# File 'lib/fatty/themes/resolver.rb', line 107 def self.resolve(registry, name) raw = merge_theme_chain(registry, name.to_sym, stack: []) raw[:roles] = resolve_role_inheritance(raw[:roles]) raw[:roles] = add_composite_roles(raw[:roles]) raw[:name] = name.to_sym raw end |
.resolve_role(name, roles, resolved, stack:) ⇒ Object
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 |
# File 'lib/fatty/themes/resolver.rb', line 154 def self.resolve_role(name, roles, resolved, stack:) return resolved[name] if resolved.key?(name) if stack.include?(name) raise InheritanceCycleError, "Role inheritance cycle: #{(stack + [name]).join(' -> ')}" end spec = roles[name] || DEFAULT_ROLE_SPECS.fetch(name, {}) parent_name = if spec.key?(:inherit) spec[:inherit]&.to_sym else ROLE_PARENTS[name] end parent_spec = if parent_name resolve_role(parent_name, roles, resolved, stack: stack + [name]) else {} end resolved[name] = deep_merge_hash(parent_spec, spec.reject { |k, _| k == :inherit }) end |
.resolve_role_inheritance(roles) ⇒ Object
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 |
# File 'lib/fatty/themes/resolver.rb', line 138 def self.resolve_role_inheritance(roles) resolved = {} names = ( ROLE_PARENTS.keys + ROLE_PARENTS.values + DEFAULT_ROLE_SPECS.keys + roles.keys ).compact.uniq names.each do |name| resolve_role(name, roles, resolved, stack: []) end resolved end |