Advertisement
Imgoodisher

CC File Browser

Jan 6th, 2013
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 22.82 KB | None | 0 0
  1. local w, h = term.getSize()
  2. local pasteID = "fr6PJ4rF"
  3. local dir = "/.browse"
  4. local configpath = dir.."/config"
  5. local filelocation = "/"..shell.getRunningProgram()
  6.  
  7. ------------
  8. -- Config --
  9. ------------
  10.  
  11. local config = {
  12.     hiddenfiles = false,
  13.     sleepafterprograms = true,
  14.     showfiletypes = false,
  15.     autoupdate = (http~=nil),
  16. }
  17. local function loadConfig()
  18.     local file = io.open(configpath, "r")
  19.     config = textutils.unserialize(file:read("*a"))
  20.     file:close()
  21. end
  22. local function saveConfig()
  23.     local file = io.open(configpath, "w")
  24.     file:write(textutils.serialize(config))
  25.     file:close()
  26. end
  27.  
  28. if not fs.exists(dir) then fs.makeDir(dir) end
  29.  
  30. if fs.exists(configpath) then
  31.     loadConfig()
  32. else
  33.     saveConfig()
  34. end
  35.  
  36. function cPrint(str)
  37.     str = tostring(str)
  38.     local x, y = term.getCursorPos()
  39.     local w, h = term.getSize()
  40.     term.setCursorPos(math.floor(w/2-str:len()/2), y)
  41.     print(str)
  42. end
  43.  
  44. -------------------
  45. -- Auto Updating --
  46. -------------------
  47.  
  48. if config.autoupdate then
  49.     term.clear()
  50.     term.setCursorPos(1, h/2-2)
  51.     term.setTextColor(colors.lime)
  52.     cPrint("Checking for updates...")
  53.    
  54.     local chars = {"|", "/", "-", "\\"}
  55.     local i = 1
  56.     http.request("http://www.pastebin.com/raw.php?i="..pasteID)
  57.     local timer = os.startTimer(0.1)
  58.     local timeout = os.startTimer(10)
  59.     while true do
  60.         event = { os.pullEvent() }
  61.         if event[1] == "http_success" then
  62.             local newdata = event[3]:readAll()
  63.             local file = io.open(filelocation, "r")
  64.             local currentdata = file:read("*a")
  65.             file:close()
  66.            
  67.             if newdata ~= currentdata then
  68.                 local file = io.open(filelocation, "w")
  69.                 file:write(newdata)
  70.                 file:close()
  71.                
  72.                 shell.run(filelocation)
  73.                 error("", 0)
  74.             end
  75.            
  76.             break
  77.         elseif event[1] == "timer" and event[2] == timer then
  78.             term.setCursorPos(w/2, h/2)
  79.             print(chars[i])
  80.             i = (i % #chars) + 1
  81.             timer = os.startTimer(0.1)
  82.         elseif (event[1] == "timer" and event[2] == timeout) or event[1] == "http_failed" then
  83.             term.clear()
  84.             term.setCursorPos(1, h/2-2)
  85.             cPrint("Error connecting to pastebin")
  86.             cPrint("Running program as-is")
  87.             break
  88.         end
  89.     end
  90. end
  91.  
  92. local filetypes = {
  93.     lua = "LUA Program",
  94.     nfa = "Animation File",
  95.     nfp = "Image File",
  96.     txt = "Text Document",
  97.     lol = "LOLZ File",
  98. }
  99. local filecolors = {
  100.     dir = colors.lime,
  101.     lua = colors.yellow,
  102.     nfa = colors.green,
  103.     nfp = colors.red,
  104.     txt = colors.gray,
  105.     lol = "RAINBOWS",
  106.     other = colors.white,
  107. }
  108.  
  109. local rainbowcolors = {colors.red, colors.orange, colors.yellow, colors.lime, colors.blue, colors.purple}
  110.  
  111. local defaultprograms = {
  112.     lua = "&p",
  113.     nfa = "npaintpro -a &p",
  114.     nfp = "paint &p",
  115.     txt = "edit &p",
  116.     other = "&p"
  117. }
  118.  
  119. --------------------
  120. -- Error Handling --
  121. --------------------
  122.  
  123. local function showError(err)
  124.     term.clear()
  125.     term.setTextColor(colors.red)
  126.     term.setCursorPos(1, h/2-3)
  127.     cPrint("Error: "..err)
  128.     sleep(0)
  129.     cPrint("Press Any Key to Continue")
  130.     os.pullEvent("key")
  131. end
  132.  
  133. --------------------
  134. -- File Funcitons --
  135. --------------------
  136.  
  137. local var_0 = {200, 200, 208, 208, 203, 205, 203, 205, 48, 30}
  138. local var_2 = false
  139.  
  140. local function filetype(str)
  141.     if fs.isDir(shell.dir().."/"..str) then
  142.         return "Directory"
  143.     end
  144.     local _, _, suffix = string.find(str, "%.?[^%.]+%.(.+)$")
  145.     if not suffix then
  146.         return "File"
  147.     else
  148.         return filetypes[suffix] or "."..suffix.." File"
  149.     end
  150. end
  151.  
  152. local function filecolor(str)
  153.     if str == "Cmd:" or str == "lua:" then
  154.         term.setTextColor(colors.blue)
  155.         return false
  156.     end
  157.     term.setTextColor(colors.blue)
  158.     if fs.isDir(shell.dir().."/"..str) then
  159.         term.setTextColor(filecolors.dir)
  160.     else
  161.         local _, _, suffix = string.find(str, "%.(.+)$")
  162.         if not suffix then
  163.             if filecolors.other == "RAINBOWS" then
  164.                 return true
  165.             end
  166.             term.setTextColor(filecolors.other)
  167.         else
  168.             if filecolors[suffix] then
  169.                 if filecolors[suffix] == "RAINBOWS" then
  170.                     return true
  171.                 end
  172.                 term.setTextColor(filecolors[suffix])
  173.             else
  174.                 if filecolors.other == "RAINBOWS" then
  175.                     return true
  176.                 end
  177.                 term.setTextColor(filecolors.other)
  178.             end
  179.         end
  180.     end
  181.     return false
  182. end
  183.  
  184. -----------------------
  185. -- Running / Reading --
  186. -----------------------
  187.  
  188. local function readwid( wid, _sReplaceChar, _tHistory )
  189.     term.setCursorBlink( true )
  190.  
  191.     local sLine = ""
  192.     local nHistoryPos = nil
  193.     local nPos = 0
  194.     if _sReplaceChar then
  195.         _sReplaceChar = string.sub( _sReplaceChar, 1, 1 )
  196.     end
  197.    
  198.     local w, h = term.getSize()
  199.     local sx, sy = term.getCursorPos() 
  200.     w = wid or w
  201.    
  202.     local function redraw( _sCustomReplaceChar )
  203.         local nScroll = 0
  204.         if sx + nPos >= w then
  205.             nScroll = (sx + nPos) - w
  206.         end
  207.            
  208.         term.setCursorPos( sx, sy )
  209.         local sReplace = _sCustomReplaceChar or _sReplaceChar
  210.         if sReplace then
  211.             term.write( string.rep(sReplace, string.len(sLine) - nScroll) )
  212.         else
  213.             term.write( string.sub( sLine, nScroll + 1 ) )
  214.         end
  215.         term.setCursorPos( sx + nPos - nScroll, sy )
  216.     end
  217.    
  218.     while true do
  219.         local sEvent, param = os.pullEvent()
  220.         if sEvent == "char" then
  221.             sLine = string.sub( sLine, 1, nPos ) .. param .. string.sub( sLine, nPos + 1 )
  222.             nPos = nPos + 1
  223.             redraw()
  224.            
  225.         elseif sEvent == "key" then
  226.             if param == keys.enter then
  227.                 -- Enter
  228.                 break
  229.                
  230.             elseif param == keys.left then
  231.                 -- Left
  232.                 if nPos > 0 then
  233.                     nPos = nPos - 1
  234.                     redraw()
  235.                 end
  236.                
  237.             elseif param == keys.right then
  238.                 -- Right               
  239.                 if nPos < string.len(sLine) then
  240.                     nPos = nPos + 1
  241.                     redraw()
  242.                 end
  243.            
  244.             elseif param == keys.up or param == keys.down then
  245.                 -- Up or down
  246.                 if _tHistory then
  247.                     redraw(" ");
  248.                     if param == keys.up then
  249.                         -- Up
  250.                         if nHistoryPos == nil then
  251.                             if #_tHistory > 0 then
  252.                                 nHistoryPos = #_tHistory
  253.                             end
  254.                         elseif nHistoryPos > 1 then
  255.                             nHistoryPos = nHistoryPos - 1
  256.                         end
  257.                     else
  258.                         -- Down
  259.                         if nHistoryPos == #_tHistory then
  260.                             nHistoryPos = nil
  261.                         elseif nHistoryPos ~= nil then
  262.                             nHistoryPos = nHistoryPos + 1
  263.                         end                    
  264.                     end
  265.                    
  266.                     if nHistoryPos then
  267.                         sLine = _tHistory[nHistoryPos]
  268.                         nPos = string.len( sLine )
  269.                     else
  270.                         sLine = ""
  271.                         nPos = 0
  272.                     end
  273.                     redraw()
  274.                 end
  275.             elseif param == keys.backspace then
  276.                 -- Backspace
  277.                 if nPos > 0 then
  278.                     redraw(" ");
  279.                     sLine = string.sub( sLine, 1, nPos - 1 ) .. string.sub( sLine, nPos + 1 )
  280.                     nPos = nPos - 1                
  281.                     redraw()
  282.                 end
  283.             elseif param == keys.home then
  284.                 -- Home
  285.                 nPos = 0
  286.                 redraw()       
  287.             elseif param == keys.delete then
  288.                 if nPos < string.len(sLine) then
  289.                     redraw(" ");
  290.                     sLine = string.sub( sLine, 1, nPos ) .. string.sub( sLine, nPos + 2 )              
  291.                     redraw()
  292.                 end
  293.             elseif param == keys["end"] then
  294.                 -- End
  295.                 nPos = string.len(sLine)
  296.                 redraw()
  297.             end
  298.         end
  299.     end
  300.    
  301.     term.setCursorBlink( false )
  302.     term.setCursorPos( w + 1, sy )
  303.     print()
  304.    
  305.     return sLine
  306. end
  307.  
  308. -- Function from chroot by Lymia Aluysia
  309. local function getRealPath(s)
  310.     s = s .. "/"
  311.     local index = 1
  312.     local array = {}
  313.     for path in string.gmatch(s,"[^/]*[/]") do
  314.         path = string.gsub(path,"[/]","")
  315.         if path == ".." then
  316.             if index ~= 1 then
  317.                 index = index - 1
  318.                 array[index] = nil
  319.             else
  320.                 return "/" -- This should do the trick...
  321.             end
  322.         elseif path == "" then
  323.             --Ignore empty pathes
  324.         else
  325.             array[index] = path
  326.             index = index + 1
  327.         end
  328.     end
  329.  
  330.     local path = ""
  331.     for k,str in ipairs(array) do
  332.         path = path .. "/" .. str
  333.     end
  334.  
  335.     return path
  336. end
  337.  
  338. local function safeRun(...)
  339.     local args = {...}
  340.     local function _osrun( _tEnv, _sPath, ... ) -- Replace os.run and shell.run to remove stupid printError function
  341.         local tArgs = { ... }
  342.         local fnFile, err = loadfile( _sPath )
  343.         if fnFile then
  344.             local tEnv = _tEnv
  345.             --setmetatable( tEnv, { __index = function(t,k) return _G[k] end } )
  346.             setmetatable( tEnv, { __index = _G } )
  347.             setfenv( fnFile, tEnv )
  348.             local ok, err = pcall( function()
  349.                 fnFile( unpack( tArgs ) )
  350.             end )
  351.             if not ok then
  352.                 if err and err ~= "" then
  353.                     error( err )
  354.                 end
  355.                 return false
  356.             end
  357.             return true
  358.         end
  359.         if err and err ~= "" then
  360.             error( err )
  361.         end
  362.         return false
  363.     end
  364.     local function _run( _sCommand, ... )
  365.         local sPath = shell.resolveProgram( _sCommand )
  366.         if sPath ~= nil then
  367.             --tProgramStack[#tProgramStack + 1] = sPath
  368.             local result = _osrun( {_G = _G, shell = shell}, sPath, ... )
  369.             --tProgramStack[#tProgramStack] = nil
  370.             return result
  371.         else
  372.             error( "No such program" )
  373.             return false
  374.         end
  375.     end
  376.    
  377.     term.clear()
  378.     term.setCursorPos(1, 1)
  379.     term.setTextColor(colors.white)
  380.     local ok, err = pcall(_run, unpack(args))
  381.     if not ok and err then
  382.         term.clear()
  383.         term.setTextColor(colors.red)
  384.         term.setCursorPos(1, h/2-3)
  385.         local _, _, line, err2 = string.find(err, "%]:(%d+):%s*(.+)")
  386.         if line and err2 then
  387.             cPrint("Error on line "..line..":")
  388.             cPrint(err2)
  389.         else
  390.             cPrint("Error:")
  391.             cPrint(err)
  392.         end
  393.         print("")
  394.         sleep(0)
  395.         cPrint("Press Any Key to Continue")
  396.         os.pullEvent("key")
  397.     end
  398.     term.setBackgroundColor(colors.black)
  399.     if config.sleepafterprograms then
  400.         sleep(1)
  401.     end
  402. end
  403.  
  404. local function textBox(prompt, prefix)
  405.     prefix = prefix or ""
  406.     prompt = prompt or ""
  407.     if prefix:len() > 10 then
  408.         prefix = string.sub(prefix, 1, 3)..".."..string.sub(prefix, prefix:len()-6)
  409.     end
  410.     ok, stuff = pcall(function()
  411.         term.clear()
  412.         term.setTextColor(colors.yellow)
  413.         w, h = term.getSize()
  414.         term.setCursorPos(1, (h/2)-1)
  415.         cPrint(prompt)
  416.         term.setCursorPos(5, (h/2))
  417.         write("[ "..prefix)
  418.         term.setCursorPos(w - 4, (h/2))
  419.         write(" ]")
  420.         term.setCursorPos(7+prefix:len(), h/2)
  421.         return readwid(w-7)
  422.     end)
  423.     if not ok then return false else return stuff end
  424. end
  425.  
  426. local function run(path)
  427.     path = getRealPath(path)
  428.     local function parsestr(str)
  429.         local args = {}
  430.         for match in string.gmatch(str, "[^%s]+") do
  431.             table.insert(args, match)
  432.         end
  433.         safeRun(unpack(args))
  434.     end
  435.     local _, _, suffix = string.find(path, "%.(.+)$")
  436.     if string.find(defaultprograms[suffix] or defaultprograms.other, "^&p") then
  437.         file = io.open(path, "r")
  438.         if string.find(file:read("*a"), "{%s*%.%.%.%s*}") then -- If file contains {...}
  439.             local args = textBox("Program Arguments", path.." ")
  440.             if args then
  441.                 parsestr(path.." "..args)
  442.             end
  443.         else
  444.             parsestr(path)
  445.         end
  446.         file:close()
  447.     else
  448.         parsestr(string.gsub(defaultprograms[suffix] or defaultprograms.other, "%&%w", {["&p"] = path}))
  449.     end
  450. end
  451.  
  452. -----------
  453. -- Menus --
  454. -----------
  455.  
  456. local function rightClickMenu(file, contents)
  457.     local w, h = term.getSize()
  458.     local scroll = 0
  459.    
  460.     local function redraw()
  461.         term.clear()
  462.         term.setCursorPos(1, 1)
  463.         term.setTextColor(colors.blue)
  464.         if file then
  465.             write("[ "..file.." ]")
  466.         end
  467.         for i=4, h-1 do
  468.             if contents[scroll+i-3] then
  469.                 --filecolor(contents[scroll+i-1])
  470.                 if contents[scroll+i-3] == "New Directory" or contents[scroll+i-3] == "New File" or contents[scroll+i-3] == "Are you sure?" then
  471.                     term.setTextColor(colors.lime)
  472.                 elseif string.match(contents[scroll+i-3], "true$") then
  473.                     term.setTextColor(colors.lime)
  474.                 elseif string.match(contents[scroll+i-3], "false$") then
  475.                     term.setTextColor(colors.red)
  476.                 else
  477.                     term.setTextColor(colors.yellow)
  478.                 end
  479.                 term.setCursorPos(2, i)
  480.                 write("[ ")
  481.                 term.setCursorPos(w - 3, i)
  482.                 write(" ]")
  483.                 cPrint(contents[scroll+i-3])
  484.             end
  485.         end
  486.     end
  487.    
  488.     while true do
  489.         redraw()
  490.         event = { os.pullEvent() }
  491.         if event[1] == "mouse_click" or event[1] == "monitor_touch" then
  492.             if event[1] == "monitor_touch" then
  493.                 if event[2] == "left" then event[2] = 1 else event[2] = 2 end
  494.             end
  495.             if contents[event[4]+scroll-3] then
  496.                 return contents[event[4]+scroll-3]
  497.             end
  498.         elseif event[1] == "mouse_scroll" then
  499.             if event[2] == 1 and scroll < #contents - (h-1) then
  500.                 scroll = scroll + 1
  501.             elseif event[2] == -1 and scroll > 0 then
  502.                 scroll = scroll - 1
  503.             end
  504.         end
  505.     end
  506. end
  507.  
  508. local function list()
  509.    
  510.     local var_1 = 1
  511.     local contents = {}
  512.     local function refreshList()
  513.         contents = {}
  514.         local _files = fs.list(shell.dir())
  515.         local files, dirs = {}, {}
  516.        
  517.         for i,v in pairs(_files) do
  518.             if fs.isDir(shell.dir().."/"..v) then
  519.                 if string.sub(v, 1, 1) ~= "." or config.hiddenfiles then
  520.                     table.insert(dirs, v)
  521.                 end
  522.             elseif string.sub(v, 1, 1) ~= "." or config.hiddenfiles then
  523.                 table.insert(files, v)
  524.             end
  525.         end
  526.        
  527.         for i,v in pairs(dirs) do
  528.             table.insert(contents, v)
  529.         end
  530.         for i,v in pairs(files) do
  531.             table.insert(contents, v)
  532.         end
  533.        
  534.         if shell.dir() ~= "" then
  535.             table.insert(contents, 1, "/..")
  536.         end
  537.        
  538.         --contents[#contents + 1] = ""
  539.         --contents[#contents + 2] = "Cmd:"
  540.         table.insert(contents, "")
  541.         table.insert(contents, "Cmd:")
  542.         --table.insert(contents, "lua:")
  543.     end
  544.     refreshList()
  545.  
  546.     local w, h = term.getSize()
  547.     local scroll = 0
  548.    
  549.     local function redraw()
  550.         term.clear()
  551.         term.setCursorPos(1, 1)
  552.         term.setTextColor(colors.blue)
  553.         if shell.dir() == "" then
  554.             write("[ / ]")
  555.         else
  556.             local dir = shell.dir()
  557.             if dir:len() > w-20 then
  558.                 dir = string.sub(dir, 1, 5)..".."..string.sub(dir, dir:len()-10)
  559.             end
  560.             write("[ "..dir.." ]")
  561.         end
  562.         term.setCursorPos(w-15, 1)
  563.         write("[ Options ]")
  564.         for i=2, h do
  565.             if contents[scroll+i-1] and contents[scroll+i-1] ~= "" then
  566.                 rainbowmode = filecolor(contents[scroll+i-1])
  567.                 term.setCursorPos(2, i)
  568.                 write("[ ")
  569.                 local pos = 1
  570.                 if rainbowmode then
  571.                     local name
  572.                     if not config.showfiletypes then
  573.                         name = string.match(contents[scroll+i-1], "(.+)%.%w+") or contents[scroll+i-1]
  574.                     else
  575.                         name = contents[scroll+i-1]
  576.                     end
  577.                     for x = 1, name:len() do
  578.                         local char = string.sub(name or "", x, x) or ""
  579.                         --term.setTextColor(rainbowcolors[math.random(#rainbowcolors)])
  580.                         term.setTextColor(rainbowcolors[pos])
  581.                         write(char)
  582.                         pos = (pos % #rainbowcolors) + 1
  583.                     end
  584.                 else
  585.                     local name, filetype = string.match(contents[scroll+i-1], "^(.+)%.(.-)$")
  586.                     if filetypes[filetype] and not config.showfiletypes then
  587.                         write(name)
  588.                     else
  589.                         write(contents[scroll+i-1])
  590.                     end
  591.                 end
  592.                
  593.                 if contents[scroll+i-1] == "Cmd:" or contents[scroll+i-1] == "lua:" then
  594.                     term.setCursorPos(w - 3, i)
  595.                     write(" ]")
  596.                    
  597.                 elseif rainbowmode then
  598.                     term.setCursorPos(w - filetype(contents[scroll+i-1]):len() - 3, i)
  599.                     for x = 1, filetype(contents[scroll+i-1]):len() do
  600.                         local char = string.sub(filetype(contents[scroll+i-1]) or "", x, x) or ""
  601.                         --term.setTextColor(rainbowcolors[math.random(#rainbowcolors)])
  602.                         term.setTextColor(rainbowcolors[pos])
  603.                         write(char)
  604.                         pos = (pos % #rainbowcolors) + 1
  605.                     end
  606.                     term.setTextColor(colors.blue)
  607.                     write(" ]")
  608.                    
  609.                 else
  610.                     term.setCursorPos(w - filetype(contents[scroll+i-1]):len() - 3, i)
  611.                     write(filetype(contents[scroll+i-1]).." ]")
  612.                 end
  613.             end
  614.         end
  615.     end
  616.    
  617.     while true do
  618.         redraw()
  619.         event = { os.pullEvent() }
  620.        
  621.         if event[1] == "key" then
  622.             if event[2] == var_0[var_1] then
  623.                 var_1 = var_1+1
  624.                 if var_1 == #var_0+1 then
  625.                     var_2 = not var_2
  626.                     if var_2 == true then
  627.                    
  628.                         filecolors = {
  629.                             dir = colors.pink,
  630.                             other = "RAINBOWS",
  631.                         }
  632.                        
  633.                     else
  634.                    
  635.                         filecolors = {
  636.                             dir = colors.lime,
  637.                             lua = colors.yellow,
  638.                             nfa = colors.green,
  639.                             nfp = colors.red,
  640.                             txt = colors.gray,
  641.                             lol = "RAINBOWS",
  642.                             other = colors.white,
  643.                         }
  644.                        
  645.                     end
  646.                     var_1 = 1
  647.                 end
  648.             else
  649.                 var_1 = 1
  650.             end
  651.         end
  652.        
  653.         if event[1] == "mouse_click" or event[1] == "monitor_touch" then
  654.             if event[1] == "monitor_touch" then
  655.                 if event[2] == "left" then event[2] = 1 else event[2] = 2 end
  656.             end
  657.             if event[4] == 1 and event[3] > 14 then
  658.                                             --- Config ---
  659.                 local choice
  660.                 while choice ~= "Done" do
  661.                
  662.                     choice = rightClickMenu("", {
  663.                         "Show Hidden Files: "..tostring(config.hiddenfiles),
  664.                         "Sleep After Programs: "..tostring(config.sleepafterprograms),
  665.                         "Show Known File Extentions: "..tostring(config.showfiletypes),
  666.                         "Auto-Update: "..tostring(config.autoupdate),
  667.                         nil,
  668.                         "Exit",
  669.                         nil,
  670.                         "Done",
  671.                     })
  672.                    
  673.                     if choice == "Show Hidden Files: "..tostring(config.hiddenfiles) then
  674.                         config.hiddenfiles = not config.hiddenfiles
  675.                        
  676.                     elseif choice == "Sleep After Programs: "..tostring(config.sleepafterprograms) then
  677.                         config.sleepafterprograms = not config.sleepafterprograms
  678.                        
  679.                     elseif choice == "Show Known File Extentions: "..tostring(config.showfiletypes) then
  680.                         config.showfiletypes = not config.showfiletypes
  681.                        
  682.                     elseif choice == "Auto-Update: "..tostring(config.autoupdate) then
  683.                         config.autoupdate = not config.autoupdate
  684.                        
  685.                     elseif choice == "Exit" then
  686.                         saveConfig()
  687.                         term.clear()
  688.                         term.setCursorPos(1, 1)
  689.                         term.setTextColor(colors.yellow)
  690.                         print(os.version())
  691.                         shell.setDir("")
  692.                         error("", 0)
  693.                     end
  694.                 end
  695.                
  696.                 saveConfig()
  697.                 refreshList()
  698.                 sleep(0.1)
  699.                
  700.             elseif contents[event[4]+scroll-1] then
  701.            
  702.                 if contents[event[4]+scroll-1] == "Cmd:" then
  703.                     pcall(function() -- pcall for terminating
  704.                         term.setCursorPos(9, event[4])
  705.                         term.setTextColor(colors.yellow)
  706.                         local str = readwid(w-3)
  707.                         local tbl = {}
  708.                         for match in string.gmatch(str, "[^%s]+") do
  709.                             table.insert(tbl, match)
  710.                         end
  711.                         if tbl[1] then
  712.                             safeRun(unpack(tbl))
  713.                         end
  714.                     end)
  715.                     term.setCursorBlink(false)
  716.                     refreshList()
  717.                    
  718.                 elseif contents[event[4]+scroll-1] == "lua:" then
  719.                     pcall(function() -- pcall for terminating
  720.                         term.setCursorPos(9, event[4])
  721.                         term.setTextColor(colors.yellow)
  722.                         local str = readwid(w-3)
  723.                         local result
  724.                        
  725.                         local func = loadstring(str, "lua")
  726.                         if not func then
  727.                             local func2 = loadstring("return "..str, "lua")
  728.                             if not func2 then
  729.                                 showError("Error")
  730.                             else
  731.                                 result = { pcall(func2()) }
  732.                             end
  733.                         else
  734.                             result = { pcall(func()) }
  735.                         end
  736.                        
  737.                         if result and result[1] then
  738.                             term.clear()
  739.                             term.setCursorPos(1, 1)
  740.                             term.setTextColor(colors.yellow)
  741.                             for i=2, #result do
  742.                                 print(result[i])
  743.                             end
  744.                             os.startTimer(0.1) -- to get rid of
  745.                             os.pullEvent() ------ stray events
  746.                             os.pullEvent("key")
  747.                         elseif result and not result[1] then
  748.                             showError(result[2])
  749.                         else
  750.                             showError("BLAH")
  751.                         end
  752.                        
  753.                         --[[local func, e = loadstring(str, "lua")
  754.                         local func2, e2 = loadstring("return "..s, "lua")
  755.                         if not func then
  756.                             if func2 then
  757.                                 func = func2
  758.                                 e = nil
  759.                                 nForcePrint = 1
  760.                             end
  761.                         else
  762.                             if func2 then
  763.                                 func = func2
  764.                             end
  765.                         end
  766.                        
  767.                         if func then
  768.                             local tResults = { pcall( function() return func() end ) }
  769.                             if tResults[1] then
  770.                                 local n = 1
  771.                                 while (tResults[n+1] ~= nil) do
  772.                                     table.insert(results, tostring( tResults[n+1] ) )
  773.                                     n = n + 1
  774.                                 end
  775.                             else
  776.                                 table.insert(results, tResults[2])
  777.                             end
  778.                         else
  779.                             table.insert(results, e)
  780.                         end
  781.                        
  782.                         if #results ~= 0 then
  783.                             term.clear()
  784.                             term.setCursorPos(1, 1)
  785.                             for i,v in pairs(results) do
  786.                                 print(v)
  787.                             end
  788.                             print("")
  789.                             print("Press any key")
  790.                            
  791.                             -- get rid of stray events
  792.                             --local timer = os.startTimer(0.1)
  793.                             --local event = {}
  794.                             --while event[1] ~= "timer" and event[2] ~= timer do
  795.                             --  event = { os.pullEvent() }
  796.                             --end
  797.                            
  798.                             os.pullEvent("key")
  799.                         else
  800.                             print("No results")
  801.                             sleep(3)
  802.                         end
  803.                         --]]
  804.                     end)
  805.                     term.setCursorBlink(false)
  806.                     refreshList()
  807.                    
  808.                 else
  809.                     if event[2] == 1 then
  810.                         return true, contents[event[4]+scroll-1]
  811.                     elseif event[2] == 2 then
  812.                         return false, contents[event[4]+scroll-1]
  813.                     end
  814.                    
  815.                 end
  816.                
  817.             elseif event[2] == 2 then
  818.                 return false, nil
  819.             end
  820.            
  821.         elseif event[1] == "mouse_scroll" then
  822.             if event[2] == 1 and scroll < #contents - (h-1) then
  823.                 scroll = scroll + 1
  824.             elseif event[2] == -1 and scroll > 0 then
  825.                 scroll = scroll - 1
  826.             end
  827.         end
  828.     end
  829. end
  830.  
  831. ---------------
  832. -- Main Loop --
  833. ---------------
  834.  
  835. function main() -- Main Loop (in a function for some reason)
  836.  
  837. while true do
  838.     term.setCursorBlink(false)
  839.  
  840.     local left, choice = list(_list)
  841.     if left then
  842.         if choice then
  843.             choice = shell.dir().."/"..choice
  844.             if fs.isDir(choice) then
  845.                 shell.setDir(getRealPath(choice))
  846.             else   
  847.                 run(choice)
  848.             end
  849.         end
  850.     else
  851.         if choice then
  852.             choice = shell.dir().."/"..choice
  853.             choice2 = rightClickMenu(choice, {
  854.                 "Run",
  855.                 "Edit",
  856.                 "Delete",
  857.                 "Copy",
  858.                 "Move",
  859.                 nil,
  860.                 "New Directory",
  861.                 "New File",
  862.                 nil,
  863.                 "Cancel",
  864.             })
  865.         else
  866.             --choice = shell.dir().."/"..choice
  867.             choice2 = rightClickMenu("", {
  868.                 "New Directory",
  869.                 "New File",
  870.                 nil,
  871.                 "Cancel"
  872.             })
  873.         end
  874.         if choice2 == "New Directory" then
  875.             local name = textBox("Directory Name")
  876.             if name then
  877.                 name = shell.dir().."/"..name
  878.                 ok, err = pcall(fs.makeDir, name)
  879.                 if not ok then
  880.                     showError(err)
  881.                 end
  882.             end
  883.         elseif choice2 == "New File" then
  884.             _name = textBox("File Name")
  885.             if _name then
  886.                 local name = shell.dir().."/".._name
  887.                 local file = io.open(name, "w")
  888.                 if not file then
  889.                     showError(err)
  890.                 else
  891.                     file.write("")
  892.                     file:close()
  893.                 end
  894.             end
  895.         elseif choice2 == "Run" then
  896.             local function parsestr(str)
  897.                 local args = {}
  898.                 for match in string.gmatch(str, "[^%s]+") do
  899.                     table.insert(args, match)
  900.                 end
  901.                 safeRun(unpack(args))
  902.             end
  903.             file = io.open(choice, "r")
  904.             if string.find(file:read("*a"), "{%s*%.%.%.%s*}") then -- If file contains {...}
  905.                 local args = textBox("Program Arguments", choice.." ")
  906.                 if args then
  907.                     parsestr(choice.." "..args)
  908.                 end
  909.             else
  910.                 parsestr(choice)
  911.             end
  912.         elseif choice2 == "Edit" then
  913.             shell.run("edit", choice)
  914.         elseif choice2 == "Delete" then
  915.             local delete
  916.             while delete ~= "Yes" and delete ~= "No" do
  917.                 delete = rightClickMenu(choice, {
  918.                     "Are you sure?",
  919.                     nil,
  920.                     "Yes",
  921.                     "No",
  922.                 })
  923.             end
  924.             if delete == "Yes" then
  925.                 fs.delete(choice)
  926.             end
  927.         elseif choice2 == "Move" then
  928.             local moveto = textBox("Move to...")
  929.             if moveto then
  930.                 ok, err = pcall(fs.move, choice, shell.dir().."/"..moveto)
  931.                 if not ok then
  932.                     showError(err)
  933.                 end
  934.             end
  935.         elseif choice2 == "Copy" then
  936.             local moveto = textBox("Copy to...")
  937.             if moveto then
  938.                 pcall(fs.copy, choice, shell.dir().."/"..moveto)
  939.                 if not ok then
  940.                     showError(err)
  941.                 end
  942.             end
  943.         end
  944.        
  945.     end
  946. end
  947.  
  948. end
  949.  
  950. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement