Class: Vedeu::Views::Char

Inherits:
Object
  • Object
show all
Includes:
Comparable, Presentation
Defined in:
lib/vedeu/models/views/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

#render_colour, #render_position, #render_style, #to_s

Methods included from Presentation::Styles

#style, #style=

Methods included from Presentation::Colour

#background, #background=, #colour, #colour=, #foreground, #foreground=

Constructor Details

#initialize(attributes = {}) ⇒ Vedeu::Views::Char

Returns a new instance of Vedeu::Views::Char.

Parameters:

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

Options Hash (attributes):



43
44
45
46
47
48
# File 'lib/vedeu/models/views/char.rb', line 43

def initialize(attributes = {})
  @attributes = attributes
  @border     = @attributes[:border]
  @parent     = @attributes[:parent]
  @value      = @attributes[:value]
end

Instance Attribute Details

#attributesHash (readonly)

Returns:

  • (Hash)


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

def attributes
  @attributes
end

#borderNilClass|Symbol

Returns:

  • (NilClass|Symbol)


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

def border
  @border
end

#parentVedeu::Views::Line

Returns:



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

def parent
  @parent
end

#valueString

Returns:

  • (String)


86
87
88
89
90
91
92
93
94
# File 'lib/vedeu/models/views/char.rb', line 86

def value
  if border
    Vedeu::Esc.border { @value }

  else
    @value

  end
end

Instance Method Details

#charsArray

When Output::Viewport#show has less lines that required to fill the visible area of the interface, it creates a line that contains a single Vedeu::Views::Char containing a space (0x20); later, attempts to call ‘#chars` on an expected Line and fail; this method fixes that.

Returns:

  • (Array)


57
58
59
# File 'lib/vedeu/models/views/char.rb', line 57

def chars
  []
end

#colour_to_hashHash<Symbol => String> (private)

Returns:

  • (Hash<Symbol => String>)


139
140
141
142
143
144
# File 'lib/vedeu/models/views/char.rb', line 139

def colour_to_hash
  {
    background: background.to_s,
    foreground: foreground.to_s,
  }
end

#eql?(other) ⇒ Boolean Also known as: ==

An object is equal when its values are the same.

Parameters:

Returns:

  • (Boolean)


65
66
67
68
# File 'lib/vedeu/models/views/char.rb', line 65

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

#parent_to_hashHash<Symbol => String> (private)

Returns:

  • (Hash<Symbol => String>)


147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/vedeu/models/views/char.rb', line 147

def parent_to_hash
  if parent
    {
      background: parent.background.to_s,
      foreground: parent.foreground.to_s,
      style:      parent.style.to_s,
    }

  else
    {}

  end
end

#positionVedeu::Geometry::Position



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

def position
  @position = Vedeu::Geometry::Position.coerce(@attributes[:position])
end

#position=(value) ⇒ Vedeu::Geometry::Position

Sets the position of the Vedeu::Views::Char.

Parameters:

Returns:



80
81
82
83
# File 'lib/vedeu/models/views/char.rb', line 80

def position=(value)
  @position = @attributes[:position] = Vedeu::Geometry::Position
                                       .coerce(value)
end

#position_to_hashHas<Symbol => Fixnum> (private)

Returns:

  • (Has<Symbol => Fixnum>)


162
163
164
165
166
167
# File 'lib/vedeu/models/views/char.rb', line 162

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

#to_hashHash<Symbol => Hash, String>

Note:

From this hash we should be able to construct a new instance of Vedeu::Views::Char, however, at the moment, ‘:parent` cannot be coerced.

Returns a Hash of all the values before coercion.

Returns:

  • (Hash<Symbol => Hash, String>)


118
119
120
121
122
123
124
125
126
127
# File 'lib/vedeu/models/views/char.rb', line 118

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(options = {}) ⇒ String

Parameters:

Returns:

  • (String)


132
133
134
# File 'lib/vedeu/models/views/char.rb', line 132

def to_html(options = {})
  @to_html ||= Vedeu::Views::HTMLChar.render(self, options)
end

#xFixnum|NilClass

Returns the x position for the Vedeu::Views::Char when set.

Returns:

  • (Fixnum|NilClass)


99
100
101
# File 'lib/vedeu/models/views/char.rb', line 99

def x
  position.x if position
end

#yFixnum|NilClass

Returns the y position for the Vedeu::Views::Char when set.

Returns:

  • (Fixnum|NilClass)


106
107
108
# File 'lib/vedeu/models/views/char.rb', line 106

def y
  position.y if position
end