-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdrinks.rb
173 lines (142 loc) · 4.14 KB
/
drinks.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
require 'json'
require 'colorize'
require 'readline'
require 'launchy'
require_relative 'data_classes'
@pantry = Pantry.new("pantry.json")
@recipes = Recipes.new("recipes.json")
@all_ingreds = @recipes.all_ingredients
@evernote_drinks = EvernoteData.new
def add_drink(name)
ingreds = []
comp = proc { |s| @all_ingreds.grep( /^#{Regexp.escape(s)}/) }
Readline.completion_proc = comp
Readline.completion_append_character = ""
while (true)
puts "[ingredient] | quit"
input = Readline.readline('', true)
break if input == "quit" || input == "q"
if (@all_ingreds.include?(input))
ingreds << input
else
puts "are you sure you want to add a new ingredient? [y] | [n]"
action = gets.chomp
ingreds << input if action == "y"
end
end
@recipes = @recipes.add(name, ingreds)
puts ""
p "#{name} - #{ingreds.join(", ")}"
end
def print_recipes(recipes)
recipes.each_with_index do |(name, ingreds), i|
made = @evernote_drinks[name][:made]
favorite = @evernote_drinks[name][:favorite]
if favorite
bkg, color = :blue, :light_white
elsif !made
bkg, color = :black, :red
else
bkg, color = :black, :light_blue
end
print "#{i + 1}. #{name}".ljust(25).colorize(background: bkg, color: color)
part = ingreds.partition { |ing| @pantry.data.include?(ing)}
print " #{part[1].map(&:upcase).join(", ")}".yellow
print " "
print "#{part[0].join(", ")}".white
puts ""
end
end
while (true)
unsynced = @evernote_drinks.titles - @recipes.titles
if (unsynced.length > 0)
puts "You have not synced \(press number to sync\):".red
unsynced.each_with_index do |name, i|
print "#{i + 1}.".ljust(3).light_yellow
print "#{name}".light_yellow
puts ""
end
end
puts ""
puts "[m]ake | [p]antry | [r]ecipe | [q]uit"
selection = gets.chomp.split(" ")
action = selection[0].downcase[0]
if action.to_i > 0
drink = unsynced[action.to_i - 1]
Launchy.open(@evernote_drinks[drink][:url])
add_drink(drink) if drink
elsif action == "m"
ingredient = selection[1]
puts ""
puts "You can make (Enter number for recipe)"
makeable = @recipes.missing_ingredients(@pantry, 0, ingredient)
one_off = @recipes.missing_ingredients(@pantry, 1, ingredient)
print_recipes(makeable)
puts ""
if (one_off.length > 0)
puts "You are one off from:"
print_recipes(one_off)
end
index = gets.chomp.to_i
if (index > 0)
title = makeable[index - 1][0]
Launchy.open(@evernote_drinks[title][:url])
end
elsif action == "p"
puts "[a]dd [ingredient] | [r]emove [ingredient] | shopping [l]ist | [s]how"
input = gets.chomp.split(" ")
pantry_action = input[0]
ingredient = input[1..-1].join(" ")
case pantry_action
when "a"
@pantry = @pantry.add(ingredient)
puts @pantry.sort
when "r"
@pantry = @pantry.remove(ingredient)
puts @pantry.sort
when "l"
puts (@all_ingreds - @pantry.data).sort
else
puts @pantry.sort
end
elsif action == "r"
puts "[a]dd [name] | [r]emove | [s]how | [u]nmade"
input = gets.chomp.split(" ")
recipe_action = input[0]
name = input[1..-1].join(" ")
case recipe_action
when "a"
add_drink(name)
when "r"
comp = proc { |s| @recipes.titles.grep( /^#{Regexp.escape(s)}/) }
Readline.completion_proc = comp
Readline.completion_append_character = ""
input = Readline.readline('', true)
@recipes = @recipes.remove(input)
puts @recipes.titles
when "s"
@recipes.titles.sort.each_with_index do |t, i|
print "#{i + 1}.".ljust(5).light_yellow
print "#{t}".light_yellow
puts ""
end
puts ""
puts "Enter number for recipe:".light_cyan
index = gets.chomp.to_i
if (index > 0)
title = @recipes.titles.sort[index - 1]
Launchy.open(@evernote_drinks[title][:url])
end
when "u"
unmade = @recipes.data.reject { |r| @evernote_drinks[r][:made] }
print_recipes(unmade)
end
elsif action == "q"
break
else
p "I don't recognize that"
end
end
=begin
TODO
=end