Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Load the web code from a file
- local function loadWebsite()
- local filePath = "web.code"
- if not fs.exists(filePath) then
- return nil
- end
- local file = fs.open(filePath, "r")
- local code = file.readAll()
- file.close()
- return code
- end
- -- Save the web code to a file
- local function saveWebsite(code)
- local filePath = "web.code"
- local file = fs.open(filePath, "w")
- file.write(code)
- file.close()
- end
- -- Simple text editor UI
- local function textEditor()
- term.clear()
- term.setCursorPos(1, 1)
- term.write("Web Code Editor\n")
- term.write("Press Ctrl+S to save, Ctrl+Q to quit\n\n")
- local code = loadWebsite() or ""
- local x, y = term.getCursorPos()
- term.setCursorPos(1, y + 2)
- term.write(code)
- local function drawText()
- term.setCursorPos(1, y + 2)
- term.write(string.rep(" ", term.getSize()))
- term.setCursorPos(1, y + 2)
- term.write(code)
- end
- while true do
- local event, key = os.pullEvent("key")
- if key == keys.s and (event == "key" or event == "char") then
- saveWebsite(code)
- term.setCursorPos(1, term.getSize() - 1)
- term.write("Code saved.")
- elseif key == keys.q and (event == "key" or event == "char") then
- return
- elseif key == keys.backspace then
- code = code:sub(1, -2)
- drawText()
- elseif key == keys.enter then
- code = code .. "\n"
- drawText()
- elseif key >= 32 and key <= 126 then
- code = code .. string.char(key)
- drawText()
- end
- end
- end
- -- Web server code
- local function webServer()
- rednet.open("top") -- Assuming the modem is on the top
- print("Webserver running...")
- while true do
- local senderID, message = rednet.receive()
- if message == "GET" then
- local code = loadWebsite()
- if code then
- rednet.send(senderID, code)
- else
- rednet.send(senderID, "Website not found.")
- end
- end
- end
- end
- -- Run the editor or web server based on user input
- term.clear()
- term.setCursorPos(1, 1)
- term.write("Enter 'editor' to start the editor or 'server' to start the web server:\n")
- local input = read()
- if input == "editor" then
- textEditor()
- elseif input == "server" then
- webServer()
- else
- print("Invalid input.")
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement