Class: Vedeu::Char

Inherits:
Object
  • Object
show all
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

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.

Parameters:

  • attributes (Hash) (defaults to: {})

Options Hash (attributes):



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

#attributesHash (readonly)

Returns:

  • (Hash)


27
28
29
# File 'lib/vedeu/models/char.rb', line 27

def attributes
  @attributes
end

#borderNilClass|Symbol

Returns:

  • (NilClass|Symbol)


19
20
21
# File 'lib/vedeu/models/char.rb', line 19

def border
  @border
end

#parentVedeu::Line

Returns:



23
24
25
# File 'lib/vedeu/models/char.rb', line 23

def parent
  @parent
end

#valueString

Returns:

  • (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

#charsArray

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.

Returns:

  • (Array)


59
60
61
# File 'lib/vedeu/models/char.rb', line 59

def chars
  []
end

#colour_to_hashHash (private)

Returns:

  • (Hash)


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

#defaultsHash (private)

The default values for a new instance of this class.

Returns:

  • (Hash)


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.

Parameters:

Returns:

  • (Boolean)


67
68
69
# File 'lib/vedeu/models/char.rb', line 67

def eql?(other)
  self.class == other.class && value == other.value
end

#inspectString

Returns:

  • (String)


73
74
75
# File 'lib/vedeu/models/char.rb', line 73

def inspect
  "<Vedeu::Char '#{Vedeu::Esc.escape(to_s)}'>"
end

#parent_to_hashHash (private)

Returns:

  • (Hash)


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

#positionVedeu::Position

Returns:



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.

Parameters:

Returns:



86
87
88
# File 'lib/vedeu/models/char.rb', line 86

def position=(value)
  @position = Vedeu::Position.coerce(value)
end

#position_to_hashHash (private)

Returns:

  • (Hash)


177
178
179
180
181
182
# File 'lib/vedeu/models/char.rb', line 177

def position_to_hash
  {
    y: y,
    x: x,
  }
end

#to_hashHash

Note:

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.

Returns:

  • (Hash)


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_htmlString

Returns:

  • (String)


134
135
136
# File 'lib/vedeu/models/char.rb', line 134

def to_html
  @to_html ||= Vedeu::HTMLChar.render(self)
end

#to_jsonString

Returns:

  • (String)


139
140
141
# File 'lib/vedeu/models/char.rb', line 139

def to_json
  @to_json ||= JSON.generate(to_hash)
end

#xFixnum|NilClass

Returns the x position for the Char if set.

Returns:

  • (Fixnum|NilClass)


104
105
106
# File 'lib/vedeu/models/char.rb', line 104

def x
  position.x if position
end

#yFixnum|NilClass

Returns the y position for the Char if set.

Returns:

  • (Fixnum|NilClass)


111
112
113
# File 'lib/vedeu/models/char.rb', line 111

def y
  position.y if position
end