Class: Vedeu::Renderers::Text

Inherits:
File
  • Object
show all
Includes:
Options
Defined in:
lib/vedeu/renderers/text.rb

Overview

Converts a grid of Cells objects or Cells::Char objects into a stream of characters without escape sequences.

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#optionsHash<Symbol => void> Originally defined in module Options

Returns:

  • (Hash<Symbol => void>)

Instance Method Details

#absent?(variable) ⇒ Boolean Originally defined in module Common

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.

Returns a boolean indicating whether a variable is nil or empty.

Parameters:

  • variable (String|Symbol|Array|Fixnum)

    The variable to check.

Returns:

#become(klass, attributes) ⇒ Class Originally defined in module Common

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 one class into another.

Parameters:

  • klass (Class)

    The class to become an instance of.

  • attributes (Hash)

    The attributes of klass.

Returns:

  • (Class)

    Returns a new instance of klass.

#boolean(value) ⇒ Boolean Originally defined in module Common

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.

Returns a boolean indicating the value was a boolean.

Parameters:

  • value (void)

Returns:

#boolean?(value) ⇒ Boolean Originally defined in module Common

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.

Returns a boolean indicating whether the value is a Boolean.

Parameters:

Returns:

#bufferArray<String> (private)

Returns:

  • (Array<String>)


39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/vedeu/renderers/text.rb', line 39

def buffer
  empty = Array.new(Vedeu.height) { Array.new(Vedeu.width) { ' ' } }

  output.each do |row|
    row.each do |char|
      next unless renderable?(char) &&
                  positionable?(char) &&
                  textual?(char)

      empty[char.position.y - 1][char.position.x - 1] = char.text
    end
  end

  empty
end

#clearString

Render a cleared output.

Returns:

  • (String)


18
19
20
# File 'lib/vedeu/renderers/text.rb', line 18

def clear
  render('')
end

#compressionString (private) Originally defined in module Options

Compresses the output depending on configuration.

Returns:

  • (String)

#compression?Boolean (private) Originally defined in module Options

Returns a boolean indicating whether the content should be compressed if compression is available.

Returns:

#contentString (private)

