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



52
53
54
55
56
57
# File 'lib/vedeu/models/views/char.rb', line 52

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

Instance Attribute Details

#attributesHash (readonly)

Returns:

  • (Hash)


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

def attributes
  @attributes
end

#borderNilClass|Symbol

Returns:

  • (NilClass|Symbol)


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

def border
  @border
end

#parentVedeu::Views::Line

Returns:



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

def parent
  @parent
end

#valueString

Returns:

  • (String)


100
101
102
103
104
# File 'lib/vedeu/models/views/char.rb', line 100

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)


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

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)


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

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


149
150
151
152
153
154
# File 'lib/vedeu/models/views/char.rb', line 149

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)


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

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.

#parent_to_hashHash<Symbol => String> (private)

Returns:

  • (Hash<Symbol => String>)


157
158
159
160
161
162
163
164
165
# File 'lib/vedeu/models/views/char.rb', line 157

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



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

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:



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

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


168
169
170
171
172
173
# File 'lib/vedeu/models/views/char.rb', line 168

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


128
129
130
131
132
133
134
135
136
137
# File 'lib/vedeu/models/views/char.rb', line 128

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)


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

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)


109
110
111
# File 'lib/vedeu/models/views/char.rb', line 109

def x
  position.x if position
end

#yFixnum|NilClass

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

Returns:

  • (Fixnum|NilClass)


116
117
118
# File 'lib/vedeu/models/views/char.rb', line 116

def y
  position.y if position
end