Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Enhanced File Manager for CC Tweaked with Arrow Key Navigation, F1 for Back, and More Features
- -- Function to display the list of files and directories with cursor
- local function listFiles(path, cursor, screenHeight)
- local files = fs.list(path)
- term.clear()
- term.setCursorPos(1, 1)
- -- Breadcrumb navigation
- print("Current Path: " .. path)
- -- Display only files that fit on the screen
- local startIndex = math.max(1, cursor - math.floor(screenHeight / 2))
- local endIndex = math.min(#files, startIndex + screenHeight - 1)
- -- File/Directory List
- print("\nFiles and Folders:")
- for i = startIndex, endIndex do
- local file = files[i]
- if i == cursor then
- term.setBackgroundColor(colors.gray)
- term.setTextColor(colors.white)
- else
- term.setBackgroundColor(colors.black)
- term.setTextColor(colors.white)
- end
- -- Differentiate files and directories by color
- if fs.isDir(path .. "/" .. file) then
- term.setTextColor(colors.blue) -- Directories are blue
- else
- term.setTextColor(colors.green) -- Files are green
- end
- print(file)
- end
- term.setBackgroundColor(colors.black)
- term.setTextColor(colors.white)
- end
- -- Function to handle file preview
- local function previewFile(path)
- term.clear()
- term.setCursorPos(1, 1)
- print("Previewing file: " .. path)
- local file = fs.open(path, "r")
- local contents = file.readAll()
- file.close()
- print(contents)
- print("\nPress any key to go back.")
- os.pullEvent("key")
- end
- -- Function to create a new directory
- local function createDirectory(path)
- term.clear()
- term.setCursorPos(1, 1)
- print("Enter directory name to create:")
- local dirName = read()
- local newDir = path .. "/" .. dirName
- if fs.exists(newDir) then
- print("Directory already exists!")
- else
- fs.makeDir(newDir)
- print("Directory created: " .. newDir)
- end
- print("\nPress any key to continue.")
- os.pullEvent("key")
- end
- -- Function to create a new text file
- local function createFile(path)
- term.clear()
- term.setCursorPos(1, 1)
- print("Enter filename to create:")
- local fileName = read()
- local newFile = path .. "/" .. fileName
- if fs.exists(newFile) then
- print("File already exists!")
- else
- local file = fs.open(newFile, "w")
- print("Enter content for the file (end with a blank line):")
- local content = ""
- while true do
- local line = read()
- if line == "" then
- break
- end
- content = content .. line .. "\n"
- end
- file.write(content)
- file.close()
- print("File created: " .. newFile)
- end
- print("\nPress any key to continue.")
- os.pullEvent("key")
- end
- -- Function to delete a file or directory
- local function deleteItem(path)
- term.clear()
- term.setCursorPos(1, 1)
- print("Enter the name of the file/directory to delete:")
- local itemName = read()
- local itemPath = path .. "/" .. itemName
- if fs.exists(itemPath) then
- if fs.isDir(itemPath) then
- fs.delete(itemPath)
- print("Directory deleted: " .. itemPath)
- else
- fs.delete(itemPath)
- print("File deleted: " .. itemPath)
- end
- else
- print("Item not found.")
- end
- print("\nPress any key to continue.")
- os.pullEvent("key")
- end
- -- Function to navigate the file system using arrow keys
- local function navigate()
- local currentDir = "/"
- local cursor = 1
- local files
- local screenHeight = term.getSize() - 3 -- Ensure room for the bottom info and prompt
- while true do
- files = fs.list(currentDir)
- listFiles(currentDir, cursor, screenHeight)
- print("\nOptions: [Enter] to open, [F1] to go back, [C] to create, [D] to delete, [Q] to quit")
- local event, key = os.pullEvent("key")
- if key == 200 then -- Up Arrow
- cursor = math.max(1, cursor - 1)
- elseif key == 208 then -- Down Arrow
- cursor = math.min(#files, cursor + 1)
- elseif key == 28 then -- Enter Key
- local selectedFile = files[cursor]
- local selectedPath = currentDir .. "/" .. selectedFile
- if fs.isDir(selectedPath) then
- currentDir = selectedPath
- cursor = 1
- else
- previewFile(selectedPath)
- end
- elseif key == 59 then -- F1 key to go back
- if currentDir ~= "/" then
- currentDir = fs.getDir(currentDir)
- cursor = 1
- end
- elseif key == 67 then -- C key to create file or directory
- term.clear()
- term.setCursorPos(1, 1)
- print("Create [D]irectory or [F]ile?")
- local choice = read()
- if choice == "D" or choice == "d" then
- createDirectory(currentDir)
- elseif choice == "F" or choice == "f" then
- createFile(currentDir)
- else
- print("Invalid option!")
- os.pullEvent("key")
- end
- elseif key == 68 then -- D key to delete file or directory
- deleteItem(currentDir)
- elseif key == 17 then -- Q key to quit
- break
- end
- end
- end
- -- Main function to start the file manager
- local function startFileManager()
- navigate()
- end
- startFileManager()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement