public
Created

  • Download Gist
todo.rb
Ruby
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 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258
class ToDoList
def initialize
puts "What's your name, good sir?"
name = gets.strip
puts greeting(name) #this uses the greeting below
puts "1.) Make A To-Do List \n2.) Load A To-Do List\n"
while do_action = gets.chomp
case do_action
when "1"
make_list #this should optionally take params, like name.
when "2"
get_list #This should ask for a file name that gets passed as a param to the 'get_list' method like get_list('grocery')
else
puts "I'm sorry, I don't understand. You can only enter from the options! :)"
end
end
end
def greeting(name)
unless name.nil?
case Time.now.hour
when (6...12)
time_phase = "morning"
when (12...16)
time_phase = "afternoon"
when (16...20)
time_phase = "evening"
when (20..24) || (1...6)
time_phase = "night"
end
return "\nGood #{time_phase}, Master #{name}! What would you like me to do?"
end
end
#OK...so these methods are terrible. I don't have time to refactor them and they should be broken up into multiple methods, one for each 'concern' or action
#Also, the if/case statements are a terrible mess. I'll finish the refactor later so you can see a better way to do this.
def make_list
puts "Okay! What name would you like to name the list?"
topic_header = gets.chomp.upcase
file = File.new("#{topic_header}.txt", 'w')
file << "////////////////////////// #{topic_header} ////////////////////////// \n"
puts "\nAlright, #{name}! \nTemme the list items!"
i = 0
i2 = 0
i3 = 0
while input = gets
i += 1
if input =~ /^END/
# if i == 1
# num_tasks = "task"
# elsif i == 2
# num_tasks = "task"
# else
# num_tasks = "tasks"
# end
file << "\n----------------------------------------------------------------------------------------------------------------------"
file << "\n #{"#"*i} Main Tasks: \t Total: #{i - 1} Fin: 0 left: #{i - 1}"
file << "\n #{"#"*i} Sub Tasks: \t\t Total:#{i2} Fin:0 left:#{i2}"
file << "\n----------------------------------------------------------------------------------------------------------------------"
break
end
puts ">> " + input
file << "\n#{i}.) " + input.strip + " [_] \n"
puts "Would you like any other items under this? Please answer in yes or no."
do_action2 = gets.chomp
case do_action2.downcase
when 'yes'
i2 -= 1
#i3 -= 1
puts "Okay! Tell me then!"
while input2 = gets
i2 += 1
#i3 += 1
if input2 =~ /^END/
#file << "\n\t#{i3} tasks, 0 finished, #{i3} left.\n"
#i3 = 0
break
end
puts ">> \t" + input2
file << "\t #{i2 + 1}-> " + input2.strip + " ||\n"
end
puts "Enter the next list item!"
when 'no'
puts "Okay, as you wish!"
puts "Enter the next list item then!"
else
puts "Awwwwhhh man...you shouldn't have done that. Program terminating in 3..2..1"
return
end
end
puts "Alrighto, #{name}o! Your to-do list has been prepared using Ruby magic. Goodbye for now! :D"
file.close
break
end
def get_list
puts "Which file would you like to read, bro?"
#1) you could even use the Ruby "File" lib here (in place of IO) to list files in
# the current dir and choose one.
#2) This is extremely error-prone. What if they enter a non-existent file? An integer? etc.
commnd = gets.strip
filelines = IO.readlines("#{commnd}.txt")
puts "\nYour file has been loaded, good sir. What would you like to do with it?\n\n"
 
# OK.. so there are at least 2 better ways to structure this:
# 1) Break the various actions into methods (i.e. "list_tasks","number_tasks_remaining",etc)
# and call them inside 'get_list'
# 2) Keep this code below as a method, but pass the action in as a parameter (i.e. list_action(params) )
puts "1.) List the number of tasks left. \n2.) List each task. \n3.) List completed tasks. \n4.) List incomplete tasks. \n5.) Mark a task complete."
while do_action3 = gets.strip
case do_action3
when "1"
if filelines[-3] =~ /Main Tasks/
puts filelines[-3]
else
puts "Sorry, this file doesn't specify main tasks."
end
if filelines[-2] =~ /Sub Tasks/
puts filelines[-2]
else
puts "Sorry, this file doesn't specify sub tasks."
end
break
when "2"
$task_lines = []
def capture_task(val)
$task_lines << val
end
i4 = 0
filelines.each do |line|
i4 += 1
if line =~ /\.\)/ || line =~ /->/
if line =~ /\.\)/
puts "\n" + line
elsif line =~ /->/
puts line
end
capture_task(line)
end
end
if $task_lines[0] == $task_lines[-1]
puts "No tasks found."
end
break
when "3"
$fin_lines = []
def capture_fin(val)
$fin_lines << val
end
filelines.each do |fin_task|
if fin_task =~ /\[X\]/ || fin_task =~ /\|X\|/
puts fin_task
capture_fin(fin_task)
#else
#puts "Looks like you haven't completed anything!" if fin_task == filelines[-1]
end
end
if $fin_lines[0] == $fin_lines[-1] && $fin_lines[0].class != String
puts "No completed tasks found!"
end
break
when "4"
$left_lines = []
def capture_left(val)
$left_lines << val
end
filelines.each do |left_task|
if left_task =~ /\[_\]/ || left_task =~ /\|\|/
puts left_task
capture_left(left_task)
#else
#puts "Looks like you've completed everything!" if left_task == filelines[-1]
end
end
if $left_lines[0] == $left_lines[-1] && $left_lines[0].class != String
puts "No incomplete tasks found!"
end
break
when "5"
puts "Which task would you like to mark as completed?"
filelines.each do |left_task|
puts left_task if left_task =~ /\[_\]/ || left_task =~ /\|\|/
end
numenum = (total_tasks, fin_tasks, left_tasks = filelines[-3].scan(/\d+/).each)
subnumenum = (total_subtasks, fin_subtasks, left_subtasks = filelines[-2].scan(/\d+/).each)
total_tasks, total_subtasks = numenum.next.to_i, subnumenum.next.to_i
fin_tasks, fin_subtasks = numenum.next.to_i, subnumenum.next.to_i
left_tasks, left_subtasks = numenum.next.to_i, subnumenum.next.to_i
# puts total_tasks
# puts fin_tasks
# puts left_tasks
while do_action4 = gets.chomp
if do_action4 =~ /^END/
file2 = File.new("#{commnd}.txt", "w")
file2.puts $capt_fin
break
else
if do_action4 =~ /\.\)/
fin_tasks += 1
left_tasks -= 1
total_tasks = fin_tasks + left_tasks
elsif do_action4 =~ /->/
fin_subtasks += 1
left_subtasks -= 1
total_subtasks = fin_subtasks + left_subtasks
end
puts "Do you want to mark another task as completed?"
$capt_fin = []
end
if do_action4 =~ /\.\)/
do_action4.gsub!(/\./, '\.')
do_action4.gsub!(/\)/, '\)')
end
if do_action4 =~ /->/
if do_action4.length == 3
do_action4 = " " + do_action4
end
end
filelines.each do |left_task|
if left_task =~ Regexp.new(do_action4)
if left_task =~ /\.\)/
left_task.gsub!(/\[_\]/,"[X] \t\t(DONE MOFO!)")
puts "BTW, Congratulations for finishing a main task! :D"
# fin_tasks += 1
# left_tasks -= 1
# total_tasks = fin_tasks + left_tasks
# filelines[-3].gsub!(Regexp.new("Fin:" + (fin_tasks - 1).to_s), "Fin:#{fin_tasks}")
# filelines[-3].gsub!(Regexp.new("left:" + (left_tasks - 1).to_s), "#left:{left_tasks}")
elsif left_task =~ /->/
left_task.gsub!(/\|\|/, "|X|")
puts "BTW, you're one step ahead! :D"
end
$capt_fin << left_task
elsif left_task =~ /Main Tasks/
left_task.gsub!(Regexp.new("Fin: " + (fin_tasks - 1).to_s), "Fin: #{fin_tasks}")
left_task.gsub!(Regexp.new("left: " + (left_tasks + 1).to_s), "left: #{left_tasks}")
$capt_fin << left_task
elsif left_task =~ /Sub Tasks/
left_task.gsub!(Regexp.new("Fin: " + (fin_subtasks - 1).to_s), "Fin: #{fin_subtasks}")
left_task.gsub!(Regexp.new("left: " + (left_subtasks + 1).to_s), "left: #{left_subtasks}")
$capt_fin << left_task
else
$capt_fin << left_task
end
end
end
break
end
end
break
end
end

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.