Advertisement
Guest User

Untitled

a guest
Oct 21st, 2014
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.67 KB | None | 0 0
  1. local monitor = peripheral.wrap("right")
  2. local sFile = "MessageBoardLog.txt"
  3. local w, h = monitor.getSize()
  4. local sLog = {}
  5.  
  6.  
  7. function writeCenter(sString, yPos)
  8. x = math.floor((w / 2) - (#sString / 2))
  9. monitor.setCursorPos(x, yPos)
  10. monitor.write(sString)
  11. end
  12.  
  13. function saveLog()
  14. local sFile = "MessageBoardLog.txt"
  15. if fs.exists(sFile) then
  16. local hRead = assert(fs.open(sFile, "r")) -- assert will error for us if we can't open the file.
  17. hRead.close() -- Close the handle! This is extremely important, as not doing so can cause errors when writin/opening the file.
  18. end
  19.  
  20. hWrite = fs.open(sFile, "w") -- w for write! Note that this will also clear the file.
  21. hWrite.write(textutils.serialize(sLog))
  22. hWrite.close()
  23.  
  24. return
  25. end
  26.  
  27. function loadLog()
  28. if fs.exists(sFile) == true then
  29. local file = fs.open(sFile,"r")
  30. local data = file.readAll()
  31. file.close()
  32. sLog = textutils.unserialize(data)
  33. for i=1,#sLog do
  34. print(sLog[i])
  35. writeLine( sLog[i] )
  36. end
  37. else
  38. print("file does not exist, check path..")
  39. end
  40. end
  41.  
  42. function addMessage()
  43. shell.run("clear")
  44. print ("Enter Message")
  45. local input = read()
  46. table.insert(sLog, input)
  47. writeLine(input)
  48. saveLog()
  49. end
  50.  
  51. function writeLine(text)
  52. local _,row=monitor.getCursorPos()
  53. local _,height=monitor.getSize()
  54.  
  55. --write the line
  56. monitor.write(text)
  57. --move to next row
  58. row=row+1
  59. --if this went off the screen, scroll
  60.  
  61. --set to start of next line
  62. monitor.setCursorPos(1,row)
  63. end
  64.  
  65. monitor.clear()
  66. loadLog()
  67. while true do
  68. addMessage()
  69. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement