Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --==============================
- -- File browser using GrimShell
- --
- -- Written by PaymentOption
- -- 19 November 2012
- --==============================
- -- Variables --
- local scroll = 0 -- The scroll offest of items drawn in the current directory.
- local currentDirectory = "" -- The current directory that the interface is in.
- local interfaceTitle = "Browser"
- local interfaceHeight = 8 -- The amount of options minus the MORE option (if any) allowed to be displayed at any time.
- local interfaceWidth = interfaceTitle:len() + 2
- local interface_xPos, interface_yPos = 2, 2
- local interfaceOptions = {}
- local browserInterface = nil
- ---------------
- -- Functions --
- -- This function will be assigned to each file so that
- -- when they are clicked, nothing will be executed.
- function emptyFunction()
- end
- -- This function will be assigend to each directory so
- -- that when they are clicked, they will be entered.
- -- Params: optionIndex - The index of this option in the
- -- interface options table.
- function enterDirectory(path)
- updateInterfaceData(browserInterface)
- currentDirectory = currentDirectory .. '/' .. path
- local newOptions = setOptionsProperly(getObjectsAsOptions(currentDirectory, scroll))
- browserInterface.setOptions(newOptions)
- end
- function setOptionsProperly(options)
- for index, option in pairs(options) do
- if option.label == "MORE" and not option.path then
- option.associatedFunction = function() showMore(); end
- elseif option.path and fs.isDir(currentDirectory .. '/' .. option.path) then
- option.associatedFunction = function() enterDirectory(option.path); end
- else
- option.associatedFunction = emptyFunction
- end
- end
- return options
- end
- -- This function updates the scroll of the displayed files/directories.
- function showMore()
- scroll = scroll + interfaceHeight
- updateInterfaceData(browserInterface)
- local newOptions = setOptionsProperly(getObjectsAsOptions(currentDirectory, scroll))
- browserInterface.setOptions(newOptions)
- end
- -- Returns an options table that contains options which act
- -- as the files/directories within the current directory.
- -- Only directories will have functions assigned to them that allow
- -- them to be entered; files will have empty functions assigned to them.
- -- Format for an options table:
- -- interfaceOptions[n] = {label = (string), xPos = (number), yPos = (number), associatedFunction = (function),
- -- itemTextColor = (number(color)), itemBackgroundColor = (number(color))}
- function getObjectsAsOptions(path, scroll)
- -- Truncates the name of a file/directory in the case that it is too long.
- local function truncateName(name)
- if name:len() > interfaceWidth - 2 then
- name = name:sub(1, interfaceWidth - 5) .. "..."
- end
- return name
- end
- local files = fs.list(path)
- local areMoreFiles = (#files - scroll > interfaceHeight) -- Whether or not there are more files to be displayed.
- local newOptions = {}
- local breakPoint = (scroll/interfaceHeight == 0) and interfaceHeight or interfaceHeight*(scroll/interfaceHeight)
- local fileIndex = 0
- for fileIndex = scroll + 1, #files do
- if fileIndex == breakPoint then
- break
- end
- 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,
- itemTextColor = colors.black, itemBackgroundColor = colors.white, path = files[fileIndex]})
- end
- -- Add the 'MORE' option if there is one.
- if areMoreFiles then
- table.insert(newOptions, {label = "MORE", xPos = interface_xPos + 1, yPos = interface_yPos + interfaceHeight + 1, associatedFunction = showMore,
- itemTextColor = colors.black, itemBackgroundColor = colors.white})
- end
- return newOptions
- end
- -- Takes a new path for the interface to enter, then
- -- sets the current directory to the given path.
- function setDirectory(newDirectory)
- currentDirectory = newDirectory
- end
- -- Updates all necessary information from the interface to run the program.
- function updateInterfaceData(interface)
- interface_xPos, interface_yPos = interface.getPosition()
- end
- ---------------
- interfaceOptions = getObjectsAsOptions(currentDirectory, scroll)
- 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