Advertisement
BigSHinyToys

modified mouseFB

May 19th, 2013
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 33.77 KB | None | 0 0
  1. function selectFile()
  2.   local tArgs = {}
  3.   local ver = "1.4"
  4.   local sTitle = "File Browser"
  5.   local bugTest, norun, dir, showAll
  6.   local _tArgs = {}
  7.   local config = "mouse.cfg"
  8.  
  9.   local temp
  10.   if shell and shell.getRunningProgram then
  11.     temp = shell.getRunningProgram()
  12.   end
  13.  
  14.   temp = temp or "/bla"
  15.   local localPath = string.sub(temp,1,#temp-string.len(fs.getName(temp)))
  16. temp = nil -- just because not needed
  17.  
  18. -- load config file
  19.  
  20. local configSet = {}
  21. local cnf = {}
  22.  
  23. if fs.exists(localPath.."/"..config) then
  24.   local file = fs.open(localPath.."/"..config,"r")
  25.   if file then
  26.     local item = file.readLine()
  27.     while item do
  28.           table.insert(cnf,item)
  29.           item = file.readLine()
  30.     end
  31.     file.close()
  32.   end
  33. end
  34.  
  35. for i = 1,10 do
  36.   local test,data = pcall(textutils.unserialize,cnf[i])
  37.   if test then
  38.     configSet[i] = data
  39.   else
  40.     configSet[i] = nil
  41.   end
  42. end
  43. cnf = nil
  44.  
  45. -- color configuration work in progress
  46. local titleBar = configSet[1] or {txt = colors.black,back = colors.blue}
  47. local addressBar = configSet[2] or {txt = colors.black,back = colors.lightGray}
  48. local itemWindo = configSet[3] or {txt = colors.black,back = colors.cyan}
  49. local rcmList = configSet[4] or {txt = colors.black,back = colors.lightGray} -- rcm = Right Click Menu List
  50. local rcmTitle = configSet[5] or {txt = colors.black,back = colors.blue}
  51. local dialogTitle = configSet[6] or {txt = colors.black,back = colors.blue}
  52. local dialogWindo = configSet[7] or {txt = colors.black,back = colors.white}
  53. local scrollCol = configSet[8] or {off = colors.gray, button = colors.gray,back = colors.lightGray}
  54.  
  55. local tIcons = configSet[9] or {
  56. back = {tCol = "lightGray",bCol = "blue",txt = " < "},
  57. disk = {tCol = "lime",bCol = "green",txt = "[*]"},
  58. audio = {tCol = "yellow",bCol = "red",txt = "(o)"},
  59. folder = {tCol = "lightGray",bCol = "blue",txt = "[=]"},
  60. file = {tCol = nil ,bCol = nil ,txt = nil}
  61. }
  62.  
  63. local customLaunch = configSet[10] or {
  64. ["Edit"] = "rom/programs/edit",
  65. ["Paint"] = "rom/programs/color/paint"
  66. }
  67.  
  68. local function saveCFG(overWrite)
  69.   if not fs.exists(localPath.."/"..config) or overWrite then
  70.     local cnf = {}
  71.     local file = fs.open(localPath.."/"..config,"w")
  72.     if file then
  73.           file.write(textutils.serialize(titleBar).."\n")
  74.           file.write(textutils.serialize(addressBar).."\n")
  75.           file.write(textutils.serialize(itemWindo).."\n")
  76.           file.write(textutils.serialize(rcmList).."\n")
  77.           file.write(textutils.serialize(rcmTitle).."\n")
  78.           file.write(textutils.serialize(dialogTitle).."\n")
  79.           file.write(textutils.serialize(dialogWindo).."\n")
  80.           file.write(textutils.serialize(scrollCol).."\n")
  81.           file.write(textutils.serialize(tIcons).."\n")
  82.           file.write(textutils.serialize(customLaunch).."\n")
  83.           file.close()
  84.     elseif overWrite then
  85.     end
  86.   end
  87. end
  88.  
  89. saveCFG()
  90.  
  91. -- end configuration
  92.  
  93. local function help()
  94.   print([[Usage: browser [-d] [-h] [-a] [-u] [--debug] [--help] [--dir <dir>] [--all] [--update]
  95.     --debug or -d: enable debug mode
  96.     --help or -h: display this screen
  97.     --dir: define initial directory
  98.     --all or -a: show hidden files
  99.     --update -u: update]])
  100. end
  101.  
  102. local function inBouwndry(clickX,clickY,boxX,boxY,width,hight)
  103.   return ( clickX >= boxX and clickX < boxX + width and clickY >= boxY and clickY < boxY + hight )
  104. end
  105.  
  106. local function update()
  107.   print("Checking for Updates")
  108.   local isHTTP = false
  109.   local response
  110.   if http then
  111.     isHTTP = true
  112.     print("http on")
  113.     response = http.get("http://pastebin.com/raw.php?i=rLbnyM1U")
  114.   end
  115.   local flag = false
  116.   local isNewFlag = false
  117.   local newVerID
  118.   if response and isHTTP then
  119.     print("recived")
  120.     local sInfo = response.readLine()
  121.     print(sInfo)
  122.     while sInfo do
  123.           print(sInfo)
  124.           if flag then
  125.             if sInfo == ver then
  126.                   print("Mouse File Browser is up to date")
  127.                   break
  128.             else
  129.                   newVerID = sInfo
  130.                   flag = false
  131.                   isNewFlag = true
  132.             end
  133.           elseif sInfo == sTitle then
  134.             flag = true
  135.           elseif isNewFlag then
  136.             isNewFlag = sInfo
  137.             response.close()
  138.             break
  139.           end
  140.           sInfo = response.readLine()
  141.     end
  142.     if isNewFlag then
  143.           print("New vershion avalible "..newVerID)
  144.           print('downloading to \Browser')
  145.           if fs.exists("Browser") then
  146.             write("Browser exists OverWrite Browser Y/N : ")
  147.             local input = string.lower(read())
  148.             while input ~= "y" and input ~= "n" do
  149.                   print("y or n required")
  150.                   input = string.lower(read())
  151.             end
  152.             if input == "y" then
  153.                   print("Over Writeing Browser")
  154.                   print("Downloading new File")
  155.                   local response = http.get("http://pastebin.com/raw.php?i="..isNewFlag)
  156.                   if response then
  157.                     print("file downloaded")
  158.                     print("installing")
  159.                     fs.delete("Browser")
  160.                     local handel = fs.open("Browser","w")
  161.                     if handel then
  162.                           handel.write(response.readAll())
  163.                           handel.close()
  164.                           print("Update Complete")
  165.                     end
  166.                     response.close()
  167.                   end
  168.             else
  169.                   print("Update aborted")
  170.             end
  171.           else
  172.             print("Downloading new File")
  173.             local response = http.get("http://pastebin.com/raw.php?i="..isNewFlag)
  174.             if response then
  175.                   print("file downloaded")
  176.                   print("installing")
  177.                   local handel = fs.open("Browser","w")
  178.                   if handel then
  179.                     handel.write(response.readAll())
  180.                     handel.close()
  181.                     print("Update Complete")
  182.                   end
  183.                   response.close()
  184.             end
  185.           end
  186.     end
  187.   elseif isHTTP then
  188.     print("Error downloading update file Please contact BigSHinyToys on the CC forums")
  189.     print("http://www.computercraft.info/forums2/index.php?/topic/5509-advanced-computer-mouse-file-browser/")
  190.   elseif not isHTTP then
  191.     print("HTTP API is turned off")
  192.     print("Access Computer Craft Configer and change line")
  193.     print([[enableapi_http {
  194.           # Enable the "http" API on Computers
  195.           general=false
  196.     }
  197.     TO THIS :
  198.     enableapi_http {
  199.     # Enable the "http" API on Computers
  200.     general=true
  201.     }]])
  202.   end
  203.   notCMD = false
  204.   norun = true
  205. end
  206.  
  207. for a = 1, #tArgs do
  208.   if tArgs[a]:sub(1,2) == "--" then
  209.     local cmd = tArgs[a]:sub(3):lower()
  210.     if cmd == "debug" then
  211.           bugTest = true
  212.     elseif cmd == "help" then
  213.           help()
  214.           norun = true
  215.     elseif cmd == "dir" then
  216.           dir = tArgs[a+1]
  217.           a = a + 1
  218.     elseif cmd == "all" then
  219.           showAll = true
  220.     elseif cmd == "update" then
  221.           update()
  222.     end
  223.   elseif tArgs[a]:sub(1,1) == "-" then
  224.     for b = 2, #tArgs[a] do
  225.           cmd = tArgs[a]:sub( b , b )
  226.           if cmd == "d" then
  227.             bugTest = true
  228.           elseif cmd == "h" then
  229.             help()
  230.             norun = true
  231.           elseif cmd == "p" then
  232.             dir = tArgs[a+1]
  233.             a = a + 1
  234.           elseif cmd == "a" then
  235.             showAll = true
  236.           elseif cmd == "u" then
  237.             update()
  238.           end
  239.     end
  240.   else
  241.     table.insert(_tArgs, tArgs[a])
  242.   end
  243. end
  244.  
  245. if (not dir) and shell and shell.dir then
  246.   dir = shell.dir()
  247. end
  248.  
  249. if dir and shell and shell.resolve then
  250.   dir = shell.resolve(dir)
  251. end
  252.  
  253. dir = dir or "/"
  254.  
  255. if bugTest then -- this is that the var is for testing
  256.   print("Dir: "..dir)
  257.   os.startTimer(4)
  258.   os.pullEvent()
  259. end
  260.  
  261. local function clear()
  262.   term.setBackgroundColor(colors.black)
  263.   term.setTextColor(colors.white)
  264.   term.clear()
  265.   term.setCursorBlink(false)
  266.   term.setCursorPos(1,1)
  267. end
  268.  
  269. --[[
  270.     Code thanks to Cruor
  271.     http://www.computercraft.info/forums2/index.php?/topic/5802-support-for-shell/
  272. ]]--
  273.  
  274. local function fixArgs(...)
  275.   local tReturn={}
  276.   local str=table.concat({...}," ")
  277.   local sMatch
  278.   while str and #str>0 do
  279.     if string.sub(str,1,1)=="\"" then
  280.           sMatch, str=string.match(str, "\"(.-)\"%s*(.*)")
  281.     else
  282.           sMatch, str=string.match(str, "(%S+)%s*(.*)")
  283.     end
  284.     table.insert(tReturn,sMatch)
  285.   end
  286.   return tReturn
  287. end
  288.  
  289. --[[ end Cruor function ]]--
  290.  
  291.  
  292. -- modified read made to play nice with coroutines
  293.  
  294. local function readMOD( _sReplaceChar, _tHistory,_wdth)
  295.   local sLine = ""
  296.   term.setCursorBlink( true )
  297.  
  298.   local nHistoryPos = nil
  299.   local nPos = 0
  300.   if _sReplaceChar then
  301.     _sReplaceChar = string.sub( _sReplaceChar, 1, 1 )
  302.   end
  303.  
  304.   local sx, sy = term.getCursorPos()  
  305.  
  306.   local w, h = term.getSize()
  307.   if _wdth and type(_wdth) == "number" then
  308.     w = sx + _wdth - 1
  309.   end
  310.  
  311.   local function redraw( _sCustomReplaceChar )
  312.     local nScroll = 0
  313.     if sx + nPos >= w then
  314.           nScroll = (sx + nPos) - w
  315.     end
  316.    
  317.     term.setCursorPos( sx + _wdth - 1, sy )
  318.     term.write(" ")
  319.     term.setCursorPos( sx, sy )
  320.     local sReplace = _sCustomReplaceChar or _sReplaceChar
  321.     if sReplace then
  322.           term.write( string.rep(sReplace,_wdth) )
  323.     else
  324.           term.write( string.sub( sLine, nScroll + 1 ,nScroll + _wdth) )
  325.     end
  326.     term.setCursorPos( sx + nPos - nScroll, sy )
  327.   end
  328.  
  329.   while true do
  330.     local sEvent, param = os.pullEvent()
  331.     if sEvent == "char" then
  332.           sLine = string.sub( sLine, 1, nPos ) .. param .. string.sub( sLine, nPos + 1 )
  333.           nPos = nPos + 1
  334.           redraw()
  335.          
  336.     elseif sEvent == "key" then
  337.          
  338.           if param == keys.left then
  339.             -- Left
  340.             if nPos > 0 then
  341.                   nPos = nPos - 1
  342.                   redraw()
  343.             end
  344.            
  345.           elseif param == keys.right then
  346.             -- Right        
  347.             if nPos < string.len(sLine) then
  348.                   nPos = nPos + 1
  349.                   redraw()
  350.             end
  351.            
  352.           elseif param == keys.up or param == keys.down then
  353.                             -- Up or down
  354.                             if _tHistory then
  355.                                   redraw(" ");
  356.                                   if param == keys.up then
  357.                     -- Up
  358.                     if nHistoryPos == nil then
  359.                           if #_tHistory > 0 then
  360.                             nHistoryPos = #_tHistory
  361.                           end
  362.                     elseif nHistoryPos > 1 then
  363.                           nHistoryPos = nHistoryPos - 1
  364.                     end
  365.                   else
  366.                     -- Down
  367.                     if nHistoryPos == #_tHistory then
  368.                           nHistoryPos = nil
  369.                     elseif nHistoryPos ~= nil then
  370.                           nHistoryPos = nHistoryPos + 1
  371.                     end            
  372.                   end
  373.                  
  374.                   if nHistoryPos then
  375.                     sLine = _tHistory[nHistoryPos]
  376.                     nPos = string.len( sLine )
  377.                   else
  378.                     sLine = ""
  379.                     nPos = 0
  380.                   end
  381.                   redraw()
  382.             end
  383.           elseif param == keys.backspace then
  384.             -- Backspace
  385.             if nPos > 0 then
  386.                   redraw(" ");
  387.                   sLine = string.sub( sLine, 1, nPos - 1 ) .. string.sub( sLine, nPos + 1 )
  388.                   nPos = nPos - 1            
  389.                   redraw()
  390.             end
  391.           elseif param == keys.home then
  392.             -- Home
  393.             nPos = 0
  394.             redraw()    
  395.           elseif param == keys.delete then
  396.             if nPos < string.len(sLine) then
  397.                   redraw(" ");
  398.                   sLine = string.sub( sLine, 1, nPos ) .. string.sub( sLine, nPos + 2 )    
  399.                   redraw()
  400.             end
  401.           elseif param == keys["end"] then
  402.             -- End
  403.             nPos = string.len(sLine)
  404.             redraw()
  405.           end
  406.     elseif sEvent == "redraw" then
  407.           redraw()
  408.     elseif sEvent == "return" then
  409.           term.setCursorBlink( false )
  410.           return sLine
  411.     end
  412.   end
  413.  
  414.   term.setCursorBlink( false )
  415.  
  416.   return sLine
  417. end
  418.  
  419. -- end modified read
  420.  
  421. local function printC(posX,posY,textCol,backCol,text)
  422.   term.setCursorPos(posX,posY)
  423.   term.setTextColor(colors[textCol] or textCol)
  424.   term.setBackgroundColor(colors[backCol] or backCol)
  425.   term.write(text)
  426. end
  427.  
  428. local function InputBox(title)
  429.   local boxW,boxH = 26,3
  430.   local termX,termY = term.getSize()
  431.   local ofsX,ofsY = math.ceil((termX/2) - (boxW/2)) , math.ceil((termY/2) - (boxH/2)) -- offset from top left
  432.   local options = {"ok","cancel"}
  433.  
  434.   local selected = 1
  435.   local space = 0
  436.   local range = {}
  437.   for i = 1,#options do
  438.     range[i] = {s = space,f = space + string.len(options[i])}
  439.     space = space + string.len(options[i])+3
  440.   end
  441.   local ofC = (boxW/2) - (space/2)
  442.  
  443.   local function drawBox()
  444.     printC(ofsX,ofsY,colors.black,colors.blue,string.rep(" ",boxW))
  445.     printC(ofsX+1,ofsY,colors.black,colors.blue,(title or "User Input"))
  446.     printC(ofsX,ofsY+1,colors.black,colors.white,string.rep(" ",boxW))
  447.     printC(ofsX,ofsY+2,colors.black,colors.white,string.rep(" ",boxW))
  448.     printC(ofsX,ofsY+3,colors.black,colors.white,string.rep(" ",boxW))
  449.    
  450.     for i = 1,#options do
  451.           if i == selected then
  452.             term.setBackgroundColor(colors.lightGray)
  453.             term.setCursorPos(range[i].s + ofC + ofsX - 1,ofsY + 3)
  454.             term.write("["..options[i].."]")
  455.             term.setBackgroundColor(colors.white)
  456.             term.write(" ")
  457.           else
  458.             term.setCursorPos(range[i].s + ofC + ofsX - 1,ofsY + 3)
  459.             term.write(" "..options[i].." ")
  460.           end
  461.     end
  462.    
  463.     printC(ofsX+2,ofsY+2,colors.black,colors.lightGray,string.rep(" ",boxW-4))
  464.   end
  465.   drawBox()
  466.   term.setCursorPos(ofsX+2,ofsY+2)
  467.   local co = coroutine.create(function() return readMOD(nil,nil,boxW - 4) end)
  468.   while true do
  469.     local event = {os.pullEvent()}
  470.     if event[1] == "key" or event[1] == "char" then
  471.           if event[2] == 28 then
  472.             local test,data = coroutine.resume(co,"return")
  473.             return data
  474.           else
  475.             coroutine.resume(co,unpack(event))
  476.           end
  477.     elseif event[1] == "mouse_click" then
  478.           if event[4] == ofsY + 3 then
  479.             for i = 1,#options do
  480.                   if event[3] >= range[i].s + ofC + ofsX - 1 and event[3] <= range[i].f + ofC + ofsX then
  481.                     if options[i] == "ok" then
  482.                           local test,data = coroutine.resume(co,"return")
  483.                           return data
  484.                     elseif options[i] == "cancel" then
  485.                           return false
  486.                     end
  487.                   end
  488.             end
  489.           end
  490.     end
  491.   end
  492. end
  493. local function dialogBox(title,message,options, h, w)
  494.   term.setCursorBlink(false)
  495.   local selected = 1
  496.   title = title or ""
  497.   message = message or ""
  498.   options = options or {}
  499.   local boxW,boxH = (w or 26), (h or 3)
  500.   local termX,termY = term.getSize()
  501.   local ofsX,ofsY = math.ceil((termX/2) - (boxW/2)) , math.ceil((termY/2) - (boxH/2)) -- offset from top left
  502.  
  503.   local space = 0
  504.   local range = {}
  505.   for i = 1,#options do
  506.     range[i] = {s = space,f = space + string.len(options[i])}
  507.     space = space + string.len(options[i])+3
  508.   end
  509.   local ofC = math.ceil((boxW/2)) - math.ceil((space/2))
  510.  
  511.   local function drawBox()
  512.     printC(ofsX,ofsY,dialogTitle.txt,dialogTitle.back," "..title..string.rep(" ",boxW-#title-5).."_[]")
  513.     term.setBackgroundColor(colors.red)
  514.     term.setTextColor(colors.white)
  515.     term.write("X")
  516.     printC(ofsX,ofsY+1,dialogWindo.txt,dialogWindo.back,string.sub(" "..message..string.rep(" ",boxW),1,boxW))
  517.     term.setCursorPos(ofsX,ofsY+2)
  518.     term.write(string.rep(" ",boxW))
  519.     term.setCursorPos(ofsX,ofsY+3)
  520.     term.write(string.rep(" ",boxW))
  521.     for i = 1,#options do
  522.           if i == selected then
  523.             printC(range[i].s + ofC + ofsX - 1,ofsY + 3,"black","lightGray","["..options[i].."]")
  524.             term.setBackgroundColor(dialogWindo.back)
  525.             term.setTextColor(dialogWindo.txt)
  526.             term.write(" ")
  527.           else
  528.             term.setCursorPos(range[i].s + ofC + ofsX - 1,ofsY + 3)
  529.             term.write(" "..options[i].." ")
  530.           end
  531.     end
  532.     term.setCursorPos(ofsX + ofC + space,ofsY + 3)
  533.     term.write(string.rep(" ",boxW - (ofC + space)))
  534.     term.setBackgroundColor(colors.black)
  535.     term.setTextColor(colors.white)    
  536.   end
  537.   while true do
  538.     drawBox()
  539.     event = {os.pullEvent()}
  540.     if event[1] == "key" then
  541.           if event[2] == 203 then -- left
  542.             selected = selected - 1
  543.             if selected < 1 then
  544.                   selected = #options
  545.             end
  546.           elseif event[2] == 205 then -- right
  547.             selected = selected + 1
  548.             if selected > #options then
  549.                   selected = 1
  550.             end
  551.           elseif event[2] == 28 then -- enter
  552.             return selected , options[selected]
  553.           end
  554.     elseif event[1] == "mouse_click" then
  555.          
  556.           if bugTest then term.write("M "..event[2].." X "..event[3].." Y "..event[4].."    ") end
  557.          
  558.           if event[2] == 1 then
  559.             if event[4] == ofsY + 3 then
  560.                   for i = 1,#options do
  561.                     if event[3] >= range[i].s + ofC + ofsX - 1 and event[3] <= range[i].f + ofC + ofsX then
  562.                           return i , options[i]
  563.                     end
  564.                   end
  565.             end
  566.           end
  567.     end
  568.   end
  569. end
  570.  
  571. local flag = true
  572. local fSlash = "/"
  573. local path = {dir:match("[^/]+")}
  574. local function stringPath() -- compacted this a lot
  575.   return fSlash..table.concat(path,fSlash)
  576. end
  577.  
  578. local function osRunSpaces(sFileLocation,...) -- getRunningProgram() ["shell"] = shell
  579.   clear()
  580.   if os.newThread then
  581.     os.newThread(false,...)
  582.   else
  583.     local fProg,probblem = loadfile(sFileLocation)
  584.     if fProg then
  585.           local tEnv = {["shell"] = {}}
  586.           setmetatable(tEnv.shell,{ __index = shell})
  587.           tEnv.shell.getRunningProgram = function()
  588.             return sFileLocation
  589.           end
  590.           setmetatable(tEnv,{ __index = _G})
  591.           setfenv(fProg,tEnv)
  592.           local test,probblem = pcall(fProg,...)
  593.           if not test then
  594.             print(probblem)
  595.             dialogBox("ERROR",tostring(probblem),{"ok"},3,30)
  596.           else
  597.             return true
  598.           end
  599.     else
  600.           print(probblem)
  601.           dialogBox("ERROR",tostring(probblem),{"ok"},3,30)
  602.     end
  603.   end
  604. end
  605.  
  606.  
  607.  
  608.  
  609. local flag = true
  610. local fSlash = "/"
  611. local path = {dir:match("[^/]+")}
  612. local function stringPath() -- compacted this a lot
  613.   return fSlash..table.concat(path,fSlash)
  614. end
  615.  
  616. local function osRunSpaces(sFileLocation,...) -- getRunningProgram() ["shell"] = shell
  617.   clear()
  618.   if os.newThread then
  619.     os.newThread(false,...)
  620.   else
  621.     local fProg,probblem = loadfile(sFileLocation)
  622.     if fProg then
  623.           local tEnv = {["shell"] = {}}
  624.           setmetatable(tEnv.shell,{ __index = shell})
  625.           tEnv.shell.getRunningProgram = function()
  626.             return sFileLocation
  627.           end
  628.           setmetatable(tEnv,{ __index = _G})
  629.           setfenv(fProg,tEnv)
  630.           local test,probblem = pcall(fProg,...)
  631.           if not test then
  632.             print(probblem)
  633.             dialogBox("ERROR",tostring(probblem),{"ok"},3,30)
  634.           else
  635.             return true
  636.           end
  637.     else
  638.           print(probblem)
  639.           dialogBox("ERROR",tostring(probblem),{"ok"},3,30)
  640.     end
  641.   end
  642. end
  643.  
  644. local function fileSelect(mode) -- save_file open_file browse < not yet implemented
  645.  
  646.   local title = sTitle.." "..ver
  647.   local bRun = true
  648.   local clipboard = nil
  649.   local cut = false
  650.  
  651.   local termX,termY = term.getSize()
  652.   local offsetX,offsetY = 1,1
  653.   local hight,width = math.ceil(termY-2),math.ceil(termX-2)
  654.   local oldHight,oldWidth
  655.  
  656.   -- offsets
  657.   local boxOffX,boxOffY = offsetX,offsetY + 2
  658.   local boxH,boxW = hight - 2 ,width - 2
  659.  
  660.   local barX,barY = offsetX + 1,offsetY + 2
  661.   local barH,barW = 1,width - 1
  662.  
  663.   local tbarX,tbarY = offsetX + 1,offsetY + 1
  664.   local tbarH,tbarW = 1,width - 1
  665.  
  666.   local exitX,exitY = offsetX + width - 1 ,offsetY + 1
  667.  
  668.   local pading = string.rep(" ",boxW)
  669.   local list
  670.  
  671.   local listOff = 0
  672.  
  673.   local sPath
  674.   local tItemList = {}
  675.  
  676.   local function newList()
  677.     listOff = 0
  678.     flag = true
  679.     tItemList = {{n = "..", id = "back"}} -- adds a back item at top of list
  680.     sPath = stringPath()
  681.     local folders = {}
  682.     local files = {}
  683.     local disks = {}
  684.     if not fs.exists(sPath) then
  685.           path = {}
  686.           sPath = stringPath()
  687.           dialogBox("ERROR:","Path no longer exists",{"ok"})
  688.     end
  689.     local test,list = pcall(fs.list,sPath) -- stopes fs.list crash
  690.     if list == nil then
  691.           list = {}
  692.           dialogBox("ERROR : ","fs.list crashed",{"ok"})
  693.     end
  694.     if #path == 0 then
  695.           for i,v in pairs(rs.getSides()) do
  696.             if disk.isPresent(v) then
  697.                   if disk.hasData(v) then
  698.                     table.insert(tItemList,{n = disk.getMountPath(v), id = "disk",s = v})
  699.                     disks[disk.getMountPath(v)] = true
  700.                   elseif disk.hasAudio(v) then
  701.                     table.insert(tItemList,{n = disk.getAudioTitle(v), id = "audio",s = v})
  702.                   end
  703.             end
  704.           end
  705.     end
  706.     for i,v in pairs(list) do
  707.           if fs.isDir(sPath..fSlash..v) then
  708.             table.insert(folders,v)
  709.           else
  710.             table.insert(files,v)
  711.           end
  712.     end
  713.     table.sort(folders)
  714.     table.sort(files)
  715.     for i,v in pairs(folders) do
  716.           if disks[v] == nil then
  717.             table.insert(tItemList,{n = v, id = "folder"})
  718.           end
  719.     end
  720.     for i,v in pairs(files) do
  721.           table.insert(tItemList,{n = v, id = "file"})
  722.     end
  723.   end
  724.  
  725.   local function paste()
  726.     if cut then
  727.           local s, m = pcall(
  728.             function()
  729.                   fs.move(clipboard[1]..fSlash..clipboard[2], stringPath()..fSlash..clipboard[2])
  730.                   cut = false
  731.                   clipboard = nil
  732.                   end)
  733.           if not s then
  734.             dialogBox("Error", (m or "Couldn't move"), {"ok"}, 4, 30)
  735.           end
  736.           if bugTest then
  737.             local x, y = term.getCursorPos()
  738.             term.setCursorPos(1, ({term.getSize()})[2])
  739.             write("from "..clipboard[1]..fSlash..clipboard[2].." to "..stringPath()..fSlash..clipboard[2])
  740.           end
  741.     else
  742.           local s, m = pcall(function()
  743.             if fs.exists(stringPath()..fSlash..clipboard[2]) then
  744.                   fs.copy(clipboard[1]..fSlash..clipboard[2], stringPath()..fSlash.."copy-"..clipboard[2])
  745.             else
  746.                   fs.copy(clipboard[1]..fSlash..clipboard[2], stringPath()..fSlash..clipboard[2])
  747.             end
  748.             end)
  749.           if not s then
  750.             dialogBox("Error", (m or "Couldn't copy"), {"ok"}, 4, 30)
  751.           end
  752.           if bugTest then
  753.             local x, y = term.getCursorPos()
  754.             term.setCursorPos(1, ({term.getSize()})[2])
  755.             write("from "..clipboard[1]..fSlash..clipboard[2].." to "..stringPath()..fSlash..clipboard[2])
  756.           end
  757.     end
  758.     newList()
  759.   end
  760.  
  761.   -- this section bellow handles the right click menu
  762.  
  763.   local tmenu = {
  764.   disk = {
  765.   ["Open"] = function(tItem)
  766.   table.insert(path,tItem.n)
  767.   newList()
  768.   end,
  769.   ["Copy"] = function(tItem)
  770.   clipboard = {stringPath(), tItem.n}
  771.   cut = false
  772.   end,
  773.   ["Eject"] = function(tItem)
  774.   if dialogBox("Confirm","Eject "..fSlash..tItem.n.." "..tItem.s,{"yes","no"}) == 1 then
  775.     disk.eject(tItem.s)
  776.     newList()
  777.   end
  778.   end,
  779.   ["ID label"] = function(tItem)
  780.   dialogBox("ID label",disk.getDiskID(tItem.s).." "..tostring(disk.getLabel(tItem.s)),{"ok"})
  781.   end,
  782.   ["Set label"] = function(tItem)
  783.   local name = InputBox("Label?")
  784.   if name then
  785.     disk.setLabel(tItem.s,name)
  786.   end
  787.   end,
  788.   ["Clear label"] = function(tItem)
  789.   if dialogBox("Confirm","Cleal Label from "..tItem.s,{"yes","no"}) == 1 then
  790.     disk.setLabel(tItem.s)
  791.   end
  792. end
  793. },
  794. folder = {
  795. ["Open"] = function(temp)
  796. table.insert(path,temp.n)
  797. newList()
  798. end,
  799. ["Copy"] = function(tItem)
  800. clipboard = {stringPath(), tItem.n}
  801. cut = false
  802. end,
  803. ["Cut"] = function(tItem)
  804. clipboard = {stringPath(), tItem.n}
  805. cut = true
  806. end,
  807. ["Delete"] = function(tItem)
  808. if dialogBox("Confirm","Delete "..tItem.id.." "..tItem.n,{"yes","no"}) == 1 then
  809.   if fs.isReadOnly(stringPath()..fSlash..tItem.n) then
  810.     dialogBox("ERROR",tItem.id.." Is read Only",{"ok"})
  811.   else
  812.     fs.delete(stringPath()..fSlash..tItem.n)
  813.     newList()
  814.   end
  815. end
  816. end,
  817. ["Rename"] = function(tItem)
  818. local sName = InputBox("New Name")
  819. if type(sName) == "string" and sName ~= "" then
  820.   local s, m = pcall(function()
  821.     fs.move(stringPath()..fSlash..tItem.n,stringPath()..fSlash..sName)
  822.     end)
  823.   if not s then
  824.     dialogBox("Error", (m or "Rename failed"), {"ok"})
  825.   end
  826. end
  827. newList()
  828. end
  829. },
  830. file = {
  831. ["Run"] = {
  832. ["Run"] = function(tItem)
  833. osRunSpaces(stringPath()..fSlash..tItem.n)
  834. end,
  835. ["Run CMD"] = function(tItem)
  836. local cmd = InputBox("Commands")
  837. if cmd then
  838.   osRunSpaces(stringPath()..fSlash..tItem.n,unpack(fixArgs(cmd)))
  839. end
  840. end,
  841. },
  842. ["Open With"] = customLaunch,
  843. ["Rename"] = function(tItem)
  844. local sName = InputBox("New Name")
  845. if type(sName) == "string" and sName ~= "" then
  846.   local s, m = pcall(function()
  847.     fs.move(stringPath()..fSlash..tItem.n,stringPath()..fSlash..sName)
  848.     end)
  849.   if not s then
  850.     dialogBox("Error", (m or "Rename failed"), {"ok"})
  851.   end
  852. end
  853. newList()
  854. end,
  855. ["Delete"] = function(tItem)
  856. if dialogBox("Confirm","Delete "..tItem.id.." "..tItem.n,{"yes","no"}) == 1 then
  857.   if fs.isReadOnly(stringPath()..fSlash..tItem.n) then
  858.     dialogBox("ERROR",tItem.id.." Is read Only",{"ok"})
  859.   else
  860.     fs.delete(stringPath()..fSlash..tItem.n)
  861.     newList()
  862.   end
  863. end
  864. end,
  865. ["Cut"] = function(tItem)
  866. clipboard = {stringPath(), tItem.n}
  867. cut = true
  868. end,
  869. ["Copy"] = function(tItem)
  870. clipboard = {stringPath(), tItem.n}
  871. cut = false
  872. end
  873. },
  874. audio = {
  875. ["Play"] = 1,
  876. ["Eject"] = 1
  877. },
  878. back = {
  879. },
  880.     blank = { -- tmenu.blank.Paste =
  881.     ["Paste"] = nil,
  882.     ["New File"] = function()
  883.     local name = InputBox()
  884.     if name then
  885.           if fs.exists(stringPath()..fSlash..name) then
  886.             dialogBox("ERROR","Name exists",{"ok"})
  887.           else
  888.             local file = fs.open(stringPath()..fSlash..name,"w")
  889.             if file then
  890.                   file.write("")
  891.                   file.close()
  892.                   newList()
  893.             else
  894.                   dialogBox("ERROR","File not created",{"ok"})
  895.             end
  896.           end
  897.     end
  898.     end,
  899.     ["New Folder"] = function()
  900.     local name = InputBox()
  901.     if name then
  902.           if fs.exists(stringPath()..fSlash..name) then
  903.             dialogBox("ERROR","Name exists",{"ok"})
  904.           else
  905.             if pcall(fs.makeDir,stringPath()..fSlash..name) then
  906.                   newList()
  907.             else
  908.                   dialogBox("ERROR","Access Denied",{"ok"})
  909.             end
  910.           end
  911.     end
  912.     end,
  913.     ["Preferences"] = preferences
  914.     },
  915.   }
  916.  
  917.   -- end right click menu
  918.  
  919.   local function scrollBar(posX,posY)
  920.     if posX == boxOffX+boxW+1 and posY > boxOffY and posY <= boxOffY+boxH then
  921.           if #tItemList > boxH then
  922.             if posY == boxOffY + 1 then
  923.                   listOff = 0
  924.             elseif posY == boxOffY+boxH then
  925.                   listOff = #tItemList + 1 - boxH
  926.             else
  927.                   listOff = math.ceil((posY - boxOffY - 1 )*(((#tItemList - boxH+2)/boxH)))
  928.             end
  929.             flag = true
  930.           end
  931.     end
  932.   end
  933.  
  934.   newList()
  935.  
  936.   while bRun do
  937.     if flag then
  938.           flag = false
  939.           -- clear
  940.           if oldHight ~= hight and oldWidth ~= width then
  941.             term.setBackgroundColor(colors.black)
  942.             term.clear()
  943.             oldHight,oldWidth = hight,width
  944.           end
  945.           -- draw top title bar
  946.           local b = tbarW - #title -2
  947.           if b < 0 then
  948.             b = 0
  949.           end
  950.           printC(tbarX,tbarY,titleBar.txt,titleBar.back,string.sub(" "..title,1,tbarW)..string.rep(" ", b ))
  951.           term.setTextColor(colors.white)
  952.           term.setBackgroundColor(colors.red)
  953.           term.write("X")
  954.          
  955.           -- draw location bar
  956.           local a = barW - #sPath - 1
  957.           if a < 0 then
  958.             a = 0
  959.           end
  960.           local tmppath = sPath
  961.           if shell and shell.getDisplayName then
  962.             tmppath = shell.getDisplayName(sPath)
  963.             --dialogBox("yay")
  964.           else
  965.             --dialogBox("moop")
  966.           end
  967.           tmppath = tmppath or sPath
  968.           local a = barW - #tmppath - 1
  969.           if a < 0 then
  970.             a = 0
  971.           end
  972.           printC(barX,barY,addressBar.txt,addressBar.back,string.sub(" "..tmppath,1,barW)..string.rep(" ",a))
  973.          
  974.           -- draw scroll bar
  975.           if #tItemList > boxH then
  976.             term.setBackgroundColor(scrollCol.back)
  977.             for i = 1,boxH do
  978.                   term.setCursorPos(boxOffX+boxW+1,i + boxOffY)
  979.                   local scroll = math.floor( boxH* (listOff/(#tItemList-boxH+2)) )+1
  980.                   if i == scroll then
  981.                     term.setBackgroundColor(scrollCol.button)
  982.                     term.write(" ")
  983.                     term.setBackgroundColor(scrollCol.back)
  984.                   else
  985.                     term.write(" ")
  986.                   end
  987.             end
  988.           else
  989.             term.setBackgroundColor(scrollCol.off)
  990.             for i = 1,boxH do
  991.                   term.setCursorPos(boxOffX+boxW+1,i + boxOffY)
  992.                   term.write(" ")
  993.             end
  994.           end
  995.          
  996.           -- draw main section
  997.  
  998.           for i = 1,boxH do -- listOff
  999.             local sel = i+listOff
  1000.             if tItemList[sel] then
  1001.                   printC(1+boxOffX,i+boxOffY,(tIcons[tItemList[sel].id].tCol or itemWindo.txt),(tIcons[tItemList[sel].id].bCol or itemWindo.back),( tIcons[tItemList[sel].id].txt or "   "))
  1002.                   printC(4+boxOffX,i+boxOffY,itemWindo.txt,itemWindo.back,string.sub(" "..tItemList[sel].n..pading,1,boxW-3))
  1003.             else
  1004.                   printC(1+boxOffX,i+boxOffY,itemWindo.txt,itemWindo.back,pading)
  1005.             end
  1006.           end
  1007.          
  1008.           if bugTest then
  1009.             printC(1,1,"black","white",listOff.." "..boxOffY.." "..boxH)
  1010.           end
  1011.          
  1012.     end
  1013.    
  1014.     -- react to events
  1015.     local event = {os.pullEvent()}
  1016.    
  1017.     if event[1] == "mouse_click" then
  1018.           if inBouwndry(event[3],event[4],boxOffX+1,boxOffY+1,boxW,boxH) then
  1019.             local selected = tItemList[event[4]+listOff-boxOffY]
  1020.             if selected and inBouwndry(event[3],event[4],boxOffX+1,event[4],#selected.n + 4,1) then
  1021.                   if event[2] == 1 then -- left mouse
  1022.                     if selected.id == "back" then
  1023.                           table.remove(path,#path)
  1024.                           newList()
  1025.                     elseif selected.id == "folder" or selected.id == "disk" then
  1026.                           table.insert(path,selected.n)
  1027.                           newList()
  1028.                     elseif selected.id == "file" then
  1029.                           if dialogBox("Open File?",selected.n,{"yes","no"}) == 1 then
  1030.                             local sOut = stringPath()..fSlash..selected.n
  1031.                             return sOut
  1032.                           end
  1033.                           flag = true
  1034.                     end
  1035.                   end
  1036.             end
  1037.           elseif event[2] == 1 and event[3] == exitX and event[4] == exitY then
  1038.             if dialogBox("Confirm","Exit application",{"yes","no"}) == 1 then
  1039.                   bRun = false
  1040.             end
  1041.             flag = true
  1042.           elseif event[2] == 1 then
  1043.             scrollBar(event[3],event[4])
  1044.           end
  1045.     elseif event[1] == "mouse_scroll" then -- flag this needs new math
  1046.           local old = listOff
  1047.           listOff = listOff + event[2]
  1048.           if listOff < 0 then
  1049.             listOff = 0
  1050.           end
  1051.           if #tItemList + 1 - boxH > 0 and listOff > #tItemList + 1 - boxH then
  1052.             listOff = #tItemList + 1 - boxH
  1053.           elseif listOff > 0 and #tItemList + 1 - boxH < 0 then
  1054.             listOff = 0
  1055.           end
  1056.           if listOff ~= old then
  1057.             flag = true
  1058.           end
  1059.          
  1060.     elseif event[1] == "mouse_drag" then -- scroll bar
  1061.           scrollBar(event[3],event[4])
  1062.     elseif event[1] == "disk" or event[1] == "disk_eject" then
  1063.           newList()
  1064.     elseif event[1] == "window_resize" then
  1065.           termX,termY = term.getSize()
  1066.           offsetX,offsetY = 1,1
  1067.           hight,width = math.ceil(termY-2),math.ceil(termX-2)
  1068.          
  1069.           boxOffX,boxOffY = offsetX,offsetY + 2
  1070.           boxH,boxW = hight - 2 ,width - 2
  1071.          
  1072.           barX,barY = offsetX + 1,offsetY + 2
  1073.           barH,barW = 1,width - 1
  1074.          
  1075.           tbarX,tbarY = offsetX + 1,offsetY + 1
  1076.           tbarH,tbarW = 1,width - 1
  1077.          
  1078.           exitX,exitY = offsetX + width - 1 ,offsetY + 1
  1079.           pading = string.rep(" ",boxW)
  1080.          
  1081.           flag = true
  1082.     elseif event[1] == "redraw" then
  1083.           flag = true
  1084.     end
  1085.   end
  1086. end
  1087. return fileSelect()
  1088. end
  1089. local out = selectFile()
  1090. term.clear()
  1091. term.setCursorPos(1,1)
  1092. print(out or "No out")
  1093. os.pullEvent()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement