Class: Gloo::App::Theme

Inherits:
Object
  • Object
show all
Defined in:
lib/gloo/app/theme.rb

Constant Summary collapse

ORANGE =

256-color values picked by eye against a real light-background terminal - see design note above. 256-color support has been effectively universal in terminals for well over a decade.

'38;5;208'.freeze
'38;5;18'.freeze
DARK_GREEN =
'38;5;22'.freeze
DARK =

Dark-background palette. This matches the colors gloo has always used, so it's the default and non-breaking.

{
  :heading    => { :color => :blue, :mode => :bold },
  :subheading => { :color => :cyan, :mode => :bold },
  :accent     => { :color => :yellow },
  :emphasis   => { :color => :white },
  :muted      => { :color => :light_black },
  :warn       => { :color => :yellow },
  :error      => { :color => :red }
}.freeze
LIGHT =

Light-background palette.

{
  :heading    => { :color => :blue, :mode => :bold },
  :subheading => { :ansi => NAVY },
  :accent     => { :ansi => DARK_GREEN },
  :emphasis   => { :color => :black },
  :muted      => { :color => :light_black },
  :warn       => { :ansi => ORANGE },
  :error      => { :color => :red }
}.freeze
PALETTES =
{ 'dark' => DARK, 'light' => LIGHT }.freeze
ROLES =
DARK.keys.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name = nil) ⇒ Theme

Set up the theme for the given theme name ('dark' or 'light'). Falls back to the default (dark) for anything else.



87
88
89
90
# File 'lib/gloo/app/theme.rb', line 87

def initialize( name = nil )
  @name = PALETTES.key?( name ) ? name : Gloo::App::Settings::DEFAULT_THEME
  @palette = PALETTES[ @name ]
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



81
82
83
# File 'lib/gloo/app/theme.rb', line 81

def name
  @name
end

Class Method Details

.for_engine(engine) ⇒ Object

Build a Theme from the engine's settings.



95
96
97
# File 'lib/gloo/app/theme.rb', line 95

def self.for_engine( engine )
  return new( engine&.settings&.theme )
end

Instance Method Details

#apply(str, role) ⇒ Object

Colorize the given string for the given semantic role. Unknown roles are returned unchanged rather than raising, so a typo'd role degrades to plain text instead of crashing.

Named colors (:color/:mode) go through the colorize gem; :ansi is a raw SGR parameter string for colors the gem's named palette doesn't have (eg. 256-color orange).



108
109
110
111
112
113
114
115
116
117
# File 'lib/gloo/app/theme.rb', line 108

def apply( str, role )
  style = @palette[ role ]
  return str.to_s unless style
  return ansi_wrap( str.to_s, style ) if style[ :ansi ]

  params = {}
  params[ :color ] = style[ :color ] if style[ :color ]
  params[ :mode ] = style[ :mode ] if style[ :mode ]
  return ColorizedString[ str.to_s ].colorize( params ).to_s
end