Created
October 23, 2012 14:31
-
-
Save bouk/3939072 to your computer and use it in GitHub Desktop.
Blah
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
# encoding: UTF-8 | |
TodoItem = Struct.new(:done, :contents) | |
todos = [] | |
if File.exists? 'list.td' | |
File.open('list.td') do |f| | |
todos = f.read.split("\n") | |
todos.map! { |item| | |
new_item = TodoItem.new | |
new_item.done, new_item.contents = item.split("|") | |
new_item | |
} | |
end | |
end | |
def print_todo_items(todo_items) | |
todo_items.each_with_index do |item, index| | |
puts "#{item.done}|#{index + 1}|#{item.contents}" | |
end | |
end | |
if ARGV.size == 0 | |
print_todo_items(todos) | |
else | |
command = ARGV.shift | |
arguments = ARGV | |
case command | |
when 'list' | |
if arguments.size == 0 | |
print_todo_items(todos) | |
else | |
"Too many arguments" | |
end | |
when 'add' | |
if arguments.size == 0 | |
puts %(USAGE: todo.rb add "list item") | |
else | |
arguments.each { |item| | |
new_item = TodoItem.new | |
new_item.done = "✗" | |
new_item.contents = item | |
todos << new_item | |
} | |
puts %(Added "#{arguments.join(', ')}" to todo list) | |
end | |
when 'remove' | |
if arguments.size == 0 | |
puts %(USAGE: todo.rb remove "number") | |
else | |
arguments.sort!.reverse! | |
arguments.each { |item| todos.delete_at(item.to_i - 1)} | |
end | |
when 'done' | |
if arguments.size == 0 | |
puts %(USAGE: todo.rb done "number") | |
else | |
arguments.each { |item| | |
if todos[item.to_i - 1] | |
todos[item.to_i - 1].done = "✓" | |
end | |
} | |
end | |
else | |
puts "Unknown command #{command}" | |
end | |
end | |
File.open('list.td', 'w') do |f| | |
f.write(todos.map{|item| item.done + "|" + item.contents}.join("\n")) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment