Skip to content

@sukima /todoit.rb
Created

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
#!/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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.