Advertisement
Kman3107

NoteComputerCraft

Jan 10th, 2017
176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. -- JHC's Quick Note Program
  2.  
  3. -- Prevent Termination
  4. os.pullEvent = os.pullEventRaw
  5.  
  6. -- Functions
  7. function readFile(Name)
  8.     local file = fs.open(Name, "r")
  9.     local fileLine = {}
  10.    
  11.     i = 1
  12.     while true do
  13.         line = file.readLine()
  14.         if not line then break end
  15.         fileLine[i] = line
  16.         i=i+1
  17.     end
  18.    
  19.     file.close()
  20.  
  21.     return fileLine
  22. end
  23.  
  24. function setup()
  25.     local setupFile = fs.open(".setup", "w")
  26.    
  27.     term.clear()
  28.     term.setCursorPos(1,1)
  29.     term.write("Monitor Side? (top, left, right, bottom)")
  30.     local input = read()
  31.     setupFile.writeLine(input)
  32.     term.setCursorPos(1,2)
  33.     term.write("Name?")
  34.     input = read()
  35.     setupFile.writeLine(input)
  36.     term.setCursorPos(1,3)
  37.     term.write("Pass?")
  38.     input = read("*")
  39.     setupFile.writeLine(input)
  40.  
  41.     setupFile.close()
  42. end
  43.  
  44. function readVariables()
  45.     variables = readFile(".setup")
  46.     monSide = variables[1]
  47.     user = variables[2]
  48.     pass = variables[3]
  49. end
  50.  
  51. function menu()
  52.     term.clear()
  53.     term.setCursorPos(1,1)
  54.     term.write("1. Refresh Display")
  55.     term.setCursorPos(1,2)
  56.     term.write("2. Add Notes")
  57.     term.setCursorPos(1,3)
  58.     term.write("3. Delete Line")
  59.     term.setCursorPos(1,4)
  60.     term.write("4. Quit")
  61.     term.setCursorPos(1,5)
  62. end
  63.  
  64. function refreshDisplay()
  65.     mon.clear()
  66.     mon.setCursorPos(1,1)
  67.     mon.write("#".. user .." NOTES")
  68.  
  69.     if fs.exists(".notes") then
  70.         list = readFile(".notes")
  71.         local monPos = 2
  72.         for i=1, #list do
  73.             mon.setCursorPos(1,monPos)
  74.             mon.write(i..". "..list[i])
  75.             monPos = monPos+1
  76.         end
  77.     else
  78.         mon.setCursorPos(1,2)
  79.         mon.write("No Notes")
  80.     end
  81. end
  82.  
  83. function addNote()
  84.     term.clear()
  85.     term.setCursorPos(1,1)
  86.     term.write("Note? ")
  87.     local note = read()
  88.  
  89.     local noteFile = fs.open(".notes", "a")
  90.     noteFile.writeLine(note)
  91.     noteFile.close()
  92.    
  93. end
  94.  
  95. function deleteLine()
  96.     term.clear()
  97.     term.setCursorPos(1,1)
  98.  
  99.     term.write("Enter line number: ")
  100.     local input = read()
  101.    
  102.     local notes = readFile(".notes")
  103.     table.remove(notes, tonumber(input))
  104.  
  105.     local output = fs.open(".notes", "w")
  106.     for i=1, #notes do
  107.         output.writeLine(notes[i])
  108.     end
  109.     output.close()
  110.    
  111. end
  112.  
  113. function password()
  114.     term.clear()
  115.     term.setCursorPos(1,1) 
  116.     term.write("Password: ")
  117.     local passInput = read("*")
  118.    
  119.     if passInput == pass then
  120.         return true
  121.     else
  122.         return false
  123.     end
  124. end
  125.    
  126. -- ########### PROGRAM ########### --
  127.  
  128. -- Check for Setup File
  129. if not fs.exists(".setup") then
  130.     setup()
  131. end
  132.  
  133. readVariables()
  134.  
  135. mon = peripheral.wrap(monSide)
  136. mon.clear()
  137. mon.setTextScale(.5)
  138.  
  139. refreshDisplay()
  140.  
  141. -- Main Loop
  142. while true do
  143.     menu()
  144.     local input = read()
  145.  
  146.     if input == "1" then
  147.         refreshDisplay()
  148.     elseif input == "2" then   
  149.         addNote()
  150.         refreshDisplay()
  151.     elseif input == "3" then
  152.         if password() then
  153.             deleteLine()
  154.         else
  155.             term.setCursorPos(1,3)
  156.             term.write("Incorrect Password")
  157.             os.sleep(1)
  158.         end
  159.         refreshDisplay()
  160.     elseif input == "4" then
  161.         if password() == true then
  162.             return
  163.         else
  164.             term.setCursorPos(1,3)
  165.             term.write("Incorrect Password")
  166.             os.sleep(1)
  167.         end
  168.     end
  169. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement