Class: Whatsa::CLI
- Inherits:
-
Object
show all
- Includes:
- Format
- Defined in:
- lib/whatsa/cli.rb
Instance Method Summary
collapse
Methods included from Format
#bulletize_lines, #heading_to_title, #remove_citation_markers, #url_friendly, #word_wrap
Instance Method Details
#ask ⇒ Object
28
29
30
31
|
# File 'lib/whatsa/cli.rb', line 28
def ask
puts "What would you like to know about?"
gets_command
end
|
#clear_screen ⇒ Object
8
9
10
11
|
# File 'lib/whatsa/cli.rb', line 8
def clear_screen
50.times { puts "\n" }
system('clear')
end
|
#display_dmb(dmb) ⇒ Object
52
53
54
55
56
57
58
59
60
61
62
63
64
|
# File 'lib/whatsa/cli.rb', line 52
def display_dmb(dmb)
raise TypeError unless dmb.is_a?(Whatsa::Disambig)
clear_screen
stripped_title = dmb.title.gsub("(disambiguation)", "").strip
puts word_wrap("Hmmm... #{stripped_title} could mean a few different things:\n")
dmb.descriptions.each_with_index do |kvp, i|
num = "#{i + 1}. "
item = "#{kvp[0].to_s}"
desc = kvp[1].empty? ? "" : " - #{kvp[1]}"
puts word_wrap(num + item + desc, num.length)
end
puts "\nPlease select a choice, either by name or number."
end
|
#display_sections(text) ⇒ Object
66
67
68
69
70
71
72
73
74
|
# File 'lib/whatsa/cli.rb', line 66
def display_sections(text)
text = text.article if text.is_a?(Whatsa::Section)
clear_screen
puts word_wrap("Here are some specific subjects about '#{text.title}':\n")
text.section_titles.each_with_index do |title, i|
puts word_wrap("#{i + 1}. #{title}", "#{i + 1}. ".length)
end
puts "\nPlease select a choice, either by name or number."
end
|
#full(text) ⇒ Object
115
116
117
118
119
120
|
# File 'lib/whatsa/cli.rb', line 115
def full(text)
clear_screen
puts word_wrap(text.full_text)
full_text_helpline
gets_command
end
|
#full_text_helpline ⇒ Object
83
84
85
86
87
|
# File 'lib/whatsa/cli.rb', line 83
def full_text_helpline
puts " _______________________________________________________________"
puts " (type 'other' if you'd like to select a specific category of"
puts " information on the topic, or 'new' to find out about something else)"
end
|
#get_dmb_choice(disambig) ⇒ Object
89
90
91
92
93
94
95
96
97
98
|
# File 'lib/whatsa/cli.rb', line 89
def get_dmb_choice(disambig)
display_dmb(disambig)
choice = nil
loop do
choice = gets_command
in_choices = disambig.choices.detect { |c| c.downcase == choice }
break if in_choices || choice.to_i > 0
end
disambig.choose_article(choice)
end
|
#get_sec_choice(article) ⇒ Object
100
101
102
103
104
|
# File 'lib/whatsa/cli.rb', line 100
def get_sec_choice(article)
display_sections(article)
choice = gets_command
article.choose_section(choice)
end
|
#gets_command ⇒ Object
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
|
# File 'lib/whatsa/cli.rb', line 33
def gets_command
input = nil
loop do
print "> "
input = gets.strip.downcase
case input
when "exit"
exit
when "help"
instructions
when "new"
run
else
break
end
end
input
end
|
#instructions ⇒ Object
19
20
21
22
23
24
25
26
|
# File 'lib/whatsa/cli.rb', line 19
def instructions
puts "Enter a word (or phrase) to get a brief summary of that subject."
puts "If you aren't satisfied, type 'more' to get a slightly longer"
puts "description, or type 'other' to get a list of specific categories"
puts "regarding that subject, which you can choose by number or name."
puts "You can type 'exit' to close the program (or 'help' to receive"
puts "these instructions again) at any time!"
end
|
#run ⇒ Object
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
|
# File 'lib/whatsa/cli.rb', line 122
def run
if ARGV.empty?
welcome
instructions
ask
else
input = ARGV.join(" ")
ARGV.clear
end
scraper = Whatsa::Scraper.new(input)
if scraper.not_found?
print word_wrap("Hmmm... I don't know what '#{input}' means!\nPress 'enter' to try something else.")
gets
run
elsif scraper.disambig?
article = get_dmb_choice(scraper.make_disambig)
else
article = scraper.make_article
end
input = summarize(article)
loop { input = input == "other" ? summarize(get_sec_choice(article)) : gets_command }
end
|
#summarize(text) ⇒ Object
106
107
108
109
110
111
112
113
|
# File 'lib/whatsa/cli.rb', line 106
def summarize(text)
clear_screen
return full(text) if text.summary == text.full_text
puts word_wrap(text.summary)
summary_helpline
input = gets_command
input == "more" ? full(text) : input
end
|
#summary_helpline ⇒ Object
76
77
78
79
80
81
|
# File 'lib/whatsa/cli.rb', line 76
def summary_helpline
puts " _______________________________________________________________"
puts " (type 'more' for a potentially longer summary, 'other' if you'd"
puts " like to select a specific category of information on the topic,"
puts " or 'new' to find out about something else)"
end
|
#welcome ⇒ Object
13
14
15
16
17
|
# File 'lib/whatsa/cli.rb', line 13
def welcome
clear_screen
puts "Whatsa is a quick-and-dirty lookup utility, powered by Wikipedia!"
puts "-----------------------------------------------------------------"
end
|