Top Level Namespace
Defined Under Namespace
Classes: Todo
Instance Method Summary collapse
- #add(task) ⇒ Object
- #check(task) ⇒ Object
- #delete ⇒ Object
- #error(type) ⇒ Object
- #help ⇒ Object
- #remove(task) ⇒ Object
- #show ⇒ Object
- #uncheck(task) ⇒ Object
Instance Method Details
#add(task) ⇒ Object
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
# File 'lib/todo/add.rb', line 1 def add (task) if File.exist?("TODO") content = File.read("TODO") if content[task] error(:alreadythere) end end File.open("TODO", "a") do |file| file.puts(task) end puts " "+Rainbow("Added").green.bright()+" => "+Rainbow(task).blue.bright() end |
#check(task) ⇒ Object
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 |
# File 'lib/todo/check.rb', line 1 def check (task) content = File.read("TODO") if task["..."] pattern = task.chomp("...") if content =~ /\##{pattern}(.*)\n/ error(:alreadydone) elsif content =~ /#{pattern}(.*)\n/ content.gsub!(/#{pattern}(.*)\n/, "##{pattern}#{$1}\n") else error(:notfound) end output = pattern + $1 else if content =~ /\##{task}\n/ error(:alreadydone) elsif content =~ /#{task}\n/ content.sub!("#{task}\n", "##{task}\n") else error(:notfound) end output = task end File.write("TODO", content) puts " "+Rainbow("Checked").green.bright()+" => "+Rainbow(output).blue.bright() end |
#delete ⇒ Object
1 2 3 4 5 6 7 8 |
# File 'lib/todo/delete.rb', line 1 def delete if File.exist?("TODO") File.delete("TODO") puts " "+Rainbow("Deleted TODO list").green.bright() else error(:nofile) end end |
#error(type) ⇒ Object
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/todo/error.rb', line 1 def error (type) case type when :cmd output = "Invalid command" when :task output = "You need to specify a task" when :nofile output = "There is no TODO file in this directory" when :notfound output = "The given task is not in your TODO list" when :alreadythere output = "The given task is already in your TODO list" when :alreadydone output = "The given task is already checked" when :notchecked output = "The given task is not checked" end puts " "+Rainbow(output).red.bright() exit end |
#help ⇒ Object
1 2 3 4 5 6 7 8 9 10 11 12 |
# File 'lib/todo/help.rb', line 1 def help puts "\n" puts " $ todo #{Rainbow('[command]').blue.bright} #{Rainbow('[task]').green.bright}\n\n" puts " #{Rainbow('show').blue.bright} => Shows the TODO list" puts " #{Rainbow('delete').blue.bright} => Deletes the TODO list" puts " #{Rainbow('add').blue.bright} #{Rainbow('[task]').green.bright} => Adds a task (creates file if not available)" puts " #{Rainbow('rm').blue.bright} #{Rainbow('[task]').green.bright} => Removes a task (removes file when last task)" puts " #{Rainbow('check').blue.bright} #{Rainbow('[task]').green.bright} => Markes a task as done" puts " #{Rainbow('uncheck').blue.bright} #{Rainbow('[task]').green.bright} => Unmarks a task when already marked\n\n" puts " Pro tip: You can match the task #{Rainbow('\'Do something\'').blue.bright} with #{Rainbow('\'Do...\'').blue.bright}" puts "\n" end |
#remove(task) ⇒ Object
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 |
# File 'lib/todo/remove.rb', line 1 def remove (task) content = File.read("TODO") if task["..."] pattern = task.chomp("...") if content =~ /\##{pattern}(.*)\n/ content.gsub!(/\##{pattern}(.*)\n/, "") elsif content =~ /#{pattern}(.*)\n/ content.gsub!(/#{pattern}(.*)\n/, "") else error(:notfound) end output = pattern + $1 else if content =~ /\##{task}\n/ content.sub!("\##{task}\n", "") elsif content =~ /#{task}\n/ content.sub!("#{task}\n", "") else error(:notfound) end output = task end File.write("TODO", content) puts " "+Rainbow("Removed").green.bright()+" => "+Rainbow(output).blue.bright() File.delete("TODO") if content == "" end |
#show ⇒ Object
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
# File 'lib/todo/show.rb', line 3 def show error(:nofile) unless File.exist?("TODO") puts Rainbow("\n Your TODO list:").blue.bright()+" (#{Dir.pwd}/TODO)\n\n" File.read("TODO").each_line do |line| if line[0,1] == "#" line.sub!("#", "") line.sub!("\n", "") puts " » "+ Rainbow(line +" ✔").green.bright else puts " » "+ line end end puts "\n" end |
#uncheck(task) ⇒ Object
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 |
# File 'lib/todo/uncheck.rb', line 1 def uncheck (task) content = File.read("TODO") if task["..."] pattern = task.chomp("...") if content =~ /\##{pattern}(.*)\n/ content.gsub!(/##{pattern}(.*)\n/, "#{pattern}#{$1}\n") elsif content =~ /#{pattern}(.*)\n/ error(:notchecked) else error(:notfound) end output = pattern + $1 else if content =~ /\##{task}\n/ content.sub!("##{task}\n", "#{task}\n") elsif content =~ /#{task}\n/ error(:notchecked) else error(:notfound) end output = task end File.write("TODO", content) puts " "+Rainbow("Unchecked").green.bright()+" => "+Rainbow(output).blue.bright() end |