Advertisement
DOGGYWOOF

Untitled

Oct 23rd, 2024 (edited)
17
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.39 KB | None | 0 0
  1. -- Multi-Line Text Editor for CC:Tweaked
  2.  
  3.  
  4. local function clearScreen()
  5. term.clear()
  6. term.setCursorPos(1, 1)
  7. end
  8.  
  9. local function printMenu()
  10. print("==== Simple Text Editor ====")
  11. print("Press F1 to save")
  12. print("Press F9 to quit")
  13. print("============================")
  14. end
  15.  
  16. local function promptInput(prompt)
  17. write(prompt)
  18. return read()
  19. end
  20.  
  21. local function drawText(lines, cursorLine, startLine)
  22. clearScreen()
  23. printMenu()
  24. for i = 1, 16 do
  25. if startLine + i <= #lines then
  26. term.setCursorPos(1, i + 3)
  27. write(lines[startLine + i])
  28. else
  29. term.setCursorPos(1, i + 3)
  30. write("") -- Empty line
  31. end
  32. end
  33. term.setCursorPos(1, cursorLine + 3)
  34. end
  35.  
  36. local function editFile(content)
  37. local lines = content or {}
  38. local cursorLine = #lines > 0 and #lines or 1
  39. local startLine = 1
  40.  
  41. while true do
  42. drawText(lines, cursorLine, startLine)
  43. local event, key = os.pullEvent()
  44.  
  45. if event == "key" then
  46. if key == keys.f1 then -- F1 to save
  47. return lines
  48. elseif key == keys.f9 then -- F9 to quit
  49. return nil
  50. elseif key == keys.up and cursorLine > 1 then
  51. cursorLine = cursorLine - 1
  52. elseif key == keys.down and cursorLine < #lines + 1 then
  53. cursorLine = cursorLine + 1
  54. elseif key == keys.enter then
  55. local newLine = ""
  56. table.insert(lines, cursorLine, newLine)
  57. cursorLine = cursorLine + 1
  58. elseif key == keys.backspace then
  59. if cursorLine > 1 then
  60. lines[cursorLine - 1] = lines[cursorLine - 1]:sub(1, -2) -- Remove last character
  61. if lines[cursorLine - 1] == "" then
  62. table.remove(lines, cursorLine - 1) -- Remove the line if empty
  63. cursorLine = cursorLine - 1
  64. end
  65. end
  66. else
  67. local char = string.char(key)
  68. if char and #char == 1 then
  69. if cursorLine <= #lines then
  70. lines[cursorLine] = (lines[cursorLine] or "") .. char
  71. else
  72. lines[cursorLine] = char
  73. end
  74. end
  75. end
  76. end
  77.  
  78. -- Scroll if cursorLine exceeds the visible area
  79. if cursorLine > startLine + 13 then
  80. startLine = startLine + 1
  81. elseif cursorLine < startLine then
  82. startLine = startLine - 1
  83. end
  84. end
  85. end
  86.  
  87. local function saveFile(filename, content)
  88. local file = fs.open(filename, "w")
  89. for _, line in ipairs(content) do
  90. file.writeLine(line)
  91. end
  92. file.close()
  93. end
  94.  
  95. local function runEditor()
  96. while true do
  97. clearScreen()
  98. printMenu()
  99. local choice = promptInput("Choose an option (1: New File, 2: Open File, 3: Exit): ")
  100.  
  101. if choice == "1" then
  102. local newContent = editFile(nil)
  103. if newContent then
  104. local filename = promptInput("Enter filename to save: ")
  105. saveFile(filename, newContent)
  106. print("File saved!")
  107. os.sleep(2)
  108. end
  109. elseif choice == "2" then
  110. local filename = promptInput("Enter filename to open: ")
  111. if fs.exists(filename) then
  112. local file = fs.open(filename, "r")
  113. local content = {}
  114. while true do
  115. local line = file.readLine()
  116. if not line then break end
  117. table.insert(content, line)
  118. end
  119. file.close()
  120. local editedContent = editFile(content)
  121. if editedContent then
  122. saveFile(filename, editedContent)
  123. print("File updated!")
  124. os.sleep(2)
  125. end
  126. else
  127. print("File not found!")
  128. os.sleep(2)
  129. end
  130. elseif choice == "3" then
  131. print("Exiting editor...")
  132. break
  133. else
  134. print("Invalid choice. Please select again.")
  135. os.sleep(2)
  136. end
  137. end
  138. end
  139.  
  140. -- Run the text editor
  141. runEditor()
  142.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement