DrBurlao

Computercraft Notepad v1.0 (ENG)

May 20th, 2023
194
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.47 KB | Gaming | 0 0
  1. local noteDirectory = "/user/documents/"
  2. local textColor = colors.white
  3.  
  4. local function createDirectoryIfNotExists(directory)
  5.   if not fs.exists(directory) then
  6.     fs.makeDir(directory)
  7.   end
  8. end
  9.  
  10. local function saveToFile(filename, text)
  11.   local file = io.open(noteDirectory .. filename, "w")
  12.   if file then
  13.     file:write(text)
  14.     file:close()
  15.     print("File saved as: " .. filename)
  16.   else
  17.     print("Failed to open file for writing: " .. filename)
  18.   end
  19. end
  20.  
  21. local function readFromFile(filename)
  22.   local filePath = noteDirectory .. filename
  23.   if fs.exists(filePath) then
  24.     local file = io.open(filePath, "r")
  25.     if file then
  26.       local text = file:read("*a")
  27.       file:close()
  28.       return text
  29.     else
  30.       print("Failed to open file for reading: " .. filename)
  31.     end
  32.   else
  33.     print("The file does not exist.")
  34.   end
  35. end
  36.  
  37. local function deleteFile(filename)
  38.   local filePath = noteDirectory .. filename
  39.   if fs.exists(filePath) then
  40.     fs.delete(filePath)
  41.     print("File deleted: " .. filename)
  42.   else
  43.     print("The file does not exist.")
  44.   end
  45. end
  46.  
  47. local function listFiles(directory, indent)
  48.   local files = fs.list(directory)
  49.   for _, file in ipairs(files) do
  50.     local path = fs.combine(directory, file)
  51.     if fs.isDir(path) then
  52.       listFiles(path, indent .. "  ")
  53.     else
  54.       print(indent .. file)
  55.     end
  56.   end
  57. end
  58.  
  59. -- Additional function with logic
  60. local function renameFile(oldFilename, newFilename)
  61.   local oldFilePath = noteDirectory .. oldFilename
  62.   local newFilePath = noteDirectory .. newFilename
  63.  
  64.   if fs.exists(oldFilePath) then
  65.     if fs.exists(newFilePath) then
  66.       print("A file with the new name already exists.")
  67.     else
  68.       fs.move(oldFilePath, newFilePath)
  69.       print("File renamed from " .. oldFilename .. " to " .. newFilename)
  70.     end
  71.   else
  72.     print("The file does not exist.")
  73.   end
  74. end
  75.  
  76. -- User interface and other functions
  77. local function printMenu()
  78.   term.clear()
  79.   term.setCursorPos(1, 1)
  80.   term.setTextColor(colors.cyan)
  81.   print("=== Notepad ===")
  82.   term.setTextColor(textColor)
  83.   print("1. Read file")
  84.   print("2. Write file")
  85.   print("3. Delete file")
  86.   print("4. Rename file")
  87.   print("5. List files")
  88.   term.setTextColor(colors.red)
  89.   print("6. Exit")
  90.   term.setTextColor(colors.white)
  91. end
  92.  
  93. local function getInput(prompt)
  94.   term.setTextColor(colors.yellow)
  95.   term.write(prompt)
  96.   term.setTextColor(colors.white)
  97.   return read()
  98. end
  99.  
  100. local function handleReadFile()
  101.   local filename = getInput("Enter the filename: ")
  102.   local text = readFromFile(filename)
  103.   if text then
  104.     term.clear()
  105.     term.setCursorPos(1, 1)
  106.     term.setTextColor(colors.green)
  107.     print("Content of file " .. filename .. ":")
  108.     term.setTextColor(textColor)
  109.     print(text)
  110.     term.setTextColor(colors.white)
  111.     getInput("Press Enter to continue...")
  112.   end
  113. end
  114.  
  115. local function handleWriteFile()
  116.   local filename = getInput("Enter the filename: ")
  117.   print("Enter the text (Press Enter to save and exit):")
  118.  
  119.   local lines = {}
  120.   while true do
  121.     local line = getInput("")
  122.     if line == "" then
  123.       break
  124.     end
  125.     table.insert(lines, line)
  126.   end
  127.  
  128.   if #lines > 0 then
  129.     local text = table.concat(lines, "\n")
  130.     saveToFile(filename, text)
  131.   else
  132.     print("No file saved.")
  133.     getInput("Press Enter to continue...")
  134.   end
  135. end
  136.  
  137. local function handleDeleteFile()
  138.   local filename = getInput("Enter the filename to delete: ")
  139.   deleteFile(filename)
  140. end
  141.  
  142. local function handleRenameFile()
  143.   local oldFilename = getInput("Enter the filename to rename: ")
  144.   local newFilename = getInput("Enter the new filename: ")
  145.   renameFile(oldFilename, newFilename)
  146. end
  147.  
  148. local function handleListFiles()
  149.   print("Files in the directory and subdirectories:")
  150.   listFiles(noteDirectory, "")
  151.   getInput("Press Enter to continue...")
  152. end
  153.  
  154. -- Main program
  155. createDirectoryIfNotExists(noteDirectory)
  156.  
  157. while true do
  158.   printMenu()
  159.   local option = tonumber(getInput("Select an option: "))
  160.  
  161.   if option == 1 then
  162.     handleReadFile()
  163.   elseif option == 2 then
  164.     handleWriteFile()
  165.   elseif option == 3 then
  166.     handleDeleteFile()
  167.   elseif option == 4 then
  168.     handleRenameFile()
  169.   elseif option == 5 then
  170.     handleListFiles()
  171.   elseif option == 6 then
  172.     term.setTextColor(colors.red)
  173.     print("Exiting...")
  174.     term.setTextColor(colors.white)
  175.     break
  176.   else
  177.     print("Invalid option. Please try again.")
  178.     getInput("Press Enter to continue...")
  179.   end
  180. end
  181.  
Advertisement
Add Comment
Please, Sign In to add comment