Advertisement
dannysmc95

Blaze File Browser

Jan 14th, 2015
298
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 11.42 KB | None | 0 0
  1. --[[
  2.     Name: Blaze File Browser (w/ LuaIDE)
  3.     Version: 1.0.0
  4.     Creator: DannySMc (dannysmc95)
  5.     Description: A simplistic file browser
  6.  
  7.     License:
  8.  
  9.         This work is licensed under the Creative Commons
  10.         Attribution-NonCommercial-NoDerivatives 4.0 International
  11.         License. To view a copy of this license, visit
  12.         http://creativecommons.org/licenses/by-nc-nd/4.0/.
  13.  
  14.         You are free to:
  15.         Share — copy and redistribute the material in any medium or format
  16.         (The licensor cannot revoke these freedoms as long as you follow the license terms.)
  17.  
  18.         Under the following terms:
  19.         Attribution — You must give appropriate credit, provide a link to
  20.         the license, and indicate if changes were made. You may do so in
  21.         any reasonable manner, but not in any way that suggests the licensor
  22.         endorses you or your use.
  23.  
  24.         NonCommercial — You may not use the material for commercial purposes.
  25.  
  26.         NoDerivatives — If you remix, transform, or build upon the material,
  27.         you may not distribute the modified material.
  28.        
  29.         No additional restrictions — You may not apply legal terms or technological
  30.         measures that legally restrict others from doing anything the license permits.
  31. ]]
  32.  
  33. -- Set Vars:
  34. scroll = 1
  35. histFold = {}
  36. table.insert(histFold, "/")
  37.  
  38. function fb_getClick(sButton, nX, nY, scroll, sDir)
  39.     if sButton == 1 then
  40.         local tFilesClean = tFilesClean
  41.         tFilesCount = #tFilesClean
  42.         local count = nY - 2 - scroll
  43.         oldDir = sDir
  44.         if sDir == "/" then
  45.             newDir = sDir..""..(tFilesClean[count])
  46.         else
  47.             newDir = sDir.."/"..tFilesClean[count]
  48.         end
  49.         sDir = newDir
  50.  
  51.         if fs.isDir(newDir) then
  52.             table.insert(histFold, sDir)
  53.             fb_finder(newDir)
  54.         else
  55.             fb_runFile(newDir, sDir)
  56.         end
  57.     elseif sButton == 2 then
  58.         -- Do delete or edit
  59.         if sOption == "delete" then
  60.             local tFilesClean = tFilesClean
  61.             tFilesCount = #tFilesClean
  62.             local count = nY - 2 - scroll
  63.             oldDir = sDir
  64.             if sDir == "/" then
  65.                 newDir = sDir..""..(tFilesClean[count])
  66.             else
  67.                 newDir = sDir.."/"..tFilesClean[count]
  68.             end
  69.             fs.delete(newDir)
  70.             fb_finder(sDir)
  71.         elseif sOption == "edit" then
  72.             -- Empty
  73.         else
  74.             fb_drawOptions(nX, nY, sDir)
  75.         end
  76.     end
  77. end
  78.  
  79. function fb_drawOptions(tempX, tempY, sDir)
  80.     drawBox(tempX, 8, tempY, 2, " ", "black", "lightGrey")
  81.     printA("Delete", tempX, tempY, false, "black", "lightGrey")
  82.     printA("Edit", tempX, tempY + 1, false, "black", "lightGrey")
  83.  
  84.     while true do
  85.         local args = { os.pullEvent() }
  86.         if args[1] == "mouse_click" then
  87.             if args[2] == 1 then
  88.                 if args[3] >= tempX and args[3] <= tempX + 8 then
  89.                     if args[4] == tempY then
  90.                         local tFilesClean = tFilesClean
  91.                         tFilesCount = #tFilesClean
  92.                         local count = tempY - 2 - scroll
  93.                         oldDir = sDir
  94.                         if sDir == "/" then
  95.                             newDir = sDir..""..(tFilesClean[count])
  96.                         else
  97.                             newDir = sDir.."/"..tFilesClean[count]
  98.                         end
  99.                         fs.delete(newDir)
  100.                         fb_finder(sDir)
  101.                     elseif args[4] == tempY + 1 then
  102.                         local tFilesClean = tFilesClean
  103.                         tFilesCount = #tFilesClean
  104.                         local count = tempY - 2 - scroll
  105.                         oldDir = sDir
  106.                         if sDir == "/" then
  107.                             newDir = sDir..""..(tFilesClean[count])
  108.                         else
  109.                             newDir = sDir.."/"..tFilesClean[count]
  110.                         end
  111.                         shell.run("edit "..newDir)
  112.                         fb_finder(sDir)
  113.                     else
  114.                         fb_finder(sDir)
  115.                     end
  116.                 else
  117.                     fb_finder(sDir)
  118.                 end
  119.             else
  120.                 fb_finder(sDir)
  121.             end
  122.         end
  123.     end
  124. end
  125.  
  126. function fb_runFile(path, sDir)
  127.     cs()
  128.     setCol("black", "white")
  129.     shell.run(path)
  130.     fb_finder(sDir)
  131. end
  132.  
  133. function fb_drawMenu(sDir)
  134.     -- Get files
  135.     maintc = "blue"
  136.     mainbc = "grey"
  137.     mainbc2 = "lightGrey"
  138.     texttc = "black"
  139.     textbc = "white"
  140.     dirtc = "red"
  141.     xtc = "lime"
  142.     sidetc = "white"
  143.     sidebc = "cyan"
  144.     sidetc1 = "lime"
  145.     setCol("white", "white")
  146.     cs()
  147.     drawBox(1, 51, 1, 1, " ", maintc, mainbc)
  148.     printA(" "..time().." ", 44, 1, false, maintc, mainbc2)
  149.     printA("X", 1, 1, false, xtc, dirtc)
  150.     printA(" Path: ", 3, 1, false, texttc, mainbc2)
  151.     printA(sDir.." ", 10, 1, false, dirtc, mainbc2)
  152.     drawBox(40, 12, 2, 18, " ", sidetc, sidebc)
  153.     drawBox(41, 10, 3, 16, " ", sidetc, sidebc)
  154.     drawBox(42, 8, 4, 14, " ", sidetc, sidebc)
  155.     drawBox(43, 6, 5, 12, " ", sidetc, sidebc)
  156.     drawBox(44, 4, 6, 10, " ", sidetc, sidebc)
  157.     drawBox(45, 2, 7, 8, " ", sidetc, sidebc)
  158.     printA("< ..", 2, 3, false, "blue", "white")
  159.     printA("Create", 41, 3, false, sidetc1, sidebc)
  160.     printA("File", 43, 4, false, sidetc, sidebc)
  161.     printA("Folder", 43, 5, false, sidetc, sidebc)
  162.     setCol(texttc, textbc)
  163. end
  164.  
  165. function fb_finder(sDir)
  166.     -- Get files
  167.     fb_drawMenu(sDir)
  168.     fb_drawDir(sDir, scroll)
  169.  
  170.     while true do
  171.         local args = { os.pullEvent() }
  172.         if args[1] == "timer" then
  173.             fb_drawMenu(sDir)
  174.             fb_drawDir(sDir, scroll)
  175.         elseif args[1] == "mouse_scroll" then
  176.             if args[2] == 1 then
  177.                 scroll = scroll + 1
  178.                 fb_drawMenu(sDir)
  179.                 fb_drawDir(sDir, scroll)
  180.             elseif args[2] == -1 then
  181.                 scroll = scroll - 1
  182.                 fb_drawMenu(sDir)
  183.                 fb_drawDir(sDir, scroll)
  184.             end
  185.         elseif args[1] == "mouse_click" then
  186.             if args[3] == 1 and args[4] == 1 then
  187.                 if RunningUnder == "Verinian" then
  188.                     home()
  189.                 else
  190.                     setCol("white", "black")
  191.                     break
  192.                 end
  193.             elseif (args[3] >= 2 and args[3] <= 40) and (args[4] == 3) then
  194.                 table.remove(histFold)
  195.                 historyCount = #histFold
  196.                 sDir = histFold[historyCount]
  197.                 fb_finder(sDir)
  198.             elseif (args[3] >= 2 and args[3] <= 40) and (args[4] >= 4 and args[4] <= 18) then
  199.                 fb_getClick(args[2], args[3], args[4], scroll, sDir)
  200.             elseif (args[3] >= 43 and args[3] <= 50) then
  201.                 if args[4] == 4 then
  202.                     fb_drawInput("file", sDir)
  203.                 elseif args[4] == 5 then
  204.                     fb_drawInput("folder", sDir)
  205.                 end
  206.             end
  207.         end
  208.     end
  209. end
  210.  
  211. function fb_drawInput(object, sDir)
  212.     poptc1 = "white"
  213.     popbc1 = "red"
  214.     poptc2 = "black"
  215.     popbc2 = "white"
  216.     if object == "file" then
  217.         drawBox(6, 39, 6, 3, " ", poptc1, popbc1)
  218.         drawBox(6, 39, 7, 1, " ", poptc1, popbc1)
  219.         drawBox(7, 37, 7, 1, " ", poptc2, popbc2)
  220.         printC("Name For File:", 6, false, poptc1, popbc1)
  221.         setCol(poptc2, popbc2)
  222.         term.setCursorPos(7,7)
  223.         write(": ")
  224.         local newFileName = tostring(read())
  225.         if sDir == "/" then
  226.             fileDirName = "/"..newFileName
  227.         else
  228.             fileDirName = sDir.."/"..newFileName
  229.         end
  230.         shell.run("edit "..fileDirName)
  231.         fb_finder(sDir)
  232.     elseif object == "folder" then
  233.         drawBox(6, 39, 6, 3, " ", poptc1, popbc1)
  234.         drawBox(6, 39, 7, 1, " ", poptc1, popbc1)
  235.         drawBox(7, 37, 7, 1, " ", poptc2, popbc2)
  236.         printC("Name For Folder:", 6, false, poptc1, popbc1)
  237.         setCol(poptc2, popbc2)
  238.         term.setCursorPos(7,7)
  239.         write(": ")
  240.         local newFolderName = tostring(read())
  241.         if sDir == "/" then
  242.             fileDirName = "/"..newFolderName
  243.         else
  244.             fileDirName = sDir.."/"..newFolderName
  245.         end
  246.         fs.makeDir(fileDirName)
  247.         fb_finder(sDir)
  248.     end
  249. end
  250.  
  251. -- Code by: wieselkatze (Modified by dannysmc95)
  252.  
  253. local function fb_getDir( sDir )
  254.     sDir = ( "/" .. sDir .. "/" ):gsub( "\\+", "/" ):gsub( "/+", "/" )
  255.     local folder, list = sDir:match( ".*/" ), {}
  256.  
  257.     tFilesClean = {}
  258.  
  259.     if not fs.isDir( sDir ) then
  260.         return {}
  261.     end
  262.  
  263.     for k, v in pairs( fs.list( sDir ) ) do
  264.         if fs.isDir( folder .. v ) then
  265.             table.insert( list, 1, "+ " .. v )
  266.             table.insert( tFilesClean, 1, v)
  267.         else
  268.             list[ #list+1 ] = "- " .. v
  269.             tFilesClean[ #tFilesClean + 1 ] = v
  270.         end
  271.     end
  272.  
  273.     return list
  274. end
  275.  
  276. function fb_drawDir(sDir, scroll)
  277.     local intX = 2
  278.     local intY = 4
  279.  
  280.     tFiles = fb_getDir(sDir)
  281.  
  282.     for k, v in ipairs(tFiles) do
  283.         if k >= scroll and k <= 15 then
  284.             term.setCursorPos(intX, intY)
  285.             if v:find("+") then
  286.                 setCol("blue", "white")
  287.                 print(v)
  288.             else
  289.                 setCol("red", "white")
  290.                 print(v)
  291.             end
  292.             intY = intY + 1
  293.         end
  294.     end
  295. end
  296.  
  297. function fb_main()
  298.     if fs.exists("vde.lua") then
  299.         RunningUnder = "Verinian"
  300.     else
  301.         RunningUnder = "Unknown"
  302.     end
  303.     fb_finder("/")
  304. end
  305.  
  306.  
  307.  
  308.  
  309.  
  310.  
  311.  
  312.  
  313.  
  314.  
  315.  
  316.  
  317.  
  318.  
  319.  
  320.  
  321.  
  322.  
  323.  
  324.  
  325.  
  326.  
  327.  
  328.  
  329.  
  330.  
  331.  
  332.  
  333.  
  334.  
  335.  
  336.  
  337.  
  338.  
  339.  
  340.  
  341.  
  342.  
  343. -- Configuration File Protocols
  344. function saveConfig(table, file)
  345.   fConfig = fs.open(file, "w") or error("Cannot open file "..file, 2)
  346.   fConfig.write(textutils.serialize(table))
  347.   fConfig.close()
  348. end
  349.  
  350. function loadConfig(file)
  351.   fConfig = fs.open(file, "r")
  352.   ret = textutils.unserialize(fConfig.readAll())
  353.   return ret
  354. end
  355.  
  356. -- Peripheral Functions
  357. function findPeripheral(Perihp) --Returns side of first matching peripheral matching passed string  
  358.   for _,s in ipairs(rs.getSides()) do
  359.     if peripheral.isPresent(s) and peripheral.getType(s) == Perihp then
  360.       return s  
  361.     end
  362.   end
  363.   return false
  364. end
  365.  
  366. -- Common Draw Functions
  367. function cs()
  368.     term.clear()
  369.     term.setCursorPos(1,1)
  370.     return
  371. end
  372.  
  373. function setCol(textColour, backgroundColour)
  374.     if textColour and backgroundColour then
  375.         if term.isColour() then
  376.             term.setTextColour(colours[textColour])
  377.             term.setBackgroundColour(colours[backgroundColour])
  378.             return true
  379.         else
  380.             return false
  381.         end
  382.     else
  383.         return false
  384.     end
  385. end
  386.  
  387. function resetCol()
  388.     if term.isColour then
  389.         term.setTextColour(colours.white)
  390.         term.setBackgroundColour(colours.black)
  391.         return true
  392.     else
  393.         return false
  394.     end
  395. end
  396.  
  397. -- Print Functions
  398. function printC(Text, Line, NextLine, Color, BkgColor) -- print centered
  399.   local x, y = term.getSize()
  400.   x = x/2 - #Text/2
  401.   term.setCursorPos(x, Line)
  402.   if Color then setCol(Color, BkgColor) end
  403.   term.write(Text)
  404.   if NextLine then
  405.     term.setCursorPos(1, NextLine)
  406.   end
  407.   if Color then resetCol(Color, BkgColor) end
  408.   return true  
  409. end
  410.  
  411. function printL(Text, Line, NextLine, Color, BkgColor) -- print line
  412.  
  413.  
  414.   local x, y = term.getSize()
  415.   if ((term.isColor) and (term.isColor() == false) and (Text == " ")) then Text = "-" end
  416.   for i = 1, x do
  417.     term.setCursorPos(i, Line)
  418.     if Color then setCol(Color, BkgColor) end
  419.     term.write(Text)
  420.   end
  421.   if NextLine then  
  422.     term.setCursorPos(1, NextLine)
  423.   end
  424.   if Color then resetCol(Color, BkgColor) end
  425.   return true  
  426. end
  427.  
  428. function printA(Text, xx, yy, NextLine, Color, BkgColor) -- print anywhere
  429.   term.setCursorPos(xx,yy)
  430.   if Color then setCol(Color, BkgColor) end
  431.   term.write(Text)
  432.   if NextLine then  
  433.     term.setCursorPos(1, NextLine)
  434.   end
  435.   if Color then resetCol(Color, BkgColor) end
  436.   return true  
  437. end
  438.  
  439. function clearLine(Line, NextLine) -- May seem a bit odd, but it may be usefull sometimes
  440.   local x, y = term.getSize()
  441.   for i = 1, x do
  442.     term.setCursorPos(i, Line)
  443.     term.write(" ")
  444.   end  
  445.   if not NextLine then  
  446.     x, y = term.getCursorPos()
  447.     term.setCursorPos(1, y+1)
  448.   end
  449.   return true  
  450. end
  451.  
  452. function drawBox(StartX, lengthX, StartY, lengthY, Text, Color, BkgColor) -- does what is says on the tin.
  453.   local x, y = term.getSize()
  454.   if Color then setCol(Color, BkgColor) end
  455.   if not Text then Text = "*" end
  456.   lengthX = lengthX - 1
  457.   lengthY = lengthY - 1
  458.   EndX = StartX + lengthX  
  459.   EndY = StartY + lengthY
  460.   term.setCursorPos(StartX, StartY)
  461.   term.write(string.rep(Text, lengthX))
  462.   term.setCursorPos(StartX, EndY)
  463.   term.write(string.rep(Text, lengthX))
  464.   for i = StartY, EndY do
  465.     term.setCursorPos(StartX, i)
  466.     term.write(Text)
  467.     term.setCursorPos(EndX, i)    
  468.     term.write(Text)
  469.   end
  470.   resetCol(Color, BkgColor)
  471.   return true  
  472. end
  473.  
  474. function time()
  475.     local nTime = textutils.formatTime(os.time(), true)
  476.     if string.len(nTime) == 4 then
  477.         nTime = "0"..nTime
  478.     end
  479.     os.startTimer(0.5)
  480.     return nTime
  481. end
  482.  
  483. fb_main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement