Advertisement
nonogamer9

notepad for computercraft

May 15th, 2024 (edited)
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.33 KB | Writing | 0 0
  1. local function clearScreen()
  2.     term.clear()
  3.     term.setCursorPos(1, 1)
  4. end
  5.  
  6. local function drawMenu()
  7.     clearScreen()
  8.     print("=== Notepad ===")
  9.     print("1. New File")
  10.     print("2. Open File")
  11.     print("3. Save File (Insert key)")
  12.     print("4. Convert .nono to .lua")
  13.     print("5. Exit")
  14. end
  15.  
  16. local function readFile(filename)
  17.     local file = fs.open(filename, "r")
  18.     if file then
  19.         local contents = file.readAll()
  20.         file.close()
  21.         return contents
  22.     else
  23.         return nil
  24.     end
  25. end
  26.  
  27. local function writeFile(filename, contents)
  28.     -- Append ".nono" file extension if not present
  29.     if not string.find(filename, "%.nono$") then
  30.         filename = filename .. ".nono"
  31.     end
  32.  
  33.     local file = fs.open(filename, "w")
  34.     if file then
  35.         file.write(contents)
  36.         file.close()
  37.         return true
  38.     else
  39.         return false
  40.     end
  41. end
  42.  
  43. local function editFile(filename)
  44.     clearScreen()
  45.     local contents = readFile(filename) or ""
  46.     local cursorX, cursorY = 1, 1
  47.     local cursorVisible = false
  48.  
  49.     local function redraw()
  50.         clearScreen()
  51.         print("=== Editing: " .. filename .. " ===")
  52.         print("Press Insert to save and exit.")
  53.         print("-----------------------")
  54.         print(contents)
  55.         term.setCursorPos(cursorX, cursorY)
  56.         if cursorVisible then
  57.             term.setCursorBlink(true)
  58.         else
  59.             term.setCursorBlink(false)
  60.         end
  61.     end
  62.  
  63.     redraw()
  64.  
  65.     while true do
  66.         local event, key = os.pullEvent()
  67.         if event == "char" then
  68.             -- Insert the character at cursor position
  69.             contents = contents:sub(1, cursorY * (cursorX - 1)) .. key .. contents:sub(cursorY * cursorX)
  70.             cursorX = cursorX + 1
  71.         elseif event == "key" then
  72.             if key == keys.insert then
  73.                 -- Save and exit
  74.                 writeFile(filename, contents)
  75.                 break
  76.             elseif key == keys.enter then
  77.                 -- Insert a newline character
  78.                 contents = contents:sub(1, cursorY * (cursorX - 1)) .. "\n" .. contents:sub(cursorY * cursorX)
  79.                 cursorY = cursorY + 1
  80.                 cursorX = 1
  81.             elseif key == keys.backspace then
  82.                 -- Handle backspace
  83.                 if cursorX > 1 then
  84.                     contents = contents:sub(1, cursorY * (cursorX - 2) + cursorX) .. contents:sub(cursorY * cursorX)
  85.                     cursorX = cursorX - 1
  86.                 elseif cursorY > 1 then
  87.                     local lineLength = contents:find("\n", (cursorY - 1) * cursorX) or #contents + 1
  88.                     contents = contents:sub(1, cursorY * (cursorX - 2) + cursorX) .. contents:sub((cursorY - 1) * cursorX)
  89.                     cursorY = cursorY - 1
  90.                     cursorX = lineLength % (cursorY * cursorX)
  91.                 end
  92.             elseif key == keys.left then
  93.                 -- Move cursor left
  94.                 if cursorX > 1 then
  95.                     cursorX = cursorX - 1
  96.                 elseif cursorY > 1 then
  97.                     cursorY = cursorY - 1
  98.                     cursorX = #contents:sub(1, (cursorY - 1) * cursorX)
  99.                 end
  100.             elseif key == keys.right then
  101.                 -- Move cursor right
  102.                 local lineLength = contents:find("\n", cursorY * cursorX) or #contents + 1
  103.                 if cursorX < lineLength % (cursorY * cursorX) then
  104.                     cursorX = cursorX + 1
  105.                 elseif cursorY < contents:gsub("\n", "\n") then
  106.                     cursorY = cursorY + 1
  107.                     cursorX = 1
  108.                 end
  109.             elseif key == keys.up then
  110.                 -- Move cursor up
  111.                 if cursorY > 1 then
  112.                     cursorY = cursorY - 1
  113.                     local lineLength = contents:find("\n", (cursorY - 1) * cursorX) or #contents + 1
  114.                     cursorX = math.min(cursorX, lineLength % (cursorY * cursorX))
  115.                 end
  116.             elseif key == keys.down then
  117.                 -- Move cursor down
  118.                 local lineCount = select(2, contents:gsub('\n', '\n'))
  119.                 if cursorY < lineCount then
  120.                     cursorY = cursorY + 1
  121.                     cursorX = math.min(cursorX, #contents:sub(cursorY * cursorX, cursorY * cursorX))
  122.                 end
  123.             end
  124.             redraw()
  125.         elseif event == "monitor_touch" then
  126.             if key == 1 then
  127.                 cursorX = math.min(term.getSize(), math.max(1, key))
  128.                 cursorY = math.min(select(2, contents:gsub('\n', '\n')), math.max(1, key))
  129.             end
  130.             redraw()
  131.         end
  132.         cursorVisible = not cursorVisible
  133.     end
  134. end
  135.  
  136. local function convertFile(nonoFilename, luaFilename)
  137.     local contents = readFile(nonoFilename)
  138.     if contents then
  139.         writeFile(luaFilename, contents)
  140.         print("Conversion successful.")
  141.     else
  142.         print("Error: Unable to read .nono file.")
  143.     end
  144. end
  145.  
  146. local function main()
  147.     while true do
  148.         drawMenu()
  149.         local choice = tonumber(read())
  150.         if choice == 1 then
  151.             clearScreen()
  152.             print("Enter filename:")
  153.             local filename = read()
  154.             editFile(filename)
  155.         elseif choice == 2 then
  156.             clearScreen()
  157.             print("Enter filename to open:")
  158.             local filename = read()
  159.             editFile(filename)
  160.         elseif choice == 3 then
  161.             clearScreen()
  162.             print("Enter filename to save:")
  163.             local filename = read()
  164.             local contents = readFile(filename) or ""
  165.             writeFile(filename, contents)
  166.             print("File saved.")
  167.             sleep(2)
  168.         elseif choice == 4 then
  169.             clearScreen()
  170.             print("Enter .nono filename to convert:")
  171.             local nonoFilename = read()
  172.             print("Enter .lua filename to save:")
  173.             local luaFilename = read()
  174.             convertFile(nonoFilename, luaFilename)
  175.             sleep(2)
  176.         elseif choice == 5 then
  177.             clearScreen()
  178.             print("Exiting...")
  179.             sleep(1)
  180.             clearScreen()
  181.             break
  182.         else
  183.             print("Invalid choice. Please try again.")
  184.             sleep(1)
  185.         end
  186.     end
  187. end
  188.  
  189. main()
  190.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement