Advertisement
ShadowLoop

OpenComputers RemoteEdit

May 29th, 2020
1,536
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.71 KB | None | 0 0
  1. --modular code
  2. local application = "RemoteEdit"
  3. local dlTbl = {
  4.   {link = "https://raw.githubusercontent.com/feldim2425/OC-Programs/master/websocket_client/websocket_client.lua", file = "/usr/lib/websocket_client.lua"},
  5.   {link = "https://raw.github.com/feldim2425/OC-Programs/master/websocket_client/websocket/ws_component_layer.lua", file = "/usr/lib/websocket/ws_component_layer.lua"},
  6.   {link = "https://raw.github.com/feldim2425/OC-Programs/master/websocket_client/websocket/ws_tools.lua", file = "/usr/lib/websocket/ws_tools.lua"},
  7.   {link = "https://raw.githubusercontent.com/ShaneYu/react-oc-remote-edit/master/opencomputers/remoteedit.lua", file = "/usr/lib/remoteedit.lua"},
  8.   {link = "http://regex.info/code/JSON.lua", file = "/usr/lib/JSON.lua"}
  9. }
  10.  
  11. --internal
  12.  
  13. local component = require("component")
  14. local fs = require("filesystem")
  15. local shell = require("shell")
  16. local term = require("term")
  17.  
  18. if not component.isAvailable("internet") then
  19.   print("Need an internet card!")
  20.   return
  21. end
  22. local internet = require("internet")
  23.  
  24.  
  25. local function writeFile(data,name)
  26.   if fs.exists(name) then
  27.     fs.remove(name)
  28.   else
  29.     local path = fs.path(name)
  30.     if path then
  31.       local pSeg = fs.segments(path)
  32.       local pCur = "/"
  33.       for _, seg in pairs(pSeg) do
  34.         pCur = fs.concat(pCur, seg)
  35.         if not fs.exists(pCur) then
  36.           fs.makeDirectory(pCur)
  37.         end
  38.       end
  39.     end
  40.    end
  41.    
  42.    local file = io.open(name, "wb")
  43.    if file == nil then
  44.      return false
  45.    end
  46.    file:write(data)
  47.    file:close()
  48.    return true
  49. end
  50.  
  51. print("Downloading "..application)
  52.  
  53.  
  54. local termW, termH,_,_,_,termY = term.getViewport()
  55. term.setCursor(1,termY)
  56.  
  57. if termY+5 >= termH then
  58.   termY = termH-5
  59.   for i=1,5 do
  60.     print("")
  61.   end
  62. end
  63.  
  64. local step = 100 / #dlTbl
  65. local percent = 0
  66. local barMlen = (termW-8)
  67. local cstep =  barMlen / 100
  68.  
  69. term.setCursor(1,termY+2)
  70. term.write(string.format("% 5.1f%% ",percent)..string.rep("β–‘",barMlen))
  71.  
  72. for _,pk in pairs(dlTbl) do
  73.   term.setCursor(1,termY+3)
  74.   term.write(pk.file)
  75.  
  76.   local webData = internet.request(pk.link)
  77.   if not webData then
  78.     term.setCursor(1,termY+5)
  79.     print("Error while downloading "..pk.link)
  80.     return
  81.   end
  82.  
  83.   local content = ""
  84.   for chunk in webData do
  85.     content = content..chunk
  86.   end
  87.  
  88.   if not writeFile(content, pk.file) then
  89.     term.setCursor(1,termY+5)
  90.     print("Error while writing "..pk.file)
  91.     return
  92.   end
  93.  
  94.   percent = percent + step
  95.  
  96.   term.setCursor(1,termY+2)
  97.   barLen = math.floor(percent * cstep + 0.5)
  98.   term.write(string.format("%5.1f%% ",percent)..string.rep("β–ˆ",barLen)..string.rep("β–‘",barMlen-barLen))
  99. end
  100.  
  101. term.setCursor(1,termY+5)
  102. print("Done")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement