Class: ProjectEulerCli::CLI
- Inherits:
-
Object
- Object
- ProjectEulerCli::CLI
- Defined in:
- lib/project_euler_cli/cli.rb
Overview
Manages the command line interface for the program.
Instance Method Summary collapse
- #banner ⇒ Object
-
#initialize ⇒ CLI
constructor
A new instance of CLI.
- #main_menu ⇒ Object
- #page_menu(page) ⇒ Object
- #problem_menu(id) ⇒ Object
- #prompt ⇒ Object
- #recent_menu ⇒ Object
- #search_menu ⇒ Object
- #search_results_menu ⇒ Object
- #start ⇒ Object
Constructor Details
#initialize ⇒ CLI
Returns a new instance of CLI.
6 7 8 9 |
# File 'lib/project_euler_cli/cli.rb', line 6 def initialize @av = ArchiveViewer.new @as = ArchiveSearcher.new end |
Instance Method Details
#banner ⇒ Object
21 22 23 24 25 26 27 |
# File 'lib/project_euler_cli/cli.rb', line 21 def puts puts " ---------------------------------- " puts " [ Project Euler ] " puts " [ e^iπ = -1 ] " puts " ---------------------------------- " end |
#main_menu ⇒ Object
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/project_euler_cli/cli.rb', line 29 def puts " - List recent problems (r) - " puts " - List archived problems (l) - " puts " - Search (s) - " puts " - Exit (x) - " input = prompt if input == 'r' elsif input == 'l' (1) elsif input == 's' elsif input == 'x' return else end end |
#page_menu(page) ⇒ Object
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/project_euler_cli/cli.rb', line 70 def (page) page = [1, page, Page.total].sort[1] #clamp @av.display_page(page) puts puts "[#{page}/#{Page.total}] (n)ext (p)rev (g)oto e(x)it" input = prompt first_problem = Page::LENGTH * (page - 1) + 1 last_problem = Page::LENGTH * page if input.to_i.between?(first_problem, last_problem) (input.to_i) elsif input == 'n' (page + 1) elsif input == 'p' (page - 1) elsif input.start_with?('g') (input.gsub('g', '').to_i) elsif input == 'x' return else (page) end end |
#problem_menu(id) ⇒ Object
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/project_euler_cli/cli.rb', line 97 def (id) @av.display_problem(id) puts puts "(b)ack e(x)it" input = prompt if input == 'b' if @as.searching else page = Problem.page(id) page == 0 ? : (page) end elsif input == 'x' return else (id) end end |
#prompt ⇒ Object
16 17 18 19 |
# File 'lib/project_euler_cli/cli.rb', line 16 def prompt print "e: " gets.strip end |
#recent_menu ⇒ Object
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/project_euler_cli/cli.rb', line 53 def @av.display_recent puts puts "e(x)it" input = prompt if input.to_i.between?(Problem.total - 9, Problem.total) (input.to_i) elsif input == 'x' return else end end |
#search_menu ⇒ Object
119 120 121 122 123 124 125 |
# File 'lib/project_euler_cli/cli.rb', line 119 def print "search: " search_terms = gets.strip @as.search(search_terms) end |
#search_results_menu ⇒ Object
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 |
# File 'lib/project_euler_cli/cli.rb', line 127 def @av.display_custom_page(@as.results) puts puts "(s)earch e(x)it" input = prompt if @as.results.include?(input.to_i) (input.to_i) elsif input == 's' elsif input == 'x' @as.searching = false return else end end |
#start ⇒ Object
11 12 13 14 |
# File 'lib/project_euler_cli/cli.rb', line 11 def start end |