Advertisement
BigSHinyToys

[Computer Craft] Mouse File Browser ver 1.3 exp

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