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


353
354
355
356
357
# File 'lib/bovem/console.rb', line 353

def begin(message, suffix = "\n", indent = true, wrap = false, plain = false, indented_banner = false, full_colored = false, print = true)
  banner = self.get_banner("*", "bright green")
  message = self.indent(message, indented_banner ? 0 : indent)
  self.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


410
411
412
# File 'lib/bovem/console.rb', line 410

def debug(message, suffix = "\n", indent = true, wrap = false, plain = false, indented_banner = false, full_colored = false, print = true)
  self.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


443
444
445
# File 'lib/bovem/console.rb', line 443

def error(message, suffix = "\n", indent = true, wrap = false, plain = false, indented_banner = false, full_colored = false, print = true)
  self.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


372
373
374
375
# File 'lib/bovem/console.rb', line 372

def fatal(message, suffix = "\n", indent = true, wrap = false, plain = false, indented_banner = false, full_colored = false, return_code = -1, print = true)
  self.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


334
335
336
337
338
339
# File 'lib/bovem/console.rb', line 334

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


390
391
392
393
394
395
396
# File 'lib/bovem/console.rb', line 390

def info(message, suffix = "\n", indent = true, wrap = false, plain = false, indented_banner = false, full_colored = false, print = true, *banner)
  banner = banner.ensure_array.flatten
  banner = ["I", "bright cyan"] if banner.blank?
  banner = self.get_banner(banner[0], banner[1], full_colored)
  message = self.indent(message, indented_banner ? 0 : indent)
  self.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.



301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
# File 'lib/bovem/console.rb', line 301

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 = self.get_banner(rv[:label], rv[:color])

    if right then
      Kernel.puts self.format_right(banner + " ", true, go_up, plain)
    else
      Kernel.puts self.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


426
427
428
429
# File 'lib/bovem/console.rb', line 426

def warn(message, suffix = "\n", indent = true, wrap = false, plain = false, indented_banner = false, full_colored = false, print = true)
  warn_banner = ["W", "bright yellow"]
  self.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


272
273
274
275
276
# File 'lib/bovem/console.rb', line 272

def write(message, suffix = "\n", indent = true, wrap = false, plain = false, print = true)
  rv = self.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


289
290
291
# File 'lib/bovem/console.rb', line 289

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