Module: Bovem::ConsoleMethods::Output

Included in:
Bovem::Console
Defined in:
lib/bovem/console.rb

Overview

Methods for formatting output messages.

Instance Method Summary collapse

Instance Method Details

#emphasize(message, style = "bright") ⇒ String

Embeds a message in a style.



244
245
246
# File 'lib/bovem/console.rb', line 244

def emphasize(message, style = "bright")
  "{mark=#{style}}#{message}{/mark}"
end

#format(message, suffix: "\n", indented: true, wrap: true, plain: false) ⇒ String

Formats a message.

You can style text by using {mark} and {/mark} syntax.

See Also:

  • #replace_markers


207
208
209
210
211
212
213
214
215
216
217
218
# File 'lib/bovem/console.rb', line 207

def format(message, suffix: "\n", indented: true, wrap: true, plain: false)
  rv = message

  rv = replace_markers(rv, plain) # Replace markers

  # Compute the real width available for the screen, if we both indent and wrap
  wrap = compute_wrap(indented) if wrap.is_a?(TrueClass)

  rv = indent(wrap(rv, wrap), indented) # Wrap & Indent
  rv += (suffix.is_a?(TrueClass) ? "\n" : suffix.ensure_string) if suffix # Add the suffix
  rv
end

#format_right(message, width: true, go_up: true, plain: false) ⇒ String

Formats a message to be written right-aligned.



227
228
229
230
231
232
233
234
235
236
237
# File 'lib/bovem/console.rb', line 227

def format_right(message, width: true, go_up: true, plain: false)
  message = replace_markers(message, plain)

  width = (width == true || width.to_integer < 1 ? line_width : to_integer)

  # Get padding
  padding = width - message.to_s.gsub(/(\e\[[0-9]*[a-z]?)|(\\n)/i, "").length

  # Return
  "#{go_up ? "\e[A" : ""}\e[0G\e[#{padding}C#{message}"
end

#indent(message, width = true, newline_separator = "\n") ⇒ String

Indents a message.



179
180
181
182
183
184
185
186
187
188
189
190
191
192
# File 'lib/bovem/console.rb', line 179

def indent(message, width = true, newline_separator = "\n")
  if width.to_integer != 0
    width = (width.is_a?(TrueClass) ? 0 : width.to_integer)
    width = width < 0 ? -width : @indentation + width

    rv = message.split(newline_separator).map do |line|
      (@indentation_string * width) + line
    end

    message = rv.join(newline_separator)
  end

  message
end

#reset_indentationFixnum

Resets indentation width to 0.



135
136
137
# File 'lib/bovem/console.rb', line 135

def reset_indentation
  @indentation = 0
end

#set_indentation(width, is_absolute = false) ⇒ Fixnum

Sets the new indentation width.



128
129
130
# File 'lib/bovem/console.rb', line 128

def set_indentation(width, is_absolute = false)
  @indentation = [(!is_absolute ? @indentation : 0) + width, 0].max.to_i
end

#with_indentation(width = 3, is_absolute = false) ⇒ Fixnum

Starts a indented region of text.



144
145
146
147
148
149
150
151
# File 'lib/bovem/console.rb', line 144

def with_indentation(width = 3, is_absolute = false)
  old = @indentation
  set_indentation(width, is_absolute)
  yield
  set_indentation(old, true)

  @indentation
end

#wrap(message, width = nil) ⇒ String

Wraps a message in fixed line width.



158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/bovem/console.rb', line 158

def wrap(message, width = nil)
  if width.to_integer <= 0
    message
  else
    width = (width == true || width.to_integer < 0 ? line_width : width.to_integer)

    rv = message.split("\n").map do |line|
      wrap_line(line, width)
    end

    rv.join("\n")
  end
end