Class: Vedeu::Output::Text

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

Overview

Present a string (or object responding to ‘to_s`).

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(value = '', options = {}) ⇒ Vedeu::Output::Text

Returns a new instance of Vedeu::Output::Text.

Parameters:

  • value (String) (defaults to: '')
  • options (Hash) (defaults to: {})

Options Hash (options):

  • anchor (Symbol)

    See #anchor

  • background (String)
  • colour (Hash|NilClass)
  • foreground (String)
  • model (Object)
    Vedeu::Views::View|Vedeu::Views::Line|Vedeu::Views::Stream
  • mode (Object) — default: see Vedeu::Output::Wordwrap#mode
  • pad (String)
  • width (Integer)


33
34
35
36
# File 'lib/vedeu/output/text.rb', line 33

def initialize(value = '', options = {})
  @value   = value
  @options = defaults.merge!(options)
end

Instance Attribute Details

#optionsHash (readonly, protected)

Returns:

  • (Hash)


76
77
78
# File 'lib/vedeu/output/text.rb', line 76

def options
  @options
end

#valueString (readonly, protected)

Returns:

  • (String)


72
73
74
# File 'lib/vedeu/output/text.rb', line 72

def value
  @value
end

Class Method Details

.add(value = '', options = {}) ⇒ Object

See Also:



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

def self.add(value = '', options = {})
  new(value, options).add
end

.with(value = '', options = {}) ⇒ Object

See Also:



15
16
17
# File 'lib/vedeu/output/text.rb', line 15

def self.with(value = '', options = {})
  new(value, options).aligned
end

Instance Method Details

#addvoid

This method returns an undefined value.

Adds the content to the model.



58
59
60
61
62
63
64
65
66
# File 'lib/vedeu/output/text.rb', line 58

def add
  if wrap?
    model.add(wrapped)

  else
    model.add(content)

  end
end

#alignedString

Aligns the value.

Returns:

  • (String)


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

def aligned
  return string unless width

  return truncated if truncate?

  case anchor
  when :align, :left, :text then left
  when :centre, :center     then centre
  when :right               then right
  else
    left
  end
end

#anchorSymbol (private)

Returns One of :align, :centre, :center, :left, :right, :text.

Returns:

  • (Symbol)

    One of :align, :centre, :center, :left, :right, :text



82
83
84
# File 'lib/vedeu/output/text.rb', line 82

def anchor
  options[:anchor]
end

#centreString (private)

The string padded to width, centralized.

Returns:

  • (String)


89
90
91
# File 'lib/vedeu/output/text.rb', line 89

def centre
  string.center(width, pad)
end

#clientObject (private)

Returns:

  • (Object)


94
95
96
# File 'lib/vedeu/output/text.rb', line 94

def client
  options[:client]
end

#colourvoid (private)

This method returns an undefined value.

If a colour, background or foreground option is set, use them as the colour settings for the new Vedeu::Views::Stream.



102
103
104
105
106
107
108
109
110
# File 'lib/vedeu/output/text.rb', line 102

def colour
  if options[:colour] || options[:background] || options[:foreground]
    Vedeu::Colours::Colour.coerce(options)

  else
    model.colour

  end
end

#contentVedeu::Views::Line|Vedeu::Views::Stream (private)

Returns either a Vedeu::Views::Line or Vedeu::Views::Stream containing the text value.



116
117
118
119
120
121
122
# File 'lib/vedeu/output/text.rb', line 116

def content
  return stream unless model.is_a?(Vedeu::Views::View)

  stream.parent = line
  line.add(stream)
  line
end

#defaultsHash<Symbol => NilClass, String, Symbol> (private)

The default values for a new instance of this class.

Returns:

  • (Hash<Symbol => NilClass, String, Symbol>)


127
128
129
130
131
132
133
134
135
136
137
# File 'lib/vedeu/output/text.rb', line 127

def defaults
  {
    client: nil,
    anchor: :left,
    colour: nil,
    model:  nil,
    mode:   :default,
    pad:    ' ',
    width:  nil,
  }
end

#leftString (private)

The string padded to width, left justified.

Returns:

  • (String)


142
143
144
# File 'lib/vedeu/output/text.rb', line 142

def left
  string.ljust(width, pad)
end

#lineVedeu::Views::Line (private)

Returns:



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

def line
  @line ||= Vedeu::Views::Line.build(parent: parent, client: client)
end

#modeSymbol (private)

Returns:

  • (Symbol)

See Also:



163
164
165
# File 'lib/vedeu/output/text.rb', line 163

def mode
  options[:mode]
end

#modelVedeu::Views::View| Vedeu::Views::Line| Vedeu::Null::Generic| Vedeu::Views::Stream (private)

Returns the model option when set.



157
158
159
# File 'lib/vedeu/output/text.rb', line 157

def model
  @model ||= options[:model] || Vedeu::Null::Generic.new
end

#padString (private)

The character to use for padding the string.

Returns:

  • (String)


170
171
172
# File 'lib/vedeu/output/text.rb', line 170

def pad
  options[:pad]
end

#parentvoid (private)

This method returns an undefined value.

Returns the parent for the new Vedeu::Views::Stream.



177
178
179
# File 'lib/vedeu/output/text.rb', line 177

def parent
  model.is_a?(Vedeu::Views::Stream) ? model.parent : model
end

#rightString (private)

The string padded to width, right justified.

Returns:

  • (String)


184
185
186
# File 'lib/vedeu/output/text.rb', line 184

def right
  string.rjust(width, pad)
end

#streamvoid (private)

This method returns an undefined value.

Builds and returns a new Vedeu::Views::Stream.



191
192
193
194
195
196
197
# File 'lib/vedeu/output/text.rb', line 191

def stream
  @stream ||= Vedeu::Views::Stream.build(client: client,
                                         colour: colour,
                                         parent: parent,
                                         style:  style,
                                         value:  aligned)
end

#stringString (private)

The string, coerced.

Returns:

  • (String)


202
203
204
# File 'lib/vedeu/output/text.rb', line 202

def string
  value.to_s
end

#stylevoid (private)

This method returns an undefined value.

Returns the model’s styles.



209
210
211
# File 'lib/vedeu/output/text.rb', line 209

def style
  model.style
end

#truncate?Boolean (private)

Return a boolean indicating that the string is greater than the width.

Returns:

  • (Boolean)


217
218
219
# File 'lib/vedeu/output/text.rb', line 217

def truncate?
  string.size > width
end

#truncatedString (private)

Return the string truncated to the width.

Returns:

  • (String)


224
225
226
# File 'lib/vedeu/output/text.rb', line 224

def truncated
  string.slice(0, width)
end

#widthFixnum (private)

Return the width.

Returns:

  • (Fixnum)


231
232
233
# File 'lib/vedeu/output/text.rb', line 231

def width
  options[:width]
end

#wrap?Boolean (private)

Return a boolean indicating whether the string should be wrapped.

Returns:

  • (Boolean)


239
240
241
# File 'lib/vedeu/output/text.rb', line 239

def wrap?
  options[:mode] == :wrap
end

#wrappedVedeu::Views::Lines (private)

Return the content as wrapped lines.

Returns:



246
247
248
# File 'lib/vedeu/output/text.rb', line 246

def wrapped
  Vedeu::Output::Wordwrap.for(string, options)
end