Module: Bovem::ConsoleMethods::Logging

Extended by:
ActiveSupport::Concern
Included in:
Bovem::Console
Defined in:
lib/bovem/console.rb

Overview

Methods for logging activities to the user.

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary (collapse)

Instance Method Details

- (Object) begin(message, suffix = "\n", indent = true, wrap = false, plain = false, indented_banner = false, full_colored = false, print = true)

Writes a message prepending a green banner.

Parameters:

  • message (String)

    The message to format.

  • suffix (Object) (defaults to: "\n")

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

  • indent (Object) (defaults to: true)

    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) (defaults to: false)

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

  • plain (Boolean) (defaults to: false)

    If ignore color markers into the message.

  • indented_banner (Boolean) (defaults to: false)

    If also the banner should be indented.

  • full_colored (Boolean) (defaults to: false)

    If the banner should be fully colored.

  • print (Boolean) (defaults to: true)

    If false, the result will be returned instead of be printed.

See Also:

  • #format


347
348
349
350
351
# File 'lib/bovem/console.rb', line 347

def begin(message, suffix = "\n", indent = true, wrap = false, plain = false, indented_banner = false, full_colored = false, print = true)
  banner = get_banner("*", "bright green", full_colored)
  message = indent(message, indented_banner ? 0 : indent)
  write(banner + " " + message, suffix, indented_banner ? indent : 0, wrap, plain, print)
end

- (Object) debug(message, suffix = "\n", indent = true, wrap = false, plain = false, indented_banner = false, full_colored = false, print = true)

Writes a message prepending a magenta banner.

Parameters:

  • message (String)

    The message to format.

  • suffix (Object) (defaults to: "\n")

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

  • indent (Object) (defaults to: true)

    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) (defaults to: false)

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

  • plain (Boolean) (defaults to: false)

    If ignore color markers into the message.

  • indented_banner (Boolean) (defaults to: false)

    If also the banner should be indented.

  • full_colored (Boolean) (defaults to: false)

    If the banner should be fully colored.

  • print (Boolean) (defaults to: true)

    If false, the result will be returned instead of be printed.

See Also:

  • #format


404
405
406
# File 'lib/bovem/console.rb', line 404

def debug(message, suffix = "\n", indent = true, wrap = false, plain = false, indented_banner = false, full_colored = false, print = true)
  info(message, suffix, indent, wrap, plain, indented_banner, full_colored, print, ["D", "bright magenta"])
end

- (Object) error(message, suffix = "\n", indent = true, wrap = false, plain = false, indented_banner = false, full_colored = false, print = true)

Writes a message prepending a red banner.

Parameters:

  • message (String)

    The message to format.

  • suffix (Object) (defaults to: "\n")

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

  • indent (Object) (defaults to: true)

    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) (defaults to: false)

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

  • plain (Boolean) (defaults to: false)

    If ignore color markers into the message.

  • indented_banner (Boolean) (defaults to: false)

    If also the banner should be indented.

  • full_colored (Boolean) (defaults to: false)

    If the banner should be fully colored.

  • print (Boolean) (defaults to: true)

    If false, the result will be returned instead of be printed.

See Also:

  • #format


437
438
439
# File 'lib/bovem/console.rb', line 437

def error(message, suffix = "\n", indent = true, wrap = false, plain = false, indented_banner = false, full_colored = false, print = true)
  info(message, suffix, indent, wrap, plain, indented_banner, full_colored, print, "E", "bright red")
end

- (Object) fatal(message, suffix = "\n", indent = true, wrap = false, plain = false, indented_banner = false, full_colored = false, return_code = -1,, print = true)

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

Parameters:

  • message (String)

    The message to format.

  • suffix (Object) (defaults to: "\n")

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

  • indent (Object) (defaults to: true)

    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) (defaults to: false)

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

  • plain (Boolean) (defaults to: false)

    If ignore color markers into the message.

  • indented_banner (Boolean) (defaults to: false)

    If also the banner should be indented.

  • full_colored (Boolean) (defaults to: false)

    If the banner should be fully colored.

  • return_code (Fixnum) (defaults to: -1,)

    The code to return to the shell.

  • print (Boolean) (defaults to: true)

    If false, the result will be returned instead of be printed.

See Also:

  • #format


366
367
368
369
# File 'lib/bovem/console.rb', line 366

def fatal(message, suffix = "\n", indent = true, wrap = false, plain = false, indented_banner = false, full_colored = false, return_code = -1, print = true)
  error(message, suffix, indent, wrap, plain, indented_banner, full_colored, print)
  Kernel.exit(return_code.to_integer(-1))
end

- (String) get_banner(label, base_color, full_colored = false, bracket_color = "blue", brackets = ["[", "]"])

Gets a banner for the messages.

Parameters:

  • label (String)

    The label for the banner.

  • base_color (String)

    The color for the label.

  • full_colored (String) (defaults to: false)

    If all the message should be of the label color.

  • bracket_color (String) (defaults to: "blue")

    The color of the brackets.

  • brackets (Array) (defaults to: ["[", "]"])

    An array of dimension 2 to use for brackets.

Returns:

  • (String)

    The banner.

See Also:

  • #format


328
329
330
331
332
333
# File 'lib/bovem/console.rb', line 328

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
  "{mark=%s}%s{mark=%s}%s{/mark}%s{/mark}" % [bracket_color.parameterize, brackets[0], base_color.parameterize, label, brackets[1]]
