Advertisement
DOGGYWOOF

Untitled

Jan 10th, 2025 (edited)
15
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.63 KB | None | 0 0
  1. -- Enhanced File Manager for CC Tweaked with Arrow Key Navigation, F1 for Back, and More Features
  2.  
  3. -- Function to display the list of files and directories with cursor
  4. local function listFiles(path, cursor, screenHeight)
  5. local files = fs.list(path)
  6. term.clear()
  7. term.setCursorPos(1, 1)
  8.  
  9. -- Breadcrumb navigation
  10. print("Current Path: " .. path)
  11.  
  12. -- Display only files that fit on the screen
  13. local startIndex = math.max(1, cursor - math.floor(screenHeight / 2))
  14. local endIndex = math.min(#files, startIndex + screenHeight - 1)
  15.  
  16. -- File/Directory List
  17. print("\nFiles and Folders:")
  18. for i = startIndex, endIndex do
  19. local file = files[i]
  20. if i == cursor then
  21. term.setBackgroundColor(colors.gray)
  22. term.setTextColor(colors.white)
  23. else
  24. term.setBackgroundColor(colors.black)
  25. term.setTextColor(colors.white)
  26. end
  27.  
  28. -- Differentiate files and directories by color
  29. if fs.isDir(path .. "/" .. file) then
  30. term.setTextColor(colors.blue) -- Directories are blue
  31. else
  32. term.setTextColor(colors.green) -- Files are green
  33. end
  34.  
  35. print(file)
  36. end
  37. term.setBackgroundColor(colors.black)
  38. term.setTextColor(colors.white)
  39. end
  40.  
  41. -- Function to handle file preview
  42. local function previewFile(path)
  43. term.clear()
  44. term.setCursorPos(1, 1)
  45. print("Previewing file: " .. path)
  46. local file = fs.open(path, "r")
  47. local contents = file.readAll()
  48. file.close()
  49. print(contents)
  50. print("\nPress any key to go back.")
  51. os.pullEvent("key")
  52. end
  53.  
  54. -- Function to create a new directory
  55. local function createDirectory(path)
  56. term.clear()
  57. term.setCursorPos(1, 1)
  58. print("Enter directory name to create:")
  59. local dirName = read()
  60. local newDir = path .. "/" .. dirName
  61. if fs.exists(newDir) then
  62. print("Directory already exists!")
  63. else
  64. fs.makeDir(newDir)
  65. print("Directory created: " .. newDir)
  66. end
  67. print("\nPress any key to continue.")
  68. os.pullEvent("key")
  69. end
  70.  
  71. -- Function to create a new text file
  72. local function createFile(path)
  73. term.clear()
  74. term.setCursorPos(1, 1)
  75. print("Enter filename to create:")
  76. local fileName = read()
  77. local newFile = path .. "/" .. fileName
  78. if fs.exists(newFile) then
  79. print("File already exists!")
  80. else
  81. local file = fs.open(newFile, "w")
  82. print("Enter content for the file (end with a blank line):")
  83. local content = ""
  84. while true do
  85. local line = read()
  86. if line == "" then
  87. break
  88. end
  89. content = content .. line .. "\n"
  90. end
  91. file.write(content)
  92. file.close()
  93. print("File created: " .. newFile)
  94. end
  95. print("\nPress any key to continue.")
  96. os.pullEvent("key")
  97. end
  98.  
  99. -- Function to delete a file or directory
  100. local function deleteItem(path)
  101. term.clear()
  102. term.setCursorPos(1, 1)
  103. print("Enter the name of the file/directory to delete:")
  104. local itemName = read()
  105. local itemPath = path .. "/" .. itemName
  106. if fs.exists(itemPath) then
  107. if fs.isDir(itemPath) then
  108. fs.delete(itemPath)
  109. print("Directory deleted: " .. itemPath)
  110. else
  111. fs.delete(itemPath)
  112. print("File deleted: " .. itemPath)
  113. end
  114. else
  115. print("Item not found.")
  116. end
  117. print("\nPress any key to continue.")
  118. os.pullEvent("key")
  119. end
  120.  
  121. -- Function to navigate the file system using arrow keys
  122. local function navigate()
  123. local currentDir = "/"
  124. local cursor = 1
  125. local files
  126. local screenHeight = term.getSize() - 3 -- Ensure room for the bottom info and prompt
  127.  
  128. while true do
  129. files = fs.list(currentDir)
  130. listFiles(currentDir, cursor, screenHeight)
  131.  
  132. print("\nOptions: [Enter] to open, [F1] to go back, [C] to create, [D] to delete, [Q] to quit")
  133.  
  134. local event, key = os.pullEvent("key")
  135.  
  136. if key == 200 then -- Up Arrow
  137. cursor = math.max(1, cursor - 1)
  138. elseif key == 208 then -- Down Arrow
  139. cursor = math.min(#files, cursor + 1)
  140. elseif key == 28 then -- Enter Key
  141. local selectedFile = files[cursor]
  142. local selectedPath = currentDir .. "/" .. selectedFile
  143. if fs.isDir(selectedPath) then
  144. currentDir = selectedPath
  145. cursor = 1
  146. else
  147. previewFile(selectedPath)
  148. end
  149. elseif key == 59 then -- F1 key to go back
  150. if currentDir ~= "/" then
  151. currentDir = fs.getDir(currentDir)
  152. cursor = 1
  153. end
  154. elseif key == 67 then -- C key to create file or directory
  155. term.clear()
  156. term.setCursorPos(1, 1)
  157. print("Create [D]irectory or [F]ile?")
  158. local choice = read()
  159. if choice == "D" or choice == "d" then
  160. createDirectory(currentDir)
  161. elseif choice == "F" or choice == "f" then
  162. createFile(currentDir)
  163. else
  164. print("Invalid option!")
  165. os.pullEvent("key")
  166. end
  167. elseif key == 68 then -- D key to delete file or directory
  168. deleteItem(currentDir)
  169. elseif key == 17 then -- Q key to quit
  170. break
  171. end
  172. end
  173. end
  174.  
  175. -- Main function to start the file manager
  176. local function startFileManager()
  177. navigate()
  178. end
  179.  
  180. startFileManager()
  181.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement