Advertisement
BigSHinyToys

[Computer Craft] Mosue file browser 0.9

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