Class: CLIUtils::Messenger

Inherits:
Object
  • Object
show all
Includes:
PrettyIO
Defined in:
lib/cliutils/messenger.rb

Overview

Allows for messages to be sent to STDOUT, as well as any number of Logger instances.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from PrettyIO

#color_chart, included

Constructor Details

#initialize(targets = {}) ⇒ Messenger

Initializes a new Messenger with an optional Hash of targets.

Parameters:

  • targets (Hash) (defaults to: {})

    Logger targets



66
67
68
# File 'lib/cliutils/messenger.rb', line 66

def initialize(targets = {})
  @targets = targets
end

Instance Attribute Details

#targetsArray (readonly)

The endpoints to which delegation occurs.

Returns:

  • (Array)


12
13
14
# File 'lib/cliutils/messenger.rb', line 12

def targets
  @targets
end

Instance Method Details

#attach(target) ⇒ void

This method returns an undefined value.

Attaches a new target to delegate to.

Parameters:

  • target (Hash)

    A hash describing a reference key and a Logger



17
18
19
20
# File 'lib/cliutils/messenger.rb', line 17

def attach(target)
  fail "Cannot add invalid target: #{ target }" unless target.is_a?(Hash)
  @targets.merge!(target)
end

#debug(m) ⇒ void

This method returns an undefined value.

Outputs a debug message; since this shouldn’t appear in STDOUT, only send it to the Logger targets.



25
26
27
# File 'lib/cliutils/messenger.rb', line 25

def debug(m)
  @targets.each { |_, t| t.debug { m } }
end

#detach(target_name) ⇒ void

This method returns an undefined value.

Detaches a delegation target.

Parameters:

  • target_name (<String, Symbol>)

    The target to remove



32
33
34
35
36
37
# File 'lib/cliutils/messenger.rb', line 32

def detach(target_name)
  unless @targets.key?(target_name)
    fail "Cannot detach invalid target: #{ target_name }"
  end
  @targets.delete(target_name)
end

#error(m) ⇒ void

This method returns an undefined value.

Outputs a formatted-red error message.

Parameters:

  • m (String)

    The message to output



42
43
44
45
# File 'lib/cliutils/messenger.rb', line 42

def error(m)
  puts _word_wrap(m, '# ').red
  @targets.each { |_, t| t.error(m) }
end

#info(m) ⇒ void

This method returns an undefined value.

Outputs a formatted-blue informational message.

Parameters:

  • m (String)

    The message to output



50
51
52
53
# File 'lib/cliutils/messenger.rb', line 50

def info(m)
  puts _word_wrap(m, '# ').blue
  @targets.each { |_, t| t.info(m) }
end

#info_block(*params) ⇒ void

This method returns an undefined value.

Deprecated method to show info messages around a block of actions

Parameters:

  • params (Array)


59
60
61
# File 'lib/cliutils/messenger.rb', line 59

def info_block(*params)
  warn('As of 2.2.0, `info_block` is deprecated and nonfunctioning.')
end

#prompt(prompt, default = nil) ⇒ String

Outputs a prompt, collects the user’s response, and returns it.

Parameters:

  • prompt (String)

    The prompt to output

  • default (String) (defaults to: nil)

    The default option

Returns:



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/cliutils/messenger.rb', line 75

def prompt(prompt, default = nil)
  Readline.completion_append_character = nil
  Readline.completion_proc = Proc.new do |str|
    Dir[str + '*'].grep( /^#{ Regexp.escape(str) }/ )
  end

  p = "# #{ prompt }#{ default.nil? ? ':' : " [default: #{ default }]:" } "
  r = ''
  choice = Readline.readline(p.cyan)
  if choice.nil? || choice.empty?
    r = default
  else
    r = choice
  end

  @targets.each { |_, t| t.prompt("Answer to '#{ prompt }': #{ r }") }
  r
end

#section(m) ⇒ void

This method returns an undefined value.

Outputs a formatted-purple section message.

Parameters:

  • m (String)

    The message to output



97
98
99
100
# File 'lib/cliutils/messenger.rb', line 97

def section(m)
  puts _word_wrap(m, '---> ').purple
  @targets.each { |_, t| t.section(m) }
end

#success(m) ⇒ void

This method returns an undefined value.

Outputs a formatted-green success message.

Parameters:

  • m (String)

    The message to output



105
106
107
108
# File 'lib/cliutils/messenger.rb', line 105

def success(m)
  puts _word_wrap(m, '# ').green
  @targets.each { |_, t| t.success(m) }
end

#warn(m) ⇒ void

This method returns an undefined value.

Outputs a formatted-yellow warning message.

Parameters:

  • m (String)

    The message to output



113
114
115
116
# File 'lib/cliutils/messenger.rb', line 113

def warn(m)
  puts _word_wrap(m, '# ').yellow
  @targets.each { |_, t| t.warn(m) }
end