Hastwell

TodoList-NG-pre16

Jan 1st, 2015
179
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.10 KB | None | 0 0
  1. --retrieve perph IDs
  2. local periList = peripheral.getNames()
  3. for i = 1, #periList do
  4.         if peripheral.getType(periList[i]) == "monitor" then
  5.           mon = peripheral.wrap(periList[i])
  6.           print("Monitor wrapped as... "..periList[i])
  7.         end
  8. end
  9.  
  10. if mon == nil then -- if no monitor, don't redirect, run on main screen
  11.  mon = term.native
  12. else
  13.  term.redirect(mon)
  14. end
  15.  
  16. local currentPage = 1 --start on page 1!
  17. local todo = {} --begin with empty todo list
  18. local width,height = mon.getSize() --get size of screen
  19. local line = height - 7 --6 lines of buttons, 1 line of border, remainder for list
  20.  
  21. function textCol(color) --useful function, only set color if a color monitor/computer
  22.   if mon.isColor() then
  23.     term.setTextColor(color)
  24.   end
  25. end
  26.  
  27.  
  28. function openTODO()
  29.   local file = fs.open("todolist","r") --file should be called todolist
  30.   if file ~= nil then  --if file exists
  31.     local data = file.readAll()  --read entire file
  32.     file.close() --close to ensure writing later
  33.     if (textutils.unserialize(data)==nil) then --if file is invalid or blank data
  34.       todo[1]="make todo list" --setup dummy table
  35.     else
  36.       todo = textutils.unserialize(data) --if its full, use data
  37.      end
  38.   else
  39.    todo[1]="make todo list" --if file doesn't exist, setup dummy table
  40.   end
  41. end
  42.  
  43. function saveTODO() --reverse of previous function
  44.   local file = fs.open("todolist","w")
  45.   file.write(textutils.serialize(todo))
  46.   file.close()
  47. end
  48.  
  49. function displayPage(p) --change table display
  50.  textCol(colors.blue) --blue is nice
  51.   term.setCursorPos(1,6) --start on line 6
  52.   for i = (1+((p-1)*line)),(p*line) do --page 1 starts at 1, if 6 lines, ends on 6. Improve?
  53.     if todo[i] ~= nil then  --if line isn't blank
  54.     print(" "..todo[i]) --print line!
  55.     end
  56.   end
  57. end
  58.  
  59. function printButtons() --basic add/cycle buttons
  60.   term.setCursorPos(1,2)
  61.   textCol(colors.green)
  62.   term.write(" Add item")
  63.   term.setCursorPos(1,4)
  64.   textCol(colors.yellow)
  65.   term.write(" Cycle page")
  66. end
  67.  
  68. function itemRemove(itm) --doesn't work if in a function, investigage why...
  69. term.clear()
  70. term.setCursorPos(2,2)
  71. term.write("Really remove item "..itm.."?")
  72. term.setCursorPos(2,4)
  73. term.write(todo[itm])
  74. term.setCursorPos(2,6)
  75. textCol(colors.green)
  76. term.write("YES")
  77. textCol(colors.red)
  78. term.setCursorPos(2,8)
  79. term.write("NO")
  80. --term.write("Y for yes, N for no")
  81. --os.pullEvent("char")
  82. y=1
  83. while (y~=6 and y~=8) do
  84. --if mon == term.native then
  85. -- event,but,x,y = os.pullEvent("mouse_click")
  86. -- else
  87. -- event,side,x,y = os.pullEvent("monitor_touch")
  88. -- end
  89. event,side,x,y = os.pullEvent("monitor_touch")
  90. end
  91. if y == 6 then
  92. table.remove(todo,tonumber(itm))
  93. end
  94. end
  95. --end broke function -------------------------------------
  96.  
  97. function addItem() --pretty simple
  98.   term.clear() --clear terminal
  99.   term.setCursorPos(2,2) --indent
  100.   print("please see computer for input") --display on monitor
  101.   term.redirect(term.native) --reset to terminal
  102.   term.clear() --clear terminal
  103.   print("Please enter new item:")
  104.   local input = read() --get item
  105.  
  106. for i=#todo+1,2,-1 do --for every item in the table,
  107.  todo[i] = todo[i-1] --shift 1 position over
  108.  end
  109. todo[1] = input --add to TOP of list. This is optional and can be easily undone to add to the bottom instead
  110. print("thanks!") --output on terminal
  111. term.redirect(mon) --redirect to monitor (if applicable)
  112. end
  113.  
  114.  
  115.  
  116. --initial printing
  117. openTODO()
  118. term.clear()
  119. printButtons()
  120. displayPage(1)
  121.  
  122. while (true) do --forever loop!
  123.  
  124. -- if mon == term.native() then --if get where the user clicks, monitor or computer)
  125. --  event,but,x,y = os.pullEvent("mouse_click")
  126. -- else
  127. --  event,side,x,y = os.pullEvent("monitor_touch")
  128. -- end
  129.  
  130. event,side,x,y = os.pullEvent("monitor_touch")
  131.  
  132. if (y == 2) then -- user clicks "add item
  133.   addItem()
  134. end
  135.  
  136. if (y == 4) then -- user clicks cycle page
  137.   if (currentPage >= #todo/(line)) then --#todo/line = max pages
  138.    currentPage = 1
  139.   else
  140.    currentPage = currentPage +1
  141.   end
  142. end
  143.  
  144. if y >= 6 and y < 6+line then --user clicks a todo item
  145.   itm = ((currentPage-1)*line)+y-5
  146.   if todo[itm]~=nil then --item actually exists
  147.     --itemRemove(itm)
  148.     term.clear()
  149.     term.setCursorPos(2,2)
  150.     term.write("Really remove item "..itm.."?")
  151.     term.setCursorPos(2,4)
  152.     term.write(todo[itm])
  153.     term.setCursorPos(2,6)
  154.     textCol(colors.green)
  155.     term.write("YES")
  156.     textCol(colors.red)
  157.     term.setCursorPos(2,8)
  158.     term.write("NO")
  159.     y=1
  160.     while (y~=6 and y~=8) do
  161.      -- if mon == term.native() then
  162.      --  event,but,x,y = os.pullEvent("mouse_click")
  163.      -- else
  164.      --  event,side,x,y = os.pullEvent("monitor_touch")
  165.      -- end
  166.      event,side,x,y = os.pullEvent("monitor_touch")
  167.     end
  168.     if y == 6 then
  169.      table.remove(todo,tonumber(itm))
  170.     end
  171.   end
  172. --ends remove code, MUCH MUCH neater in a function, but returns NIL errors if it is in one...
  173. end
  174.  
  175. term.clear() --reset terminal
  176. printButtons() --redraw everything
  177. term.setCursorPos(1,1) --not needed, but keeps terminal tidy
  178. displayPage(currentPage)
  179.  
  180. saveTODO() --save list so any additions/removals are preserved in case of crash.
  181. end
Advertisement
Add Comment
Please, Sign In to add comment