Module: Begin::Input

Defined in:
lib/begin/input.rb

Overview

All console input is routed through this module

Class Method Summary collapse

Class Method Details

.eof_shortcutObject

Returns the keyboard accelerator shortcut for the EOF signal, which is dependant on the host terminal



57
58
59
60
61
62
# File 'lib/begin/input.rb', line 57

def eof_shortcut
  if ENV.key? 'ComSpec'
    return 'CTRL+Z' if ENV['ComSpec'].upcase.end_with? '\CMD.EXE'
  end
  'CTRL+D'
end

.prompt(msg) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/begin/input.rb', line 8

def prompt(msg)
  STDOUT.write msg
  begin
    value = STDIN.gets
    raise EOFError if value.nil?

    value.chomp
  rescue StandardError, Interrupt
    Output.newline
    raise
  end
end

.prompt_user_for_array_tag(tag, level = 0) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/begin/input.rb', line 33

def prompt_user_for_array_tag(tag, level = 0)
  result = []
  loop do
    begin
      value = prompt_user_for_tag tag, level, true
      result.push value
    rescue EOFError
      break
    end
  end
  result
end

.prompt_user_for_tag(tag, level = 0, in_array = false) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
# File 'lib/begin/input.rb', line 21

def prompt_user_for_tag(tag, level = 0, in_array = false)
  indent = '  ' * level
  array_msg = in_array ? " (#{eof_shortcut} to stop)" : ''
  case tag
  when HashTag
    Output.info "#{indent}#{tag.label}#{array_msg}:"
    prompt_user_for_tag_values tag.children, level + 1
  when ValueTag
    prompt "#{indent}#{tag.label}#{array_msg}: "
  end
end

.prompt_user_for_tag_values(tags, level = 0) ⇒ Object



46
47
48
49
50
51
52
53
# File 'lib/begin/input.rb', line 46

def prompt_user_for_tag_values(tags, level = 0)
  context = {}
  tags.each do |x|
    context[x.key] = prompt_user_for_array_tag(x, level) if x.array
    context[x.key] = prompt_user_for_tag(x, level) unless x.array
  end
  context
end