Advertisement
Guest User

[ComputerCraft] Mouse file browser by BigSHinyToys

a guest
Nov 11th, 2012
274
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 21.70 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 ver = "0.4b"
  9.  
  10. local bugTest = false
  11.  
  12. if tArgs[1] and tArgs[1] == "-d" then
  13.     bugTest = true
  14. end
  15.  
  16. local function clear()
  17.     term.setBackgroundColor(colors.black)
  18.     term.setTextColor(colors.white)
  19.     term.clear()
  20.     term.setCursorBlink(false)
  21.     term.setCursorPos(1,1)
  22. end
  23.  
  24. -- modified read made to play nice with coroutines
  25.  
  26.  
  27.  
  28. local function readMOD( _sReplaceChar, _tHistory,_wdth)
  29.     local sLine = ""
  30.     term.setCursorBlink( true )
  31.  
  32.     local nHistoryPos = nil
  33.     local nPos = 0
  34.     if _sReplaceChar then
  35.         _sReplaceChar = string.sub( _sReplaceChar, 1, 1 )
  36.     end
  37.    
  38.     local sx, sy = term.getCursorPos() 
  39.  
  40.     local w, h = term.getSize()
  41.     if _wdth and type(_wdth) == "number" then
  42.         w = sx + _wdth - 1
  43.     end
  44.    
  45.     local function redraw( _sCustomReplaceChar )
  46.         local nScroll = 0
  47.         if sx + nPos >= w then
  48.             nScroll = (sx + nPos) - w
  49.         end
  50.            
  51.         term.setCursorPos( sx + _wdth - 1, sy )
  52.         term.write(" ")
  53.         term.setCursorPos( sx, sy )
  54.         local sReplace = _sCustomReplaceChar or _sReplaceChar
  55.         if sReplace then
  56.             term.write( string.rep(sReplace,_wdth) )
  57.         else
  58.             term.write( string.sub( sLine, nScroll + 1 ,nScroll + _wdth) )
  59.         end
  60.         term.setCursorPos( sx + nPos - nScroll, sy )
  61.     end
  62.    
  63.     while true do
  64.         local sEvent, param = os.pullEvent()
  65.         if sEvent == "char" then
  66.             sLine = string.sub( sLine, 1, nPos ) .. param .. string.sub( sLine, nPos + 1 )
  67.             nPos = nPos + 1
  68.             redraw()
  69.            
  70.         elseif sEvent == "key" then
  71.             if param == keys.enter then
  72.                 -- Enter
  73.                 -- break -- may look at this latter
  74.                
  75.             elseif param == keys.left then
  76.                 -- Left
  77.                 if nPos > 0 then
  78.                     nPos = nPos - 1
  79.                     redraw()
  80.                 end
  81.                
  82.             elseif param == keys.right then
  83.                 -- Right               
  84.                 if nPos < string.len(sLine) then
  85.                     nPos = nPos + 1
  86.                     redraw()
  87.                 end
  88.            
  89.             elseif param == keys.up or param == keys.down then
  90.                 -- Up or down
  91.                 if _tHistory then
  92.                     redraw(" ");
  93.                     if param == keys.up then
  94.                         -- Up
  95.                         if nHistoryPos == nil then
  96.                             if #_tHistory > 0 then
  97.                                 nHistoryPos = #_tHistory
  98.                             end
  99.                         elseif nHistoryPos > 1 then
  100.                             nHistoryPos = nHistoryPos - 1
  101.                         end
  102.                     else
  103.                         -- Down
  104.                         if nHistoryPos == #_tHistory then
  105.                             nHistoryPos = nil
  106.                         elseif nHistoryPos ~= nil then
  107.                             nHistoryPos = nHistoryPos + 1
  108.                         end                    
  109.                     end
  110.                    
  111.                     if nHistoryPos then
  112.                         sLine = _tHistory[nHistoryPos]
  113.                         nPos = string.len( sLine )
  114.                     else
  115.                         sLine = ""
  116.                         nPos = 0
  117.                     end
  118.                     redraw()
  119.                 end
  120.             elseif param == keys.backspace then
  121.                 -- Backspace
  122.                 if nPos > 0 then
  123.                     redraw(" ");
  124.                     sLine = string.sub( sLine, 1, nPos - 1 ) .. string.sub( sLine, nPos + 1 )
  125.                     nPos = nPos - 1                
  126.                     redraw()
  127.                 end
  128.             elseif param == keys.home then
  129.                 -- Home
  130.                 nPos = 0
  131.                 redraw()       
  132.             elseif param == keys.delete then
  133.                 if nPos < string.len(sLine) then
  134.                     redraw(" ");
  135.                     sLine = string.sub( sLine, 1, nPos ) .. string.sub( sLine, nPos + 2 )              
  136.                     redraw()
  137.                 end
  138.             elseif param == keys["end"] then
  139.                 -- End
  140.                 nPos = string.len(sLine)
  141.                 redraw()
  142.             end
  143.         elseif sEvent == "redraw" then
  144.             redraw()
  145.         elseif sEvent == "return" then
  146.             term.setCursorBlink( false )
  147.             return sLine
  148.         end
  149.     end
  150.    
  151.     term.setCursorBlink( false )
  152.     -- term.setCursorPos( w + 1, sy )
  153.     -- print()
  154.    
  155.     return sLine
  156. end
  157.  
  158. -- end modified read
  159.  
  160. local function printC(posX,posY,textCol,backCol,text)
  161.     term.setCursorPos(posX,posY)
  162.     term.setTextColor(textCol)
  163.     term.setBackgroundColor(backCol)
  164.     term.write(text)
  165. end
  166.  
  167. local function InputBox()
  168.     local boxW,boxH = 26,3
  169.     local termX,termY = term.getSize()
  170.     local ofsX,ofsY = math.ceil((termX/2) - (boxW/2)) , math.ceil((termY/2) - (boxH/2)) -- offset from top left
  171.     local options = {"ok","cancel"}
  172.    
  173.     local selected = 1
  174.     local space = 0
  175.     local range = {}
  176.     for i = 1,#options do
  177.         range[i] = {s = space,f = space + string.len(options[i])}
  178.         space = space + string.len(options[i])+3
  179.     end
  180.     local ofC = (boxW/2) - (space/2)
  181.    
  182.     local function drawBox()
  183.         printC(ofsX,ofsY,colors.black,colors.blue,string.rep(" ",boxW))
  184.         printC(ofsX+1,ofsY,colors.black,colors.blue,"User Input")
  185.         printC(ofsX,ofsY+1,colors.black,colors.white,string.rep(" ",boxW))
  186.         printC(ofsX,ofsY+2,colors.black,colors.white,string.rep(" ",boxW))
  187.         printC(ofsX,ofsY+3,colors.black,colors.white,string.rep(" ",boxW))
  188.        
  189.         for i = 1,#options do
  190.             if i == selected then
  191.                 term.setBackgroundColor(colors.lightGray)
  192.                 term.setCursorPos(range[i].s + ofC + ofsX - 1,ofsY + 3)
  193.                 term.write("["..options[i].."]")
  194.                 term.setBackgroundColor(colors.white)
  195.                 term.write(" ")
  196.             else
  197.                 term.setCursorPos(range[i].s + ofC + ofsX - 1,ofsY + 3)
  198.                 term.write(" "..options[i].." ")
  199.             end
  200.         end
  201.        
  202.         printC(ofsX+2,ofsY+2,colors.black,colors.lightGray,string.rep(" ",boxW-4))
  203.     end
  204.     drawBox()
  205.     term.setCursorPos(ofsX+2,ofsY+2)
  206.     local co = coroutine.create(function() return readMOD(nil,nil,boxW - 4) end)
  207.     while true do
  208.         local event = {os.pullEvent()}
  209.         if event[1] == "key" or event[1] == "char" then
  210.             if event[2] == 28 then
  211.                 local test,data = coroutine.resume(co,"return")
  212.                 return data
  213.             else
  214.                 coroutine.resume(co,unpack(event))
  215.             end
  216.         elseif event[1] == "mouse_click" then
  217.             if event[4] == ofsY + 3 then
  218.                 for i = 1,#options do
  219.                     if event[3] >= range[i].s + ofC + ofsX - 1 and event[3] <= range[i].f + ofC + ofsX then
  220.                         if options[i] == "ok" then
  221.                             local test,data = coroutine.resume(co,"return")
  222.                             return data
  223.                         elseif options[i] == "cancel" then
  224.                             return
  225.                         end
  226.                     end
  227.                 end
  228.             end
  229.         end
  230.     end
  231. end
  232.  
  233. local function dialogBox(title,message,options)
  234.     term.setCursorBlink(false)
  235.     local selected = 1
  236.     local boxW,boxH = 26,3
  237.     local termX,termY = term.getSize()
  238.     local ofsX,ofsY = math.ceil((termX/2) - (boxW/2)) , math.ceil((termY/2) - (boxH/2)) -- offset from top left
  239.    
  240.     local space = 0
  241.     local range = {}
  242.     for i = 1,#options do
  243.         range[i] = {s = space,f = space + string.len(options[i])}
  244.         space = space + string.len(options[i])+3
  245.     end
  246.     local ofC = math.ceil((boxW/2)) - math.ceil((space/2))
  247.    
  248.     local function drawBox()
  249.         if term.isColor() then
  250.             term.setTextColor(colors.black)
  251.             term.setCursorPos(ofsX,ofsY) -- F*** YOU 3 hours to find out i forgot to round numbers before sending them to this Fing function
  252.             term.setBackgroundColor(colors.blue)
  253.             term.write(" "..title..string.rep(" ",boxW-#title-5).."_[]")
  254.             term.setBackgroundColor(colors.red)
  255.             term.setTextColor(colors.white)
  256.             term.write("X")
  257.             term.setCursorPos(ofsX,ofsY+1)
  258.             term.setBackgroundColor(colors.white)
  259.             term.setTextColor(colors.black)
  260.             term.write(string.sub(" "..message..string.rep(" ",boxW),1,boxW)) -- edited
  261.             term.setCursorPos(ofsX,ofsY+2)
  262.             term.write(string.rep(" ",boxW))
  263.             term.setCursorPos(ofsX,ofsY+3)
  264.             term.write(string.rep(" ",boxW))
  265.            
  266.             for i = 1,#options do
  267.                 if i == selected then
  268.                     term.setBackgroundColor(colors.lightGray)
  269.                     term.setCursorPos(range[i].s + ofC + ofsX - 1,ofsY + 3)
  270.                     term.write("["..options[i].."]")
  271.                     term.setBackgroundColor(colors.white)
  272.                     term.write(" ")
  273.                 else
  274.                     term.setCursorPos(range[i].s + ofC + ofsX - 1,ofsY + 3)
  275.                     term.write(" "..options[i].." ")
  276.                 end
  277.             end
  278.             term.setCursorPos(ofsX + ofC + space,ofsY + 3)
  279.             term.write(string.rep(" ",boxW - (ofC + space)))
  280.             term.setBackgroundColor(colors.black)
  281.             term.setTextColor(colors.white)        
  282.         end
  283.     end
  284.     while true do
  285.         drawBox()
  286.         event = {os.pullEvent()}
  287.         if event[1] == "key" then
  288.             if event[2] == 203 then -- left
  289.                 selected = selected - 1
  290.                 if selected < 1 then
  291.                     selected = #options
  292.                 end
  293.             elseif event[2] == 205 then -- right
  294.                 selected = selected + 1
  295.                 if selected > #options then
  296.                     selected = 1
  297.                 end
  298.             elseif event[2] == 28 then -- enter
  299.                 return selected , options[selected]
  300.             end
  301.         elseif event[1] == "mouse_click" then
  302.             term.setCursorPos(1,1)
  303.             term.clearLine()
  304.            
  305.             if bugTest then term.write("M "..event[2].." X "..event[3].." Y "..event[4]) end
  306.            
  307.             if event[2] == 1 then
  308.                 if event[4] == ofsY + 3 then
  309.                     for i = 1,#options do
  310.                         if event[3] >= range[i].s + ofC + ofsX - 1 and event[3] <= range[i].f + ofC + ofsX then
  311.                             return i , options[i]
  312.                         end
  313.                     end
  314.                 end
  315.             end
  316.         end
  317.     end
  318. end
  319.  
  320. local function rClickMenu(title,tList,posX,posY)
  321.  
  322.     term.setCursorBlink(false)
  323.     local BoxTitle = title
  324.     local choices = tList
  325.     local termX,termY = term.getSize()
  326.     local offX,offY
  327.    
  328.     local width = #BoxTitle + 2
  329.     local hight = #choices + 1
  330.    
  331.     for i = 1,#choices do
  332.         if width < #choices[i] + 2 then
  333.             width = #choices[i] + 2
  334.         end
  335.     end
  336.    
  337.     offX,offY = math.ceil((termX/2) - (width/2)),math.ceil((termY/2) - (hight/2))
  338.    
  339.     if posX and posY then -- offX,offY = posX,posY
  340.         if posX >= termX - width - 1 then
  341.             offX = termX - width - 1
  342.         else
  343.             offX = posX
  344.         end
  345.         if posY >= termY - hight then
  346.             offY = termY - hight
  347.         else
  348.             offY = posY
  349.         end
  350.     end
  351.    
  352.     local function reDrawer()
  353.         printC(offX,offY,colors.black,colors.blue," "..BoxTitle..string.rep(" ",width - #BoxTitle - 1))
  354.         for i = 1,#choices do
  355.             printC(offX,offY + i,colors.black,colors.white," "..choices[i]..string.rep(" ",width - #choices[i] - 1))
  356.         end
  357.     end
  358.    
  359.     while true do
  360.         reDrawer()
  361.         local event = {os.pullEvent()}
  362.         if event[1] == "mouse_click" then
  363.             if event[2] == 1 then -- event[3] = x event[4] = y
  364.                 if event[4] > offY and event[4] < hight + offY and event[3] >= offX and event[3] < width + offX then
  365.                     return choices[event[4] - offY]
  366.                 else
  367.                     return
  368.                 end
  369.             elseif event[2] == 2 then
  370.                 return
  371.             end
  372.         end
  373.     end
  374.    
  375. end
  376.  
  377. local function fileSelect(mode) -- save_file open_file browse < not yet implemented
  378.    
  379.     local title = "File Browser "..ver
  380.     local bRun = true
  381.     local flag = true
  382.    
  383.     local termX,termY = term.getSize()
  384.     local offsetX,offsetY = 1,1
  385.     local hight,width = math.ceil(termY-2),math.ceil(termX-2)
  386.     local oldHight,oldWidth
  387.    
  388.     local boxOffX,boxOffY = offsetX,offsetY + 2
  389.     local boxH,boxW = hight - 2 ,width - 2
  390.    
  391.     local barX,barY = offsetX + 1,offsetY + 2
  392.     local barH,barW = 1,width - 1
  393.    
  394.     local tbarX,tbarY = offsetX + 1,offsetY + 1
  395.     local tbarH,tbarW = 1,width - 1
  396.    
  397.     local exitX,exitY = offsetX + width - 1 ,offsetY + 1
  398.    
  399.     local parth = {}
  400.     local list
  401.    
  402.     local fSlash = [[\]]
  403.     local listOff = 0
  404.    
  405.     local function stringParth()
  406.         local temp = fSlash
  407.         for i = 1,#parth do
  408.             if i == #parth then
  409.                 temp = temp..parth[i]
  410.             else
  411.                 temp = temp..parth[i]..fSlash
  412.             end
  413.         end
  414.         return temp
  415.     end
  416.    
  417.     local sParth = stringParth()
  418.     local files = {}
  419.     local folders = {}
  420.     local disks = {}
  421.  
  422.     local function NewList()
  423.         flag = true
  424.         listOff = 0
  425.         sParth = stringParth()
  426.         files = {}
  427.         folders = {}
  428.         disks = {}
  429.         test,list = pcall(fs.list,sParth)
  430.         for i = 1,#list do
  431.             if fs.isDir(sParth..fSlash..list[i]) then
  432.                 table.insert(folders,list[i])
  433.             else
  434.                 table.insert(files,list[i])
  435.             end
  436.         end
  437.         if #parth == 0 then
  438.             for i,v in pairs(rs.getSides()) do
  439.                 if disk.isPresent(v) and disk.hasData(v) then
  440.                     disks[disk.getMountPath(v)] = v
  441.                 end
  442.             end
  443.         end
  444.         table.sort(disks)
  445.         table.sort(folders)
  446.         table.sort(files)
  447.     end
  448.    
  449.     NewList()
  450.    
  451.     local function size(test)
  452.         if #test > boxW - 3 then
  453.             return string.sub(test,1,boxW-3)
  454.         end
  455.         return test
  456.     end
  457.    
  458.     --[[
  459.    
  460.     local tRead = coroutine.create(function()
  461.         term.setCursorPos(boxOffX + 2,boxOffY + boxH + 1)
  462.         readMOD(nil,nil,boxOffX + boxW)
  463.     end)
  464.    
  465.     local function resume(...)
  466.         term.setTextColor(colors.black)
  467.         term.setBackgroundColor(colors.white)
  468.         term.setCursorPos(boxOffX + 1,boxOffY + boxH + 1)
  469.         term.write(string.rep(" ",boxW+1))
  470.         coroutine.resume(tRead,...)
  471.         term.setTextColor(colors.white)
  472.         term.setBackgroundColor(colors.black)
  473.     end
  474.    
  475.     ]]--
  476.    
  477.     while bRun do
  478.         if flag then
  479.             flag = false
  480.             -- clear
  481.             if oldHight ~= hight and oldWidth ~= width then
  482.                 term.setBackgroundColor(colors.black)
  483.                 term.clear()
  484.                 oldHight,oldWidth = hight,width
  485.             end
  486.             -- draw top title bar
  487.             term.setCursorPos(tbarX,tbarY)
  488.             term.setTextColor(colors.black)
  489.             term.setBackgroundColor(colors.blue)
  490.             local b = tbarW - #title -2
  491.             if b < 0 then
  492.                 b = 0
  493.             end
  494.             term.write(string.sub(" "..title,1,tbarW)..string.rep(" ",b))
  495.             term.setTextColor(colors.white)
  496.             term.setBackgroundColor(colors.red)
  497.             term.write("X")
  498.            
  499.             -- draw location bar
  500.            
  501.             term.setCursorPos(barX,barY)
  502.             term.setTextColor(colors.black)
  503.             term.setBackgroundColor(colors.lightGray)
  504.             local a = barW - #sParth - 1
  505.             if a < 0 then
  506.                 a = 0
  507.             end
  508.             term.write(string.sub(" "..sParth,1,barW)..string.rep(" ",a))
  509.            
  510.             -- draw scroll bar
  511.            
  512.             if #folders + #files > boxH then
  513.                 term.setBackgroundColor(colors.lightGray)
  514.                 for i = 1,boxH do
  515.                     term.setCursorPos(boxOffX+boxW+1,i + boxOffY)
  516.                     local scroll = math.floor( boxH* (listOff/(#folders + #files-boxH+2)) )+1
  517.                     if i == scroll then
  518.                         term.setBackgroundColor(colors.gray)
  519.                         term.write(" ")
  520.                         term.setBackgroundColor(colors.lightGray)
  521.                     else
  522.                         term.write(" ")
  523.                     end
  524.                 end
  525.             else
  526.                 term.setBackgroundColor(colors.gray)
  527.                 for i = 1,boxH do
  528.                     term.setCursorPos(boxOffX+boxW+1,i + boxOffY)
  529.                     term.write(" ")
  530.                 end
  531.             end
  532.            
  533.             -- draw main section
  534.            
  535.             term.setTextColor(colors.black)
  536.             term.setBackgroundColor(colors.cyan)
  537.             for i = 1,boxH do
  538.                 term.setCursorPos(1+boxOffX,i+boxOffY)
  539.                 if folders[i+listOff] then
  540.                     if disks[folders[i+listOff]] then
  541.                         term.setTextColor(colors.orange)
  542.                         term.setBackgroundColor(colors.blue)
  543.                         term.write("D!")
  544.                     else
  545.                         term.setTextColor(colors.blue)
  546.                         term.setBackgroundColor(colors.lightBlue)
  547.                         term.write("[]")
  548.                     end
  549.                     term.setTextColor(colors.black)
  550.                     term.setBackgroundColor(colors.cyan)
  551.                     local repTimes = boxW - #folders[i+listOff] - 3
  552.                     if repTimes < 0 then
  553.                         repTimes = 0
  554.                     end
  555.                     term.write(" "..size(folders[i+listOff])..string.rep(" ",repTimes))
  556.                 elseif files[i-#folders+listOff] then
  557.                     local repTimes = boxW - #files[i-#folders+listOff] - 3
  558.                     if repTimes < 0 then
  559.                         repTimes = 0
  560.                     end
  561.                     term.write("   "..size(files[i-#folders+listOff])..string.rep(" ",repTimes))
  562.                 else
  563.                     term.write(string.rep(" ",boxW))
  564.                 end
  565.             end
  566.             term.setTextColor(colors.white)
  567.             term.setBackgroundColor(colors.black)
  568.            
  569.             if bugTest then
  570.                 term.setCursorPos(1,1)
  571.                 term.write(listOff.." "..boxOffY.." "..boxH)
  572.             end
  573.            
  574.             -- resume("redraw")
  575.         end
  576.         -- react to events
  577.        
  578.         local event = {os.pullEvent()}
  579.        
  580.         if event[1] == "mouse_click" then
  581.             if event[2] == 1 then -- left mouse
  582.                 local temp = nil
  583.                 if event[4] > boxOffY and event[4] <= boxH + boxOffY then
  584.                     temp = folders[event[4]+listOff-boxOffY] or files[event[4]-#folders+listOff-boxOffY]
  585.                 end
  586.                 if temp and event[3] > boxOffX and event[3] <= #temp + 3 + boxOffX and event[3] < boxOffX + boxW then
  587.                     if fs.isDir(stringParth()..fSlash..temp) then
  588.                         table.insert(parth,temp)
  589.                         NewList()
  590.                     else
  591.                         if fs.exists(stringParth()..fSlash..temp) then
  592.                             if dialogBox("Run file ?",temp,{"yes","no"}) == 1 then
  593.                                 local test,info = shell.run(stringParth()..fSlash..temp)
  594.                                 term.setCursorBlink(false)
  595.                                 if not test then
  596.                                     dialogBox("ERROR",tostring(info),{"ok"})
  597.                                 end
  598.                             end
  599.                             flag = true
  600.                         end
  601.                     end
  602.                 elseif event[3] == boxOffX+boxW+1 and event[4] > boxOffY and event[4] <= boxOffY+boxH then
  603.                     if #folders + #files > boxH then
  604.                         if event[4] == boxOffY + 1 then
  605.                             listOff = 0
  606.                         elseif event[4] == boxOffY+boxH then
  607.                             listOff = #folders + #files + 1 - boxH
  608.                         else
  609.                             listOff = math.ceil((event[4] - boxOffY - 1 )*(((#folders + #files - boxH+2)/boxH)))
  610.                         end
  611.                         flag = true
  612.                     end
  613.                 elseif event[3] == exitX and event[4] == exitY then
  614.                     if dialogBox("Confirm","Exit application",{"yes","no"}) == 1 then
  615.                         bRun = false
  616.                     end
  617.                     flag = true
  618.                 end
  619.             elseif event[2] == 2 then -- right mouse
  620.                 local temp = nil
  621.                 if event[4] > boxOffY and event[4] <= boxH + boxOffY then
  622.                     temp = folders[event[4]+listOff-boxOffY] or files[event[4]-#folders+listOff-boxOffY]
  623.                 end
  624.                 local sChoice = nil
  625.                 local sType = nil
  626.                 if temp and event[3] > boxOffX and event[3] <= #temp + 3 + boxOffX and event[3] < boxOffX + boxW then
  627.                     sType = "File"
  628.                    
  629.                     if fs.isDir(stringParth()..fSlash..temp) then
  630.                         sType = "Folder"
  631.                     end
  632.                    
  633.                    
  634.                    
  635.                     if disks[temp] then
  636.                         sChoice = rClickMenu("Options",{"Open","Eject"},event[3]+1,event[4]+1)
  637.                     else
  638.                         if sType == "Folder" then
  639.                             sChoice = rClickMenu("Options",{"Open","Delete"},event[3]+1,event[4]+1)
  640.                         else
  641.                             if windowDimensions then
  642.                                 sChoice = rClickMenu("Options",{"Run","Run CMD","Run win","Edit","Paint","Delete"},event[3]+1,event[4]+1)
  643.                             else
  644.                                 sChoice = rClickMenu("Options",{"Run","Run CMD","Edit","Paint","Delete"},event[3]+1,event[4]+1)
  645.                             end
  646.                         end
  647.                     end
  648.                    
  649.                 else
  650.                     if event[3] > boxOffX and event[3] <= 5 + boxOffX and event[3] < boxOffX + boxW then
  651.                         if event[4] > offsetY and event[4] < hight + offsetY then
  652.                             sChoice = rClickMenu("Options",{"New File","New Folder"},event[3]+1,event[4]+1)
  653.                         end
  654.                     else
  655.                         table.remove(parth,#parth)
  656.                         NewList()
  657.                     end
  658.                 end
  659.                
  660.                 if sChoice == "Run win" and windowDimensions then -- choice execution
  661.                     shell.run("start",stringParth()..fSlash..temp)
  662.                     flag = true
  663.                 elseif sChoice == "New File" then -- experemntal
  664.                     local name = InputBox()
  665.                     if name then
  666.                         if fs.exists(stringParth()..fSlash..name) then
  667.                             dialogBox("ERROR","Name exists",{"ok"})
  668.                         else
  669.                             local file = fs.open(stringParth()..fSlash..name,"w")
  670.                             if file then
  671.                                 file.write("")
  672.                                 file.close()
  673.                                 NewList()
  674.                             else
  675.                                 dialogBox("ERROR","File not created",{"ok"})
  676.                             end
  677.                         end
  678.                     end
  679.                 elseif sChoice == "New Folder" then -- experemental
  680.                     local name = InputBox()
  681.                     if name then
  682.                         if fs.exists(stringParth()..fSlash..name) then
  683.                             dialogBox("ERROR","Name exists",{"ok"})
  684.                         else
  685.                             if pcall(fs.makeDir,stringParth()..fSlash..name) then
  686.                                 NewList()
  687.                             else
  688.                                 dialogBox("ERROR","Access Denied",{"ok"})
  689.                             end
  690.                         end
  691.                     end
  692.                 elseif sChoice == "Open" then
  693.                     table.insert(parth,temp)
  694.                     NewList()
  695.                 elseif sChoice == "Run" then
  696.                     clear()
  697.                     local test,info = shell.run(stringParth()..fSlash..temp)
  698.                     term.setCursorBlink(false)
  699.                     if not test then
  700.                         dialogBox("ERROR",tostring(info),{"ok"})
  701.                     end
  702.                 elseif sChoice == "Run CMD" then
  703.                     local cmd = InputBox()
  704.                     if cmd ~= nil then
  705.                         local test,info = shell.run(stringParth()..fSlash..temp,cmd)
  706.                         term.setCursorBlink(false)
  707.                         if not test then
  708.                             dialogBox("ERROR",tostring(info),{"ok"})
  709.                         end
  710.                     end
  711.                 elseif sChoice == "Edit" then
  712.                     if sType == "File" then
  713.                         shell.run("edit",stringParth()..fSlash..temp)
  714.                     else
  715.                         dialogBox("ERROR","Can't edit a Folder",{"ok"})
  716.                     end
  717.                 elseif sChoice == "Delete" then
  718.                     if dialogBox("Confirm","Delete "..sType.." "..temp,{"yes","no"}) == 1 then
  719.                         if fs.isReadOnly(stringParth()..fSlash..temp) then
  720.                             dialogBox("ERROR",sType.." Is read Only",{"ok"})
  721.                         else
  722.                             fs.delete(stringParth()..fSlash..temp)
  723.                             NewList()
  724.                         end
  725.                     end
  726.                 elseif sChoice == "Paint" then
  727.                     if sType == "File" then
  728.                         shell.run("paint",stringParth()..fSlash..temp)
  729.                     else
  730.                         dialogBox("ERROR","Can't edit a Folder",{"ok"})
  731.                     end
  732.                 elseif sChoice == "Eject" then
  733.                     if dialogBox("Confirm","Eject disk "..disks[temp].." "..fSlash..temp,{"yes","no"}) == 1 then
  734.                         disk.eject(disks[temp])
  735.                         NewList()
  736.                     end
  737.                 end
  738.                 flag = true
  739.             end
  740.         elseif event[1] == "mouse_scroll" then -- flag
  741.             local old = listOff
  742.             listOff = listOff + event[2]
  743.             if listOff < 0 then
  744.                 listOff = 0
  745.             end
  746.             if #folders + #files + 1 - boxH > 0 and listOff > #folders + #files + 1 - boxH then
  747.                 listOff = #folders + #files + 1 - boxH
  748.             elseif listOff > 0 and #folders + #files + 1 - boxH < 0 then
  749.                 listOff = 0
  750.             end
  751.             if listOff ~= old then
  752.                 flag = true
  753.             end
  754.        
  755.         elseif event[1] == "mouse_drag" then -- test
  756.             if event[3] == boxOffX+boxW+1 and event[4] > boxOffY and event[4] <= boxOffY+boxH then
  757.                 if #folders + #files > boxH then
  758.                     if event[4] == boxOffY + 1 then
  759.                         listOff = 0
  760.                     elseif event[4] == boxOffY+boxH then
  761.                         listOff = #folders + #files + 1 - boxH
  762.                     else
  763.                         listOff = math.ceil((event[4] - boxOffY - 1 )*(((#folders + #files - boxH+2)/boxH)))
  764.                     end
  765.                     flag = true
  766.                 end
  767.             end
  768.         elseif event[1] == "key" then
  769.             -- resume(unpack(event))
  770.             -- flag = true
  771.            
  772.         elseif event[1] == "char" then
  773.             -- resume(unpack(event))
  774.             -- flag = true
  775.            
  776.         elseif event[1] == "disk" or event[1] == "disk_eject" then
  777.             NewList()
  778.         elseif event[1] == "window_resize" then
  779.             termX,termY = term.getSize()
  780.             offsetX,offsetY = 1,1
  781.             hight,width = math.ceil(termY-2),math.ceil(termX-2)
  782.            
  783.             boxOffX,boxOffY = offsetX,offsetY + 2
  784.             boxH,boxW = hight - 2 ,width - 2
  785.            
  786.             barX,barY = offsetX + 1,offsetY + 2
  787.             barH,barW = 1,width - 1
  788.            
  789.             tbarX,tbarY = offsetX + 1,offsetY + 1
  790.             tbarH,tbarW = 1,width - 1
  791.            
  792.             exitX,exitY = offsetX + width - 1 ,offsetY + 1
  793.            
  794.             flag = true
  795.         end
  796.     end
  797. end
  798.  
  799. clear()
  800. fileSelect()
  801. clear()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement