Class: Vedeu::Render
- Inherits:
-
Object
- Object
- Vedeu::Render
- Defined in:
- lib/vedeu/support/render.rb
Instance Attribute Summary collapse
-
#interface ⇒ Object
readonly
private
Returns the value of attribute interface.
Class Method Summary collapse
-
.call(interface) ⇒ String
private
Attempts to convert the provided interface object with associated lines, streams, colours, styles, etc, into a single string containing all content and escape sequences.
Instance Method Summary collapse
-
#exceeds_width?(line) ⇒ TrueClass|FalseClass
private
private
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.
-
#height ⇒ Fixnum
private
private
Provides the currently available height of the interface.
-
#initialize(interface) ⇒ Render
constructor
Initializes a new Render object with the provided interface.
-
#lines ⇒ Array
private
private
Provides a collection of lines associated with the interface.
-
#processed_lines ⇒ Array
private
private
The client application may have created a line that us too long for the interface.
-
#render ⇒ String
Produces a single string which contains all content and escape sequences required to render this interface to a position in the terminal window.
-
#truncate(text, value) ⇒ String
private
private
Truncates the provided text.
-
#width ⇒ Fixnum
private
private
Provides the currently available width of the interface.
Constructor Details
#initialize(interface) ⇒ Render
Initializes a new Render object with the provided interface.
19 20 21 |
# File 'lib/vedeu/support/render.rb', line 19 def initialize(interface) @interface = interface end |
Instance Attribute Details
#interface ⇒ Object (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.
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.
94 95 96 |
# File 'lib/vedeu/support/render.rb', line 94 def exceeds_width?(line) line.streams.map(&:text).join.size > width end |
#height ⇒ Fixnum (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.
120 121 122 |
# File 'lib/vedeu/support/render.rb', line 120 def height interface. end |
#lines ⇒ Array (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.
112 113 114 |
# File 'lib/vedeu/support/render.rb', line 112 def lines interface.lines end |
#processed_lines ⇒ Array (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.
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 |
#render ⇒ String
Produces a single string which contains all content and escape sequences required to render this interface to a position in the terminal window.
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.
104 105 106 |
# File 'lib/vedeu/support/render.rb', line 104 def truncate(text, value) text.chomp.slice(0...value) end |
#width ⇒ Fixnum (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.
128 129 130 |
# File 'lib/vedeu/support/render.rb', line 128 def width interface. end |