Advertisement
BigSHinyToys

[Computer Craft] Mouse File Browser v 0.7

Nov 18th, 2012
314
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 28.92 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.7"
  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.     sleep(4)
  173. end
  174.  
  175. local function clear()
  176.     term.setBackgroundColor(colors.black)
  177.     term.setTextColor(colors.white)
  178.     term.clear()
  179.     term.setCursorBlink(false)
  180.     term.setCursorPos(1,1)
  181. end
  182.  
  183. --[[
  184.         Code thanks to Cruor
  185.         http://www.computercraft.info/forums2/index.php?/topic/5802-support-for-shell/
  186. ]]--
  187. local function fixArgs(...)
  188.     local tReturn={}
  189.     local str=table.concat({...}," ")
  190.     local sMatch
  191.     while str and #str>0 do
  192.         if string.sub(str,1,1)=="\"" then
  193.             sMatch, str=string.match(str, "\"(.-)\"%s*(.*)")
  194.         else
  195.             sMatch, str=string.match(str, "(%S+)%s*(.*)")
  196.         end
  197.         table.insert(tReturn,sMatch)
  198.     end
  199.     return tReturn
  200. end
  201. --[[ end Cruor function ]]--
  202.  
  203.  
  204. -- modified read made to play nice with coroutines
  205.  
  206. local function readMOD( _sReplaceChar, _tHistory,_wdth)
  207.     local sLine = ""
  208.     term.setCursorBlink( true )
  209.  
  210.     local nHistoryPos = nil
  211.     local nPos = 0
  212.     if _sReplaceChar then
  213.         _sReplaceChar = string.sub( _sReplaceChar, 1, 1 )
  214.     end
  215.    
  216.     local sx, sy = term.getCursorPos() 
  217.  
  218.     local w, h = term.getSize()
  219.     if _wdth and type(_wdth) == "number" then
  220.         w = sx + _wdth - 1
  221.     end
  222.    
  223.     local function redraw( _sCustomReplaceChar )
  224.         local nScroll = 0
  225.         if sx + nPos >= w then
  226.             nScroll = (sx + nPos) - w
  227.         end
  228.            
  229.         term.setCursorPos( sx + _wdth - 1, sy )
  230.         term.write(" ")
  231.         term.setCursorPos( sx, sy )
  232.         local sReplace = _sCustomReplaceChar or _sReplaceChar
  233.         if sReplace then
  234.             term.write( string.rep(sReplace,_wdth) )
  235.         else
  236.             term.write( string.sub( sLine, nScroll + 1 ,nScroll + _wdth) )
  237.         end
  238.         term.setCursorPos( sx + nPos - nScroll, sy )
  239.     end
  240.    
  241.     while true do
  242.         local sEvent, param = os.pullEvent()
  243.         if sEvent == "char" then
  244.             sLine = string.sub( sLine, 1, nPos ) .. param .. string.sub( sLine, nPos + 1 )
  245.             nPos = nPos + 1
  246.             redraw()
  247.            
  248.         elseif sEvent == "key" then
  249.            
  250.             if param == keys.left then
  251.                 -- Left
  252.                 if nPos > 0 then
  253.                     nPos = nPos - 1
  254.                     redraw()
  255.                 end
  256.                
  257.             elseif param == keys.right then
  258.                 -- Right               
  259.                 if nPos < string.len(sLine) then
  260.                     nPos = nPos + 1
  261.                     redraw()
  262.                 end
  263.            
  264.             elseif param == keys.up or param == keys.down then
  265.                 -- Up or down
  266.                 if _tHistory then
  267.                     redraw(" ");
  268.                     if param == keys.up then
  269.                         -- Up
  270.                         if nHistoryPos == nil then
  271.                             if #_tHistory > 0 then
  272.                                 nHistoryPos = #_tHistory
  273.                             end
  274.                         elseif nHistoryPos > 1 then
  275.                             nHistoryPos = nHistoryPos - 1
  276.                         end
  277.                     else
  278.                         -- Down
  279.                         if nHistoryPos == #_tHistory then
  280.                             nHistoryPos = nil
  281.                         elseif nHistoryPos ~= nil then
  282.                             nHistoryPos = nHistoryPos + 1
  283.                         end                    
  284.                     end
  285.                    
  286.                     if nHistoryPos then
  287.                         sLine = _tHistory[nHistoryPos]
  288.                         nPos = string.len( sLine )
  289.                     else
  290.                         sLine = ""
  291.                         nPos = 0
  292.                     end
  293.                     redraw()
  294.                 end
  295.             elseif param == keys.backspace then
  296.                 -- Backspace
  297.                 if nPos > 0 then
  298.                     redraw(" ");
  299.                     sLine = string.sub( sLine, 1, nPos - 1 ) .. string.sub( sLine, nPos + 1 )
  300.                     nPos = nPos - 1                
  301.                     redraw()
  302.                 end
  303.             elseif param == keys.home then
  304.                 -- Home
  305.                 nPos = 0
  306.                 redraw()       
  307.             elseif param == keys.delete then
  308.                 if nPos < string.len(sLine) then
  309.                     redraw(" ");
  310.                     sLine = string.sub( sLine, 1, nPos ) .. string.sub( sLine, nPos + 2 )              
  311.                     redraw()
  312.                 end
  313.             elseif param == keys["end"] then
  314.                 -- End
  315.                 nPos = string.len(sLine)
  316.                 redraw()
  317.             end
  318.         elseif sEvent == "redraw" then
  319.             redraw()
  320.         elseif sEvent == "return" then
  321.             term.setCursorBlink( false )
  322.             return sLine
  323.         end
  324.     end
  325.    
  326.     term.setCursorBlink( false )
  327.    
  328.     return sLine
  329. end
  330.  
  331. -- end modified read
  332.  
  333. local function printC(posX,posY,textCol,backCol,text)
  334.     term.setCursorPos(posX,posY)
  335.     term.setTextColor(textCol)
  336.     term.setBackgroundColor(backCol)
  337.     term.write(text)
  338. end
  339.  
  340. local function InputBox(title)
  341.     local boxW,boxH = 26,3
  342.     local termX,termY = term.getSize()
  343.     local ofsX,ofsY = math.ceil((termX/2) - (boxW/2)) , math.ceil((termY/2) - (boxH/2)) -- offset from top left
  344.     local options = {"ok","cancel"}
  345.    
  346.     local selected = 1
  347.     local space = 0
  348.     local range = {}
  349.     for i = 1,#options do
  350.         range[i] = {s = space,f = space + string.len(options[i])}
  351.         space = space + string.len(options[i])+3
  352.     end
  353.     local ofC = (boxW/2) - (space/2)
  354.    
  355.     local function drawBox()
  356.         printC(ofsX,ofsY,colors.black,colors.blue,string.rep(" ",boxW))
  357.         printC(ofsX+1,ofsY,colors.black,colors.blue,(title or "User Input"))
  358.         printC(ofsX,ofsY+1,colors.black,colors.white,string.rep(" ",boxW))
  359.         printC(ofsX,ofsY+2,colors.black,colors.white,string.rep(" ",boxW))
  360.         printC(ofsX,ofsY+3,colors.black,colors.white,string.rep(" ",boxW))
  361.        
  362.         for i = 1,#options do
  363.             if i == selected then
  364.                 term.setBackgroundColor(colors.lightGray)
  365.                 term.setCursorPos(range[i].s + ofC + ofsX - 1,ofsY + 3)
  366.                 term.write("["..options[i].."]")
  367.                 term.setBackgroundColor(colors.white)
  368.                 term.write(" ")
  369.             else
  370.                 term.setCursorPos(range[i].s + ofC + ofsX - 1,ofsY + 3)
  371.                 term.write(" "..options[i].." ")
  372.             end
  373.         end
  374.        
  375.         printC(ofsX+2,ofsY+2,colors.black,colors.lightGray,string.rep(" ",boxW-4))
  376.     end
  377.     drawBox()
  378.     term.setCursorPos(ofsX+2,ofsY+2)
  379.     local co = coroutine.create(function() return readMOD(nil,nil,boxW - 4) end)
  380.     while true do
  381.         local event = {os.pullEvent()}
  382.         if event[1] == "key" or event[1] == "char" then
  383.             if event[2] == 28 then
  384.                 local test,data = coroutine.resume(co,"return")
  385.                 return data
  386.             else
  387.                 coroutine.resume(co,unpack(event))
  388.             end
  389.         elseif event[1] == "mouse_click" then
  390.             if event[4] == ofsY + 3 then
  391.                 for i = 1,#options do
  392.                     if event[3] >= range[i].s + ofC + ofsX - 1 and event[3] <= range[i].f + ofC + ofsX then
  393.                         if options[i] == "ok" then
  394.                             local test,data = coroutine.resume(co,"return")
  395.                             return data
  396.                         elseif options[i] == "cancel" then
  397.                             return
  398.                         end
  399.                     end
  400.                 end
  401.             end
  402.         end
  403.     end
  404. end
  405.  
  406. local function dialogBox(title,message,options, h, w)
  407.     term.setCursorBlink(false)
  408.     local selected = 1
  409.     title = title or ""
  410.     message = message or ""
  411.     options = options or {}
  412.     local boxW,boxH = (w or 26), (h or 3)
  413.     local termX,termY = term.getSize()
  414.     local ofsX,ofsY = math.ceil((termX/2) - (boxW/2)) , math.ceil((termY/2) - (boxH/2)) -- offset from top left
  415.    
  416.     local space = 0
  417.     local range = {}
  418.     for i = 1,#options do
  419.         range[i] = {s = space,f = space + string.len(options[i])}
  420.         space = space + string.len(options[i])+3
  421.     end
  422.     local ofC = math.ceil((boxW/2)) - math.ceil((space/2))
  423.    
  424.     local function drawBox()
  425.         if term.isColor() then
  426.             term.setTextColor(colors.black)
  427.             term.setCursorPos(ofsX,ofsY) -- F*** YOU 3 hours to find out i forgot to round numbers before sending them to this Fing function
  428.             term.setBackgroundColor(colors.blue)
  429.             term.write(" "..title..string.rep(" ",boxW-#title-5).."_[]")
  430.             term.setBackgroundColor(colors.red)
  431.             term.setTextColor(colors.white)
  432.             term.write("X")
  433.             term.setCursorPos(ofsX,ofsY+1)
  434.             term.setBackgroundColor(colors.white)
  435.             term.setTextColor(colors.black)
  436.             term.write(string.sub(" "..message..string.rep(" ",boxW),1,boxW)) -- edited
  437.             term.setCursorPos(ofsX,ofsY+2)
  438.             term.write(string.rep(" ",boxW))
  439.             term.setCursorPos(ofsX,ofsY+3)
  440.             term.write(string.rep(" ",boxW))
  441.            
  442.             for i = 1,#options do
  443.                 if i == selected then
  444.                     term.setBackgroundColor(colors.lightGray)
  445.                     term.setCursorPos(range[i].s + ofC + ofsX - 1,ofsY + 3)
  446.                     term.write("["..options[i].."]")
  447.                     term.setBackgroundColor(colors.white)
  448.                     term.write(" ")
  449.                 else
  450.                     term.setCursorPos(range[i].s + ofC + ofsX - 1,ofsY + 3)
  451.                     term.write(" "..options[i].." ")
  452.                 end
  453.             end
  454.             term.setCursorPos(ofsX + ofC + space,ofsY + 3)
  455.             term.write(string.rep(" ",boxW - (ofC + space)))
  456.             term.setBackgroundColor(colors.black)
  457.             term.setTextColor(colors.white)        
  458.         end
  459.     end
  460.     while true do
  461.         drawBox()
  462.         event = {os.pullEvent()}
  463.         if event[1] == "key" then
  464.             if event[2] == 203 then -- left
  465.                 selected = selected - 1
  466.                 if selected < 1 then
  467.                     selected = #options
  468.                 end
  469.             elseif event[2] == 205 then -- right
  470.                 selected = selected + 1
  471.                 if selected > #options then
  472.                     selected = 1
  473.                 end
  474.             elseif event[2] == 28 then -- enter
  475.                 return selected , options[selected]
  476.             end
  477.         elseif event[1] == "mouse_click" then
  478.             term.setCursorPos(1,1)
  479.             term.clearLine()
  480.            
  481.             if bugTest then term.write("M "..event[2].." X "..event[3].." Y "..event[4]) end
  482.            
  483.             if event[2] == 1 then
  484.                 if event[4] == ofsY + 3 then
  485.                     for i = 1,#options do
  486.                         if event[3] >= range[i].s + ofC + ofsX - 1 and event[3] <= range[i].f + ofC + ofsX then
  487.                             return i , options[i]
  488.                         end
  489.                     end
  490.                 end
  491.             end
  492.         end
  493.     end
  494. end
  495.  
  496. local function rClickMenu(title,tList,posX,posY)
  497.  
  498.     term.setCursorBlink(false)
  499.     local BoxTitle = title
  500.     local choices = tList
  501.     local termX,termY = term.getSize()
  502.     local offX,offY
  503.    
  504.     local width = #BoxTitle + 2
  505.     local hight = #choices + 1
  506.    
  507.     for i = 1,#choices do
  508.         if width < #choices[i] + 2 then
  509.             width = #choices[i] + 2
  510.         end
  511.     end
  512.    
  513.     offX,offY = math.ceil((termX/2) - (width/2)),math.ceil((termY/2) - (hight/2))
  514.    
  515.     if posX and posY then -- offX,offY = posX,posY
  516.         if posX >= termX - width - 1 then
  517.             offX = termX - width - 1
  518.         else
  519.             offX = posX
  520.         end
  521.         if posY >= termY - hight then
  522.             offY = termY - hight
  523.         else
  524.             offY = posY
  525.         end
  526.     end
  527.    
  528.     local function reDrawer()
  529.         printC(offX,offY,colors.black,colors.blue," "..BoxTitle..string.rep(" ",width - #BoxTitle - 1))
  530.         for i = 1,#choices do
  531.             printC(offX,offY + i,colors.black,colors.white," "..choices[i]..string.rep(" ",width - #choices[i] - 1))
  532.         end
  533.     end
  534.    
  535.     while true do
  536.         reDrawer()
  537.         local event = {os.pullEvent()}
  538.         if event[1] == "mouse_click" then
  539.             if event[2] == 1 then -- event[3] = x event[4] = y
  540.                 if event[4] > offY and event[4] < hight + offY and event[3] >= offX and event[3] < width + offX then
  541.                     return choices[event[4] - offY]
  542.                 else
  543.                     return
  544.                 end
  545.             elseif event[2] == 2 then
  546.                 return
  547.             end
  548.         end
  549.     end
  550.    
  551. end
  552.  
  553. local function osRunSpaces(...)
  554.     clear()
  555.     return os.run(getfenv(),...)
  556. end
  557.  
  558. local function fileSelect(mode) -- save_file open_file browse < not yet implemented
  559.    
  560.     local title = sTitle.." "..ver
  561.     local bRun = true
  562.     local flag = true
  563.     local clipboard = nil
  564.     local cut = false
  565.    
  566.     local termX,termY = term.getSize()
  567.     local offsetX,offsetY = 1,1
  568.     local hight,width = math.ceil(termY-2),math.ceil(termX-2)
  569.     local oldHight,oldWidth
  570.    
  571.     local boxOffX,boxOffY = offsetX,offsetY + 2
  572.     local boxH,boxW = hight - 2 ,width - 2
  573.    
  574.     local barX,barY = offsetX + 1,offsetY + 2
  575.     local barH,barW = 1,width - 1
  576.    
  577.     local tbarX,tbarY = offsetX + 1,offsetY + 1
  578.     local tbarH,tbarW = 1,width - 1
  579.    
  580.     local exitX,exitY = offsetX + width - 1 ,offsetY + 1
  581.    
  582.     local parth = {dir:match("[^/]+")}
  583.     local list
  584.    
  585.     local fSlash = "/"
  586.     local listOff = 0
  587.    
  588.     local function stringParth() -- compacted this alot
  589.         return fSlash..table.concat(parth,fSlash)
  590.     end
  591.    
  592.     local sParth = stringParth()
  593.     local files = {}
  594.     local folders = {}
  595.     local disks = {}
  596.  
  597.     local function NewList()
  598.         flag = true
  599.         listOff = 0
  600.         sParth = stringParth()
  601.         files = {}
  602.         folders = {}
  603.         disks = {}
  604.         test,list = pcall(fs.list,sParth)
  605.         if list == nil then
  606.             list = {}
  607.             dialogBox("ERROR : ","fs.list crashed",{"ok"})
  608.         end
  609.         for i = 1,#list do
  610.             if showAll or list[i]:sub(1,1) ~= "." then
  611.                 if fs.isDir(sParth..fSlash..list[i]) then
  612.                     table.insert(folders,list[i])
  613.                 else
  614.                     table.insert(files,list[i])
  615.                 end
  616.             end
  617.         end
  618.         if #parth == 0 then
  619.             for i,v in pairs(rs.getSides()) do
  620.                 if disk.isPresent(v) and disk.hasData(v) then
  621.                     disks[disk.getMountPath(v)] = v
  622.                 end
  623.             end
  624.         end
  625.         table.sort(disks)
  626.         table.sort(folders)
  627.         table.sort(files)
  628.     end
  629.    
  630.     NewList()
  631.    
  632.     local function size(test)
  633.         if #test > boxW - 3 then
  634.             return string.sub(test,1,boxW-3)
  635.         end
  636.         return test
  637.     end
  638.    
  639.    
  640.     while bRun do
  641.         if flag then
  642.             flag = false
  643.             -- clear
  644.             if oldHight ~= hight and oldWidth ~= width then
  645.                 term.setBackgroundColor(colors.black)
  646.                 term.clear()
  647.                 oldHight,oldWidth = hight,width
  648.             end
  649.             -- draw top title bar
  650.             term.setCursorPos(tbarX,tbarY)
  651.             term.setTextColor(colors.black)
  652.             term.setBackgroundColor(colors.blue)
  653.             local b = tbarW - #title -2
  654.             if b < 0 then
  655.                 b = 0
  656.             end
  657.             term.write(string.sub(" "..title,1,tbarW)..string.rep(" ",b))
  658.             term.setTextColor(colors.white)
  659.             term.setBackgroundColor(colors.red)
  660.             term.write("X")
  661.            
  662.             -- draw location bar
  663.            
  664.             term.setCursorPos(barX,barY)
  665.             term.setTextColor(colors.black)
  666.             term.setBackgroundColor(colors.lightGray)
  667.             local a = barW - #sParth - 1
  668.             if a < 0 then
  669.                 a = 0
  670.             end
  671.             local tmppath = sParth
  672.             if shell and shell.getDisplayName then
  673.                 tmppath = shell.getDisplayName(sParth)
  674.                 --dialogBox("yay")
  675.             else
  676.                 --dialogBox("moop")
  677.             end
  678.             tmppath = tmppath or sParth
  679.             local a = barW - #tmppath - 1
  680.             if a < 0 then
  681.                 a = 0
  682.             end
  683.             term.write(string.sub(" "..tmppath,1,barW)..string.rep(" ",a))
  684.            
  685.             -- draw scroll bar
  686.             if folders[1] ~= ".." then
  687.               table.insert(folders, 1, "..")
  688.             end
  689.            
  690.             if #folders + #files > boxH then
  691.                 term.setBackgroundColor(colors.lightGray)
  692.                 for i = 1,boxH do
  693.                     term.setCursorPos(boxOffX+boxW+1,i + boxOffY)
  694.                     local scroll = math.floor( boxH* (listOff/(#folders + #files-boxH+2)) )+1
  695.                     if i == scroll then
  696.                         term.setBackgroundColor(colors.gray)
  697.                         term.write(" ")
  698.                         term.setBackgroundColor(colors.lightGray)
  699.                     else
  700.                         term.write(" ")
  701.                     end
  702.                 end
  703.             else
  704.                 term.setBackgroundColor(colors.gray)
  705.                 for i = 1,boxH do
  706.                     term.setCursorPos(boxOffX+boxW+1,i + boxOffY)
  707.                     term.write(" ")
  708.                 end
  709.             end
  710.            
  711.             -- draw main section
  712.            
  713.             term.setTextColor(colors.black)
  714.             term.setBackgroundColor(colors.cyan)
  715.             for i = 1,boxH do
  716.                 term.setCursorPos(1+boxOffX,i+boxOffY)
  717.                 if folders[i+listOff] then
  718.                     if disks[folders[i+listOff]] then
  719.                         term.setTextColor(colors.orange)
  720.                         term.setBackgroundColor(colors.blue)
  721.                         term.write("D!")
  722.                     else
  723.                         term.setTextColor(colors.blue)
  724.                         term.setBackgroundColor(colors.lightBlue)
  725.                         term.write("[]")
  726.                     end
  727.                     if folders[i+listOff]:sub(1,1) == "." and folders[i+listOff] ~= ".." then
  728.                         term.setTextColor(colors.gray)
  729.                     else
  730.                         term.setTextColor(colors.black)
  731.                     end
  732.                     term.setBackgroundColor(colors.cyan)
  733.                     local repTimes = boxW - #folders[i+listOff] - 3
  734.                     if repTimes < 0 then
  735.                         repTimes = 0
  736.                     end
  737.                     term.write(" "..size(folders[i+listOff])..string.rep(" ",repTimes))
  738.                 elseif files[i-#folders+listOff] then
  739.                     local repTimes = boxW - #files[i-#folders+listOff] - 3
  740.                     if repTimes < 0 then
  741.                         repTimes = 0
  742.                     end
  743.                     term.write("   "..size(files[i-#folders+listOff])..string.rep(" ",repTimes))
  744.                 else
  745.                     term.write(string.rep(" ",boxW))
  746.                 end
  747.             end
  748.             term.setTextColor(colors.white)
  749.             term.setBackgroundColor(colors.black)
  750.            
  751.             if bugTest then
  752.                 term.setCursorPos(1,1)
  753.                 term.write(listOff.." "..boxOffY.." "..boxH)
  754.             end
  755.            
  756.             -- resume("redraw")
  757.         end
  758.         -- react to events
  759.        
  760.         local event = {os.pullEvent()}
  761.        
  762.         if event[1] == "mouse_click" then
  763.             if event[2] == 1 then -- left mouse
  764.                 local temp = nil
  765.                 if event[4] > boxOffY and event[4] <= boxH + boxOffY then
  766.                     temp = folders[event[4]+listOff-boxOffY] or files[event[4]-#folders+listOff-boxOffY]
  767.                 end
  768.                 if temp and event[3] > boxOffX and event[3] <= #temp + 3 + boxOffX and event[3] < boxOffX + boxW then
  769.                     if temp == ".." then
  770.                         table.remove(parth,#parth)
  771.                         NewList()
  772.                     elseif fs.isDir(stringParth()..fSlash..temp) then
  773.                         table.insert(parth,temp)
  774.                         NewList()
  775.                     else
  776.                         if fs.exists(stringParth()..fSlash..temp) then
  777.                             if dialogBox("Run file ?",temp,{"yes","no"}) == 1 then
  778.                                 local test,info = osRunSpaces(stringParth()..fSlash..temp)
  779.                                 term.setCursorBlink(false)
  780.                                 if not test then
  781.                                     dialogBox("ERROR",tostring(info),{"ok"})
  782.                                 end
  783.                             end
  784.                             flag = true
  785.                         end
  786.                     end
  787.                 elseif event[3] == boxOffX+boxW+1 and event[4] > boxOffY and event[4] <= boxOffY+boxH then
  788.                     if #folders + #files > boxH then
  789.                         if event[4] == boxOffY + 1 then
  790.                             listOff = 0
  791.                         elseif event[4] == boxOffY+boxH then
  792.                             listOff = #folders + #files + 1 - boxH
  793.                         else
  794.                             listOff = math.ceil((event[4] - boxOffY - 1 )*(((#folders + #files - boxH+2)/boxH)))
  795.                         end
  796.                         flag = true
  797.                     end
  798.                 elseif event[3] == exitX and event[4] == exitY then
  799.                     if dialogBox("Confirm","Exit application",{"yes","no"}) == 1 then
  800.                         bRun = false
  801.                     end
  802.                     flag = true
  803.                 end
  804.             elseif event[2] == 2 then -- right mouse
  805.                 local temp = nil
  806.                 local sChoice = nil
  807.                 local sType = nil
  808.                
  809.                 if event[4] > boxOffY and event[4] <= boxH + boxOffY then
  810.                     temp = folders[event[4]+listOff-boxOffY] or files[event[4]-#folders+listOff-boxOffY]
  811.                 end
  812.                
  813.                 -- moved
  814.                 local paste
  815.                 if clipboard then
  816.                     paste = "Paste"
  817.                 end
  818.                
  819.                 if temp and event[3] > boxOffX and event[3] <= #temp + 3 + boxOffX and event[3] < boxOffX + boxW then
  820.                     sType = "File"
  821.                    
  822.                     if fs.isDir(stringParth()..fSlash..temp) then
  823.                         sType = "Folder"
  824.                     end
  825.                    
  826.                     if disks[temp] then
  827.                         sChoice = rClickMenu("Options",{"Open","Eject"},event[3]+1,event[4]+1)
  828.                     else
  829.                         if sType == "Folder" then
  830.                             sChoice = rClickMenu("Options",{"Open","Delete"},event[3]+1,event[4]+1)
  831.                         else
  832.                             if windowDimensions then
  833.                                 sChoice = rClickMenu("Options",{"Run","Run CMD","deBug","Run win","Rename","Edit","Paint","Delete", "Cut", "Copy", paste},event[3]+1,event[4]+1)
  834.                             else
  835.                                 sChoice = rClickMenu("Options",{"Run","Run CMD","deBug","CHIP 8","Rename","Edit","Paint","Delete", "Cut", "Copy", paste},event[3]+1,event[4]+1)
  836.                             end
  837.                         end
  838.                     end
  839.                    
  840.                 else
  841.                     if event[3] > boxOffX and event[3] <= 5 + boxOffX and event[3] < boxOffX + boxW then
  842.                         if event[4] > offsetY and event[4] < hight + offsetY then
  843.                             sChoice = rClickMenu("Options",{"New File","New Folder", paste},event[3]+1,event[4]+1)
  844.                         end
  845.                     else
  846.                         table.remove(parth,#parth)
  847.                         NewList()
  848.                     end
  849.                 end
  850.                
  851.                 if sChoice == "Run win" and windowDimensions then -- choice execution
  852.                     osRunSpaces("start",stringParth()..fSlash..temp) -- might be a probblem -- shell
  853.                     flag = true
  854.                 elseif sChoice == "deBug" then
  855.                     osRunSpaces(stringParth()..fSlash..temp)
  856.                     term.setCursorBlink(false)
  857.                     os.pullEvent("key")
  858.                 elseif sChoice == "CHIP 8" then -- this was a temo hackin will be going
  859.                     local function openDevice(sType)
  860.                         for i,v in pairs(rs.getSides()) do
  861.                             if peripheral.isPresent(v) and peripheral.getType(v) == sType then
  862.                                 return peripheral.wrap(v),v
  863.                             end
  864.                         end
  865.                     end
  866.                     local func,side = openDevice("monitor")
  867.                     osRunSpaces("chip8",stringParth()..fSlash..temp,side)
  868.                 elseif sChoice == "New File" then -- experemntal
  869.                     local name = InputBox()
  870.                     if name then
  871.                         if fs.exists(stringParth()..fSlash..name) then
  872.                             dialogBox("ERROR","Name exists",{"ok"})
  873.                         else
  874.                             local file = fs.open(stringParth()..fSlash..name,"w")
  875.                             if file then
  876.                                 file.write("")
  877.                                 file.close()
  878.                                 NewList()
  879.                             else
  880.                                 dialogBox("ERROR","File not created",{"ok"})
  881.                             end
  882.                         end
  883.                     end
  884.                 elseif sChoice == "New Folder" then -- experemental
  885.                     local name = InputBox()
  886.                     if name then
  887.                         if fs.exists(stringParth()..fSlash..name) then
  888.                             dialogBox("ERROR","Name exists",{"ok"})
  889.                         else
  890.                             if pcall(fs.makeDir,stringParth()..fSlash..name) then
  891.                                 NewList()
  892.                             else
  893.                                 dialogBox("ERROR","Access Denied",{"ok"})
  894.                             end
  895.                         end
  896.                     end
  897.                 elseif sChoice == "Open" then
  898.                     table.insert(parth,temp)
  899.                     NewList()
  900.                 elseif sChoice == "Run" then
  901.                     local test,info = osRunSpaces(stringParth()..fSlash..temp)
  902.                     term.setCursorBlink(false)
  903.                     if not test then
  904.                         dialogBox("ERROR",tostring(info),{"ok"})
  905.                     end
  906.                 elseif sChoice == "Run CMD" then
  907.                     local cmd = fixArgs(InputBox("Commands"))
  908.                     if cmd ~= nil then
  909.                         local test,info = osRunSpaces(stringParth()..fSlash..temp,unpack(cmd))
  910.                         term.setCursorBlink(false)
  911.                         if not test then
  912.                             dialogBox("ERROR",tostring(info),{"ok"})
  913.                         end
  914.                     end
  915.                 elseif sChoice == "Edit" then
  916.                     if sType == "File" then
  917.                         osRunSpaces("/rom/programs/edit",stringParth()..fSlash..temp)
  918.                     else
  919.                         dialogBox("ERROR","Can't edit a Folder",{"ok"})
  920.                     end
  921.                 elseif sChoice == "Delete" then
  922.                     if dialogBox("Confirm","Delete "..sType.." "..temp,{"yes","no"}) == 1 then
  923.                         if fs.isReadOnly(stringParth()..fSlash..temp) then
  924.                             dialogBox("ERROR",sType.." Is read Only",{"ok"})
  925.                         else
  926.                             fs.delete(stringParth()..fSlash..temp)
  927.                             NewList()
  928.                         end
  929.                     end
  930.                 elseif sChoice == "Paint" then
  931.                     if sType == "File" then
  932.                         osRunSpaces("/rom/programs/color/paint",stringParth()..fSlash..temp)
  933.                     else
  934.                         dialogBox("ERROR","Can't edit a Folder",{"ok"})
  935.                     end
  936.                 elseif sChoice == "Eject" then
  937.                     if dialogBox("Confirm","Eject disk "..disks[temp].." "..fSlash..temp,{"yes","no"}) == 1 then
  938.                         disk.eject(disks[temp])
  939.                         NewList()
  940.                     end
  941.                 elseif sChoice == "Copy" then
  942.                     clipboard = {stringParth(), temp}
  943.                     cut = false
  944.                 elseif sChoice == "Cut" then
  945.                     clipboard = {stringParth(), temp}
  946.                     cut = true
  947.                 elseif sChoice == "Paste" then
  948.                     if cut then
  949.                         local s, m = pcall(function()
  950.                             fs.move(clipboard, stringParth().."/"..temp)
  951.                             cut = false
  952.                             clipboard = nil
  953.                         end)
  954.                         if not s then
  955.                             dialogBox("Error", (m or "Couldn't move"), {"ok"}, 4, 30)
  956.                         end
  957.                         if bugTest then
  958.                             local x, y = term.getCursorPos()
  959.                             term.setCursorPos(1, ({term.getSize()})[2])
  960.                             write("from "..clipboard[1].."/"..clipboard[2].." to "..stringParth().."/"..clipboard[2])
  961.                         end
  962.                     else
  963.                         local s, m = pcall(function()
  964.                             fs.copy(clipboard[1].."/"..clipboard[2], stringParth().."/"..clipboard[2])
  965.                         end)
  966.                         if not s then
  967.                             dialogBox("Error", (m or "Couldn't copy"), {"ok"}, 4, 30)
  968.                         end
  969.                         if bugTest then
  970.                             local x, y = term.getCursorPos()
  971.                             term.setCursorPos(1, ({term.getSize()})[2])
  972.                             write("from "..clipboard[1].."/"..clipboard[2].." to "..stringParth().."/"..clipboard[2])
  973.                         end
  974.                     end
  975.                     NewList()
  976.                 elseif sChoice == "Rename" then -- new parts of the above thanks kilo
  977.                     local sName = InputBox("New Name")
  978.                     if type(sName) == "string" and sName ~= "" then
  979.                         local s, m = pcall(function()
  980.                             fs.move(stringParth()..fSlash..temp,stringParth()..fSlash..sName)
  981.                         end)
  982.                         if not s then
  983.                             dialogBox("Error", (m or "Rename failed"), {"ok"})
  984.                         end
  985.                     end
  986.                     NewList()
  987.                 end
  988.                 flag = true
  989.             end
  990.         elseif event[1] == "mouse_scroll" then -- flag this needs new math
  991.             local old = listOff
  992.             listOff = listOff + event[2]
  993.             if listOff < 0 then
  994.                 listOff = 0
  995.             end
  996.             if #folders + #files + 1 - boxH > 0 and listOff > #folders + #files + 1 - boxH then
  997.                 listOff = #folders + #files + 1 - boxH
  998.             elseif listOff > 0 and #folders + #files + 1 - boxH < 0 then
  999.                 listOff = 0
  1000.             end
  1001.             if listOff ~= old then
  1002.                 flag = true
  1003.             end
  1004.        
  1005.         elseif event[1] == "mouse_drag" then -- test
  1006.             if event[3] == boxOffX+boxW+1 and event[4] > boxOffY and event[4] <= boxOffY+boxH then
  1007.                 if #folders + #files > boxH then
  1008.                     if event[4] == boxOffY + 1 then
  1009.                         listOff = 0
  1010.                     elseif event[4] == boxOffY+boxH then
  1011.                         listOff = #folders + #files + 1 - boxH
  1012.                     else
  1013.                         listOff = math.ceil((event[4] - boxOffY - 1 )*(((#folders + #files - boxH+2)/boxH)))
  1014.                     end
  1015.                     flag = true
  1016.                 end
  1017.             end
  1018.            
  1019.         elseif event[1] == "disk" or event[1] == "disk_eject" then
  1020.             NewList()
  1021.         elseif event[1] == "window_resize" then
  1022.             termX,termY = term.getSize()
  1023.             offsetX,offsetY = 1,1
  1024.             hight,width = math.ceil(termY-2),math.ceil(termX-2)
  1025.            
  1026.             boxOffX,boxOffY = offsetX,offsetY + 2
  1027.             boxH,boxW = hight - 2 ,width - 2
  1028.            
  1029.             barX,barY = offsetX + 1,offsetY + 2
  1030.             barH,barW = 1,width - 1
  1031.            
  1032.             tbarX,tbarY = offsetX + 1,offsetY + 1
  1033.             tbarH,tbarW = 1,width - 1
  1034.            
  1035.             exitX,exitY = offsetX + width - 1 ,offsetY + 1
  1036.            
  1037.             flag = true
  1038.         end
  1039.     end
  1040. end
  1041. local function main()
  1042.     if term.isColor() then
  1043.         clear()
  1044.         fileSelect()
  1045.         clear()
  1046.     else
  1047.         error("Not an Advanced Computer (gold) ")
  1048.     end
  1049. end
  1050. local trash = (norun or main())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement