Class: Vedeu::Char
- Inherits:
-
Object
- Object
- Vedeu::Char
- Includes:
- Presentation
- Defined in:
- lib/vedeu/models/char.rb
Overview
A Char represents a single character of the terminal. It is a container for the a single character in a Stream.
Though a multi-character String can be passed as a value, only the first character is returned in the escape sequence.
Instance Attribute Summary collapse
Instance Method Summary collapse
- #chars ⇒ Array
- #colour_to_hash ⇒ Hash private
-
#defaults ⇒ Hash
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.
-
#initialize(attributes = {}) ⇒ Char
constructor
Returns a new instance of Vedeu::Char.
- #inspect ⇒ String
- #parent_to_hash ⇒ Hash private
- #position ⇒ Vedeu::Position
-
#position=(value) ⇒ Vedeu::Position
Sets the position of the Char.
- #position_to_hash ⇒ Hash private
-
#to_hash ⇒ Hash
Returns a Hash of all the values before coercion.
- #to_html ⇒ String
- #to_json ⇒ String
-
#x ⇒ Fixnum|NilClass
Returns the x position for the Char if set.
-
#y ⇒ Fixnum|NilClass
Returns the y position for the Char if set.
Methods included from Presentation
#_colour, #_style, #background, #background=, #colour, #colour=, #foreground, #foreground=, #parent_background, #parent_colour, #parent_foreground, #parent_style, #render_colour, #render_position, #render_style, #style, #style=, #to_s
Constructor Details
#initialize(attributes = {}) ⇒ Char
Returns a new instance of Vedeu::Char.
44 45 46 47 48 49 50 |
# File 'lib/vedeu/models/char.rb', line 44 def initialize(attributes = {}) @attributes = defaults.merge!(attributes) @border = @attributes[:border] @parent = @attributes[:parent] @value = @attributes[:value] end |
Instance Attribute Details
#attributes ⇒ Hash (readonly)
27 28 29 |
# File 'lib/vedeu/models/char.rb', line 27 def attributes @attributes end |
#border ⇒ NilClass|Symbol
19 20 21 |
# File 'lib/vedeu/models/char.rb', line 19 def border @border end |
#value ⇒ String
91 92 93 94 95 96 97 98 99 |
# File 'lib/vedeu/models/char.rb', line 91 def value if border Vedeu::Esc.border { @value } else @value end end |
Instance Method Details
#chars ⇒ Array
When Viewport#padded_lines has less lines that required to fill the visible area of the interface, it creates a line that contains a single Vedeu::Char containing a space (0x20); later, Viewport#padded_columns will attempts to call ‘#chars` on an expected Line and fail; this method fixes that.
59 60 61 |
# File 'lib/vedeu/models/char.rb', line 59 def chars [] end |
#colour_to_hash ⇒ Hash (private)
146 147 148 149 150 151 |
# File 'lib/vedeu/models/char.rb', line 146 def colour_to_hash { background: background.to_s, foreground: foreground.to_s, } end |
#defaults ⇒ Hash (private)
The default values for a new instance of this class.
156 157 158 159 160 161 162 163 164 165 |
# File 'lib/vedeu/models/char.rb', line 156 def defaults { border: nil, colour: nil, parent: nil, position: nil, style: nil, value: '', } end |
#eql?(other) ⇒ Boolean Also known as: ==
An object is equal when its values are the same.
67 68 69 |
# File 'lib/vedeu/models/char.rb', line 67 def eql?(other) self.class == other.class && value == other.value end |
#inspect ⇒ String
73 74 75 |
# File 'lib/vedeu/models/char.rb', line 73 def inspect "<Vedeu::Char '#{Vedeu::Esc.escape(to_s)}'>" end |
#parent_to_hash ⇒ Hash (private)
168 169 170 171 172 173 174 |
# File 'lib/vedeu/models/char.rb', line 168 def parent_to_hash { background: parent_background.to_s, foreground: parent_foreground.to_s, style: parent_style.to_s, } end |
#position ⇒ Vedeu::Position
78 79 80 |
# File 'lib/vedeu/models/char.rb', line 78 def position @position ||= Vedeu::Position.coerce(attributes[:position]) end |
#position=(value) ⇒ Vedeu::Position
Sets the position of the Char.
86 87 88 |
# File 'lib/vedeu/models/char.rb', line 86 def position=(value) @position = Vedeu::Position.coerce(value) end |
#position_to_hash ⇒ Hash (private)
177 178 179 180 181 182 |
# File 'lib/vedeu/models/char.rb', line 177 def position_to_hash { y: y, x: x, } end |
#to_hash ⇒ Hash
From this hash we should be able to construct a new instance of Vedeu::Char, however, at the moment, ‘:parent` cannot be coerced.
Returns a Hash of all the values before coercion.
122 123 124 125 126 127 128 129 130 131 |
# File 'lib/vedeu/models/char.rb', line 122 def to_hash { border: border.to_s, colour: colour_to_hash, parent: parent_to_hash, position: position_to_hash, style: style.to_s, value: value, } end |
#to_html ⇒ String
134 135 136 |
# File 'lib/vedeu/models/char.rb', line 134 def to_html @to_html ||= Vedeu::HTMLChar.render(self) end |
#to_json ⇒ String
139 140 141 |
# File 'lib/vedeu/models/char.rb', line 139 def to_json @to_json ||= JSON.generate(to_hash) end |
#x ⇒ Fixnum|NilClass
Returns the x position for the Char if set.
104 105 106 |
# File 'lib/vedeu/models/char.rb', line 104 def x position.x if position end |
#y ⇒ Fixnum|NilClass
Returns the y position for the Char if set.
111 112 113 |
# File 'lib/vedeu/models/char.rb', line 111 def y position.y if position end |