module Bovem::ConsoleMethods::Logging

Methods for logging activities to the user.

Constants

DEFAULT_STATUSES

Available statuses for tasks.

Public Instance Methods

begin(message, suffix: "\n", indented: true, wrap: false, plain: false, indented_banner: false, full_colored: false, print: true) click to toggle source

Writes a message prepending a green banner.

@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 for wrapped text. `true` means the current line width. @param plain [Boolean] If ignore color markers into the message. @param indented_banner [Boolean] If also the banner should be indented. @param full_colored [Boolean] If the banner should be fully colored. @param print [Boolean] If `false`, the result will be returned instead of be printed.

@see format

# File lib/bovem/console.rb, line 405
def begin(message, suffix: "\n", indented: true, wrap: false, plain: false, indented_banner: false, full_colored: false, print: true)
  banner = get_banner("*", "bright green", full_colored: full_colored)
  message = indent(message, indented_banner ? 0 : indented)
  write(banner + " " + message, suffix: suffix, indented: indented_banner ? indented : 0, wrap: wrap, plain: plain, print: print)
end
debug(message, suffix: "\n", indented: true, wrap: false, plain: false, indented_banner: false, full_colored: false, print: true) click to toggle source

Writes a message prepending a magenta banner.

@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 for wrapped text. `true` means the current line width. @param plain [Boolean] If ignore color markers into the message. @param indented_banner [Boolean] If also the banner should be indented. @param full_colored [Boolean] If the banner should be fully colored. @param print [Boolean] If `false`, the result will be returned instead of be printed.

@see format

# File lib/bovem/console.rb, line 465
def debug(message, suffix: "\n", indented: true, wrap: false, plain: false, indented_banner: false, full_colored: false, print: true)
  info(
    message,
    suffix: suffix,
    indented: indented,
    wrap: wrap,
    plain: plain,
    indented_banner: indented_banner,
    full_colored: full_colored,
    print: print,
    banner: ["D", "bright magenta"]
  )
end
error(message, suffix: "\n", indented: true, wrap: false, plain: false, indented_banner: false, full_colored: false, print: true) click to toggle source

Writes a message prepending a red banner.

@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 for wrapped text. `true` means the current line width. @param plain [Boolean] If ignore color markers into the message. @param indented_banner [Boolean] If also the banner should be indented. @param full_colored [Boolean] If the banner should be fully colored. @param print [Boolean] If `false`, the result will be returned instead of be printed.

@see format

# File lib/bovem/console.rb, line 519
def error(message, suffix: "\n", indented: true, wrap: false, plain: false, indented_banner: false, full_colored: false, print: true)
  info(
    message,
    suffix: suffix,
    indented: indented,
    wrap: wrap,
    plain: plain,
    indented_banner: indented_banner,
    full_colored: full_colored,
    print: print,
    banner: ["E", "bright red"]
  )
end
fatal(message, suffix: "\n", indented: true, wrap: false, plain: false, indented_banner: false, full_colored: false, return_code: -1, print: true) click to toggle source

Writes a message prepending a red banner and then quits the application.

@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 for wrapped text. `true` means the current line width. @param plain [Boolean] If ignore color markers into the message. @param indented_banner [Boolean] If also the banner should be indented. @param full_colored [Boolean] If the banner should be fully colored. @param return_code [Fixnum] The code to return to the shell. @param print [Boolean] If `false`, the result will be returned instead of be printed.

@see format

# File lib/bovem/console.rb, line 425
def fatal(message, suffix: "\n", indented: true, wrap: false, plain: false, indented_banner: false, full_colored: false, return_code: -1, print: true)
  error(message, suffix: suffix, indented: indented, wrap: wrap, plain: plain, indented_banner: indented_banner, full_colored: full_colored, print: print)
  Kernel.exit(return_code.to_integer(-1))
end
get_banner(label, base_color, full_colored: false, bracket_color: "blue", brackets: ["[", "]"]) click to toggle source

Gets a banner for the messages.

@param label [String] The label for the banner. @param base_color [String] The color for the label. @param full_colored [String] If all the message should be of the label color. @param bracket_color [String] The color of the brackets. @param brackets [Array] An array of dimension 2 to use for brackets. @return [String] The banner. @see format

# File lib/bovem/console.rb, line 368
def get_banner(label, base_color, full_colored: false, bracket_color: "blue", brackets: ["[", "]"])
  label = label.rjust(Bovem::Console.min_banner_length, " ")
  brackets = brackets.ensure_array
  bracket_color = base_color if full_colored
  sprintf("{mark=%s}%s{mark=%s}%s{/mark}%s{/mark}", bracket_color.parameterize, brackets[0], base_color.parameterize, label, brackets[1])
end
info(message, suffix: "\n", indented: true, wrap: false, plain: false, indented_banner: false, full_colored: false, print: true, banner: []) click to toggle source

Writes a message prepending a cyan banner.

