public
Created

  • Download Gist
todoit.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
#!/usr/bin/env ruby
 
class TodoItem
attr_reader :title
attr_accessor :done
 
def self.parse(todo_line)
line = todo_line.chomp
if line =~ /^x /
TodoItem.new line[2, line.size], true
else
TodoItem.new line
end
end
 
def initialize(title, done=false)
@title = title
@done = done
end
 
def to_print
done_str = @done ? "(DONE) " : ""
"#{done_str}#{@title}"
end
 
def to_s
done_str = @done ? "x " : ""
"#{done_str}#{@title}"
end
end
 
class TodoItems
def initialize()
@items = []
end
 
def add(item)
@items << item
end
 
def delete(idx)
@items.delete_at(idx)
end
 
def cleanup
@items.delete_if {|i| i.done}
end
 
def mark_as_done(idx)
@items[idx].done = true
end
 
def output(writer)
writer.write @items.join("\n")
end
 
def to_s
return_val = []
@items.each_with_index {|item, idx| return_val << "[#{idx}] #{item.to_print}"}
return_val.join("\n")
# @items.join("\n")
end
end
 
def write_it(items)
File.open "todo.txt", "w" do |f|
items.output(f)
end
end
 
def read_it
items = TodoItems.new
File.open "todo.txt", "r" do |f|
while line = f.gets do
items.add TodoItem.parse line
end
end
items
end
 
items = read_it
 
case ARGV[0]
when "add"
items.add TodoItem.parse ARGV[1, ARGV.size].join(" ")
when "del"
items.delete ARGV[1].to_i
when "done"
items.mark_as_done ARGV[1].to_i
when "clean"
items.cleanup
end
 
puts items
 
write_it(items)

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.