Class: Vedeu::Render

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(interface) ⇒ Render

Initializes a new Render object with the provided interface.

Parameters:



19
20
21
# File 'lib/vedeu/support/render.rb', line 19

def initialize(interface)
  @interface = interface
end

Instance Attribute Details

#interfaceObject (readonly, private)

Returns the value of attribute interface.



41
42
43
# File 'lib/vedeu/support/render.rb', line 41

def interface
  @interface
end

Class Method Details

.call(interface) ⇒ String

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Attempts to convert the provided interface object with associated lines, streams, colours, styles, etc, into a single string containing all content and escape sequences.

Parameters:

Returns:

  • (String)


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

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

Instance Method Details

#exceeds_width?(line) ⇒ TrueClass|FalseClass (private)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Converts all streams within a line into a single line of text to then check that this line (without formatting, as that is not visible) exceeds the width of the interface.

Parameters:

Returns:

  • (TrueClass|FalseClass)


94
95
96
# File 'lib/vedeu/support/render.rb', line 94

def exceeds_width?(line)
  line.streams.map(&:text).join.size > width
end

#heightFixnum (private)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Provides the currently available height of the interface.

Returns:



120
121
122
# File 'lib/vedeu/support/render.rb', line 120

def height
  interface.viewport_height
end

#linesArray (private)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Provides a collection of lines associated with the interface.

Returns:

  • (Array)


112
113
114
# File 'lib/vedeu/support/render.rb', line 112

def lines
  interface.lines
end

#processed_linesArray (private)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

The client application may have created a line that us too long for the interface. This code tries to truncate streams whilst preserving styles and colours.

Returns:

  • (Array)


49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/vedeu/support/render.rb', line 49

def processed_lines
  return [] unless lines.any? { |line| line.streams.any? }

  lines.map do |line|
    if exceeds_width?(line)
      line_length = 0
      processed   = []
      line.streams.each do |stream|
        next if stream.text.empty?

        if (line_length += stream.text.size) >= width
          remainder = width - line_length

          processed << Stream.new({
                         colour: stream.colour.attributes,
                         style:  stream.style.values,
                         text:   truncate(stream.text, remainder),
                       })

        else
          processed << stream

        end
      end

      Line.new({
        colour:  line.colour.attributes,
        streams: processed,
        style:   line.style.values,
      })

    else
      line

    end
  end
end

#renderString

Produces a single string which contains all content and escape sequences required to render this interface to a position in the terminal window.

Returns:

  • (String)


27
28
29
30
31
32
33
34
35
36
37
# File 'lib/vedeu/support/render.rb', line 27

def render
  out = [ Clear.call(interface) ]
  processed_lines.each_with_index do |line, index|
    if index + 1 <= height
      out << interface.origin(index)
      out << line.to_s
    end
  end
  out << interface.cursor
  out.join
end

#truncate(text, value) ⇒ String (private)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Truncates the provided text.

Parameters:

  • text (String)

    The text to be truncated.

  • value (Fixnum)

    The total length of the text after truncation.

Returns:

  • (String)


104
105
106
# File 'lib/vedeu/support/render.rb', line 104

def truncate(text, value)
  text.chomp.slice(0...value)
end

#widthFixnum (private)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Provides the currently available width of the interface.

Returns:



128
129
130
# File 'lib/vedeu/support/render.rb', line 128

def width
  interface.viewport_width
end