@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 for wrapped text. `true` means the current line width. @param plain [Boolean] If ignore color markers into the message. @param indented_banner [Boolean] If also the banner should be indented. @param full_colored [Boolean] If the banner should be fully colored. @param print [Boolean] If `false`, the result will be returned instead of be printed. @param banner [Array] An array with at last letter and style to use for the banner.

@see format

# File lib/bovem/console.rb, line 444
def info(message, suffix: "\n", indented: true, wrap: false, plain: false, indented_banner: false, full_colored: false, print: true, banner: [])
  banner = banner.ensure_array(no_duplicates: true, compact: true, flatten: true)
  banner = ["I", "bright cyan"] if banner.blank?
  banner = get_banner(banner[0], banner[1], full_colored: full_colored)
  message = indent(message, indented_banner ? 0 : indented)
  write(banner + " " + message, suffix: suffix, indented: indented_banner ? indented : 0, wrap: wrap, plain: plain, print: print)
end
progress(current, total, type: :list, precision: 0) click to toggle source

Formats a progress for pretty printing.

@param current [Fixnum] The current progress index (e.g. the number of the current operation). @param total [Fixnum] The total progress index (e.g. the total number of operations). @param type [Symbol] The progress type. Can be `:list` (e.g. 01/15) or `:percentage` (e.g. 99.56%). @param precision [Fixnum] The precision of the percentage to return. *Ignored for list progress.* @return [String] The formatted progress.

# File lib/bovem/console.rb, line 382
def progress(current, total, type: :list, precision: 0)
  if type == :list
    compute_list_progress(current, total)
  else
    precision = [0, precision].max
    result = total == 0 ? 100 : (100 * (current.to_f / total))
    sprintf("%0.#{precision}f %%", result.round(precision)).rjust(5 + (precision > 0 ? precision + 1 : 0))
  end
end
status(status, plain: false, go_up: true, right: true, print: true) click to toggle source

Writes a status to the output. Valid values are `:ok`, `:pass`, `:fail`, `:warn`, `skip`.

@param status [Symbol] The status to write. @param plain [Boolean] If not use colors. @param go_up [Boolean] If go up one line before formatting. @param right [Boolean] If to print results on the right. @param print [Boolean] If `false`, the result will be returned instead of be printed. @return [Array] An dictionary with `:label` and `:color` keys for the status.

# File lib/bovem/console.rb, line 340
def status(status, plain: false, go_up: true, right: true, print: true)
  statuses = DEFAULT_STATUSES.dup
  statuses.default = statuses[:ok]

  rv = statuses[status]

  if print
    banner = get_banner(rv[:label], rv[:color])

    if right
      Kernel.puts(format_right(banner + " ", width: true, go_up: go_up, plain: plain))
    else
      Kernel.puts(format(banner + " ", suffix: "\n", indent: true, wrap: true, plain: plain))
    end
  end

  rv
end
warn(message, suffix: "\n", indented: true, wrap: false, plain: false, indented_banner: false, full_colored: false, print: true) click to toggle source

Writes a message prepending a yellow banner.

@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 for wrapped text. `true` means the current line width. @param plain [Boolean] If ignore color markers into the message. @param indented_banner [Boolean] If also the banner should be indented. @param full_colored [Boolean] If the banner should be fully colored. @param print [Boolean] If `false`, the result will be returned instead of be printed.

@see format

# File lib/bovem/console.rb, line 492
def warn(message, suffix: "\n", indented: true, wrap: false, plain: false, indented_banner: false, full_colored: false, print: true)
  info(
    message,
    suffix: suffix,
    indented: indented,
    wrap: wrap,
    plain: plain,
    indented_banner: indented_banner,
    full_colored: full_colored,
    print: print,
    banner: ["W", "bright yellow"]
  )
end
write(message, suffix: "\n", indented: true, wrap: false, plain: false, print: true) click to toggle source

Writes a message.

@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 for wrapped text. `true` means the current line width. @param plain [Boolean] If ignore color markers into the message. @param print [Boolean] If `false`, the result will be returned instead of be printed. @return [String] The printed message.

@see format

# File lib/bovem/console.rb, line 303
def write(message, suffix: "\n", indented: true, wrap: false, plain: false, print: true)
  rv = format(message, suffix: suffix, indented: indented, wrap: wrap, plain: plain)
  Kernel.puts(rv) if print
  rv
end
write_banner_aligned(message, suffix: "\n", indented: true, wrap: false, plain: false, print: true) click to toggle source

Writes a message, aligning to a call with an empty banner.

@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 for wrapped text. `true` means the current line width. @param plain [Boolean] If ignore color markers into the message. @param print [Boolean] If `false`, the result will be returned instead of be printed. @return [String] The printed message.

@see format

# File lib/bovem/console.rb, line 321
def write_banner_aligned(message, suffix: "\n", indented: true, wrap: false, plain: false, print: true)
  write(
    (" " * (Bovem::Console.min_banner_length + 3)) + message.ensure_string,
    suffix: suffix,
    indented: indented,
    wrap: wrap,
    plain: plain,
    print: print
  )
end