module Bovem::ConsoleMethods::Output

Methods for formatting output messages.

Public Instance Methods

emphasize(message, style = "bright") click to toggle source

Embeds a message in a style.

@param message [String] The message to emphasize. @param style [String] The style to use. @return [String] The emphasized message.

# 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) click to toggle source

Formats a message.

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

@see replace_markers

@param message [String] The message to format. @param suffix [Object] If not `nil` or `false`, a suffix to add to the message. `true` means to add `n`. @param 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.

@param wrap [Object] If not `nil` or `false`, the maximum length of a line. `true` means the current line width. @param plain [Boolean] If ignore color markers into the message. @return [String] The formatted message.

# 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) click to toggle source

Formats a message to be written right-aligned.

@param message [String] The message to format. @param width [Fixnum] The screen width. If `true`, it is automatically computed. @param go_up [Boolean] If go up one line before formatting. @param plain [Boolean] If ignore color markers into the message. @return [String] The formatted message.

# 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") click to toggle source

Indents a message.

@param message [String] The message to indent. @param width [Fixnum] 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.

@param newline_separator [String] The character used for newlines. @return [String] The indented message.

# 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_indentation() click to toggle source

Resets indentation width to `0`.

@return [Fixnum] The new indentation width.

# File lib/bovem/console.rb, line 135
def reset_indentation
  @indentation = 0
end
set_indentation(width, is_absolute = false) click to toggle source

Sets the new indentation width.

@param width [Fixnum] The new width. @param is_absolute [Boolean] If the new width should not be added to the current one but rather replace it. @return [Fixnum] The new indentation width.

# 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) { || ... } click to toggle source

Starts a indented region of text.

@param width [Fixnum] The new width. @param is_absolute [Boolean] If the new width should not be added to the current one but rather replace it. @return [Fixnum] The new indentation width.

# 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) click to toggle source

Wraps a message in fixed line width.

@param message [String] The message to wrap. @param width [Fixnum] The maximum width of a line. Default to the current line width. @return [String] The wrapped message.

# 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