Advertisement
Guest User

[Computer Craft] Mouse file browser ver 0.5 by BigSHinyToys

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