Advertisement
BigSHinyToys

Mouse File Browser Ver 0.5c

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