Class: ProjectEulerCli::CLI

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

Overview

Manages the command line interface for the program.

Instance Method Summary collapse

Constructor Details

#initializeCLI

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



21
22
23
24
25
26
27
# File 'lib/project_euler_cli/cli.rb', line 21

def banner
  puts
  puts "  ----------------------------------  "
  puts " [          Project Euler           ] "
  puts " [            e^iπ = -1             ] "
  puts "  ----------------------------------  "
end


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 main_menu
  puts " -     List recent problems (r)     - "
  puts " -    List archived problems (l)    - "
  puts " -            Search (s)            - "
  puts " -             Exit (x)             - "

  input = prompt

  if input == 'r'
    recent_menu
    main_menu
  elsif input == 'l'
    page_menu(1)
    main_menu
  elsif input == 's'
    search_menu
    main_menu
  elsif input == 'x'
    return
  else
    main_menu
  end
end


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_menu(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)
    problem_menu(input.to_i)
  elsif input == 'n'
    page_menu(page + 1)
  elsif input == 'p'
    page_menu(page - 1)
  elsif input.start_with?('g')
    page_menu(input.gsub('g', '').to_i)
  elsif input == 'x'
    return
  else
    page_menu(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 problem_menu(id)
  @av.display_problem(id)

  puts
  puts "(b)ack e(x)it"

  input = prompt

  if input == 'b'
    if @as.searching
      search_results_menu
    else
      page = Problem.page(id)
      page == 0 ? recent_menu : page_menu(page)
    end
  elsif input == 'x'
    return
  else
    problem_menu(id)
  end
end

#promptObject



16
17
18
19
# File 'lib/project_euler_cli/cli.rb', line 16

def prompt
  print "e: "
  gets.strip
end

#recent_menuObject



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 recent_menu
  @av.display_recent

  puts
  puts "e(x)it"

  input = prompt

  if input.to_i.between?(Problem.total - 9, Problem.total)
    problem_menu(input.to_i)
  elsif input == 'x'
    return
  else
    recent_menu
  end
end

#search_menuObject



119
120
121
122
123
124
125
# File 'lib/project_euler_cli/cli.rb', line 119

def search_menu
  print "search: "

  search_terms = gets.strip
  @as.search(search_terms)
  search_results_menu
end

#search_results_menuObject



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 search_results_menu
  @av.display_custom_page(@as.results)

  puts
  puts "(s)earch e(x)it"

  input = prompt

  if @as.results.include?(input.to_i)
    problem_menu(input.to_i)
  elsif input == 's'
    search_menu
  elsif input == 'x'
    @as.searching = false
    return
  else
    search_results_menu
  end
end

#startObject



11
12
13
14
# File 'lib/project_euler_cli/cli.rb', line 11

def start
  banner
  main_menu
end