Module: UIExtras

Included in:
RubyDoc::CLI::UI, Scraper
Defined in:
lib/ruby_doc/ui/ui_extras.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#counterObject (readonly)

For Loading Anim



2
3
4
# File 'lib/ruby_doc/ui/ui_extras.rb', line 2

def counter
  @counter
end

Instance Method Details

#alphaGetsObject



169
170
171
# File 'lib/ruby_doc/ui/ui_extras.rb', line 169

def alphaGets 
  gets.strip.to_s.downcase
end

#browseControl(currentPg, docRange) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/ruby_doc/ui/ui_extras.rb', line 16

def browseControl(currentPg, docRange) 
  prompt
  iN = alphaGets
  
  case iN
  when "n"
    DataExtras.nextPage(currentPg) 
  when "m"
    main 
  end
  # else
  !iN.to_i.between?(1,docRange.count) ? browseError(iN, currentPg, docRange) : Doc.display(docRange[iN.to_i-1])
end

#browseError(iN, currentPg, docRange) ⇒ Object



89
90
91
92
93
94
95
96
97
# File 'lib/ruby_doc/ui/ui_extras.rb', line 89

def browseError(iN, currentPg, docRange)
  if currentPg == "Last"
    choiceError(docRange)
    browseControl(currentPg, docRange)
  else
    print redH("\n Enter a number between 1 and #{docRange.count} n for next or m for main ")
    browseControl(currentPg, docRange)
  end
end

#browseMenuObject



141
142
143
144
145
146
# File 'lib/ruby_doc/ui/ui_extras.rb', line 141

def browseMenu 
  puts "To ".cyan + "View An Item ".yellow + "From This List (Enter Doc Number eg.".cyan + "'1'".yellow + ")".cyan
  puts "To ".cyan + "Browse Next Page ".yellow + "(Enter ".cyan + "'n'".yellow + ")".cyan
  puts "\nBack to".cyan + " Main Menu".yellow + " (Enter ".cyan + "'m'".yellow + ")\n".cyan
  print randQ
end

#choiceControl(matches) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/ruby_doc/ui/ui_extras.rb', line 34

def choiceControl(matches) 
  prompt
  iN = alphaGets
  
  if iN == "m" 
    main
  elsif !iN.to_i.between?(1,matches.count)
    choiceError(matches)
  else 
    RubyDoc::CLI::UI.search_Shuttle(iN, matches)
  end   
end

#choiceError(matches) ⇒ Object



112
113
114
115
# File 'lib/ruby_doc/ui/ui_extras.rb', line 112

def choiceError(matches) 
  print redH("\n Enter a number between 1 and #{matches.count} or m for main ")
  choiceControl(matches)
end

#cyanH(str) ⇒ Object

wrap string



231
232
233
# File 'lib/ruby_doc/ui/ui_extras.rb', line 231

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

#docControl(doc) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/ruby_doc/ui/ui_extras.rb', line 47

def docControl(doc) 
  prompt
  iN = alphaGets
  
  case iN
  when "1" 
    Doc.listMeths(doc)
  when "m" 
    main
  else 
    docError(doc)
  end 
end

#docError(doc) ⇒ Object



117
118
119
120
# File 'lib/ruby_doc/ui/ui_extras.rb', line 117

def docError(doc)
  print redH("\n Please enter '1' or 'm' ")
  docControl(doc)
end

#docMenu(doc) ⇒ Object



154
155
156
157
158
# File 'lib/ruby_doc/ui/ui_extras.rb', line 154

def docMenu(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 + ")\n".cyan
  print randQ
end

#loading_animationObject

Goes inside iterator - last line



193
194
195
196
197
198
199
200
201
202
203
# File 'lib/ruby_doc/ui/ui_extras.rb', line 193

def loading_animation 
  loading = ""
  print loading << ". ".cyan if 
  counter == 100 || counter == 200 || counter == 300 || counter == 400 || 
  counter == 500 || counter == 600 || counter == 700 || counter == 800 || 
  counter == 900 || counter == 1000 || counter == 1100 || counter == 1200 || 
  counter == 1300 || counter == 1400 || counter == 1500 || counter == 1600 || 
  counter == 1700 || counter == 1800 || counter == 1900 || counter == 2000 || 
  counter == 2100 || counter == 2200 || counter == 2300 || counter == 2320 || 
  counter == 2340 || counter == 2360 || counter == 2380 || counter == 2400
end

#loading_messageObject

Goes above iterator



189
190
191
# File 'lib/ruby_doc/ui/ui_extras.rb', line 189

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

#mainObject

Shuttle=====================


6
7
8
# File 'lib/ruby_doc/ui/ui_extras.rb', line 6

def main 
  RubyDoc::CLI.start
end

#mainControlObject

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


10
11
12
13
14
# File 'lib/ruby_doc/ui/ui_extras.rb', line 10

def mainControl 
  prompt
  iN = alphaGets
  iN.split.size > 1 ? mainError : RubyDoc::CLI::UI.main_Shuttle(iN)  
end

#mainErrorObject

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


83
84
85
86
87
# File 'lib/ruby_doc/ui/ui_extras.rb', line 83

def mainError 
  sleep(0.1)
  print redH("\n Input Must Be 1 Word or b Try Again ")
  mainControl
end
Menus======================


132
133
134
135
136
137
138
139
# File 'lib/ruby_doc/ui/ui_extras.rb', line 132

def mainMenu 
  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 type".cyan + " 'b'".yellow + " to browse instead.".cyan + " Happy Hunting!".cyan
  print cyanH("\n If You're Searching... Single Word Inputs Only Please ")
end

#methControlObject



61
62
63
64
65
66
67
68
69
70
71
# File 'lib/ruby_doc/ui/ui_extras.rb', line 61

def methControl 
  prompt
  iN = alphaGets
  
  case iN
  when "m" 
    main
  else 
    methError
  end 
end

#methErrorObject



122
123
124
125
# File 'lib/ruby_doc/ui/ui_extras.rb', line 122

def methError
  print redH("\n Please enter 'm' to return to main menu ")
  methControl
end

#methListControl(doc) ⇒ Object



73
74
75
76
77
78
79
80
81
# File 'lib/ruby_doc/ui/ui_extras.rb', line 73

def methListControl(doc) 
  prompt
  iN = alphaGets
  
  if iN == "m" 
    main
  elsif !iN.to_i.between?(1,doc.methods.count) ? methListError(doc) : RubyDoc::CLI::UI.meth_Shuttle(iN, doc)
  end
end

#methListError(doc) ⇒ Object



127
128
129
130
# File 'lib/ruby_doc/ui/ui_extras.rb', line 127

def methListError(doc) 
  print redH("\n Enter a number between 1 and #{doc.methods.count} or m for main ")
  methListControl(doc)
end

#methMenuObject



160
161
162
163
# File 'lib/ruby_doc/ui/ui_extras.rb', line 160

def methMenu 
  puts "To Return To".cyan + " Main Menu".yellow + " (Enter ".cyan + "'m'".yellow + ")\n".cyan
  print randQ
end

#outputD(doc, index) ⇒ Object

Display=====================


173
174
175
# File 'lib/ruby_doc/ui/ui_extras.rb', line 173

def outputD(doc, index)
  puts "#{index + 1}. ".yellow + doc.name.cyan
end

#promptObject

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


165
166
167
# File 'lib/ruby_doc/ui/ui_extras.rb', line 165

def prompt 
  print " >> ".cyan
end

#randQObject

Quote Scraper==================


179
180
181
182
183
184
185
186
# File 'lib/ruby_doc/ui/ui_extras.rb', line 179

def randQ
  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

#redH(str) ⇒ Object

cyan highlight



235
236
237
# File 'lib/ruby_doc/ui/ui_extras.rb', line 235

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

#searchControl(matches) ⇒ Object



30
31
32
# File 'lib/ruby_doc/ui/ui_extras.rb', line 30

def searchControl(matches) 
  matches == [] ? searchError : matches.each_with_index{|doc, index| outputD(doc, index)}
end

#searchErrorObject



99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/ruby_doc/ui/ui_extras.rb', line 99

def searchError 
  puts "NO MATCH!".red
  puts "If you are searching for a ".black + "Method" + ", enter the ".black + "Class" + " or".black + "\nModule" + " it belongs to instead. This limitation will be ".black + "\naddressed in future update".black
  puts "=".black*56
  
  puts "Example: ".red + "Find".black + " 'reverse'" + " by searching".black + " 'String'"
  mess = "'Reverse' method will be included in the doc's Methods:# (Additionally feel free to browse all docs)".black
  puts wrapped(mess, 70).black
  
  print redH("\n Try A New Word or 'b' To Browse ")
  mainControl
end

#sepLObject

Separators===================


205
206
207
# File 'lib/ruby_doc/ui/ui_extras.rb', line 205

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

#sepRObject



209
210
211
# File 'lib/ruby_doc/ui/ui_extras.rb', line 209

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

#signatureObject

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


239
240
241
242
243
244
# File 'lib/ruby_doc/ui/ui_extras.rb', line 239

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

#viewMenuObject



148
149
150
151
152
# File 'lib/ruby_doc/ui/ui_extras.rb', line 148

def viewMenu 
  puts "To ".cyan + "View An Item ".yellow + "From This List (Enter ID Number eg.".cyan + "'1'".yellow + ")".cyan
  puts "\nBack to".cyan + " Main Menu".yellow + " (Enter ".cyan + "'m'".yellow + ")\n".cyan
  print randQ
end

#wrapped(s, width = 78) ⇒ Object

Strings=====================


213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
# File 'lib/ruby_doc/ui/ui_extras.rb', line 213

def wrapped(s, width=78) 
  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