Skip to content

Instantly share code, notes, and snippets.

@ox
Created August 13, 2011 16:45
Show Gist options
  • Save ox/1144022 to your computer and use it in GitHub Desktop.
Save ox/1144022 to your computer and use it in GitHub Desktop.
A small todo program I wrote in a sitting. Incredibly simple.
#!/usr/bin/env ruby
# coding: utf-8
#this is a normal todo prog which saves your todos in a local file called .todo
#./todo --help
#todo.rb usage:
# todo.rb list all active tasks
# todo.rb <task> create a task described by <task>
# todo.rb -r <string> deletes all tasks matching <string>
# todo.rb -vr <string> show what tasks would be deleted with a given <string>
#there is 'aggressive' todo matching, multiple todo's can be wiped with a
# single word. -vr flag will show you which todo's match the remove regex
# ./todo -vr todo program
#suggested .bash_profile alias: alias t='/dir/to/todo.rb'
todos = []
pwd = Dir.getwd
todo_file = pwd + '/.todo'
#create the .todo file
unless File.exists? todo_file
if !File.new(todo_file, 'w')
puts 'There was an issue creating the .todo file at #{pwd}'
end
end
#read all of the todo's and store them in todos[]
File.open(todo_file, 'r') do |f|
while line = f.gets do
todos << line
end
end
if ARGV.length >= 1
remove = false
verbose = false
case ARGV[0]
when '-r'
remove = true
when '-vr'
remove = verbose = true
when '--help'
puts "todo.rb usage:"
puts " todo.rb\t\t list all active tasks"
puts " todo.rb <task>\t create a task described by <task>"
puts " todo.rb -r <string>\t deletes all tasks matching <string>"
puts " todo.rb -vr <string>\t show what tasks would be deleted with a given <string>"
exit(0)
end
#we need to have more than just the flags in order to delete stuff
if remove and ARGV.length > 1
#lets get a list of all of the todo's that match the argv[1]
todos_to_remove = todos.find_all{|todo| /#{ARGV[1..-1].join(' ')}/ =~todo }
if verbose
if not todos_to_remove.empty?
puts "/#{ARGV[1..-1].join(' ')}/ would remove: "
todos_to_remove.each do |todo|
puts " • #{todo}"
end
else
puts "/#{ARGV[1..-1].join(' ')}/ doesn't match any tasks"
end
else
todos_left = todos - todos_to_remove
File.open(todo_file, 'w') do |file|
todos_left.each do |todo|
file.puts todo
end
end
end
elsif remove and ARGV.length == 1
puts "you forgot a string for me to match\nex: todo.rb -r make stuff"
else
todos << ARGV[0..-1].join(' ') #get all the words
File.open(todo_file, 'w') do |file|
todos.each do |todo|
file.puts "#{todo}"
end
end
end
else
#list the todo's
puts (todos.empty? ? "ka-pow! nothing left! I'm proud of you" : "listing todos:")
todos.each do |todo|
puts " • #{todo}"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment