Advertisement
Guest User

Untitled

a guest
Nov 24th, 2014
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.20 KB | None | 0 0
  1. -- Rob's ToDo List and Message board
  2.  
  3. -- NB: I realise my code is very messy
  4. -- But im not 100% sure what im doing!
  5.  
  6.  
  7. --Pulled from:
  8. -- http://www.computercraft.info/forums2/index.php?/topic/14293-wanted-the-best-community-made-programs-to-include-in-computercraft-seeking-submissions/
  9.  
  10.  
  11.  
  12.  
  13. --Peripheral Wraps
  14. todo = peripheral.wrap("right")
  15. mes = peripheral.wrap("left")
  16.  
  17. --Check For Monitors
  18. if not todo then
  19. error("No Monitor on Right Side",0)
  20. end
  21. if not mes then
  22. error("No Monitor on Left Side",0)
  23. end
  24.  
  25. -- Text Scale
  26. todo.setTextScale(.5)
  27. mes.setTextScale(.5)
  28.  
  29. --Define Tables
  30. todoData = {}
  31. mesData = {}
  32.  
  33. --Make Directory and Files
  34. if fs.exists("data") == false then
  35. fs.makeDir("data")
  36. end
  37. if fs.exists("data/todo") == false then
  38. file = fs.open("data/todo","w")
  39. file.close()
  40. end
  41. if fs.exists("data/mes") == false then
  42. file = fs.open("data/mes","w")
  43. file.close()
  44. end
  45.  
  46. --Check for Advanced Computers & Monitors
  47. if not term.isColor() then
  48. error("This Program Must Be Run on an Advanced Computer",0)
  49. end
  50. if not todo.isColor() or not mes.isColor() then
  51. error("You Must Have Color Monitors on Either Side",0)
  52. end
  53.  
  54. --Check Monitor Size
  55. w,h = mes.getSize()
  56. if w ~= 36 or h ~= 38 then
  57. error("Left Monitor Not Correct Size,\n3 High and 2 Wide is the Correct Size.", 0)
  58. end
  59. w,h = todo.getSize()
  60. if w ~= 36 or h ~= 38 then
  61. error("Right Monitor Not Correct Size,\n3 High and 2 Wide is Correct Size.",0)
  62. end
  63.  
  64.  
  65. -- Text Functions
  66. function center(mon,text,line,colour)
  67. w,h = todo.getSize()
  68. if mon == "todo" then
  69. todo.setCursorPos((w-string.len(text))/2+1,line)
  70. todo.setTextColor(colour)
  71. todo.write(text)
  72. todo.setTextColor(colors.white)
  73. else
  74. mes.setCursorPos((w-string.len(text))/2+1,line)
  75. mes.setTextColor(colour)
  76. mes.write(text)
  77. mes.setTextColor(colors.white)
  78. end
  79. end
  80.  
  81. function wr(mon,x,y,text,colour)
  82. if mon == "todo" then
  83. todo.setCursorPos(x,y)
  84. todo.setTextColor(colour)
  85. todo.write(text)
  86. todo.setTextColor(colors.white)
  87. else
  88. mes.setCursorPos(x,y)
  89. mes.setTextColor(colour)
  90. mes.write(text)
  91. mes.setTextColor(colors.white)
  92. end
  93. end
  94.  
  95. -- table functions
  96. function tableW(name,text)
  97. if name == "todo" then
  98. table.insert(todoData, text)
  99. else
  100. table.insert(mesData,text)
  101. end
  102. end
  103.  
  104. --File Functions
  105. function getData()
  106. local file = fs.open("data/todo","r")
  107. local i = 1
  108. while true do
  109. todoData[i] = file.readLine()
  110. if todoData[i] == nil then
  111. break
  112. else
  113. i = i + 1
  114. end
  115. end
  116. file.close()
  117.  
  118. local file = fs.open("data/mes","r")
  119. for line in file.readLine do
  120. local location = (line:find(":",1))
  121. if location then
  122. local usr = line:sub(1, location - 1)
  123. local msg = line:sub(location + 1)
  124. table.insert(mesData, {user = usr, message = msg})
  125. end
  126. end
  127.  
  128. file.close()
  129. end
  130.  
  131. function saveData()
  132. local file = fs.open("data/todo","w")
  133. for i = 1, #todoData do
  134. file.writeLine(todoData[i])
  135. end
  136. file.close()
  137.  
  138. local file = fs.open("data/mes","w")
  139. for index, data in pairs(mesData) do
  140. file.writeLine(data.user..":"..data.message)
  141. end
  142. file.close()
  143. end
  144.  
  145. -- Monitors
  146. function todoMon()
  147. todo.clear()
  148. center("todo","To-Do List",1,colors.magenta)
  149. posx = 1
  150. posy = 3
  151. for i=1, #todoData do
  152. wr("todo",posx,posy,tostring(i),colors.lime)
  153. posx = posx + 2
  154. wr("todo",posx,posy,todoData[i],colors.white)
  155. posx = posx - 2
  156. posy = posy + 2
  157. end
  158. end
  159.  
  160. function mesMon()
  161. mes.clear()
  162. center("mes","Message Board",1,colors.magenta)
  163. pX = 2
  164. pY = 3
  165. for i=1, #mesData do
  166. wr("mes",pX,pY,tostring(i),colors.lime)
  167. pX = pX + 3
  168. wr("mes",pX,pY,mesData[i].user..":",colors.lightBlue)
  169. pY = pY + 1
  170. wr("mes",pX,pY,mesData[i].message,colors.white)
  171. pX = pX - 3
  172. pY = pY + 2
  173. end
  174. end
  175.  
  176. -- Table functions
  177. function clear(name)
  178. if name == "todo" then
  179. for i=1, #todoData do
  180. table.remove(todoData)
  181. end
  182. end
  183. if name == "mes" then
  184. for i=1, #mesData do
  185. table.remove(mesData)
  186. end
  187. end
  188. end
  189.  
  190.  
  191. -- Gui Things
  192. -- Main Menu Table
  193. local menu_options = {
  194. [1] = {text="To-Do List", color = colors.lightBlue},
  195. [2] = {text="Message Board", color = colors.lightBlue}
  196. }
  197.  
  198. --ToDo Menu Table
  199. local todo_options = {
  200. [1] = {text="Add Task", color = colors.lime},
  201. [2] = {text="Delete Task", color = colors.red}
  202. }
  203.  
  204. --Mes Menu Table
  205. local mes_options = {
  206. [1] = {text="Add Message", color = colors.lime},
  207. [2] = {text="Delete Message", color = colors.red}
  208. }
  209. -- Find Terminal X Y
  210. local termX, termY = term.getSize()
  211.  
  212. -- Draw Main Menu
  213. function mainMenuDraw(selected)
  214. local yPos = termY/2 - #menu_options/2
  215. for index, data in pairs(menu_options) do
  216. menu_options[index].bounds = {
  217. x1 = termX/2 - (#data.text+4)/2,
  218. x2 = termX/2 + (#data.text+4)/2,
  219. y = yPos
  220. }
  221. term.setTextColor(data.color)
  222. term.setCursorPos(data.bounds.x1, data.bounds.y)
  223.  
  224. local text =
  225. index==selected and "[ "..data.text.." ]" or
  226. " "..data.text.." "
  227. term.write(text)
  228. yPos = yPos + 2
  229. end
  230. end
  231.  
  232. -- Draw ToDo Menu
  233. function todoMenuDraw(selected)
  234. local yPos = termY/2 - #todo_options/2
  235. for index, data in pairs(todo_options) do
  236. todo_options[index].bounds = {
  237. x = termX/2 - (#data.text+4)/2,
  238. y = yPos
  239. }
  240. term.setTextColor(data.color)
  241. term.setCursorPos(data.bounds.x,data.bounds.y)
  242.  
  243. local text =
  244. index == selected and "[ "..data.text.." ]" or
  245. " "..data.text.." "
  246.  
  247. term.write(text)
  248. yPos = yPos + 2
  249. end
  250. end
  251.  
  252. -- Draw Message Menu
  253. function mesMenuDraw(selected)
  254. local yPos = termY/2 - #mes_options/2
  255. for index, data in pairs(mes_options) do
  256. mes_options[index].bounds = {
  257. x = termX/2 - (#data.text+4)/2,
  258. y = yPos
  259. }
  260. term.setTextColor(data.color)
  261. term.setCursorPos(data.bounds.x,data.bounds.y)
  262.  
  263. local text =
  264. index == selected and "[ "..data.text.." ]" or
  265. " "..data.text.." "
  266.  
  267. term.write(text)
  268. yPos = yPos + 2
  269. end
  270. end
  271.  
  272. -- Check Click Position
  273. function checkClick(x,y)
  274. for index, data in pairs(menu_options) do
  275. if x >= data.bounds.x1 and x <= data.bounds.x2 and y == data.bounds.y then
  276. return index
  277. end
  278. end
  279. return false
  280. end
  281.  
  282. --Display Main Menu
  283. function mainMenu()
  284. todoMon()
  285. mesMon()
  286. term.clear()
  287. term.setCursorPos(termX/2-14,1)
  288. term.setTextColor(colors.magenta)
  289. term.write("Message Board and To-Do List")
  290.  
  291.  
  292. local selector = 1
  293. while true do
  294. mainMenuDraw(selector)
  295. local e = {os.pullEvent()}
  296. if e[1] == "key" then
  297. if e[2] == keys.down then
  298. selector = selector < #menu_options and selector+1 or 1
  299. elseif e[2] == keys.up then
  300. selector = selector > 1 and selector-1 or #menu_options
  301. elseif e[2] == keys.enter then
  302. break
  303. end
  304. end
  305. end
  306.  
  307. term.clear()
  308. if selector == 1 then
  309. todoMenu()
  310. elseif selector == 2 then
  311. mesMenu()
  312. end
  313. end
  314.  
  315. --Display ToDo Menu
  316. function todoMenu()
  317. todoMon()
  318. mesMon()
  319.  
  320. term.clear()
  321.  
  322. term.setCursorPos(termX/2-5,1)
  323. term.setTextColor(colors.magenta)
  324. term.write("To-Do List")
  325.  
  326. term.setCursorPos(termX/2-19 ,2)
  327. term.setTextColor(colors.white)
  328. term.write("Press Backspace To Go Back To Main Menu")
  329.  
  330. local selector = 1
  331. while true do
  332. todoMenuDraw(selector)
  333. local e = {os.pullEvent()}
  334. if e[1] == "key" then
  335. if e[2] == keys.down then
  336. selector = selector < #todo_options and selector+1 or 1
  337. elseif e[2] == keys.up then
  338. selector = selector > 1 and selector-1 or #todo_options
  339. elseif e[2] == keys.backspace then
  340. mainMenu()
  341. elseif e[2] == keys.enter then
  342. break
  343. end
  344. end
  345. end
  346.  
  347. term.clear()
  348. term.setCursorPos(1,1)
  349. if selector == 1 then
  350. addTaskMenu()
  351. elseif selector == 2 then
  352. deleteTaskMenu()
  353. end
  354. end
  355.  
  356. --Display Message Menu
  357. function mesMenu()
  358. todoMon()
  359. mesMon()
  360.  
  361. term.clear()
  362.  
  363. term.setCursorPos(termX/2-6,1)
  364. term.setTextColor(colors.magenta)
  365. term.write("Message Board")
  366.  
  367. term.setCursorPos(termX/2-19 ,2)
  368. term.setTextColor(colors.white)
  369. term.write("Press Backspace To Go Back To Main Menu")
  370.  
  371. local selector = 1
  372. while true do
  373. mesMenuDraw(selector)
  374. local e = {os.pullEvent()}
  375. if e[1] == "key" then
  376. if e[2] == keys.down then
  377. selector = selector < #mes_options and selector+1 or 1
  378. elseif e[2] == keys.up then
  379. selector = selector > 1 and selector-1 or #mes_options
  380. elseif e[2] == keys.backspace then
  381. mainMenu()
  382. elseif e[2] == keys.enter then
  383. break
  384. end
  385. end
  386. end
  387.  
  388. term.clear()
  389. term.setCursorPos(1,1)
  390. if selector == 1 then
  391. addMesMenu()
  392. elseif selector == 2 then
  393. deleteMesMenu()
  394. end
  395. end
  396.  
  397. --Add Task Menu
  398. function addTaskMenu()
  399. term.clear()
  400. term.setCursorPos(termX/2-13 ,1)
  401. term.setTextColor(colors.white)
  402. term.write("Type Task Then Press Enter")
  403.  
  404. term.setCursorPos(2,3)
  405. term.setTextColor(colors.lime)
  406. term.write("> ")
  407.  
  408. term.setTextColor(colors.white)
  409. local task = read()
  410.  
  411. if string.len(task) > 32 then
  412. term.clear()
  413. term.setCursorPos(termX/2-10,1)
  414. term.setTextColor(colors.red)
  415. term.write("Error: Task too long!")
  416. sleep(2)
  417. todoMenu()
  418. elseif #todoData == 18 then
  419. term.clear()
  420. term.setCursorPos(termX/2-11,1)
  421. term.setTextColor(colors.red)
  422. term.write("Error: ToDo List Full!")
  423. sleep(2)
  424. todoMenu()
  425. else
  426. tableW("todo", task)
  427. saveData()
  428. todoMenu()
  429. end
  430. end
  431.  
  432. -- Delete Task Menu
  433. function deleteTaskMenu()
  434. term.clear()
  435. term.setCursorPos(termX/2-14 ,1)
  436. term.setTextColor(colors.white)
  437. term.write("Type Number of Task To Delete")
  438. term.setCursorPos(termX/2-11 ,2)
  439. term.write("Or Type 'all' to clear")
  440. term.setCursorPos(2,3)
  441. term.setTextColor(colors.lime)
  442. term.write("> ")
  443. term.setTextColor(colors.white)
  444. number = read()
  445. if number == "all" then
  446. clear("todo")
  447. elseif tonumber(number) == nil then
  448. term.clear()
  449. term.setCursorPos(termX/2-10 ,1)
  450. term.setTextColor(colors.red)
  451. term.write("Error: Not A Number!")
  452. sleep(2)
  453. else
  454. table.remove(todoData, number)
  455. end
  456. saveData()
  457. todoMenu()
  458. end
  459.  
  460. -- Add Message Menu
  461. function addMesMenu()
  462. term.clear()
  463. term.setCursorPos(termX/2-11 ,1)
  464. term.setTextColor(colors.white)
  465. term.write("Please Type Your Name:")
  466. term.setCursorPos(2,3)
  467. term.setTextColor(colors.lime)
  468. term.write("> ")
  469. term.setTextColor(colors.white)
  470. local username = read()
  471.  
  472. term.clear()
  473. term.setCursorPos(termX/2-14 ,1)
  474. term.setTextColor(colors.white)
  475. term.write("Type Message Then Press Enter")
  476. term.setCursorPos(13 ,2)
  477. term.write("Max length is here -->|")
  478.  
  479. term.setCursorPos(2,4)
  480. term.setTextColor(colors.lime)
  481. term.write("> ")
  482. term.setTextColor(colors.white)
  483. local mes = read()
  484.  
  485. if string.len(mes) > 32 then
  486. term.clear()
  487. term.setCursorPos(termX/2-10,1)
  488. term.setTextColor(colors.red)
  489. term.write("Error: Message Too Long!")
  490. sleep(2)
  491. mesMenu()
  492. elseif #mesData == 12 then
  493. term.clear()
  494. term.setCursorPos(termX/2-13 ,1)
  495. term.setTextColor(colors.red)
  496. term.write("Error: Message Board Full!" )
  497. sleep(2)
  498. mesMenu()
  499. else
  500. n = #mesData + 1
  501. mesData[n] = {
  502. user = username,
  503. message = mes
  504. }
  505.  
  506. saveData()
  507. mesMenu()
  508. end
  509. end
  510.  
  511. -- Delete Message Menu
  512. function deleteMesMenu()
  513. term.clear()
  514. term.setCursorPos(termX/2-16 ,1)
  515. term.setTextColor(colors.white)
  516. term.write("Type Number of Message To Delete")
  517. term.setCursorPos(termX/2-11 ,2)
  518. term.write("Or Type 'all' To Clear")
  519. term.setCursorPos(2,3)
  520. term.setTextColor(colors.lime)
  521. term.write("> ")
  522. term.setTextColor(colors.white)
  523. number = read()
  524. if number == "all" then
  525. clear("mes")
  526. elseif tonumber(number) == nil then
  527. term.clear()
  528. term.setCursorPos(termX/2-10 ,1)
  529. term.setTextColor(colors.red)
  530. term.write("Error: Not a Number!")
  531. sleep(2)
  532. else
  533. table.remove(mesData, number)
  534. end
  535. saveData()
  536. mesMenu()
  537. end
  538.  
  539. getData()
  540. mainMenu()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement