Module: Earthquake::Input
- Included in:
- Earthquake
- Defined in:
- lib/earthquake/input.rb
Instance Method Summary collapse
- #alias_command(name, target) ⇒ Object
- #ask(message, prompt) ⇒ Object
- #async_e(&block) ⇒ Object
- #command(pattern, options = {}, &block) ⇒ Object
- #command_aliases ⇒ Object
- #command_names ⇒ Object
- #commands ⇒ Object
- #completion(&block) ⇒ Object
- #completions ⇒ Object
- #confirm(message, type = ) ⇒ Object
- #handle_api_error(&block) ⇒ Object
- #input(text) ⇒ Object
- #input_filter(&block) ⇒ Object
- #input_filters ⇒ Object
Instance Method Details
#alias_command(name, target) ⇒ Object
34 35 36 37 38 |
# File 'lib/earthquake/input.rb', line 34 def alias_command(name, target) name = name.is_a?(Symbol) ? ":#{name}" : name.to_s target = target.is_a?(Symbol) ? ":#{target}" : target.to_s command_aliases[name] = target end |
#ask(message, prompt) ⇒ Object
90 91 92 93 |
# File 'lib/earthquake/input.rb', line 90 def ask(, prompt) puts (Readline.readline(prompt) || "").chomp end |
#async_e(&block) ⇒ Object
95 96 97 |
# File 'lib/earthquake/input.rb', line 95 def async_e(&block) async { handle_api_error(&block) } end |
#command(pattern, options = {}, &block) ⇒ Object
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/earthquake/input.rb', line 54 def command(pattern, = {}, &block) if block if pattern.is_a?(String) || pattern.is_a?(Symbol) command_name = ":#{pattern}" command_names << command_name if block.arity > 0 pattern = %r|^#{command_name}\s+(.*)$| else pattern = %r|^#{command_name}$| end end command_names << ":#{options[:as]}" if [:as] commands.delete_if { |_| _[:pattern] == pattern } commands << {:pattern => pattern, :block => block} else commands.detect {|c| c[:pattern] =~ pattern} end end |
#command_aliases ⇒ Object
30 31 32 |
# File 'lib/earthquake/input.rb', line 30 def command_aliases @command_aliases ||= {} end |
#command_names ⇒ Object
18 19 20 |
# File 'lib/earthquake/input.rb', line 18 def command_names @command_names ||= Set.new end |
#commands ⇒ Object
14 15 16 |
# File 'lib/earthquake/input.rb', line 14 def commands @commands ||= [] end |
#completion(&block) ⇒ Object
26 27 28 |
# File 'lib/earthquake/input.rb', line 26 def completion(&block) completions << block end |
#completions ⇒ Object
22 23 24 |
# File 'lib/earthquake/input.rb', line 22 def completions @completions ||= [] end |
#confirm(message, type = ) ⇒ Object
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/earthquake/input.rb', line 73 def confirm(, type = config[:confirm_type]) s = case type when :y ask(.u.c(:notice), "[Yn] ") when :n ask(.u.c(:notice), "[yN] ") else raise "type must be :y or :n" end s = type.to_s if s.empty? if m = s.match(/^[yn]$/i) return m[0].downcase == 'y' else confirm(, type) end end |
#handle_api_error(&block) ⇒ Object
99 100 101 102 103 104 |
# File 'lib/earthquake/input.rb', line 99 def handle_api_error(&block) result = block.call if result["error"] error result["error"] end end |
#input(text) ⇒ Object
40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/earthquake/input.rb', line 40 def input(text) return if text.empty? input_filters.each { |f| text = f.call(text) } if command = command(text) command[:block].call(command[:pattern].match(text)) elsif !text.empty? puts "Command not found".c(43) end rescue Exception => e error e end |
#input_filter(&block) ⇒ Object
10 11 12 |
# File 'lib/earthquake/input.rb', line 10 def input_filter(&block) input_filters << block end |
#input_filters ⇒ Object
6 7 8 |
# File 'lib/earthquake/input.rb', line 6 def input_filters @input_filters ||= [] end |