Class: CreateRubyGem::UI::Palette

Inherits:
Object
  • Object
show all
Defined in:
lib/create_ruby_gem/ui/palette.rb

Overview

Color constants for terminal output.

Maps semantic roles (e.g. :summary_label, :arg_name) to ANSI 256-color codes or cli-ui basic color names, depending on terminal capabilities.

Constant Summary collapse

RESET =

ANSI reset sequence.

"\e[0m"
ROLE_COLORS_256 =

Role-to-color mappings for 256-color terminals.

{
  control_back: 45,
  control_exit: 203,
  summary_label: 213,
  runtime_name: 111,
  runtime_value: 190,
  command_base: 39,
  command_gem: 82,
  arg_name: 117,
  arg_eq: 250,
  arg_value: 214
}.freeze
ROLE_COLORS_BASIC =

Role-to-color mappings for basic (8/16-color) terminals.

{
  control_back: 'cyan',
  control_exit: 'red',
  summary_label: 'magenta',
  runtime_name: 'blue',
  runtime_value: 'green',
  command_base: 'blue',
  command_gem: 'green',
  arg_name: 'blue',
  arg_eq: 'white',
  arg_value: 'yellow'
}.freeze

Instance Method Summary collapse

Constructor Details

#initialize(env: ENV) ⇒ Palette



46
47
48
# File 'lib/create_ruby_gem/ui/palette.rb', line 46

def initialize(env: ENV)
  @env = env
end

Instance Method Details

#color(role, text) ⇒ String

Wraps text in the appropriate ANSI color for the given role.



55
56
57
58
59
60
61
# File 'lib/create_ruby_gem/ui/palette.rb', line 55

def color(role, text)
  if supports_256_colors?
    "\e[38;5;#{ROLE_COLORS_256.fetch(role)}m#{text}#{RESET}"
  else
    "{{#{ROLE_COLORS_BASIC.fetch(role)}:#{text}}}"
  end
end