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

Constant Summary collapse

DEFAULT_STATUSES =

Available statuses for tasks.

{
  ok: {label: " OK ", color: "bright green"},
  pass: {label: "PASS", color: "bright cyan"},
  warn: {label: "WARN", color: "bright yellow"},
  fail: {label: "FAIL", color: "bright red"},
  skip: {label: "SKIP", color: "gray"}
}.freeze

Instance Method Summary collapse

Instance Method Details

#begin(message, suffix: "\n", indented: true, wrap: false, plain: false, indented_banner: false, full_colored: false, print: true) ⇒ Object

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.

  • indented (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


405
406
407
408
409
# 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) ⇒ Object

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.

  • indented (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


465
466
467
468
469
470
471
472
473
474
475
476
477
# 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) ⇒ Object

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.

  • indented (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


519
520
521
522
523
524
525
526
527
528
529
530
531
# 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) ⇒ Object

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.

  • indented (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


425
426
427
428
# 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: ["[", "]"]) ⇒ String

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


368
369
370
371
372
373
# 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: []) ⇒ Object

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.

  • indented (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) (defaults to: [])

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

See Also:

  • #format


444
445
446
447
448
449
450
# 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) ⇒ String

Formats a progress for pretty printing.

Parameters:

  • current (Fixnum)

    The current progress index (e.g. the number of the current operation).

  • total (Fixnum)

    The total progress index (e.g. the total number of operations).

  • type (Symbol) (defaults to: :list)

    The progress type. Can be :list (e.g. 01/15) or :percentage (e.g. 99.56%).

  • precision (Fixnum) (defaults to: 0)

    The precision of the percentage to return. Ignored for list progress.

Returns:

  • (String)

    The formatted progress.



382
383
384
385
386
387
388
389
390
# 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) ⇒ Array

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

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.



340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
# 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) ⇒ Object

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.

  • indented (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


492
493
494
495
496
497
498
499
500
501
502
503
504
# 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) ⇒ String

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.

  • indented (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


303
304
305
306
307
# 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) ⇒ String

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.

  • indented (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


321
322
323
324
325
326
327
328
329
330
# 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