Skip to content

Instantly share code, notes, and snippets.

@vshan vshan/ToDo.rb
Created Jun 5, 2012

Embed
What would you like to do?
A script for making and marking-off to-do lists.
# ToDo.rb
# A program for creating and marking-off to-do lists.
puts "What's your name, good sir?"
name = gets.strip
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
puts "\nGood #{time_phase}, Master #{name}! What would you like me to do?"
puts "1.) Make A To-Do List \n2.) Load A To-Do List\n"
while do_action = gets.chomp
case do_action
when "1"
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/
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
puts "Okay! Tell me then!"
while input2 = gets
i2 += 1
if input2 =~ /^END/
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 "Sorry, program cannot comprehend your input. 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
when "2"
puts "Which file would you like to read, bro?"
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"
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)
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)
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
num_enum = (total_tasks, fin_tasks, left_tasks = filelines[-3].scan(/\d+/).each)
sub_num_enum = (total_subtasks, fin_subtasks, left_subtasks = filelines[-2].scan(/\d+/).each)
total_tasks = num_enum.next.to_i
total_subtasks = sub_num_enum.next.to_i
fin_tasks = num_enum.next.to_i
fin_subtasks = sub_num_enum.next.to_i
left_tasks = num_enum.next.to_i
left_subtasks = sub_num_enum.next.to_i
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"
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
else
puts "I'm sorry, I don't understand. You can only enter from the options! :)"
end
end
@mattroyer

This comment has been minimized.

Copy link

commented Jun 5, 2012

Good piece of code! Easy to read too. Thanks.

@vshan

This comment has been minimized.

Copy link
Owner Author

commented Jun 5, 2012

Thank you, Matt! :-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.