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

Constructor Details

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

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

Parameters:

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

Options Hash (attributes):



58
59
60
61
62
63
64
# File 'lib/vedeu/models/views/char.rb', line 58

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

Instance Attribute Details

#attributesHash (readonly)

Returns:

  • (Hash)


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

def attributes
  @attributes
end

#borderNilClass|Symbol

Returns:

  • (NilClass|Symbol)


31
32
33
# File 'lib/vedeu/models/views/char.rb', line 31

def border
  @border
end

#nameString|Symbol

Returns:

  • (String|Symbol)


35
36
37
# File 'lib/vedeu/models/views/char.rb', line 35

def name
  @name
end

#parentVedeu::Views::Line

Returns:



39
40
41
# File 'lib/vedeu/models/views/char.rb', line 39

def parent
  @parent
end

#valueString

Returns:

  • (String)


112
113
114
115
116
# File 'lib/vedeu/models/views/char.rb', line 112

def value
  return @value unless border

  Vedeu::EscapeSequences::Esc.border { @value }
end

Instance Method Details

#backgroundVedeu::Colours::Background Originally defined in module Presentation::Colour

When the background colour for the model exists, return it, otherwise returns the parent background colour, or an empty Vedeu::Colours::Background.

#background=(value) ⇒ Vedeu::Colours::Background Originally defined in module Presentation::Colour

Allows the setting of the background colour by coercing the given value into a Vedeu::Colours::Background colour.

#cell?Boolean

Returns:

  • (Boolean)


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

def cell?
  false
end

#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)


78
79
80
# File 'lib/vedeu/models/views/char.rb', line 78

def chars
  []
end

#colourVedeu::Colours::Colour Originally defined in module Presentation::Colour

#colour=(value) ⇒ Vedeu::Colours::Colour Originally defined in module Presentation::Colour

Allows the setting of the model’s colour by coercing the given value into a Vedeu::Colours::Colour.

#colour_to_hashHash<Symbol => String> (private)

Returns:

  • (Hash<Symbol => String>)


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

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)


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

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

#foregroundVedeu::Colours::Foreground Originally defined in module Presentation::Colour

When the foreground colour for the model exists, return it, otherwise returns the parent foreground colour, or an empty Vedeu::Colours::Foreground.

#foreground=(value) ⇒ Vedeu::Colours::Foreground Originally defined in module Presentation::Colour

Allows the setting of the foreground colour by coercing the given value into a Vedeu::Colours::Foreground colour.

#interfaceVedeu::Interfaces::Interface



93
94
95
# File 'lib/vedeu/models/views/char.rb', line 93

def interface
  @interface ||= Vedeu.interfaces.by_name(name)
end

#parent_to_hashHash<Symbol => String> (private)

Returns:

  • (Hash<Symbol => String>)


170
171
172
173
174
175
176
177
178
# File 'lib/vedeu/models/views/char.rb', line 170

def parent_to_hash
  return {} unless parent

  {
    background: parent.background.to_s,
    foreground: parent.foreground.to_s,
    style:      parent.style.to_s,
  }
end

#positionVedeu::Geometry::Position



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

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:



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

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

#position?Boolean (private) Originally defined in module Presentation

Returns a boolean indicating the model has a position attribute.

Returns:

  • (Boolean)

#position_to_hashHash<Symbol => Fixnum> (private)

Returns:

  • (Hash<Symbol => Fixnum>)


181
182
183
184
185
186
# File 'lib/vedeu/models/views/char.rb', line 181

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

#render_colourString (private) Originally defined in module Presentation

Renders the colour attributes of the receiver and yields (to then render the styles).

Returns:

  • (String)

#render_positionString (private) Originally defined in module Presentation

Returns:

  • (String)

#render_styleString (private) Originally defined in module Presentation

Renders the style attributes of the receiver and yields (to then render the next model, or finally, the content).

Returns:

  • (String)

#styleVedeu::Presentation::Style Originally defined in module Presentation::Styles

When the style for the model exists, return it, otherwise returns the parent style, or an empty Vedeu::Presentation::Style.

#style=(value) ⇒ Vedeu::Presentation::Style Originally defined in module Presentation::Styles

Allows the setting of the style by coercing the given value into a Vedeu::Presentation::Style.

#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>)


140
141
142
143
144
145
146
147
148
149
150
# File 'lib/vedeu/models/views/char.rb', line 140

def to_hash
  {
    border:   border.to_s,
    colour:   colour_to_hash,
    name:     name.to_s,
    parent:   parent_to_hash,
    position: position_to_hash,
    style:    style.to_s,
    value:    value,
  }
end

#to_html(options = {}) ⇒ String

Parameters:

Returns:

  • (String)


155
156
157
# File 'lib/vedeu/models/views/char.rb', line 155

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

#to_sString Also known as: to_str Originally defined in module Presentation

Converts the colours and styles to escape sequences, and when the parent model has previously set the colour and style, reverts back to that for consistent formatting.

Returns:

  • (String)

    An escape sequence with value interpolated.

#xFixnum|NilClass

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

Returns:

  • (Fixnum|NilClass)


121
122
123
# File 'lib/vedeu/models/views/char.rb', line 121

def x
  position.x if position
end

#yFixnum|NilClass

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

Returns:

  • (Fixnum|NilClass)


128
129
130
# File 'lib/vedeu/models/views/char.rb', line 128

def y
  position.y if position
end