Class: UI

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby_doc/cli/ui.rb

Direct Known Subclasses

Scraper

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Attribute Details

#counterObject (readonly)

Properties===================


3
4
5
# File 'lib/ruby_doc/cli/ui.rb', line 3

def counter
  @counter
end

Class Method Details

.browse_control(identifier, page) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/ruby_doc/cli/ui.rb', line 82

def self.browse_control(identifier, page) 
  prompt
  input = my_gets
  
  case input
  when "n" 
    paginate(identifier) 
  when "m" 
    RubyDoc::CLI.start 
  when "exit!" 
    exit!
  end
  
  if !input.to_i.between?(1,page.count) 
    browse_error(input, identifier, page) 
  else 
    Processor.load_doc(page[input.to_i-1])
  end
end

.browse_error(input, identifier, page) ⇒ Object



319
320
321
322
323
324
325
326
327
# File 'lib/ruby_doc/cli/ui.rb', line 319

def self.browse_error(input, identifier, page)  
  if identifier == "Last"
    list_error(page)
    browse_control(identifier, page)
  else
    browse_list_error(page)
    browse_control(identifier, page)
  end
end

.browse_list(page, identifier) ⇒ Object



192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
# File 'lib/ruby_doc/cli/ui.rb', line 192

def self.browse_list(page, identifier) 
  puts sepL
  page.each_with_index do |doc, index|  
    
    if doc.type == "Class" || doc.type == "Module"
      li = ["#{index + 1}.".yellow, doc.name.light_cyan]
    else
      li = ["#{index + 1}.".yellow, doc.name.cyan]
    end
    
    puts li.join(" ")
  end
  
  if identifier == "Last"
    puts sepR
    puts "End of List".red
  end
  puts sepR 
  
  last_page_menu(page) if identifier == "Last"
  browse_menu(page) if !(identifier == "Last")
  
  browse_control(identifier, page)
end

.browse_list_error(page) ⇒ Object



329
330
331
# File 'lib/ruby_doc/cli/ui.rb', line 329

def self.browse_list_error(page)
  print redH("\n Enter # to view, 'n' for next page 'm' for main or 'exit!' to leave ")
end

.browse_menu(page) ⇒ Object



265
266
267
268
269
270
271
# File 'lib/ruby_doc/cli/ui.rb', line 265

def self.browse_menu(page) 
  puts "To ".cyan + "View ".yellow + "(Enter ".cyan + "#".yellow + ")".cyan + " eg. #{page.count.to_s} for #{page[page.count-1].name}".black
  puts "To ".cyan + "Browse Next Page ".yellow + "(Enter ".cyan + "'n'".yellow + ")".cyan
  puts "To Return To".cyan + " Main Menu".yellow + " (Enter ".cyan + "'m'".yellow + ")".cyan
  puts "To".cyan + " Leave".yellow + " (".cyan + "'exit!'".yellow + ")\n".cyan
  print randQ
end

.clearObject



9
10
11
# File 'lib/ruby_doc/cli/ui.rb', line 9

def self.clear 
  system "clear" or system "cls"
end

.cyanH(str) ⇒ Object

red highlight



383
384
385
# File 'lib/ruby_doc/cli/ui.rb', line 383

def self.cyanH(str) 
  str.colorize(color: :white, background: :cyan)
end

.display_class(doc, view = "short") ⇒ Object

——————-docs———————–



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/ruby_doc/cli/ui.rb', line 116

def self.display_class(doc, view="short") 
  UI.sepL
  puts "TITLE: ".cyan + doc.name.upcase 
  puts "TYPE: ".cyan + doc.type.upcase
  puts "\nDESCRIPTION:".cyan 
  
  if view == "short"
    puts wrapped(doc.short) 
    puts view_full
    
  elsif view == "full"
    puts doc.documentation
  end
  
  puts "Methods: ".cyan + "#{doc.methods.count}".yellow
  puts "Source: #{doc.url}".red 
  puts sepR
  
  display_class_menu(doc)
  display_class_control(doc)
end

.display_class_control(doc) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/ruby_doc/cli/ui.rb', line 34

def self.display_class_control(doc) 
  prompt
  input = my_gets
  
  case input
  when "full" 
    display_class(doc, "full")
  when "1" 
    method_list(doc)
  when "m" 
    RubyDoc::CLI.start
  when "exit!" 
    exit!
  else 
    display_class_error(doc)
  end 
end

.display_class_error(doc) ⇒ Object



309
310
311
312
# File 'lib/ruby_doc/cli/ui.rb', line 309

def self.display_class_error(doc) 
  print redH("\n Please enter '1' to view methods, 'm' for main, or 'exit!' to leave ")
  display_class_control(doc)
end

.display_class_menu(doc) ⇒ Object



252
253
254
255
256
257
# File 'lib/ruby_doc/cli/ui.rb', line 252

def self.display_class_menu(doc) 
  puts "To ".cyan + "View Methods ".yellow + "For".cyan + " #{doc.name}".yellow + " (Enter ".cyan + "'1'".yellow + ")".cyan
  puts "To Return To".cyan + " Main Menu".yellow + " (Enter ".cyan + "'m'".yellow + ")".cyan
  puts "To".cyan + " Leave".yellow + " (".cyan + "'exit!'".yellow + ")\n".cyan
  print randQ
end

.display_method(doc) ⇒ Object



138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/ruby_doc/cli/ui.rb', line 138

def self.display_method(doc) 
  UI.sepL
  puts "Title: ".cyan + doc.name.upcase 
  puts "Type: ".cyan + doc.type.upcase
  puts "\nDescription:".cyan 
  puts doc.documentation
  puts "Source: #{doc.url}".red 
  puts sepR
   
  #-----------future fix------------#
  # description = doc.doc
  # puts uie.wrapped(description, 55)
  #-----------future fix------------#
  
  display_method_menu 
  display_method_control
end

.display_method_controlObject



52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/ruby_doc/cli/ui.rb', line 52

def self.display_method_control 
  prompt
  input = my_gets
  
  case input
  when "m" 
    RubyDoc::CLI.start
  when "exit!"
    exit!
  else 
    display_method_error
  end 
end

.display_method_errorObject



314
315
316
317
# File 'lib/ruby_doc/cli/ui.rb', line 314

def self.display_method_error 
  print redH("\n Please enter 'm' for main menu or 'exit!' to leave ")
  display_method_control
end

.display_method_menuObject



259
260
261
262
263
# File 'lib/ruby_doc/cli/ui.rb', line 259

def self.display_method_menu 
  puts "To Return To".cyan + " Main Menu".yellow + " (Enter ".cyan + "'m'".yellow + ")".cyan
  puts "To".cyan + " Leave".yellow + " (".cyan + "'exit!'".yellow + ")\n".cyan
  print randQ
end

.last_page_menu(page) ⇒ Object



273
274
275
276
277
278
# File 'lib/ruby_doc/cli/ui.rb', line 273

def self.last_page_menu(page)
  puts "To ".cyan + "View ".yellow + "(Enter ".cyan + "#".yellow + ")".cyan + " eg. #{page.count.to_s} for #{page[page.count-1].name}".black
  puts "To Return To".cyan + " Main Menu".yellow + " (Enter ".cyan + "'m'".yellow + ")".cyan
  puts "To".cyan + " Leave".yellow + " (".cyan + "'exit!'".yellow + ")\n".cyan
  print randQ
end

.learn_moreObject

—————–learn more——————-



217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
# File 'lib/ruby_doc/cli/ui.rb', line 217

def self.learn_more 
  puts "\n" + sepB
  puts wrapped("Alot in store for this section. For now more so a placeholder but feel free to see what's on the way, and what's been done below.").black
  puts sepB
  puts "COMING SOON".cyan
  puts sepB
  puts "\n" + Scraper.coming_soon + "\n"
  
  puts sepB
  puts "CHANGELOG".cyan
  puts sepB
  puts "\n" + Scraper.changelog
  
  main_menu("learn")
  main_control
end

.list_control(matches) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/ruby_doc/cli/ui.rb', line 66

def self.list_control(matches) 
  prompt
  input = my_gets
  
  if input == "m" 
    RubyDoc::CLI.start
  elsif input == "exit!"
    exit!
  elsif !input.to_i.between?(1,matches.count) 
    list_error(matches)
  else 
    Processor.load_doc(matches[input.to_i-1])
  end 
  list_control(matches)
end

.list_error(matches) ⇒ Object



301
302
303
# File 'lib/ruby_doc/cli/ui.rb', line 301

def self.list_error(matches) 
  print redH("\n Enter selection number, 'm' for main or 'exit!' to leave ")
end

.list_menu(matches) ⇒ Object



245
246
247
248
249
250
# File 'lib/ruby_doc/cli/ui.rb', line 245

def self.list_menu(matches) 
  puts "To ".cyan + "View ".yellow + "(Enter ".cyan + "#".yellow + ")".cyan 
  puts "To return to".cyan + " Main Menu".yellow + " (Enter ".cyan + "'m'".yellow + ")".cyan
  puts "To".cyan + " Leave".yellow + " (".cyan + "'exit!'".yellow + ")\n".cyan
  print randQ
end

.loading_animationObject

Goes inside iterator - last line



350
351
352
353
354
355
356
357
358
359
360
# File 'lib/ruby_doc/cli/ui.rb', line 350

def self.loading_animation 
  loading = ""
  print loading << ". ".cyan if 
  @counter == 50 || @counter == 100 || @counter == 150 || @counter == 200 || 
  @counter == 250 || @counter == 300 || @counter == 350 || @counter == 400 || 
  @counter == 450 || @counter == 500 || @counter == 550 || @counter == 600 || 
  @counter == 650 || @counter == 700 || @counter == 750 || @counter == 800 || 
  @counter == 850 || @counter == 900 || @counter == 950 || @counter == 1000 || 
  @counter == 1150 || @counter == 1200 || @counter == 1250 || @counter == 1300 || 
  @counter == 1350 || @counter == 1400 || @counter == 1450 || @counter == 1500 
end

.loading_messageObject

Goes above iterator



346
347
348
# File 'lib/ruby_doc/cli/ui.rb', line 346

def self.loading_message 
  puts cyanH(" Loading Database ") + " ☠️"
end

.main_controlObject

Control=====================


13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/ruby_doc/cli/ui.rb', line 13

def self.main_control 
  prompt
  input = my_gets
  
  if input.split.size > 1 
    main_error
  elsif input == "b" 
    paginate("start") 
  elsif input == "exit!" 
    exit!
  elsif input == "?" 
    clear
    learn_more
  else 
    matches = Processor.search(input) 
    
    search_error if matches.empty?
    search_list(matches) if matches
  end
end

.main_errorObject

Error======================


280
281
282
283
284
# File 'lib/ruby_doc/cli/ui.rb', line 280

def self.main_error 
  sleep(0.1)
  print redH("\n Input Must Be 1 Word, 'b' to browse, or 'exit!' to leave ")
  main_control
end
Menus======================


234
235
236
237
238
239
240
241
242
243
# File 'lib/ruby_doc/cli/ui.rb', line 234

def self.main_menu(from="default") 
  puts sepR#
  puts "Enter a ".cyan + "word ".yellow + "associated with the Ruby Language & I will ".cyan
  puts "try to find a match in my database for you.".cyan
  sepL#
  puts "\You can also enter".cyan + " 'b'".yellow + " to browse instead.".cyan + " Happy Hunting!".cyan
  sepB
  puts "Enter '?' to Learn More about Alpha Ruby-Doc".black unless from == "learn"
  print cyanH("\n If You're Searching... Single Word Inputs Only Please ")
end

.method_list(doc) ⇒ Object



176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
# File 'lib/ruby_doc/cli/ui.rb', line 176

def self.method_list(doc) 
  puts sepR
  doc.methods.each_with_index do |method, index| 
    
    if !method.nil?
      li = ["#{index + 1}.".yellow, method.name.cyan]
      puts li.join(" ")
    end
     
  end
  puts sepR
  
  list_menu(doc.methods) 
  list_control(doc.methods)
end

.my_getsObject

Input======================


5
6
7
# File 'lib/ruby_doc/cli/ui.rb', line 5

def self.my_gets 
  gets.strip.to_s.downcase
end

.nil_errorObject



305
306
307
# File 'lib/ruby_doc/cli/ui.rb', line 305

def self.nil_error 
  print redH("\n Enter selection number, 'm' for main or 'exit!' to leave ")
end

.paginate(identifier) ⇒ Object



102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/ruby_doc/cli/ui.rb', line 102

def self.paginate(identifier) 
  case identifier
  when "start" 
    Processor.page1
  when "Page1" 
    Processor.page2
  when "Page2" 
    Processor.page3
  when "Page3" 
    Processor.last
  end
end

.promptObject

——————-input———————-



363
364
365
# File 'lib/ruby_doc/cli/ui.rb', line 363

def self.prompt 
  print " >> ".cyan
end

.randQObject

Quotes=====================


335
336
337
338
339
340
341
342
343
# File 'lib/ruby_doc/cli/ui.rb', line 335

def self.randQ 
  puts sepB
  html = Nokogiri::HTML(open("https://fortrabbit.github.io/quotes/"))
  container = html.search(".row.gutter-l.wrap")
  
  quotes = container.search("p").map {|quote| quote.text.gsub(/[\n]\s+/, "")}
  quote = " "+ quotes[rand(0..180)]+ " "
  wrapped(quote, 55).black
end

.rdo_prefixObject

——————strings———————



387
388
389
# File 'lib/ruby_doc/cli/ui.rb', line 387

def self.rdo_prefix 
  "https://ruby-doc.org/core-2.4.3/"
end

.redH(str) ⇒ Object

——————messages——————–



379
380
381
# File 'lib/ruby_doc/cli/ui.rb', line 379

def self.redH(str) 
  str.colorize(color: :white, background: :red)
end

.search_errorObject



286
287
288
289
290
291
292
293
294
295
296
297
298
299
# File 'lib/ruby_doc/cli/ui.rb', line 286

def self.search_error 
  puts sepB
  puts "NO CIGAR!".red
  puts "I couldn't find what you're looking for.".black 
  puts "How about trying a Ruby ".black + "Method" + ", ".black + "Class" + " or ".black + "Module" + " name.".black
  puts sepB
  
  puts "NOT SURE?".red
  puts "You can always browse with ".black + "'b'" + " & learn something new.".black + ":)"
  puts sepB
  
  print redH("\n Try a new word, enter 'b' to browse, or 'exit!' to leave ")
  main_control
end

.search_list(matches) ⇒ Object

——————-lists———————-



156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# File 'lib/ruby_doc/cli/ui.rb', line 156

def self.search_list(matches) 
  UI.sepL
  matches.each_with_index do |doc, index| 
    
    if doc.type == "Class" || doc.type == "Module"
      li = ["#{index + 1}.".yellow, doc.name.light_cyan]
    else
      li = ["#{index + 1}.".yellow, doc.name.cyan]
    end
    
    puts li.join(" ")
  end
  puts sepR
  puts "Classes and Modules are".cyan + " Highlighted".light_cyan
  puts sepR
  
  list_menu(matches)
  list_control(matches)
end

.sepBObject



375
376
377
# File 'lib/ruby_doc/cli/ui.rb', line 375

def self.sepB 
  "=".black*56
end

.sepLObject

—————–separators——————-



367
368
369
# File 'lib/ruby_doc/cli/ui.rb', line 367

def self.sepL 
  puts "=".cyan*28 + "=".white*28
end

.sepRObject



371
372
373
# File 'lib/ruby_doc/cli/ui.rb', line 371

def self.sepR 
  "=".white*28 + "=".cyan*28
end

.signatureObject

Signature====================


416
417
418
419
420
421
# File 'lib/ruby_doc/cli/ui.rb', line 416

def self.signature 
    puts "\n"+"=".white*28 + "=".cyan*28 
puts %q(               ALPHA™ 
╦═╗╦ ╦╔╗ ╦ ╦  ╔╦╗╔═╗╔═╗╔═╗
╠╦╝║ ║╠╩╗╚╦╝   ║║║ ║║  ╚═╗
╩╚═╚═╝╚═╝ ╩   ═╩╝╚═╝╚═╝╚═╝).cyan end

.view_fullObject



391
392
393
394
395
# File 'lib/ruby_doc/cli/ui.rb', line 391

def self.view_full 
  puts sepB
  puts "To View Full Documentation Enter".cyan + " full".yellow
  puts sepB
end

.wrapped(s, width = 60) ⇒ Object

currently not being used



398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
# File 'lib/ruby_doc/cli/ui.rb', line 398

def self.wrapped(s, width=60) 
  lines = []
  line = ""
 
  s.split(/\s+/).each do |word|
    if line.size + word.size >= width
      lines << line
      line = word
    elsif line.empty?
     line = word
    else
     line << " " << word
   end
   end
   lines << line if line
  return lines.join "\n"
end