Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Multi-Line Text Editor for CC:Tweaked
- local function clearScreen()
- term.clear()
- term.setCursorPos(1, 1)
- end
- local function printMenu()
- print("==== Simple Text Editor ====")
- print("Press F1 to save")
- print("Press F9 to quit")
- print("============================")
- end
- local function promptInput(prompt)
- write(prompt)
- return read()
- end
- local function drawText(lines, cursorLine, startLine)
- clearScreen()
- printMenu()
- for i = 1, 16 do
- if startLine + i <= #lines then
- term.setCursorPos(1, i + 3)
- write(lines[startLine + i])
- else
- term.setCursorPos(1, i + 3)
- write("") -- Empty line
- end
- end
- term.setCursorPos(1, cursorLine + 3)
- end
- local function editFile(content)
- local lines = content or {}
- local cursorLine = #lines > 0 and #lines or 1
- local startLine = 1
- while true do
- drawText(lines, cursorLine, startLine)
- local event, key = os.pullEvent()
- if event == "key" then
- if key == keys.f1 then -- F1 to save
- return lines
- elseif key == keys.f9 then -- F9 to quit
- return nil
- elseif key == keys.up and cursorLine > 1 then
- cursorLine = cursorLine - 1
- elseif key == keys.down and cursorLine < #lines + 1 then
- cursorLine = cursorLine + 1
- elseif key == keys.enter then
- local newLine = ""
- table.insert(lines, cursorLine, newLine)
- cursorLine = cursorLine + 1
- elseif key == keys.backspace then
- if cursorLine > 1 then
- lines[cursorLine - 1] = lines[cursorLine - 1]:sub(1, -2) -- Remove last character
- if lines[cursorLine - 1] == "" then
- table.remove(lines, cursorLine - 1) -- Remove the line if empty
- cursorLine = cursorLine - 1
- end
- end
- else
- local char = string.char(key)
- if char and #char == 1 then
- if cursorLine <= #lines then
- lines[cursorLine] = (lines[cursorLine] or "") .. char
- else
- lines[cursorLine] = char
- end
- end
- end
- end
- -- Scroll if cursorLine exceeds the visible area
- if cursorLine > startLine + 13 then
- startLine = startLine + 1
- elseif cursorLine < startLine then
- startLine = startLine - 1
- end
- end
- end
- local function saveFile(filename, content)
- local file = fs.open(filename, "w")
- for _, line in ipairs(content) do
- file.writeLine(line)
- end
- file.close()
- end
- local function runEditor()
- while true do
- clearScreen()
- printMenu()
- local choice = promptInput("Choose an option (1: New File, 2: Open File, 3: Exit): ")
- if choice == "1" then
- local newContent = editFile(nil)
- if newContent then
- local filename = promptInput("Enter filename to save: ")
- saveFile(filename, newContent)
- print("File saved!")
- os.sleep(2)
- end
- elseif choice == "2" then
- local filename = promptInput("Enter filename to open: ")
- if fs.exists(filename) then
- local file = fs.open(filename, "r")
- local content = {}
- while true do
- local line = file.readLine()
- if not line then break end
- table.insert(content, line)
- end
- file.close()
- local editedContent = editFile(content)
- if editedContent then
- saveFile(filename, editedContent)
- print("File updated!")
- os.sleep(2)
- end
- else
- print("File not found!")
- os.sleep(2)
- end
- elseif choice == "3" then
- print("Exiting editor...")
- break
- else
- print("Invalid choice. Please select again.")
- os.sleep(2)
- end
- end
- end
- -- Run the text editor
- runEditor()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement