MtnMCG

notes app

Jul 22nd, 2024 (edited)
218
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.67 KB | None | 0 0
  1. -- File Management System for CCTweaked (Updated)
  2.  
  3. local wireless = peripheral.find("modem")
  4. local isWireless = wireless ~= nil
  5. local notesDir = "/notes"
  6.  
  7. -- Create notes directory if it doesn't exist
  8. if not fs.exists(notesDir) then
  9.     fs.makeDir(notesDir)
  10. end
  11.  
  12. -- Function to list all notes
  13. local function listNotes()
  14.     print("Available notes:")
  15.     local files = fs.list(notesDir)
  16.     for i, file in ipairs(files) do
  17.         print(i .. ". " .. file)
  18.     end
  19. end
  20.  
  21. -- Function to create a new note
  22. local function createNote()
  23.     print("Enter the filename:")
  24.     local filename = read()
  25.     local path = fs.combine(notesDir, filename)
  26.    
  27.     if fs.exists(path) then
  28.         print("File already exists!")
  29.         return
  30.     end
  31.    
  32.     print("Enter your note (press Enter twice to finish):")
  33.     local lines = {}
  34.     while true do
  35.         local line = read()
  36.         if line == "" then break end
  37.         table.insert(lines, line)
  38.     end
  39.    
  40.     local file = fs.open(path, "w")
  41.     file.write(table.concat(lines, "\n"))
  42.     file.close()
  43.     print("Note created successfully!")
  44. end
  45.  
  46. -- Function to view a note
  47. local function viewNote()
  48.     listNotes()
  49.     print("Enter the number of the note to view:")
  50.     local choice = tonumber(read())
  51.     local files = fs.list(notesDir)
  52.    
  53.     if choice and files[choice] then
  54.         local path = fs.combine(notesDir, files[choice])
  55.         local file = fs.open(path, "r")
  56.         print(file.readAll())
  57.         file.close()
  58.     else
  59.         print("Invalid choice!")
  60.     end
  61. end
  62.  
  63. -- Function to edit a note
  64. local function editNote()
  65.     listNotes()
  66.     print("Enter the number of the note to edit:")
  67.     local choice = tonumber(read())
  68.     local files = fs.list(notesDir)
  69.    
  70.     if choice and files[choice] then
  71.         local path = fs.combine(notesDir, files[choice])
  72.         local file = fs.open(path, "r")
  73.         local content = file.readAll()
  74.         file.close()
  75.        
  76.         print("Current content:")
  77.         print(content)
  78.         print("Enter new content (press Enter twice to finish):")
  79.        
  80.         local lines = {}
  81.         while true do
  82.             local line = read()
  83.             if line == "" then break end
  84.             table.insert(lines, line)
  85.         end
  86.        
  87.         file = fs.open(path, "w")
  88.         file.write(table.concat(lines, "\n"))
  89.         file.close()
  90.         print("Note updated successfully!")
  91.     else
  92.         print("Invalid choice!")
  93.     end
  94. end
  95.  
  96. -- Function to receive a single file
  97. local function receiveFile()
  98.     local event, _, channel, _, message = os.pullEvent("modem_message")
  99.     if message.action == "file" then
  100.         local path = fs.combine(notesDir, message.name)
  101.         local f = fs.open(path, "w")
  102.         f.write(message.content)
  103.         f.close()
  104.         print("Received: " .. message.name)
  105.     end
  106. end
  107.  
  108. -- Function to sync notes
  109. local function syncNotes()
  110.     if not isWireless then
  111.         print("This computer doesn't have a wireless modem!")
  112.         return
  113.     end
  114.    
  115.     print("1. Send notes")
  116.     print("2. Receive notes")
  117.     print("3. Idle mode (receive updates)")
  118.     local choice = tonumber(read())
  119.    
  120.     if choice == 1 then
  121.         wireless.transmit(1, 1, {action="sync", data=fs.list(notesDir)})
  122.         print("Sending notes...")
  123.         for _, file in ipairs(fs.list(notesDir)) do
  124.             local path = fs.combine(notesDir, file)
  125.             local f = fs.open(path, "r")
  126.             local content = f.readAll()
  127.             f.close()
  128.             wireless.transmit(1, 1, {action="file", name=file, content=content})
  129.         end
  130.         wireless.transmit(1, 1, {action="done"})
  131.         print("Notes sent successfully!")
  132.     elseif choice == 2 then
  133.         print("Waiting to receive notes...")
  134.         wireless.open(1)
  135.         while true do
  136.             local event, _, channel, _, message = os.pullEvent("modem_message")
  137.             if message.action == "sync" then
  138.                 for _, file in ipairs(message.data) do
  139.                     local path = fs.combine(notesDir, file)
  140.                     if not fs.exists(path) then
  141.                         print("Receiving: " .. file)
  142.                         local f = fs.open(path, "w")
  143.                         f.close()
  144.                     end
  145.                 end
  146.             elseif message.action == "file" then
  147.                 receiveFile()
  148.             elseif message.action == "done" then
  149.                 break
  150.             end
  151.         end
  152.         wireless.close(1)
  153.         print("Notes received successfully!")
  154.     elseif choice == 3 then
  155.         print("Entering idle mode. Press any key to exit.")
  156.         wireless.open(1)
  157.         parallel.waitForAny(
  158.             function()
  159.                 while true do
  160.                     receiveFile()
  161.                 end
  162.             end,
  163.             function()
  164.                 os.pullEvent("key")
  165.             end
  166.         )
  167.         wireless.close(1)
  168.         print("Exited idle mode.")
  169.     else
  170.         print("Invalid choice!")
  171.     end
  172. end
  173.  
  174. -- Main menu
  175. while true do
  176.     print("\nFile Management System")
  177.     print("1. List notes")
  178.     print("2. Create note")
  179.     print("3. View note")
  180.     print("4. Edit note")
  181.     print("5. Sync notes")
  182.     print("6. Exit")
  183.    
  184.     local choice = tonumber(read())
  185.    
  186.     if choice == 1 then
  187.         listNotes()
  188.     elseif choice == 2 then
  189.         createNote()
  190.     elseif choice == 3 then
  191.         viewNote()
  192.     elseif choice == 4 then
  193.         editNote()
  194.     elseif choice == 5 then
  195.         syncNotes()
  196.     elseif choice == 6 then
  197.         break
  198.     else
  199.         print("Invalid choice!")
  200.     end
  201. end
  202.  
  203. print("Thank you for using the File Management System!")
Advertisement
Add Comment
Please, Sign In to add comment