Module: EacRubyUtils::Console::Speaker

Included in:
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/_constants.rb,
lib/eac_ruby_utils/console/speaker/_class_methods.rb

Overview

Defined Under Namespace

Classes: List, Node

Constant Summary collapse

STDERR =
::EacRubyUtils::ByReference.new { $stderr }
STDIN =
::EacRubyUtils::ByReference.new { $stdin }
STDOUT =
::EacRubyUtils::ByReference.new { $stdout }

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


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

def fatal_error(string)
  puts "ERROR: #{string}".white.on_red
  Kernel.exit 1 # rubocop:disable Rails/Exit
end

#info(string) ⇒ Object


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

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

#infom(string) ⇒ Object


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

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

#infov(*args) ⇒ Object


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

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


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

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

#out(string = '') ⇒ Object


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

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

#puts(string = '') ⇒ Object


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

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.

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

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


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

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

#title(string) ⇒ Object


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

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


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

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