Trystan_C_C

Browser for GrimShell

Nov 19th, 2012
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.87 KB | None | 0 0
  1. --==============================
  2. -- File browser using GrimShell
  3. --
  4. -- Written by PaymentOption
  5. -- 19 November 2012
  6. --==============================
  7.  
  8. -- Variables --
  9. local scroll = 0 -- The scroll offest of items drawn in the current directory.
  10. local currentDirectory = "" -- The current directory that the interface is in.
  11.  
  12. local interfaceTitle = "Browser"
  13. local interfaceHeight = 8 -- The amount of options minus the MORE option (if any) allowed to be displayed at any time.
  14. local interfaceWidth = interfaceTitle:len() + 2
  15. local interface_xPos, interface_yPos = 2, 2
  16. local interfaceOptions = {}
  17.  
  18. local browserInterface = nil
  19. ---------------
  20.  
  21. -- Functions --
  22. -- This function will be assigned to each file so that
  23. -- when they are clicked, nothing will be executed.
  24. function emptyFunction()
  25. end
  26.  
  27. -- This function will be assigend to each directory so
  28. -- that when they are clicked, they will be entered.
  29. -- Params: optionIndex - The index of this option in the
  30. --                       interface options table.
  31. function enterDirectory(path)
  32.       updateInterfaceData(browserInterface)
  33.       currentDirectory = currentDirectory .. '/' .. path
  34.       local newOptions = setOptionsProperly(getObjectsAsOptions(currentDirectory, scroll))
  35.       browserInterface.setOptions(newOptions)
  36. end
  37.  
  38. function setOptionsProperly(options)
  39.       for index, option in pairs(options) do
  40.             if option.label == "MORE" and not option.path then
  41.                   option.associatedFunction = function() showMore(); end
  42.             elseif option.path and fs.isDir(currentDirectory .. '/' .. option.path) then
  43.                   option.associatedFunction = function() enterDirectory(option.path); end
  44.             else
  45.                   option.associatedFunction = emptyFunction
  46.             end
  47.       end
  48.      
  49.       return options
  50. end
  51.  
  52. -- This function updates the scroll of the displayed files/directories.
  53. function showMore()
  54.       scroll = scroll + interfaceHeight
  55.       updateInterfaceData(browserInterface)
  56.       local newOptions = setOptionsProperly(getObjectsAsOptions(currentDirectory, scroll))
  57.       browserInterface.setOptions(newOptions)
  58. end
  59.  
  60. -- Returns an options table that contains options which act
  61. -- as the files/directories within the current directory.
  62. -- Only directories will have functions assigned to them that allow
  63. -- them to be entered; files will have empty functions assigned to them.
  64. -- Format for an options table:
  65. --    interfaceOptions[n] = {label = (string), xPos = (number), yPos = (number), associatedFunction = (function),
  66. --                           itemTextColor = (number(color)), itemBackgroundColor = (number(color))}
  67. function getObjectsAsOptions(path, scroll)
  68.       -- Truncates the name of a file/directory in the case that it is too long.
  69.       local function truncateName(name)
  70.             if name:len() > interfaceWidth - 2 then
  71.                   name = name:sub(1, interfaceWidth - 5) .. "..."
  72.             end
  73.            
  74.             return name
  75.       end
  76.      
  77.       local files = fs.list(path)
  78.       local areMoreFiles = (#files - scroll > interfaceHeight) -- Whether or not there are more files to be displayed.
  79.       local newOptions = {}
  80.      
  81.       local breakPoint = (scroll/interfaceHeight == 0) and interfaceHeight or interfaceHeight*(scroll/interfaceHeight)
  82.       local fileIndex = 0
  83.       for fileIndex = scroll + 1, #files do
  84.             if fileIndex == breakPoint then
  85.                   break
  86.             end
  87.            
  88.             table.insert(newOptions, {label = truncateName(files[fileIndex]), xPos = interface_xPos + 1, yPos = fileIndex + interface_yPos, associatedFunction = (fs.isDir(files[fileIndex])) and (function() enterDirectory(files[fileIndex]); end) or emptyFunction,
  89.                                       itemTextColor = colors.black, itemBackgroundColor = colors.white, path = files[fileIndex]})
  90.       end
  91.      
  92.       -- Add the 'MORE' option if there is one.
  93.       if areMoreFiles then
  94.             table.insert(newOptions, {label = "MORE", xPos = interface_xPos + 1, yPos = interface_yPos + interfaceHeight + 1, associatedFunction = showMore,
  95.                                       itemTextColor = colors.black, itemBackgroundColor = colors.white})
  96.       end
  97.      
  98.       return newOptions
  99. end
  100.  
  101. -- Takes a new path for the interface to enter, then
  102. -- sets the current directory to the given path.
  103. function setDirectory(newDirectory)
  104.       currentDirectory = newDirectory
  105. end
  106.  
  107. -- Updates all necessary information from the interface to run the program.
  108. function updateInterfaceData(interface)
  109.       interface_xPos, interface_yPos = interface.getPosition()
  110. end
  111. ---------------
  112.  
  113. interfaceOptions = getObjectsAsOptions(currentDirectory, scroll)
  114. browserInterface = interface.Interface.new(interface_xPos, interface_yPos, interfaceOptions, colors.blue, interfaceTitle, "Browser_Interface", true, interfaceWidth)
Advertisement
Add Comment
Please, Sign In to add comment