Advertisement
incinirate

GetInfo

Nov 7th, 2015
213
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.69 KB | None | 0 0
  1. function httpCall(url,verify)
  2.     http.request(url)
  3.     while true do
  4.         local x = {os.pullEvent()}
  5.         local e = x[1]
  6.         table.remove(x,1)
  7.  
  8.         if e=="http_success" then
  9.             local get = x[2].readAll():match("[^%s]+")
  10.             if verify(get) then
  11.                 return true,get
  12.             end
  13.             break
  14.         elseif e=="http_failure" then
  15.             break
  16.         end
  17.     end
  18. end
  19.  
  20. function getInfo(x1,x2,y,label,colorsch,verify,over)
  21.     label = label or ""
  22.     local ubg = config.theme.bg
  23.     local utx = config.theme.text
  24.     if colorsch == "alt" then
  25.         ubg = config.theme.altbg
  26.         utx = config.theme.alttext
  27.     end
  28.     local stx = x1
  29.     local enx = x2
  30.     if verify then
  31.         enx = enx-1
  32.     end
  33.     if label ~= "" then
  34.         stx = x1+#label+1
  35.     end
  36.  
  37.     term.setCursorPos(x1,y)
  38.     term.setBackgroundColor(ubg)
  39.     term.setTextColor(utx)
  40.     term.write(label)
  41.  
  42.  
  43.     local get = ""
  44.     local function doIt()
  45.         term.setCursorPos(stx,y)
  46.         term.setBackgroundColor(config.theme.txtboxbg)
  47.         term.setTextColor(config.theme.txtboxtxt)
  48.         term.write(string.rep(" ",enx-stx+1))
  49.         term.setCursorPos(stx,y)
  50.         return limitRead(enx-stx+1,over)
  51.     end
  52.  
  53.     if verify then
  54.         --[[
  55.         verify = {
  56.         subdom = "/api/changePin"
  57.         exq = {
  58.         oldPin = "$INPUT$",
  59.         newPin = "$INPUT$",
  60.         address = "0000123412341234"
  61.     }
  62.         callback = function(inp)
  63.         if inp=="SUCCESS_PIN_CHANGED" then
  64.         return true
  65.     end
  66.         return false
  67.     end
  68.     }
  69.         ]]
  70.         local success = false
  71.         local reinp
  72.         term.setBackgroundColor(colors.orange)
  73.         term.setCursorPos(x2,y)
  74.         term.write(" ")
  75.         while not success do
  76.             reinp = doIt()
  77.             if verify.preprocess then
  78.                 reinp = verify.preprocess(reinp)    
  79.             end
  80.             local sbhost = config.host..verify.subdom.."?"
  81.             for k,v in pairs(verify.exq) do
  82.                 if v=="$INPUT$" then
  83.                     v = tostring(reinp)
  84.                 end
  85.                 sbhost = sbhost..tostring(k).."="..v.."&"
  86.             end
  87.             sbhost = sbhost:sub(1,#sbhost-1)
  88.             term.setBackgroundColor(ubg)
  89.             term.setTextColor(utx)
  90.             parallel.waitForAny(function() glib.spinner(x2,y) end, function()
  91.                     success = httpCall(sbhost,verify.callback)
  92.                 end)
  93.             if not success then
  94.                 term.setCursorPos(x2,y)
  95.                 term.setBackgroundColor(colors.red)
  96.                 term.write(" ")
  97.             end
  98.         end
  99.         term.setCursorPos(x2,y)
  100.         term.setBackgroundColor(colors.green)
  101.         term.write(" ")
  102.         return reinp
  103.     else
  104.         return doIt()    
  105.     end
  106. end
  107.  
  108. function getInfoPrerender(x1,x2,y,label,colorsch,verify)
  109.     label = label or ""
  110.     local ubg = config.theme.bg
  111.     local utx = config.theme.text
  112.     if colorsch == "alt" then
  113.         ubg = config.theme.altbg
  114.         utx = config.theme.alttext
  115.     end
  116.     local stx = x1
  117.     local enx = x2
  118.     if verify then
  119.         enx = enx-1
  120.     end
  121.     if label ~= "" then
  122.         stx = x1+#label+1
  123.     end
  124.  
  125.     term.setCursorPos(x1,y)
  126.     term.setBackgroundColor(ubg)
  127.     term.setTextColor(utx)
  128.     term.write(label)
  129.  
  130.     term.setCursorPos(stx,y)
  131.     term.setBackgroundColor(config.theme.txtboxbg)
  132.     term.setTextColor(config.theme.txtboxtxt)
  133.     term.write(string.rep(" ",enx-stx+1))
  134.  
  135.     if verify then
  136.         term.setBackgroundColor(colors.orange)
  137.         term.setCursorPos(x2,y)
  138.         term.write(" ")    
  139.     end
  140. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement