Module: JDC::Interactive

Includes:
Interact::Pretty, Interact::Progress, Interactive::Rewindable
Included in:
CLI
Defined in:
lib/jdc/cli/interactive.rb

Defined Under Namespace

Classes: JDCState

Instance Method Summary collapse

Instance Method Details

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



11
12
13
14
15
16
17
# File 'lib/jdc/cli/interactive.rb', line 11

def ask(question, options = {})
  if force? and options.key?(:default)
    options[:default]
  else
    super
  end
end

#handler(event, state) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/jdc/cli/interactive.rb', line 57

def handler(event, state)
  ans = state.answer
  pos = state.position

  exit if event == :eof

  if state.default?
    if event.is_a?(Array) and event[0] == :key
      # initial non-movement keypress clears default answer
      clear_input(state)
    else
      # wipe away any coloring
      redraw_input(state)
    end

    state.clear_default! if(event != :up)

    # tab with a default accepts it and moves to the end
    if event == :tab
      state.goto(ans.size)
    else
      super
    end
  else
    super
  end

  print "\n" if event == :enter
end

#input_state(options) ⇒ Object



25
26
27
28
29
30
31
# File 'lib/jdc/cli/interactive.rb', line 25

def input_state(options)
  if options.key? :default
    answer = show_default(options)
  end

  JDCState.new(options, answer)
end

#list_choices(choices, options = {}) ⇒ Object



19
20
21
22
23
# File 'lib/jdc/cli/interactive.rb', line 19

def list_choices(choices, options = {})
  choices.each_with_index do |o, i|
    puts "#{c(i + 1, :number)}: #{show_choice(o, options)}"
  end
end

#prompt(question, options) ⇒ Object



46
47
48
49
50
51
52
53
54
55
# File 'lib/jdc/cli/interactive.rb', line 46

def prompt(question, options)
  value = show_default(options)

  print "#{question}"
  print c("> ", :prompt)

  unless value.empty?
    print "#{d(value) + "\b" * value.size}"
  end
end

#show_default(options) ⇒ Object



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

def show_default(options)
  case options[:default]
  when true
    "y"
  when false
    "n"
  when nil
    ""
  else
    show_choice(options[:default], options)
  end
end