Created
June 21, 2012 22:32
-
-
Save Pcushing/2969011 to your computer and use it in GitHub Desktop.
A command line app to manage a todo list in a local file... work in progress
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require "docopt" | |
################################################## | |
# Task object | |
################################################## | |
class Task | |
attr_accessor :description, :creation_time, :completion_time, :completed | |
def initialize(description) | |
@description = description | |
@creation_time = Time.new.to_s | |
@completion_time = "" | |
@completed = "not completed" | |
end | |
def in_words | |
delim = "\t|\t" | |
@description + delim + @creation_time.to_s + delim + @completion_time + delim + @completed + "\n" | |
end | |
def complete | |
@completion_time = Time.new.to_s | |
@completed = "completed!" | |
end | |
end | |
################################################## | |
# Todo list | |
################################################## | |
class Todos | |
def initialize | |
@filename="todo_list.txt" | |
task_list = File.readlines(@filename) | |
@tasks = [] | |
if task_list[0].split("\t|\t").length >= 4 #We need a good way to keep track of columns from tasks | |
task_list.each do |line| | |
columns = line.split("\t|\t") # We need a good way to keep track of delimiters from tasks | |
task = Task.new("") | |
task.description = columns[0] | |
task.creation_time = columns[1] | |
task.completion_time = columns[2] | |
task.completed = columns[3].delete "\n" | |
@tasks << task | |
end | |
end | |
end | |
def add(description, where_to) | |
task = Task.new(description) | |
if where_to == "end" | |
write_to_file(@tasks.insert(-1,task)) | |
else | |
write_to_file(@tasks.insert(0,task)) | |
end | |
end | |
def list(order_by) | |
if order_by == "completion time" | |
puts @tasks.sort_by!{ |task| task.completion_time }.reverse! | |
@tasks.each { |task| puts task.in_words } | |
else | |
@tasks.sort_by!{ |task| task.creation_time }.reverse! | |
@tasks.each { |task| puts task.in_words } | |
end | |
end | |
def delete(line_number) | |
@tasks.delete_at(line_number.to_i - 1) | |
write_to_file(@tasks) | |
end | |
def complete(line_number) | |
@tasks[line_number.to_i - 1].complete | |
write_to_file(@tasks) | |
end | |
# Should this be private? It's kind of dangerous. | |
def write_to_file(task_objects_array) | |
words = [] | |
task_objects_array.each do |line| | |
words << line.in_words | |
end | |
File.open(@filename, "w").puts(words) | |
end | |
end | |
################################################## | |
# We initialize the task list each time we run todo_app.rb | |
################################################## | |
todos = Todos.new | |
################################################## | |
# This takes care of our command line parsing. | |
################################################## | |
doc = "Usage: ruby todo_app.rb [option] <arguments>... | |
Options: | |
-a TASK Adds a task to the end of your todo list. | |
--ab TASK Adds a task to the beginning of your list. | |
-l Print a list of your todo tasks ordered by creation date. | |
--lc Print a list of your todo tasks ordered by completion date. | |
-d TASK_NUM Delete a task item from your todo list according to line number. | |
-c TASK_NUM Complete a task item in your todo list according to line number. | |
-h Prints the help documentation. | |
-v Prints the app version. | |
" | |
if __FILE__ == $0 | |
command = ARGV[0] | |
command_arg = ARGV[1] | |
options = Docopt(doc, '0.0.1', true) | |
case command | |
when '-a' then todos.add(command_arg, "end") | |
when '--ab' then todos.add(command_arg, "beginning") | |
when '-l' then todos.list("creation time") | |
when '--lc' then todos.list("completion time") | |
when '-d' then todos.delete(command_arg) | |
when '-c' then todos.complete(command_arg) | |
else | |
puts "Sorry, I don't know how to (#{command}). Try typing -h to get some help." | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment