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.

Parameters:

  • message (String)

    The message to emphasize.

  • style (String) (defaults to: "bright")

    The style to use.

Returns:

  • (String)

    The emphasized message.



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.

Parameters:

  • message (String)

    The message to format.

  • suffix (Object)

    If not nil or false, a suffix to add to the message. true means to add \n.

  • indented (Object)

    If not nil or false, the width to use for indentation. true means to use the current indentation, a negative value of -x will indent of x absolute spaces.

  • wrap (Object)

    If not nil or false, the maximum length of a line. true means the current line width.

  • plain (Boolean)

    If ignore color markers into the message.

Returns:

  • (String)

    The formatted message.

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.

Parameters:

  • message (String)

    The message to format.

  • width (Fixnum)

    The screen width. If true, it is automatically computed.

  • go_up (Boolean)

    If go up one line before formatting.

  • plain (Boolean)

    If ignore color markers into the message.

Returns:

  • (String)

    The formatted message.



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.

Parameters:

  • message (String)

    The message to indent.

  • width (Fixnum) (defaults to: true)

    The indentation width. true means to use the current indentation, a negative value of -x will indent of x absolute spaces. nil or false will skip indentation.

  • newline_separator (String) (defaults to: "\n")

    The character used for newlines.

Returns:

  • (String)

    The indented 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.

Returns:

  • (Fixnum)

    The new indentation width.



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.

Parameters:

  • width (Fixnum)

    The new width.

  • is_absolute (Boolean) (defaults to: false)

    If the new width should not be added to the current one but rather replace it.

Returns:

  • (Fixnum)

    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.

Parameters:

  • width (Fixnum) (defaults to: 3)

    The new width.

  • is_absolute (Boolean) (defaults to: false)

    If the new width should not be added to the current one but rather replace it.

Returns:

  • (Fixnum)

    The new indentation width.



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.

Parameters:

  • message (String)

    The message to wrap.

  • width (Fixnum) (defaults to: nil)

    The maximum width of a line. Default to the current line width.

Returns:

  • (String)

    The wrapped message.



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