Advertisement
SuPeRMiNoR3

stargate

Dec 14th, 2014
204
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.32 KB | None | 0 0
  1. local version = "0.4.6"
  2. local m = {}
  3.  
  4. local component = require("component")
  5. if not component.isAvailable("internet") then
  6.   error("This program requires an internet card to run.")
  7.   return
  8. end
  9.  
  10. local serial = require("serialization")
  11. local internet = require("internet")
  12. local term = require("term")
  13. local keyboard = require("keyboard")
  14. local event = require("event")
  15. local wget = loadfile("/bin/wget.lua")
  16.  
  17. local function downloadRaw(url)
  18.   local sContent = ""
  19.   local result, response = pcall(internet.request, url)
  20.   if not result then
  21.     return nil
  22.   end
  23.     for chunk in response do
  24.       sContent = sContent..chunk
  25.     end
  26.   return sContent
  27. end
  28.  
  29. local function downloadFile(url, path)
  30.   return wget("-fq",url,path)
  31. end
  32.  
  33. function m.getVersion() --For getting the version of superlib without making an internet request
  34.   return version
  35. end
  36.  
  37. function m.checkVersions()
  38.   response = downloadFile("https://raw.githubusercontent.com/OpenPrograms/SuPeRMiNoR2-Programs/master/versions.lua", "/tmp/versions.lua")
  39.   versions = loadfile("/tmp/versions.lua")() --The () are needed
  40.   return versions, version
  41. end
  42.  
  43. function m.downloadFile(url, path)
  44.   local success, response = pcall(downloadFile, url, path)
  45.     if not success then
  46.       return nil
  47.     end
  48.     return response
  49. end
  50.  
  51. function m.download(url)
  52.   local success, response = pcall(downloadRaw, url)
  53.   if not success then
  54.     return nil
  55.   end
  56.   return response
  57. end
  58.  
  59. function m.round(what, precision)
  60.  return math.floor(what*math.pow(10,precision)+0.5) / math.pow(10,precision)
  61. end
  62.  
  63. function m.percent_gen(stored, capacity, precision)
  64.   tmp = stored / capacity
  65.   tmp = tmp * 100
  66.   tmp = m.round(tmp, precision)
  67.   return tmp
  68. end
  69.  
  70. m.pgen = m.percent_gen --Compat
  71.  
  72. function m.pad(str, len)
  73.   char = " "
  74.   if char == nil then char = ' ' end
  75.   return str .. string.rep(char, len - #str)
  76. end
  77.  
  78. function m.decode(data)
  79.   status, result = pcall(serial.unserialize, data)
  80.   return status, result
  81. end
  82.  
  83. function m.encode(data)
  84.   return serial.serialize(data)
  85. end
  86.  
  87. --Menu Section
  88. lastmenu = false
  89. menu = {}
  90.  
  91. function rendermenu(mt)
  92.   term.clear()
  93.   for i=1, #mt do
  94.     print(" "..i.."  "..mt[i]["name"].." ("..mt[i]["addr"]..")")
  95.   end
  96. end
  97.  
  98. function updatemenu(mt, sel)
  99.   if lastmenu ~= false then
  100.     term.setCursor(1, lastmenu)
  101.     term.clearLine()
  102.     term.write(" "..lastmenu.."  "..mt[lastmenu]["name"].." ("..mt[lastmenu]["addr"]..")")
  103.   end
  104.   term.setCursor(1, sel)
  105.   term.clearLine()
  106.   term.write("["..sel.."] "..mt[sel]["name"].." ("..mt[sel]["addr"]..")")
  107. end
  108.  
  109. function m.addItem(name, data)
  110.   menu[#menu + 1] = {name=name, addr=data}
  111. end
  112.  
  113. function m.clearMenu()
  114. menu = {}
  115. end
  116.  
  117. function m.runMenu()
  118.   rendermenu(menu)
  119.   sel = 1
  120.   updatemenu(menu, sel)
  121.  
  122.   while true do
  123.     e, r, t, key = event.pull("key_down")
  124.  
  125.     if key == keyboard.keys.down then
  126.       lastmenu = sel
  127.       sel = sel + 1
  128.       if sel > #menu then
  129.         sel = 1
  130.       end
  131.     end
  132.     if key == keyboard.keys.up then
  133.       lastmenu = sel
  134.       sel = sel - 1
  135.       if sel < 1 then
  136.         sel = #menu
  137.       end
  138.     end
  139.     if key == keyboard.keys.enter then
  140.       return menu[sel]["addr"]
  141.     end
  142.     if key == keyboard.keys.q then
  143.       return false
  144.     end
  145.     updatemenu(menu, sel)
  146.   end
  147. end
  148. --------------------------
  149.  
  150. return m
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement