Skip to content

Instantly share code, notes, and snippets.

@dkan
Created June 22, 2012 00:30
Show Gist options
  • Save dkan/2969492 to your computer and use it in GitHub Desktop.
Save dkan/2969492 to your computer and use it in GitHub Desktop.
TODO
DEBUG = false
LOAD_FROM_FILE = true
LOAD_FILENAME = "todo.txt"
NAME_LENGTH = 30
PRIORITY_LENGTH = 13
CREATED_LENGTH = 13
COMPLETED_LENGTH = 13
HELP_LENGTH = 40
def run
input = ""
todo_list = []
todo_list = load_from_file(LOAD_FILENAME) if LOAD_FROM_FILE
puts "Welcome to the To Do App\nYou have #{todo_list.count} tasks in your To Do list."
while input != "q"
printf "Enter command: "
input = gets.chomp
cmd = input.split[0]
parameters = input.split[1..-1].join(' ')
case cmd
when '-h'
print_help
when '--help'
print_help
when 'add'
add_args = parameters.split(', ')
new_task = {}
add_args.reverse.each do |element|
case element[0]
when /[123]/
new_task[:priority] = element
when '#'
new_task[:tags] = element.split.sort
else
new_task[:name] = element
end
new_task[:created_at] = Time.new.strftime('%m/%d/%Y')
new_task[:completion_date] = 'N/A'
end
new_task[:priority] = "N/A" unless new_task[:priority]
todo_list << new_task
todo_list.sort_by! { |task| task[:priority] }
DEBUG ? puts( todo_list.inspect ) : puts( "Task added" )
when 'delete'
if task_exists?( parameters, todo_list)
todo_list.delete_if do |task|
task[:name] == parameters
end
DEBUG ? puts( todo_list.inspect ) : puts( "Task deleted" )
else
puts 'Task does not exist'
end
when 'complete'
if task_exists?( parameters, todo_list)
todo_list.each do |task|
if task[:name] == parameters
task[:completion_date] = Time.new.strftime('%m/%d/%Y')
DEBUG ? puts( todo_list.inspect ) : puts( "Task completed" )
end
end
else
puts 'Task does not exist'
end
when 'reprioritize'
when 'list'
if parameters == 'outstanding'
puts print_header
todo_list.each { |task| puts print_task(task) if task[:completion_date] == 'N/A' }
elsif parameters == 'all'
puts print_header
todo_list.each { |task| puts print_task(task) }
elsif parameters == 'completed'
puts print_header
todo_list.each { |task| puts print_task(task) unless task[:completion_date] == 'N/A'}
elsif parameters.include?('#')
taglist = parameters.split
if todo_list.select {|task| task[:tags] & taglist == taglist } != []
puts print_header
todo_list.select {|task| task[:tags] & taglist == taglist }.each { |task| puts print_task(task) }
else
puts "No tasks found for #{parameters}"
end
elsif "123".include?(parameters) && parameters.length == 1
if todo_list.select {|task| task[:priority].to_s == parameters } != []
puts print_header
todo_list.select {|task| task[:priority].to_s == parameters }.each { |task| puts print_task(task) }
else
puts "No tasks found for priority #{parameters}"
end
else
puts 'Command invalid, \'-h\' for list of commands'
end
else
puts 'Command invalid, \'-h\' for list of commands' unless cmd == 'q'
end
write_to_file(todo_list)
end
end
def load_from_file(filename)
f = File.open(filename)
todo = []
line = f.gets
line = f.gets
while line
task = {}
task[:name] = line.slice!(0,NAME_LENGTH).rstrip
task[:priority] = line.slice!(0, PRIORITY_LENGTH).rstrip
task[:created_at] = line.slice!(0, CREATED_LENGTH).rstrip
task[:completion_date] = line.slice!(0, COMPLETED_LENGTH).rstrip
task[:tags] = line.chomp.split.sort unless line == "\n"
todo << task
line = f.gets
end
todo
end
def print_help
puts "add task_name, [priority], [tags]".ljust(HELP_LENGTH, ' ') + "add task to list. can optionally add tags and priority."
puts "complete task_name".ljust(HELP_LENGTH, ' ') + "complete task"
puts "delete task_name".ljust(HELP_LENGTH, ' ') + "delete task"
puts "list (outstanding|completed)".ljust(HELP_LENGTH, ' ') + "list outstanding or completed tasks and write to file"
end
def write_to_file(todo_list)
File.open("todo.txt")
doc = print_header
todo_list.each { |task| doc << print_task(task) + "\n" }
File.write("todo.txt", doc)
end
def print_header
"Task".ljust(NAME_LENGTH, ' ') + "Priority".ljust(PRIORITY_LENGTH, ' ') + "Created".ljust(CREATED_LENGTH, ' ') + "Completed".ljust(COMPLETED_LENGTH, ' ') + "Tags\n"
end
def print_task(task)
line = task[:name].ljust(NAME_LENGTH, ' ') + task[:priority].ljust(PRIORITY_LENGTH, ' ') + task[:created_at].ljust(CREATED_LENGTH, ' ') + task[:completion_date].ljust(COMPLETED_LENGTH, ' ')
task[:tags] ? line << task[:tags].join(' ') : ""
line
end
def task_exists?(task_name, todo_list)
flag = false
todo_list.each { |task| flag = true if task[:name] == task_name }
flag
end
run
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment