DMeville

Untitled

May 21st, 2013
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function load(name)
  2.     local file = fs.open(name, "r");
  3.     if file ~= nil then
  4.         local data = file.readAll();
  5.         file.close();
  6.         return textutils.unserialize(data);
  7.     else
  8.         return {}
  9.     end
  10. end
  11.  
  12. hasMon = false;
  13. list = load("todoList");
  14. i = 1;
  15. len = 0;
  16. if peripheral.isPresent("left") then
  17.     hasMon = true;
  18.     mon = peripheral.wrap("top");
  19.     print("Monitor was found!");
  20.     mon.clear();
  21.     mon.setCursorPos(1,1);
  22. end
  23.  
  24. function save(table, name)
  25.     local file = fs.open(name, "w");
  26.     file.write(textutils.serialize(table));
  27.     file.close();
  28. end
  29.  
  30.  
  31.  
  32. function nl()
  33.     if hasMon == true then
  34.     local x,y = mon.getCursorPos();
  35.     mon.setCursorPos(1, y+1);
  36.     end
  37. end
  38.  
  39. function printm(str)
  40.     if type(str) ~= "string" then return end
  41.     mon.write(str);
  42.     nl();
  43. end
  44.  
  45. function drawScreen()
  46.     if hasMon == true then
  47.         mon.setCursorPos(1,1);
  48.         mon.clear();
  49.         for j = 1,len do
  50.             printm(list[j]);
  51.         end
  52.     end
  53. end
  54.  
  55. function add()
  56.     print("Enter your task: ");
  57.     listAns = read();
  58.     print("\n");
  59.     if type(listAns) == "string" then
  60.         list[i] = " - " .. listAns;
  61.         printm(list[i]);
  62.         i = i +1;
  63.         len = len +1;
  64.     end
  65.     save(list, "todoList");
  66. end
  67.  
  68. function removeFromList(n)
  69.     table.remove(list, n);
  70. end
  71.  
  72. function remove()
  73.     print("Which task would you like to remove?");
  74.     removeAns = read();
  75.     print("\n");
  76.     if type(removeAns) == "string" then
  77.         removeAns = tonumber(removeAns);
  78.         if removeAns <= len then
  79.             removeFromList(removeAns);
  80.         end
  81.     end
  82.     drawScreen();
  83.     save(list, "todoList");
  84. end
  85.  
  86. function start()
  87.     while true do
  88.         print("What would you like to do?");
  89.         print(" - Type \"add\" to add an item");
  90.         print(" - Type \"remove\" to add an item");
  91.         print(" - Type \"quit\" to save and quit");
  92.         ans = read();
  93.         if type(ans) == "string" then
  94.             if ans == "add" then add() end
  95.             if ans == "remove" then remove() end
  96.             if ans == "quit" then
  97.                 printm("Goodbye!");
  98.                 os.shutdown();
  99.             end
  100.         end
  101.     end
  102. end
  103.  
  104. start();
Advertisement
Add Comment
Please, Sign In to add comment