DrBurlao

Computercraft NTB. File explorer v1.0 (ENG)

May 20th, 2023 (edited)
75
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.86 KB | Gaming | 0 0
  1. -- Function to scan directories and retrieve NBT files with their paths and sizes
  2. local function scanFiles(directory)
  3.     local nbtFiles = {}
  4.     local function scanDirectory(directory)
  5.         local files = fs.list(directory)
  6.         for _, file in ipairs(files) do
  7.             local path = fs.combine(directory, file)
  8.             if fs.isDir(path) then
  9.                 scanDirectory(path) -- Recursively scan subdirectories
  10.             elseif file:match("%.nbt$") then
  11.                 local size = fs.getSize(path)
  12.                 table.insert(nbtFiles, { path = path, size = size })
  13.             end
  14.         end
  15.     end
  16.  
  17.     scanDirectory(directory)
  18.     return nbtFiles
  19. end
  20.  
  21. -- Function to display a paginated menu
  22. local function showMenu(files, currentPage, pageSize)
  23.     local totalPages = math.ceil(#files / pageSize)
  24.     currentPage = math.max(1, math.min(currentPage, totalPages))
  25.  
  26.     term.clear()
  27.     term.setCursorPos(1, 1)
  28.     print("NBT File Explorer")
  29.     print("-----------------")
  30.  
  31.     local startIdx = (currentPage - 1) * pageSize + 1
  32.     local endIdx = math.min(startIdx + pageSize - 1, #files)
  33.  
  34.     for i = startIdx, endIdx do
  35.         local file = files[i]
  36.         print(i .. ". " .. file.path)
  37.         print("   Size: " .. file.size .. " bytes")
  38.     end
  39.  
  40.     print("-----------------")
  41.     print("Page " .. currentPage .. "/" .. totalPages)
  42.     print("Press 'p' to go to the previous page")
  43.     print("Press 'n' to go to the next page")
  44. end
  45.  
  46. -- Function to open an NBT file
  47. local function openFile(file)
  48.     if fs.exists(file) and not fs.isDir(file) then
  49.         shell.run("nbt", "edit", file)
  50.     else
  51.         print("The file does not exist or is a directory.")
  52.     end
  53. end
  54.  
  55. -- Main program
  56. local files = scanFiles("/")
  57. local currentPage = 1
  58. local pageSize = 5
  59. local running = true
  60.  
  61. while running do
  62.     showMenu(files, currentPage, pageSize)
  63.     print("0. Exit")
  64.     write("Select a file to open (file number or page): ")
  65.     local input = read()
  66.  
  67.     if input == "0" then
  68.         running = false
  69.     elseif tonumber(input) then
  70.         local fileIdx = tonumber(input)
  71.         if fileIdx >= 1 and fileIdx <= #files then
  72.             openFile(files[fileIdx].path)
  73.             write("Press any key to continue...")
  74.             read()
  75.         else
  76.             print("Invalid file index.")
  77.             sleep(2)
  78.         end
  79.     elseif input == "p" or input == "P" then
  80.         currentPage = currentPage - 1
  81.     elseif input == "n" or input == "N" then
  82.         currentPage = currentPage + 1
  83.     else
  84.         print("Invalid input. Please try again.")
  85.         sleep(2)
  86.     end
  87.  
  88.     -- Adjust current page to wrap around if exceeded the total pages
  89.     if currentPage > math.ceil(#files / pageSize) then
  90.         currentPage = 1
  91.     elseif currentPage < 1 then
  92.         currentPage = math.ceil(#files / pageSize)
  93.     end
  94. end
  95.  
Advertisement
Comments
  • DrBurlao
    2 years
    # text 1.52 KB | 0 0
    1. This script defines several functions for scanning directories and retrieving NBT files with their paths and sizes, displaying a paginated menu, and opening an NBT file. The main program uses these functions to create a file explorer-like interface.
    2.  
    3. Here's a concise summary of what the script does:
    4.  
    5. 1. The `scanFiles` function takes a directory path as input and recursively scans it for NBT files. It returns a table containing the paths and sizes of the NBT files found.
    6.  
    7. 2. The `showMenu` function takes a table of files, the current page number, and the page size as input. It displays a paginated menu showing the files' paths and sizes.
    8.  
    9. 3. The `openFile` function takes a file path as input and opens the file using the "nbt edit" command if it exists and is not a directory.
    10.  
    11. 4. The main program starts by scanning the root directory ("/") for NBT files and storing them in the `files` table. It sets the initial page number and page size.
    12.  
    13. 5. Inside the main loop, it shows the menu, prompts the user for input, and performs the corresponding action based on the input. If the input is "0", the program exits. If the input is a file number, it opens the corresponding file. If the input is "p" or "n", it navigates to the previous or next page, respectively. If the input is invalid, it displays an error message.
    14.  
    15. 6. After each action, the current page number is adjusted to wrap around if it exceeds the total number of pages.
    16.  
    17. That's a summary of the script's functionality. Let me know if you have any further questions!
Add Comment
Please, Sign In to add comment