Class: Vedeu::Colour
- Inherits:
-
Object
- Object
- Vedeu::Colour
- Defined in:
- lib/vedeu/output/colour.rb
Overview
Fix colours in all terminals. (GL: 2015-04-13)
Provides a container for terminal escape sequences controlling the foreground and background colours of a character or collection of characters.
Vedeu uses HTML/CSS style notation (i.e. ‘#aadd00’), they can be used at the stream level, the line level or for the whole interface. Terminals generally support either 8, 16 or 256 colours, with few supporting full 24-bit colour (see notes below).
Vedeu attempts to detect the colour depth using the ‘$TERM` environment variable.
To set your ‘$TERM` variable to allow 256 colour support:
“‘bash echo “export TERM=xterm-256color” >> ~/.bashrc “`
Notes: Terminals which support the 24-bit colour mode include (but are not limited to): iTerm2 (OSX), Gnome Terminal (Linux).
Setting your ‘$TERM` environment variable as above gets you up to 256 colours, but if you then add the `colour_mode 16_777_216` configuration to your client application, it’s really a hit and miss affair. iTerm2 renders all the colours correctly as does Gnome Terminal. Terminator (Linux) goes crazy though and defaults to 16 colours despite the ‘$TERM` setting. This area needs more work in Vedeu.
Instance Attribute Summary collapse
- #attributes ⇒ Hash readonly
Class Method Summary collapse
Instance Method Summary collapse
-
#background ⇒ Vedeu::Background
Returns the background colour as a Vedeu::Background.
-
#background=(value) ⇒ String
Converts the value into a Vedeu::Background.
-
#defaults ⇒ Hash<Symbol => NilClass>
private
The default values for a new instance of this class.
-
#eql?(other) ⇒ Boolean
(also: #==)
An object is equal when its values are the same.
-
#foreground ⇒ Vedeu::Foreground
Returns the foreground colour as a Vedeu::Foreground.
-
#foreground=(value) ⇒ String
Converts the value into a Vedeu::Foreground.
-
#initialize(attributes = {}) ⇒ Vedeu::Colour
constructor
Returns a new instance of Vedeu::Colour.
-
#to_s ⇒ String
Returns both or either of the converted attributes into a single escape sequence.
Constructor Details
#initialize(attributes = {}) ⇒ Vedeu::Colour
Returns a new instance of Vedeu::Colour.
66 67 68 |
# File 'lib/vedeu/output/colour.rb', line 66 def initialize(attributes = {}) @attributes = defaults.merge!(attributes) end |
Instance Attribute Details
#attributes ⇒ Hash (readonly)
37 38 39 |
# File 'lib/vedeu/output/colour.rb', line 37 def attributes @attributes end |
Class Method Details
.coerce(value) ⇒ Object
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/vedeu/output/colour.rb', line 41 def self.coerce(value) return value if value.is_a?(self) return new unless value.is_a?(Hash) if value[:colour] return value[:colour] if value[:colour].is_a?(self) return new unless value[:colour].is_a?(Hash) return new(value[:colour]) if value[:colour][:background] || value[:colour][:foreground] elsif value[:background] || value[:foreground] new(value) else new end end |
Instance Method Details
#background ⇒ Vedeu::Background
Returns the background colour as a Vedeu::Background.
73 74 75 |
# File 'lib/vedeu/output/colour.rb', line 73 def background @background ||= Vedeu::Background.coerce(attributes[:background]) end |
#background=(value) ⇒ String
Converts the value into a Vedeu::Background.
81 82 83 |
# File 'lib/vedeu/output/colour.rb', line 81 def background=(value) @background = @attributes[:background] = Vedeu::Background.coerce(value) end |
#defaults ⇒ Hash<Symbol => NilClass> (private)
The default values for a new instance of this class.
123 124 125 126 127 128 |
# File 'lib/vedeu/output/colour.rb', line 123 def defaults { background: '', foreground: '', } end |
#eql?(other) ⇒ Boolean Also known as: ==
An object is equal when its values are the same.
89 90 91 92 |
# File 'lib/vedeu/output/colour.rb', line 89 def eql?(other) self.class == other.class && background == other.background && foreground == other.foreground end |
#foreground ⇒ Vedeu::Foreground
Returns the foreground colour as a Vedeu::Foreground.
98 99 100 |
# File 'lib/vedeu/output/colour.rb', line 98 def foreground @foreground ||= Vedeu::Foreground.coerce(attributes[:foreground]) end |
#foreground=(value) ⇒ String
Converts the value into a Vedeu::Foreground.
106 107 108 |
# File 'lib/vedeu/output/colour.rb', line 106 def foreground=(value) @foreground = @attributes[:foreground] = Vedeu::Foreground.coerce(value) end |
#to_s ⇒ String
Returns both or either of the converted attributes into a single escape sequence.
114 115 116 |
# File 'lib/vedeu/output/colour.rb', line 114 def to_s foreground.to_s + background.to_s end |