Advertisement
DOGGYWOOF

Untitled

Sep 16th, 2024
9
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.46 KB | None | 0 0
  1. -- Load the web code from a file
  2. local function loadWebsite()
  3. local filePath = "web.code"
  4. if not fs.exists(filePath) then
  5. return nil
  6. end
  7. local file = fs.open(filePath, "r")
  8. local code = file.readAll()
  9. file.close()
  10. return code
  11. end
  12.  
  13. -- Save the web code to a file
  14. local function saveWebsite(code)
  15. local filePath = "web.code"
  16. local file = fs.open(filePath, "w")
  17. file.write(code)
  18. file.close()
  19. end
  20.  
  21. -- Simple text editor UI
  22. local function textEditor()
  23. term.clear()
  24. term.setCursorPos(1, 1)
  25. term.write("Web Code Editor\n")
  26. term.write("Press Ctrl+S to save, Ctrl+Q to quit\n\n")
  27.  
  28. local code = loadWebsite() or ""
  29. local x, y = term.getCursorPos()
  30. term.setCursorPos(1, y + 2)
  31. term.write(code)
  32.  
  33. local function drawText()
  34. term.setCursorPos(1, y + 2)
  35. term.write(string.rep(" ", term.getSize()))
  36. term.setCursorPos(1, y + 2)
  37. term.write(code)
  38. end
  39.  
  40. while true do
  41. local event, key = os.pullEvent("key")
  42. if key == keys.s and (event == "key" or event == "char") then
  43. saveWebsite(code)
  44. term.setCursorPos(1, term.getSize() - 1)
  45. term.write("Code saved.")
  46. elseif key == keys.q and (event == "key" or event == "char") then
  47. return
  48. elseif key == keys.backspace then
  49. code = code:sub(1, -2)
  50. drawText()
  51. elseif key == keys.enter then
  52. code = code .. "\n"
  53. drawText()
  54. elseif key >= 32 and key <= 126 then
  55. code = code .. string.char(key)
  56. drawText()
  57. end
  58. end
  59. end
  60.  
  61. -- Web server code
  62. local function webServer()
  63. rednet.open("top") -- Assuming the modem is on the top
  64. print("Webserver running...")
  65.  
  66. while true do
  67. local senderID, message = rednet.receive()
  68. if message == "GET" then
  69. local code = loadWebsite()
  70. if code then
  71. rednet.send(senderID, code)
  72. else
  73. rednet.send(senderID, "Website not found.")
  74. end
  75. end
  76. end
  77. end
  78.  
  79. -- Run the editor or web server based on user input
  80. term.clear()
  81. term.setCursorPos(1, 1)
  82. term.write("Enter 'editor' to start the editor or 'server' to start the web server:\n")
  83. local input = read()
  84.  
  85. if input == "editor" then
  86. textEditor()
  87. elseif input == "server" then
  88. webServer()
  89. else
  90. print("Invalid input.")
  91. end
  92.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement