Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local function clearScreen()
- term.clear()
- term.setCursorPos(1, 1)
- end
- local function drawMenu()
- clearScreen()
- print("=== Notepad ===")
- print("1. New File")
- print("2. Open File")
- print("3. Save File (Insert key)")
- print("4. Convert .nono to .lua")
- print("5. Exit")
- end
- local function readFile(filename)
- local file = fs.open(filename, "r")
- if file then
- local contents = file.readAll()
- file.close()
- return contents
- else
- return nil
- end
- end
- local function writeFile(filename, contents)
- -- Append ".nono" file extension if not present
- if not string.find(filename, "%.nono$") then
- filename = filename .. ".nono"
- end
- local file = fs.open(filename, "w")
- if file then
- file.write(contents)
- file.close()
- return true
- else
- return false
- end
- end
- local function editFile(filename)
- clearScreen()
- local contents = readFile(filename) or ""
- local cursorX, cursorY = 1, 1
- local cursorVisible = false
- local function redraw()
- clearScreen()
- print("=== Editing: " .. filename .. " ===")
- print("Press Insert to save and exit.")
- print("-----------------------")
- print(contents)
- term.setCursorPos(cursorX, cursorY)
- if cursorVisible then
- term.setCursorBlink(true)
- else
- term.setCursorBlink(false)
- end
- end
- redraw()
- while true do
- local event, key = os.pullEvent()
- if event == "char" then
- -- Insert the character at cursor position
- contents = contents:sub(1, cursorY * (cursorX - 1)) .. key .. contents:sub(cursorY * cursorX)
- cursorX = cursorX + 1
- elseif event == "key" then
- if key == keys.insert then
- -- Save and exit
- writeFile(filename, contents)
- break
- elseif key == keys.enter then
- -- Insert a newline character
- contents = contents:sub(1, cursorY * (cursorX - 1)) .. "\n" .. contents:sub(cursorY * cursorX)
- cursorY = cursorY + 1
- cursorX = 1
- elseif key == keys.backspace then
- -- Handle backspace
- if cursorX > 1 then
- contents = contents:sub(1, cursorY * (cursorX - 2) + cursorX) .. contents:sub(cursorY * cursorX)
- cursorX = cursorX - 1
- elseif cursorY > 1 then
- local lineLength = contents:find("\n", (cursorY - 1) * cursorX) or #contents + 1
- contents = contents:sub(1, cursorY * (cursorX - 2) + cursorX) .. contents:sub((cursorY - 1) * cursorX)
- cursorY = cursorY - 1
- cursorX = lineLength % (cursorY * cursorX)
- end
- elseif key == keys.left then
- -- Move cursor left
- if cursorX > 1 then
- cursorX = cursorX - 1
- elseif cursorY > 1 then
- cursorY = cursorY - 1
- cursorX = #contents:sub(1, (cursorY - 1) * cursorX)
- end
- elseif key == keys.right then
- -- Move cursor right
- local lineLength = contents:find("\n", cursorY * cursorX) or #contents + 1
- if cursorX < lineLength % (cursorY * cursorX) then
- cursorX = cursorX + 1
- elseif cursorY < contents:gsub("\n", "\n") then
- cursorY = cursorY + 1
- cursorX = 1
- end
- elseif key == keys.up then
- -- Move cursor up
- if cursorY > 1 then
- cursorY = cursorY - 1
- local lineLength = contents:find("\n", (cursorY - 1) * cursorX) or #contents + 1
- cursorX = math.min(cursorX, lineLength % (cursorY * cursorX))
- end
- elseif key == keys.down then
- -- Move cursor down
- local lineCount = select(2, contents:gsub('\n', '\n'))
- if cursorY < lineCount then
- cursorY = cursorY + 1
- cursorX = math.min(cursorX, #contents:sub(cursorY * cursorX, cursorY * cursorX))
- end
- end
- redraw()
- elseif event == "monitor_touch" then
- if key == 1 then
- cursorX = math.min(term.getSize(), math.max(1, key))
- cursorY = math.min(select(2, contents:gsub('\n', '\n')), math.max(1, key))
- end
- redraw()
- end
- cursorVisible = not cursorVisible
- end
- end
- local function convertFile(nonoFilename, luaFilename)
- local contents = readFile(nonoFilename)
- if contents then
- writeFile(luaFilename, contents)
- print("Conversion successful.")
- else
- print("Error: Unable to read .nono file.")
- end
- end
- local function main()
- while true do
- drawMenu()
- local choice = tonumber(read())
- if choice == 1 then
- clearScreen()
- print("Enter filename:")
- local filename = read()
- editFile(filename)
- elseif choice == 2 then
- clearScreen()
- print("Enter filename to open:")
- local filename = read()
- editFile(filename)
- elseif choice == 3 then
- clearScreen()
- print("Enter filename to save:")
- local filename = read()
- local contents = readFile(filename) or ""
- writeFile(filename, contents)
- print("File saved.")
- sleep(2)
- elseif choice == 4 then
- clearScreen()
- print("Enter .nono filename to convert:")
- local nonoFilename = read()
- print("Enter .lua filename to save:")
- local luaFilename = read()
- convertFile(nonoFilename, luaFilename)
- sleep(2)
- elseif choice == 5 then
- clearScreen()
- print("Exiting...")
- sleep(1)
- clearScreen()
- break
- else
- print("Invalid choice. Please try again.")
- sleep(1)
- end
- end
- end
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement