Class: Wikihow::CLI
- Inherits:
-
Object
- Object
- Wikihow::CLI
- Defined in:
- lib/wikihow/cli.rb
Overview
CLI Controller
Constant Summary collapse
- @@red =
31
- @@green =
32
- @@yellow =
33
- @@blue =
34
- @@cyan =
36
Instance Method Summary collapse
- #blue_text(string) ⇒ Object
- #call ⇒ Object
- #categories_menu ⇒ Object
- #color_text(string, color) ⇒ Object
- #cyan_text(string) ⇒ Object
- #display_first_layer_list(layer_1) ⇒ Object
- #display_second_layer_list(layer_2) ⇒ Object
- #display_section(section, step_number = 1) ⇒ Object
- #good_bye ⇒ Object
- #green_text(string) ⇒ Object
- #list_categories ⇒ Object
- #list_sections(topic) ⇒ Object
- #list_step(step_description, step_number) ⇒ Object
- #list_topics(category, display_index = 0) ⇒ Object
- #red_text(string) ⇒ Object
- #sections_menu(topic) ⇒ Object
- #topics_menu(category, display_index = 10) ⇒ Object
- #yellow_text(string) ⇒ Object
Instance Method Details
#blue_text(string) ⇒ Object
151 152 153 |
# File 'lib/wikihow/cli.rb', line 151 def blue_text(string) color_text(string, @@blue) end |
#call ⇒ Object
9 10 11 12 13 |
# File 'lib/wikihow/cli.rb', line 9 def call puts "WELCOME TO WIKIHOW!" list_categories end |
#categories_menu ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/wikihow/cli.rb', line 20 def input = nil puts "Enter the number of the category that you'd like to learn about. Type 'exit' to close." input = gets.strip.downcase if input.to_i > 0 && input.to_i <= @categories.count category = @categories[input.to_i - 1] display_index = list_topics(category) (category, display_index) elsif input == "exit" good_bye else list_categories puts red_text("Please enter a valid command.") end end |
#color_text(string, color) ⇒ Object
163 164 165 |
# File 'lib/wikihow/cli.rb', line 163 def color_text(string, color) "\e[#{color}m#{string}\e[0m" end |
#cyan_text(string) ⇒ Object
159 160 161 |
# File 'lib/wikihow/cli.rb', line 159 def cyan_text(string) color_text(string, @@cyan) end |
#display_first_layer_list(layer_1) ⇒ Object
127 128 129 130 131 |
# File 'lib/wikihow/cli.rb', line 127 def display_first_layer_list(layer_1) layer_1.each do |layer_2| (layer_2.is_a? Array) ? display_second_layer_list(layer_2) : puts(green_text("\n > #{layer_2}\n")) end end |
#display_second_layer_list(layer_2) ⇒ Object
133 134 135 136 137 |
# File 'lib/wikihow/cli.rb', line 133 def display_second_layer_list(layer_2) layer_2.each do |layer_3| puts blue_text("\n >> #{layer_3}\n") end end |
#display_section(section, step_number = 1) ⇒ Object
106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/wikihow/cli.rb', line 106 def display_section(section, step_number = 1) step_description = section[:section_steps][step_number - 1] list_step(step_description, step_number) if step_number == section[:section_steps].count puts "Those are all the steps! Press Enter to continue" gets else puts "Press enter for next step. Type 'topic' to return to menu." input = gets.strip.downcase if input != "topic" display_section(section, step_number + 1) end end end |
#good_bye ⇒ Object
139 140 141 |
# File 'lib/wikihow/cli.rb', line 139 def good_bye puts "Goodbye!" end |
#green_text(string) ⇒ Object
147 148 149 |
# File 'lib/wikihow/cli.rb', line 147 def green_text(string) color_text(string, @@green) end |
#list_categories ⇒ Object
15 16 17 18 |
# File 'lib/wikihow/cli.rb', line 15 def list_categories @categories = Wikihow::Category.get_or_create_categories @categories.each.with_index(1) {|category, i| puts yellow_text("#{i}. #{category.title}")} end |
#list_sections(topic) ⇒ Object
70 71 72 73 74 75 76 77 78 79 |
# File 'lib/wikihow/cli.rb', line 70 def list_sections(topic) if topic.sections.count == 1 puts blue_text("\nHow to #{topic.title}".upcase) puts blue_text(topic.intro) else puts blue_text("\nHow to #{topic.title}".upcase) puts blue_text(topic.intro) topic.sections.each.with_index(1) {|section, i|puts yellow_text("#{i}. #{section[:section_title]}")} end end |
#list_step(step_description, step_number) ⇒ Object
121 122 123 124 125 |
# File 'lib/wikihow/cli.rb', line 121 def list_step(step_description, step_number) step_description.each do |layer_1| (layer_1.is_a? Array) ? display_first_layer_list(layer_1) : puts(yellow_text("\n#{step_number}. #{layer_1}")) end end |
#list_topics(category, display_index = 0) ⇒ Object
37 38 39 40 41 42 43 44 45 46 |
# File 'lib/wikihow/cli.rb', line 37 def list_topics(category, display_index = 0) @topics = Wikihow::Topic.get_or_create_topics_from_category(category) if display_index < (@topics.count - 1) @topics[display_index..display_index + 9].each.with_index(display_index + 1) {|topic, i| puts blue_text("#{i}. #{topic.title}")} display_index + 10 else puts "Those are all the topics for this category. Here are the first ten topics for the category." list_topics(category) end end |
#red_text(string) ⇒ Object
143 144 145 |
# File 'lib/wikihow/cli.rb', line 143 def red_text(string) color_text(string, @@red) end |
#sections_menu(topic) ⇒ Object
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/wikihow/cli.rb', line 81 def (topic) if topic.sections.count == 1 display_section(topic.sections[0]) list_topics(topic.category) (topic.category) else input = nil puts "Enter the number of the section/method you'd like to learn about. Type 'topics' to return to Topics menu. Type 'exit' to quit" input = gets.strip.downcase if input.to_i > 0 && input.to_i <= topic.sections.count display_section(topic.sections[input.to_i - 1]) list_sections(topic) (topic) elsif input == "topics" list_topics(topic.category) (topic.category) elsif input == "exit" good_bye else puts red_text("Please enter a valid command.") (topic) end end end |
#topics_menu(category, display_index = 10) ⇒ Object
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/wikihow/cli.rb', line 48 def (category, display_index = 10) input = nil puts "Enter the number of the topic that you'd like to learn about. Type 'next' to display the next 10 topics. Type 'cat' to return to categories menu. Type 'exit' to quit." input = gets.strip.downcase if input.to_i > 0 && input.to_i <= category.topics.count topic = category.topics[input.to_i - 1] list_sections(topic) (topic) elsif input == "next" display_index = list_topics(category, display_index) (category, display_index) elsif input == "cat" list_categories elsif input == "exit" good_bye else puts red_text("Please enter a valid command.") (category, display_index) end end |
#yellow_text(string) ⇒ Object
155 156 157 |
# File 'lib/wikihow/cli.rb', line 155 def yellow_text(string) color_text(string, @@yellow) end |