Combine all characters in a row to produce a line, then all lines should be terminated with ‘n`. Convert to an array of UTF8 codepoints, and any codepoint above 255 should be converted to a space.

Returns:

  • (String)


30
31
32
33
34
35
36
# File 'lib/vedeu/renderers/text.rb', line 30

def content
  return '' if string?(output) || absent?(output)

  buffer.map(&:join).join("\n").unpack('U*').map do |c|
    (c > 255) ? ' ' : c.chr
  end.join
end

#default_templateString (private) Originally defined in module Options

Returns:

  • (String)

#defaultsHash<Symbol => void> (private) Originally defined in module Options

The default values for a new instance of this class.

Returns:

  • (Hash<Symbol => void>)

#end_row_tagString (private) Originally defined in module Options

Returns:

  • (String)

#end_tagString (private) Originally defined in module Options

Returns:

  • (String)

#escape?(value) ⇒ Boolean Originally defined in module Common

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.

Returns a boolean indicating whether the value is an escape sequence object (e.g. Vedeu::Cells::Escape.)

Returns:

#falsy?(value) ⇒ Boolean Originally defined in module Common

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.

Returns a boolean indicating whether the value should be considered false.

Parameters:

  • value (void)

Returns:

#filenameString (private) Originally defined in module Options

Return the filename given in the options, (or use the default), and append a timestamp if the :timestamp option was set to true.

Returns:

  • (String)

#hash?(value) ⇒ Boolean Originally defined in module Common

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.

Returns a boolean indicating whether the value is a Hash.

Parameters:

  • value (Hash|void)

Returns:

#initialize(opts = {}) ⇒ void Originally defined in module Options

Returns a new instance of the class including this module.

Parameters:

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

Options Hash (opts):

  • content (String)

    Defaults to an empty string.

  • end_tag (String)

    Defaults to ‘</td>’.

  • end_row_tag (String)

    Defaults to ‘</tr>’.

  • filename (String)

    Provide a filename for the output. Defaults to ‘out’.

  • start_tag (String)

    Defaults to ‘<td’ (note the end of the tag is missing, this is so that inline styles can be added later).

  • start_row_tag (String)

    Defaults to ‘<tr>’.

  • template (String)
  • timestamp (Boolean)

    Append a timestamp to the filename.

  • write_file (Boolean)

    Whether to write the file to the given filename.

#line_model?Boolean Originally defined in module Common

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.

Returns a boolean indicating the model is a Views::Line.

Returns:

#numeric?(value) ⇒ Boolean Originally defined in module Common

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.

Returns a boolean indicating whether the value is a Fixnum.

Parameters:

  • value (Fixnum|void)

Returns:

#outputvoid (private) Originally defined in module Options

This method returns an undefined value.

#output?Boolean (private) Originally defined in module Options

Returns:

#positionable?(char) ⇒ Boolean (private)

Returns:



56
57
58
59
# File 'lib/vedeu/renderers/text.rb', line 56

def positionable?(char)
  char.respond_to?(:position) &&
  char.position.is_a?(Vedeu::Geometries::Position)
end

#present?(variable) ⇒ Boolean Originally defined in module Common

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.

Returns a boolean indicating whether a variable has a useful value.

Parameters:

  • variable (String|Symbol|Array|Fixnum)

    The variable to check.

Returns:

#render(output = '') ⇒ void Originally defined in module Options

This method returns an undefined value.

Parameters:

#renderable?(char) ⇒ Boolean (private)

Returns:



83
84
85
# File 'lib/vedeu/renderers/text.rb', line 83

def renderable?(char)
  renderables.include?(char.class)
end

#renderablesArray<Class> (private)

Returns:

  • (Array<Class>)


62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/vedeu/renderers/text.rb', line 62

def renderables
  [
    Vedeu::Cells::Border,
    Vedeu::Cells::BottomHorizontal,
    Vedeu::Cells::BottomLeft,
    Vedeu::Cells::BottomRight,
    Vedeu::Cells::Corner,
    Vedeu::Cells::Char,
    Vedeu::Cells::Clear,
    Vedeu::Cells::Empty,
    Vedeu::Cells::Horizontal,
    Vedeu::Cells::LeftVertical,
    Vedeu::Cells::RightVertical,
    Vedeu::Cells::TopHorizontal,
    Vedeu::Cells::TopLeft,
    Vedeu::Cells::TopRight,
    Vedeu::Cells::Vertical,
  ]
end

#snake_case(klass) ⇒ String Originally defined in module Common

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 a class name to a lowercase snake case string.

Examples:

snake_case(MyClassName) # => "my_class_name"
snake_case(NameSpaced::ClassName)
# => "name_spaced/class_name"

snake_case('MyClassName') # => "my_class_name"
snake_case(NameSpaced::ClassName)
# => "name_spaced/class_name"

Parameters:

  • klass (Module|Class|String)

Returns:

  • (String)

#start_row_tagString (private) Originally defined in module Options

Returns:

  • (String)

#start_tagString (private) Originally defined in module Options

Returns:

  • (String)

#stream_model?Boolean Originally defined in module Common

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.

Returns a boolean indicating the model is a Views::Stream.

Returns:

#string?(value) ⇒ Boolean Originally defined in module Common

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.

Returns a boolean indicating whether the value is a Fixnum.

Parameters:

  • value (String|void)

Returns:

#templateString (private) Originally defined in module Options

Returns:

  • (String)

#textual?(char) ⇒ Boolean (private)

Returns:



88
89
90
# File 'lib/vedeu/renderers/text.rb', line 88

def textual?(char)
  char.respond_to?(:text)
end

#timestampString (private) Originally defined in module Options

Return a timestamp for use as part of the filename if the :timestamp option was set to true, otherwise an empty string.

Returns:

  • (String)

#timestamp?Boolean (private) Originally defined in module Options

Returns:

#truthy?(value) ⇒ Boolean Originally defined in module Common

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.

Returns a boolean indicating whether the value should be considered true.

Parameters:

  • value (void)

Returns:

#view_model?Boolean Originally defined in module Common

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.

Returns a boolean indicating the model is a Views::View.

Returns:

#writeObject Originally defined in module Options

Raises:

  • (Vedeu::Error::NotImplemented)

    When a subclass of the current class actually implements the method. Usually an indicator that the subclass should be used instead of the current class.

#write_fileString (private) Originally defined in module Options

Render the output (either content or clearing) to a file.

Returns:

  • (String)

#write_file?Boolean (private) Originally defined in module Options

Returns a boolean indicating whether a file should be written.

Returns: