Module: EacRubyUtils::Console::Speaker

Included in:
Configs, Envs::Command, Rspec::Conditional
Defined in:
lib/eac_ruby_utils/console/speaker.rb,
lib/eac_ruby_utils/console/speaker/list.rb,
lib/eac_ruby_utils/console/speaker/node.rb,
lib/eac_ruby_utils/console/speaker/_class_methods.rb

Overview

Defined Under Namespace

Classes: List, Node

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.current_nodeObject



9
10
11
# File 'lib/eac_ruby_utils/console/speaker/_class_methods.rb', line 9

def current_node
  nodes_stack.last
end

.on_node(&block) ⇒ Object



13
14
15
16
17
18
# File 'lib/eac_ruby_utils/console/speaker/_class_methods.rb', line 13

def on_node(&block)
  push
  yield(*(block.arity.zero? ? [] : [current_node]))
ensure
  pop
end

.popObject



25
26
27
28
29
# File 'lib/eac_ruby_utils/console/speaker/_class_methods.rb', line 25

def pop
  return nodes_stack.pop if nodes_stack.count > 1

  raise "Cannot remove first node (nodes_stack.count: #{nodes_stack.count})"
end

.pushObject



20
21
22
23
# File 'lib/eac_ruby_utils/console/speaker/_class_methods.rb', line 20

def push
  nodes_stack << ::EacRubyUtils::Console::Speaker::Node.new
  current_node
end

Instance Method Details

#fatal_error(string) ⇒ Object



28
29
30
31
# File 'lib/eac_ruby_utils/console/speaker.rb', line 28

def fatal_error(string)
  puts "ERROR: #{string}".white.on_red
  Kernel.exit 1
end

#info(string) ⇒ Object



41
42
43
# File 'lib/eac_ruby_utils/console/speaker.rb', line 41

def info(string)
  puts string.to_s.white
end

#infom(string) ⇒ Object



45
46
47
# File 'lib/eac_ruby_utils/console/speaker.rb', line 45

def infom(string)
  puts string.to_s.light_yellow
end

#infov(*args) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
# File 'lib/eac_ruby_utils/console/speaker.rb', line 68

def infov(*args)
  r = []
  args.each_with_index do |v, i|
    if i.even?
      r << "#{v}: ".cyan
    else
      r.last << v.to_s
    end
  end
  puts r.join(', ')
end

#on_speaker_node(&block) ⇒ Object



14
15
16
# File 'lib/eac_ruby_utils/console/speaker.rb', line 14

def on_speaker_node(&block)
  ::EacRubyUtils::Console::Speaker.on_node(&block)
end

#out(string = '') ⇒ Object



24
25
26
# File 'lib/eac_ruby_utils/console/speaker.rb', line 24

def out(string = '')
  current_node.stdout.write(string.to_s)
end

#puts(string = '') ⇒ Object



18
19
20
21
22
# File 'lib/eac_ruby_utils/console/speaker.rb', line 18

def puts(string = '')
  string.to_s.each_line do |line|
    current_node.stderr.puts(current_node.stderr_line_prefix.to_s + line)
  end
end

#request_input(question, options = {}) ⇒ Object

Options:

+bool+ ([Boolean], default: +false+): requires a answer "yes" or "no".
+list+ ([Hash] or [Array], default: +nil+): requires a answer from a list.
+noecho+ ([Boolean], default: +false+): does not output answer.


57
58
59
60
61
62
63
64
65
66
# File 'lib/eac_ruby_utils/console/speaker.rb', line 57

def request_input(question, options = {})
  bool, list, noecho = options.to_options_consumer.consume_all(:bool, :list, :noecho)
  if list
    request_from_list(question, list, noecho)
  elsif bool
    request_from_bool(question, noecho)
  else
    request_string(question, noecho)
  end
end

#success(string) ⇒ Object



80
81
82
# File 'lib/eac_ruby_utils/console/speaker.rb', line 80

def success(string)
  puts string.to_s.green
end

#title(string) ⇒ Object



33
34
35
36
37
38
39
# File 'lib/eac_ruby_utils/console/speaker.rb', line 33

def title(string)
  string = string.to_s
  puts(('-' * (8 + string.length)).green)
  puts("--- #{string} ---".green)
  puts(('-' * (8 + string.length)).green)
  puts
end

#warn(string) ⇒ Object



49
50
51
# File 'lib/eac_ruby_utils/console/speaker.rb', line 49

def warn(string)
  puts string.to_s.yellow
end