forked from pickhardt/betty
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.rb
executable file
·96 lines (81 loc) · 2.37 KB
/
main.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
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
86
87
88
89
90
91
92
93
94
95
96
#!/usr/bin/env ruby
$URL = 'https://github.com/pickhardt/betty'
$VERSION = '0.1.2'
$executors = []
Dir[File.dirname(__FILE__) + '/lib/*.rb'].each {|file| require file }
def get_input_integer(min, max, options={})
while true
input = STDIN.gets.strip
if options[:allow_no] && ['n', 'no'].include?(input.downcase)
return false
end
input_integer = input.to_i
if input_integer.to_s == input && min <= input_integer && input_integer <= max
return input_integer
end
say "Sorry, please enter an integer between #{ min } and #{ max }#{ options[:allow_no] ? ', or N for no' : '' }."
end
end
def get_input_y_n
while true
case STDIN.gets.strip.downcase
when 'y', 'yes'
return true
when 'n', 'no'
return false
end
say "Sorry, please enter Y for Yes or N for No."
end
end
def interpret(command)
responses = []
$executors.each do |executor|
executors_responses = executor.interpret(command)
responses = responses.concat(executors_responses)
end
responses
end
def run(response)
if response[:say]
say response[:say]
end
if response[:call]
response[:call].call
end
if response[:command]
say "Running #{ response[:command] }"
system response[:command]
end
end
def say(phrase, options={})
puts "#{ options[:no_name] ? '' : 'Betty: ' }#{ phrase }"
end
def main(commands)
command = commands.join(' ')
responses = interpret(command)
if responses.length == 1
response = responses[0]
if response[:ask_first]
say "I found the command '#{ responses[0][:command] }'"
say " #{ responses[0][:explanation] }", :no_name => true
say "Do you want me to run this? Y/n"
if get_input_y_n
run(response)
end
else
run(response)
end
elsif responses.length > 1
say "Okay, I have multiple ways to respond."
say "Enter the number of the command you want me to run one, or N (no) if you don't want me to run any."
responses.each_with_index do |response, index|
say "[#{ index + 1 }] #{ response[:command] }", :no_name => true
say(" #{ response[:explanation] }", :no_name => true) if response[:explanation]
end
which_to_run = get_input_integer(1, responses.length, :allow_no => true)
run(responses[which_to_run - 1]) if which_to_run
else
say "Sorry, I don't understand."
end
end
main(ARGV)