end

- (Object) info(message, suffix = "\n", indent = true, wrap = false, plain = false, indented_banner = false, full_colored = false, print = true, *banner)

Writes a message prepending a cyan banner.

Parameters:

  • message (String)

    The message to format.

  • suffix (Object) (defaults to: "\n")

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

  • indent (Object) (defaults to: true)

    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) (defaults to: false)

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

  • plain (Boolean) (defaults to: false)

    If ignore color markers into the message.

  • indented_banner (Boolean) (defaults to: false)

    If also the banner should be indented.

  • full_colored (Boolean) (defaults to: false)

    If the banner should be fully colored.

  • print (Boolean) (defaults to: true)

    If false, the result will be returned instead of be printed.

  • banner (Array)

    An array with at last letter and style to use for the banner.

See Also:

  • #format


384
385
386
387
388
389
390
# File 'lib/bovem/console.rb', line 384

def info(message, suffix = "\n", indent = true, wrap = false, plain = false, indented_banner = false, full_colored = false, print = true, *banner)
  banner = banner.ensure_array(nil, true, true, true)
  banner = ["I", "bright cyan"] if banner.blank?
  banner = get_banner(banner[0], banner[1], full_colored)
  message = indent(message, indented_banner ? 0 : indent)
  write(banner + " " + message, suffix, indented_banner ? indent : 0, wrap, plain, print)
end

- (Array) status(status, plain = false, go_up = true, right = true, print = true)

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

Parameters:

  • status (Symbol)

    The status to write.

  • plain (Boolean) (defaults to: false)

    If not use colors.

  • go_up (Boolean) (defaults to: true)

    If go up one line before formatting.

  • right (Boolean) (defaults to: true)

    If to print results on the right.

  • print (Boolean) (defaults to: true)

    If false, the result will be returned instead of be printed.

Returns:

  • (Array)

    An dictionary with :label and :color keys for the status.



295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
# File 'lib/bovem/console.rb', line 295

def status(status, plain = false, go_up = true, right = true, print = true)
  statuses = {
    ok: {label: " OK ", color: "bright green"},
    pass: {label: "PASS", color: "bright cyan"},
    warn: {label: "WARN", color: "bright yellow"},
    fail: {label: "FAIL", color: "bright red"}
  }
  statuses.default = statuses[:ok]

  rv = statuses[status]

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

    if right then
      Kernel.puts(format_right(banner + " ", true, go_up, plain))
    else
      Kernel.puts(format(banner + " ", "\n", true, true, plain))
    end
  end

  rv
end

- (Object) warn(message, suffix = "\n", indent = true, wrap = false, plain = false, indented_banner = false, full_colored = false, print = true)

Writes a message prepending a yellow banner.

Parameters:

  • message (String)

    The message to format.

  • suffix (Object) (defaults to: "\n")

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

  • indent (Object) (defaults to: true)

    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) (defaults to: false)

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

  • plain (Boolean) (defaults to: false)

    If ignore color markers into the message.

  • indented_banner (Boolean) (defaults to: false)

    If also the banner should be indented.

  • full_colored (Boolean) (defaults to: false)

    If the banner should be fully colored.

  • print (Boolean) (defaults to: true)

    If false, the result will be returned instead of be printed.

See Also:

  • #format


420
421
422
423
# File 'lib/bovem/console.rb', line 420

def warn(message, suffix = "\n", indent = true, wrap = false, plain = false, indented_banner = false, full_colored = false, print = true)
  warn_banner = ["W", "bright yellow"]
  info(message, suffix, indent, wrap, plain, indented_banner, full_colored, print, warn_banner)
end

- (String) write(message, suffix = "\n", indent = true, wrap = false, plain = false, print = true)

Writes a message.

Parameters:

  • message (String)

    The message to format.

  • suffix (Object) (defaults to: "\n")

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

  • indent (Object) (defaults to: true)

    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) (defaults to: false)

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

  • plain (Boolean) (defaults to: false)

    If ignore color markers into the message.

  • print (Boolean) (defaults to: true)

    If false, the result will be returned instead of be printed.

Returns:

  • (String)

    The printed message.

See Also:

  • #format


266
267
268
269
270
# File 'lib/bovem/console.rb', line 266

def write(message, suffix = "\n", indent = true, wrap = false, plain = false, print = true)
  rv = format(message, suffix, indent, wrap, plain)
  Kernel.puts(rv) if print
  rv
end

- (String) write_banner_aligned(message, suffix = "\n", indent = true, wrap = false, plain = false, print = true)

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

Parameters:

  • message (String)

    The message to format.

  • suffix (Object) (defaults to: "\n")

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

  • indent (Object) (defaults to: true)

    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) (defaults to: false)

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

  • plain (Boolean) (defaults to: false)

    If ignore color markers into the message.

  • print (Boolean) (defaults to: true)

    If false, the result will be returned instead of be printed.

Returns:

  • (String)

    The printed message.

See Also:

  • #format


283
284
285
# File 'lib/bovem/console.rb', line 283

def write_banner_aligned(message, suffix = "\n", indent = true, wrap = false, plain = false, print = true)
  write((" " * (Bovem::Console.min_banner_length + 3)) + message.ensure_string, suffix, indent, wrap, plain, print)
end