Class: Wikihow::CLI

Inherits:
Object
  • Object
show all
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

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

#callObject



9
10
11
12
13
# File 'lib/wikihow/cli.rb', line 9

def call
  puts "WELCOME TO WIKIHOW!"
  list_categories
  categories_menu
end

#categories_menuObject



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/wikihow/cli.rb', line 20

def categories_menu
  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)
    topics_menu(category, display_index)
  elsif input == "exit"
    good_bye
  else
    list_categories
    puts red_text("Please enter a valid command.")
    categories_menu
  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_byeObject



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_categoriesObject



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 sections_menu(topic)
  if topic.sections.count == 1
    display_section(topic.sections[0])
    list_topics(topic.category)
    topics_menu(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)
      sections_menu(topic)
    elsif input == "topics"
      list_topics(topic.category)
      topics_menu(topic.category)
    elsif input == "exit"
      good_bye
    else
      puts red_text("Please enter a valid command.")
      sections_menu(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 topics_menu(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)
    sections_menu(topic)
  elsif input == "next"
    display_index = list_topics(category, display_index)
    topics_menu(category, display_index)
  elsif input == "cat"
    list_categories
    categories_menu
  elsif input == "exit"
    good_bye
  else
    puts red_text("Please enter a valid command.")
    topics_menu(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