Module: VMC::Interactive
- Includes:
- Interact::Pretty, Interact::Progress, Interactive::Rewindable
- Included in:
- CLI
- Defined in:
- lib/vmc/cli/interactive.rb
Defined Under Namespace
Classes: CFState
Instance Method Summary collapse
- #ask(question, options = {}) ⇒ Object
- #handler(event, state) ⇒ Object
- #input_state(options) ⇒ Object
- #list_choices(choices, options = {}) ⇒ Object
- #prompt(question, options) ⇒ Object
- #show_default(options) ⇒ Object
Instance Method Details
#ask(question, options = {}) ⇒ Object
11 12 13 14 15 16 17 |
# File 'lib/vmc/cli/interactive.rb', line 11 def ask(question, = {}) if force? and .key?(:default) [: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/vmc/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! # 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/vmc/cli/interactive.rb', line 25 def input_state() if .key? :default answer = show_default() end CFState.new(, answer) end |
#list_choices(choices, options = {}) ⇒ Object
19 20 21 22 23 |
# File 'lib/vmc/cli/interactive.rb', line 19 def list_choices(choices, = {}) choices.each_with_index do |o, i| puts "#{c(i + 1, :number)}: #{show_choice(o, )}" end end |
#prompt(question, options) ⇒ Object
46 47 48 49 50 51 52 53 54 55 |
# File 'lib/vmc/cli/interactive.rb', line 46 def prompt(question, ) value = show_default() 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/vmc/cli/interactive.rb', line 33 def show_default() case [:default] when true "y" when false "n" when nil "" else show_choice([:default], ) end end |