Class: Vedeu::Output

Inherits:
Object
  • Object
show all
Defined in:
lib/vedeu/output/output.rb

Overview

Sends the interface to the terminal or output device.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(interface) ⇒ Output

Return a new instance of Output.

Parameters:



28
29
30
# File 'lib/vedeu/output/output.rb', line 28

def initialize(interface)
  @interface = interface
end

Instance Attribute Details

#interfaceObject (readonly, private)

Returns the value of attribute interface.



60
61
62
# File 'lib/vedeu/output/output.rb', line 60

def interface
  @interface
end

Class Method Details

.clear(interface) ⇒ Array|String

Clears the area defined by the interface.

Returns:

  • (Array|String)

See Also:



11
12
13
# File 'lib/vedeu/output/output.rb', line 11

def self.clear(interface)
  new(interface).clear
end

.render(interface) ⇒ Array|String

Writes content (the provided interface object with associated lines, streams, colours and styles) to the area defined by the interface.

Returns:

  • (Array|String)

See Also:



20
21
22
# File 'lib/vedeu/output/output.rb', line 20

def self.render(interface)
  new(interface).render
end

Instance Method Details

#char_builder(value, iy, ix) ⇒ Vedeu::Char (private)

Parameters:

Returns:



122
123
124
125
126
127
# File 'lib/vedeu/output/output.rb', line 122

def char_builder(value, iy, ix)
  Vedeu::Char.new({ value:    value,
                    colour:   interface.colour,
                    style:    interface.style,
                    position: origin(iy, ix) })
end

#clearArray

Clear the view and send to the terminal.

Returns:

  • (Array)


35
36
37
38
39
40
41
42
43
# File 'lib/vedeu/output/output.rb', line 35

def clear
  if Vedeu::Configuration.drb?
    Vedeu.trigger(:_drb_store_output_, virtual_clear)

    HTMLRenderer.to_file(VirtualBuffer.retrieve)
  end

  Terminal.output(Renderer.render(virtual_clear))
end

#origin(y_index = 0, x_index = 0) ⇒ Vedeu::Position (private)

Returns the position of the cursor at the top-left coordinate, relative to the interface’s position.

Parameters:

  • y_index (Fixnum) (defaults to: 0)
  • x_index (Fixnum) (defaults to: 0)

Returns:



135
136
137
# File 'lib/vedeu/output/output.rb', line 135

def origin(y_index = 0, x_index = 0)
  Vedeu::Position.new(virtual_y[y_index], virtual_x[x_index])
end

#renderArray

Send the view to the terminal.

Returns:

  • (Array)


48
49
50
51
52
53
54
55
56
# File 'lib/vedeu/output/output.rb', line 48

def render
  if Vedeu::Configuration.drb?
    Vedeu.trigger(:_drb_store_output_, virtual_view)

    HTMLRenderer.to_file(VirtualBuffer.retrieve)
  end

  Terminal.output(Renderer.render(virtual_view, interface.cursor))
end

#viewportvoid (private)

This method returns an undefined value.



114
115
116
# File 'lib/vedeu/output/output.rb', line 114

def viewport
  @viewport ||= Vedeu::Viewport.new(interface).render
end

#virtual_clearArray<Array<Vedeu::Char>> (private)

Note:

omg!

For each visible line of the interface, set the foreground and background colours to those specified when the interface was defined, then starting write space characters over the area which the interface occupies.

Returns:



70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/vedeu/output/output.rb', line 70

def virtual_clear
  Vedeu.log(type: :output, message: "Clearing: '#{interface.name}'")

  out = []
  interface.height.times do |iy|
    row = []
    interface.width.times do |ix|
      row << char_builder(' ', iy, ix)
    end
    out << row
  end
  out
end

#virtual_viewArray<Array<Vedeu::Char>> (private)

Note:

omg!

Builds up a virtual view; a grid of Vedeu::Char objects- each one holding one character along with its colour, style and position attributes.

Returns:



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/vedeu/output/output.rb', line 91

def virtual_view
  out = [ virtual_clear ]

  Vedeu.log(type: :output, message: "Rendering: '#{interface.name}'")

  viewport.each_with_index do |line, iy|
    row = []
    line.each_with_index do |char, ix|
      row << if char.is_a?(Vedeu::Char) && (char.x != ix || char.y != iy)
        char.position = origin(iy, ix)
        char

      else
        char_builder(char, iy, ix)

      end
    end
    out << row
  end
  out
end

#virtual_xArray (private)

Provides a virtual x position within the interface’s dimensions.

Examples:

# left = 9
# right = 13
# virtual_x # => [9, 10, 11, 12]

Returns:

  • (Array)


159
160
161
# File 'lib/vedeu/output/output.rb', line 159

def virtual_x
  @virtual_x ||= (interface.left...interface.right).to_a
end

#virtual_yArray (private)

Provides a virtual y position within the interface’s dimensions.

Examples:

# top = 3
# bottom = 6
# virtual_y # => [3, 4, 5]

Returns:

  • (Array)


147
148
149
# File 'lib/vedeu/output/output.rb', line 147

def virtual_y
  @virtual_y ||= (interface.top...interface.bottom).to_a
end