Class: WebConsole::Colors

Inherits:
Array
  • Object
show all
Defined in:
lib/web_console/colors.rb

Overview

Colors

Manages the creation and serialization of terminal color themes.

Colors is a subclass of Array and it stores a collection of CSS color values, to be used from the client-side terminal.

You can specify 8 or 16 colors and additional background and foreground colors. If not explicitly specified, background and foreground are considered to be the first and the last of the given colors.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.[](name) ⇒ Object

Shortcut for WebConsole::Colors.themes#[].



44
45
46
# File 'lib/web_console/colors.rb', line 44

def [](name)
  themes[name]
end

.defaultObject

The default colors theme.



39
40
41
# File 'lib/web_console/colors.rb', line 39

def default
  self[:light]
end

.register_theme(name, colors = nil) ⇒ Object

Register a color theme into the color themes registry.

Registration maps a name and Colors instance.

If a block is given, it would be yielded with a new Colors instance to populate the theme colors in.

If a Colors instance is already instantiated it can be passed directly as the second (colors) argument. In this case, if a block is given, it won’t be executed.



34
35
36
# File 'lib/web_console/colors.rb', line 34

def register_theme(name, colors = nil)
  themes[name] = colors || new.tap { |c| yield c }
end

.themesObject

Registry of color themes mapped to a name.

Don’t manually alter the registry. Use WebConsole::Colors.register_theme for adding entries.



20
21
22
# File 'lib/web_console/colors.rb', line 20

def themes
  @@themes ||= {}.with_indifferent_access
end

Instance Method Details

#background(value = nil) ⇒ Object Also known as: background=

Background color getter and setter.

If called without arguments it acts like a getter. Otherwise it acts like a setter.

The default background color will be the first entry in the colors theme.



57
58
59
60
# File 'lib/web_console/colors.rb', line 57

def background(value = nil)
  @background   = value unless value.nil?
  @background ||= self.first
end

#foreground(value = nil) ⇒ Object Also known as: foreground=

Foreground color getter and setter.

If called without arguments it acts like a getter. Otherwise it acts like a setter.

The default foreground color will be the last entry in the colors theme.



70
71
72
73
# File 'lib/web_console/colors.rb', line 70

def foreground(value = nil)
  @foreground   = value unless value.nil?
  @foreground ||= self.last
end

#to_jsonObject



77
78
79
# File 'lib/web_console/colors.rb', line 77

def to_json
  (dup << background << foreground).to_a.to_json
end