Advertisement
jordyvl

NetPrint Install

Nov 22nd, 2014
172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.54 KB | None | 0 0
  1. local function clear()
  2.     term.clear()
  3.     term.setCursorPos(1, 1)
  4. end
  5.  
  6. local function get(user, repo, bran, path, save)
  7.     if not user or not repo or not bran or not path then
  8.         error("not enough arguments, expected 4 or 5", 2)
  9.     end
  10.     local url = "https://raw.github.com/"..user.."/"..repo.."/"..bran.."/"..path
  11.     local remote = http.get(url)
  12.     if not remote then
  13.         return false
  14.     end
  15.     local text = remote.readAll()
  16.     remote.close()
  17.     if save then
  18.         local file = fs.open(save, "w")
  19.         file.write(text)
  20.         file.close()
  21.         return true
  22.     end
  23.     return text
  24. end
  25.  
  26. local function yesno(text, title, start)
  27.     local function clear()
  28.         term.clear()
  29.         term.setCursorPos(1, 1)
  30.     end
  31.    
  32.     local function drawButton(buttonText, x, y, x2, y2, enabled)
  33.         if enabled then
  34.             term.setBackgroundColor(colors.white)
  35.         else
  36.             if term.isColor() then
  37.                 term.setBackgroundColor(colors.gray)
  38.             end
  39.         end
  40.         term.setCursorPos(x, y)
  41.         for _y = y, y2 do
  42.             for _x = x, x2 do
  43.                 term.setCursorPos(_x, _y)
  44.                 write(" ")
  45.             end
  46.         end
  47.         term.setCursorPos(x+math.floor((x2-x)/2)-math.floor(buttonText:len()/2), y+math.floor((y2-y+1)/2))
  48.         term.setTextColor(colors.black)
  49.         write(buttonText)
  50.         term.setTextColor(colors.white)
  51.         term.setBackgroundColor(colors.black)
  52.     end
  53.    
  54.     local function cprint(text)
  55.         local x, y = term.getCursorPos()
  56.         local w, h = term.getSize()
  57.         term.setCursorPos(math.floor(w/2)-math.floor(text:len()/2), y)
  58.         print(text)
  59.     end
  60.    
  61.     local selected = true
  62.    
  63.     local function redraw()
  64.         clear()
  65.         cprint(title)
  66.         term.setCursorPos(1, 3)
  67.         print(text)
  68.         local w, h = term.getSize()
  69.         drawButton("Yes", 2, h-1, math.floor(w/2)-1, h-1, selected)
  70.         drawButton("No", math.floor(w/2)+1, h-1, w-1, h-1, not selected)
  71.     end
  72.    
  73.     if start ~= nil and type(start) == "boolean" then
  74.         selected = start
  75.     end
  76.     while true do
  77.         redraw()
  78.         local eventData = {os.pullEventRaw()}
  79.         if eventData[1] == "terminate" then
  80.             clear()
  81.             return
  82.         elseif eventData[1] == "key" then
  83.             if eventData[2] == keys.up or eventData[2] == keys.down or eventData[2] == keys.left or eventData[2] == keys.right then
  84.                 selected = not selected
  85.             elseif eventData[2] == keys.enter then
  86.                 clear()
  87.                 return selected
  88.             end
  89.         end
  90.         sleep(0)
  91.     end
  92. end
  93.  
  94. local function getFile(file, target)
  95.     return get("jordyvl", "dual-network-printer", "master", file, target)
  96. end
  97.  
  98. shell.setDir("")
  99.  
  100. clear()
  101.  
  102. print("Network Printing Install or Update")
  103. print("Getting files...")
  104. local files = {
  105.     ["Print"] = "Dualprint/print",
  106.     ["pview"] = "Dualprint/Connected",
  107.     ["update.lua"] = "update",
  108. }
  109. local fileCount = 0
  110. for _ in pairs(files) do
  111.     fileCount = fileCount + 1
  112. end
  113. local filesDownloaded = 0
  114.  
  115. local w, h = term.getSize()
  116. local pb
  117. if ui and term.isColor() and ui.progressBar then
  118.     pb = ui.progressBar(1,h-2,w,colors.green,true)
  119. end
  120.  
  121. for k, v in pairs(files) do
  122.     term.setTextColor(colors.black)
  123.     term.setBackgroundColor(colors.white)
  124.     clear()
  125.     term.setCursorPos(2, 2)
  126.     print("DualPrint Update!")
  127.     term.setCursorPos(2, 4)
  128.     print("File: "..v)
  129.     term.setCursorPos(2, h - 1)
  130.     if pb then
  131.         pb.percent = math.floor(filesDownloaded / fileCount * 100)
  132.         pb:draw()
  133.     else
  134.         print(tostring(math.floor(filesDownloaded / fileCount * 100)).."% - "..tostring(filesDownloaded + 1).."/"..tostring(fileCount))
  135.     end
  136.     local ok = getFile(k, v)
  137.     if not ok then
  138.         if term.isColor() then
  139.             term.setTextColor(colors.red)
  140.         end
  141.         term.setCursorPos(2, 6)
  142.         print("Error.")
  143.         sleep(1)
  144.     end
  145.     filesDownloaded = filesDownloaded + 1
  146. end
  147. term.clear()
  148. term.setCursorPos(1,1)
  149. print"Install Completed"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement