Advertisement
summerSkies

todo

Dec 31st, 2013
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 2.49 KB | None | 0 0
  1. require 'colorize' # I can haz yellow?!
  2.  
  3. #def writeFile(list)
  4.     #File.truncate(0)
  5.     #File.open("todo.txt", "r+") do |file, list, index|
  6.         #file.puts list[index]
  7.     #end
  8. #end
  9.  
  10. # Show to-do list
  11. def showList(list)
  12.     if list.length == 0
  13.         puts "There's nothing on your to-do list."
  14.     else
  15.         list.each_with_index do |item, index|
  16.             # + 1 so user doesn't see 0
  17.             puts "#{index+1}. #{item}"
  18.         end
  19.     end
  20. end
  21.  
  22. # Add to list
  23. def addItem!(list, new)
  24.     File.open($file, "a") do |file|
  25.         list.push(new)
  26.         file.puts list[(list.length - 1)]
  27.         puts "\"#{list[(list.length - 1)]}\" added."
  28.     end
  29. end
  30.  
  31. # Remove from list
  32. def removeItem!(list)
  33.     if list.length == 0
  34.         puts "There's nothing on your to-do list."
  35.     else
  36.         puts "Number to remove:"
  37.         index = gets.chomp.to_i
  38.             if index < 1 && index >= (list.length + 1)
  39.             # remember that the index is - 1  what the user sees on the screen
  40.                 puts "Enter a valid number."
  41.                 removeItem!(list)
  42.             else
  43.                 list.delete_at(index - 1)
  44.                 # We have to re-write the modified list now,
  45.                 # starting at the very begining of the file (indicated by '+'
  46.                 # as opposed to 'a'"
  47.                 File.truncate($file, 0)    # WARNING: Truncate isn't cross platform. It won't work on windows or mac.
  48.                 #File.open("todo.txt", "r+") do |file, list, index|
  49.                     list.each_with_index do |list, index|
  50.                         $file.puts list[index]
  51.                     # Ruby is error:
  52. # `block in removeItem!': undefined local variable or method `file' for main:Object (NameError)
  53.                     end
  54.                 #end
  55.             end
  56.     end
  57. end
  58.  
  59. # Edit list item
  60. def editItem!(list)
  61.     if list.length == 0
  62.         puts "There's nothing on your to-do list."
  63.     else
  64.         puts "Number to edit:"
  65.         index = gets.chomp.to_i
  66.             if index < 1 && index >= (list.length + 1)
  67.                 puts "Enter a valid number."
  68.                 editItem!(list)
  69.             else
  70.                 edited_item = gets.chomp
  71.                 list[index-1] = edited_item
  72.                 File.open($file, "r+") do |file|
  73.                 # re-write the modified list from the begining!
  74.                     file.puts list
  75.                 end
  76.             end
  77.     end
  78. end
  79.  
  80. # User interface
  81. def menu(list)
  82.     puts "\n(S)how list \t (A)dd item \t (R)emove item \t (E)dit item \t (Q)uit".green
  83.     option = gets.chomp.downcase
  84.     case option
  85.         when  's'
  86.             showList(list)
  87.         when 'a'
  88.             puts "What do you want to add?"
  89.             text =  gets.chomp
  90.             addItem!(list, text)
  91.         when 'r'
  92.             removeItem!(list)
  93.         when 'e'
  94.             editItem!(list)
  95.         when 'q'
  96.             exit
  97.         else
  98.             menu(list)
  99.     end
  100. menu(list)
  101. end
  102.  
  103. # Let's go!
  104. $file = "todo.txt"
  105. File.open($file, "a+")
  106. todo_list = IO.readlines($file)
  107. menu(todo_list)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement