smigger22

EpicBrowser

Feb 17th, 2015
348
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.57 KB | None | 0 0
  1. EpicBrowser = true
  2. local title = nil
  3. local x, y = term.getSize()
  4. -- Cool functions
  5. function allColour(tColour, bColour)
  6.     if term.isColour() then
  7.         term.setTextColour(colours[tColour])
  8.         term.setBackgroundColour(colours[bColour])
  9.     else
  10.         term.setTextColour(colours.white)
  11.         term.setBackgroundColour(colours.black)
  12.     end
  13. end
  14.  
  15. function textColour(tColour)
  16.     if term.isColour() then
  17.         term.setTextColour(colours[tColour])
  18.     else
  19.         term.setTextColour(colours.white)
  20.         term.setBackgroundColour(colours.black)
  21.     end
  22. end
  23.  
  24. function bgColour(bColour)
  25.     if term.isColour() then
  26.         term.setBackgroundColour(colours[bColour])
  27.     else
  28.         term.setTextColour(colours.white)
  29.         term.setBackgroundColour(colours.black)
  30.     end
  31. end
  32.  
  33.  function backgroundSet(bColour)
  34.     bgColour(bColour)
  35.     term.clear()
  36. end
  37.  
  38. function resetBackground()
  39.     allColour("white", "black")
  40.     term.clear()
  41. end
  42.  
  43. function centered(str, height, tColour, bColour)
  44.     term.setCursorPos(x / 2 - #str / 2, height)
  45.     if tColour and bColour then
  46.         allColour(tColour, bColour)
  47.     elseif tColour then
  48.         textColour(tColour)
  49.     elseif bColour then
  50.         bgColour(bColour)
  51.     end
  52.     term.write(str)
  53. end
  54.  
  55. function left(str, height, tColour, bColour)
  56.     term.setCursorPos(1, height)
  57.     if tColour and bColour then
  58.         allColour(tColour, bColour)
  59.     elseif tColour then
  60.         textColour(tColour)
  61.     elseif bColour then
  62.         bgColour(bColour)
  63.     end
  64.     term.write(str)
  65. end
  66.  
  67. function right(str, height, tColour, bColour)
  68.     term.setCursorPos(x - #str, height)
  69.     if tColour and bColour then
  70.         allColour(tColour, bColour)
  71.     elseif tColour then
  72.         textColour(tColour)
  73.     elseif bColour then
  74.         bgColour(bColour)
  75.     end
  76.     term.write(str)
  77. end
  78.  
  79. function indented(str, indent, height, tColour, bColour)
  80.     term.setCursorPos(indent, height)
  81.     if tColour and bColour then
  82.         allColour(tColour, bColour)
  83.     elseif tColour then
  84.         textColour(tColour)
  85.     elseif bColour then
  86.         bgColour(bColour)
  87.     end
  88.     term.write(str)
  89. end
  90.  
  91. function clearLine(bColour, height)
  92.     term.setCursorPos(1, height)
  93.     bgColour(bColour)
  94.     term.clearLine()
  95. end
  96.  
  97. function drawBorder(xMin, yMin, xMax, yMax, colour)
  98.     paintutils.drawBox(xMin, yMin, xMax, yMax, colours[colour])
  99. end
  100.  
  101. function drawBox(xMin, yMin, xMax, yMax, colour)
  102.     paintutils.drawFilledBox(xMin, yMin, xMax, yMax, colours[colour])
  103. end
  104.  
  105. function len(str) return str:len() end
  106.  
  107. function upper(str) return str:upper() end
  108.  
  109. function lower(str) return str:lower() end
  110.  
  111. function replace(str, find, replace) return str:gsub(find, replace) end
  112.  
  113. function makeLength(str, startLen, endLen) return str:sub(startLen, endLen) end
  114.  
  115. function rep(str, times) return str:rep(times) end
  116.  
  117. function findPeripheral(Perihp) --Returns side of first matching peripheral matching passed string
  118.     for _,s in ipairs(rs.getSides()) do
  119.         if peripheral.isPresent(s) and peripheral.getType(s) == Perihp then
  120.             return s
  121.         end
  122.     end
  123.     return false
  124. end
  125.  
  126. modemSide = findPeripheral("modem")
  127. rednet.open(modemSide)
  128.  
  129. epicb = {}
  130.  
  131. -- Main Code
  132. function background()
  133.     backgroundSet("white")
  134.     for i = 1, 3 do
  135.         clearLine("grey", i)
  136.     end
  137.     indented("Epic", 2, 2, "cyan")
  138.     indented("Browser", 6, 2, "lightBlue")
  139.     indented("By ", x - 6, 2, "white")
  140.     indented("R", x - 3, 2, "yellow")
  141.     indented("o", x - 2, 2, "red")
  142.     indented("d", x - 1, 2, "lightBlue")
  143.     allColour("lightBlue", "white")
  144.     left(" [ Quit ]", y - 1)
  145.     right("[ Request Page ] ", y - 1)
  146. end
  147.  
  148. function main()
  149.     background()
  150.     title = "Access Page"
  151.     centered(title, 2, "lightGrey", "grey")
  152.     bgColour("white")
  153.     indented("Server URL: ", 2, 5, "lightBlue")
  154.     url = read()
  155.     clearLine("white", 5)
  156.     indented("Page: ", 2, 5, "lightBlue")
  157.     page = read()
  158.     if page:len() == 0 then page = "index" end
  159.     clearLine("white", 5)
  160.     indented("Timeout: ", 2, 5, "lightBlue")
  161.     timeout = read()
  162.     timeout = tonumber(timeout)
  163.     if type(timeout) ~= "number" then timeout = 100 end
  164.     clearLine("white", 5)
  165.     rednet.broadcast(page, url)
  166.     local event = { rednet.receive(3) }
  167.     if not event[1] then
  168.         clearLine("white", 5)
  169.         centered("Failed to connect to "..url, 5, "red")
  170.         sleep(2)
  171.         main()
  172.     else
  173.         if not fs.exists(url) then
  174.             fs.makeDir(url)
  175.         else
  176.             if not fs.isDir(url) then
  177.                 fs.delete(url)
  178.                 fs.makeDir(url)
  179.             end
  180.         end
  181.         local file = fs.open(url.."/"..page, "w")
  182.         file.write(event[2])
  183.         file.close()
  184.         shell.run(url.."/"..page)
  185.         resetBackground()
  186.         sleep(timeout)
  187.     end
  188. end
  189.  
  190. function checkExit()
  191.     while true do
  192.         local event = { os.pullEvent("mouse_click") }
  193.         if event[2] == 1 and event[3] >= 2 and event[3] <= 9 and event[4] == y - 1 then
  194.             resetBackground()
  195.             left("CraftOS 1.6", 1, "yellow")
  196.             term.setCursorPos(1, 2)
  197.             return
  198.         end
  199.     end
  200. end
  201.  
  202. function requestPage() -- 1
  203.     local event = { os.pullEvent("mouse_click") }
  204.     if event[2] == 1 and event[3] >= x - 16 and event[3] <= x - 2 and event[4] == y - 1 then -- 2
  205.         clearLine("white", 5)
  206.         title = "Download Page"
  207.         centered(title, 2, "lightGrey", "grey")
  208.         indented("Server URL: ", 2, 5, "lightBlue")
  209.         url = read()
  210.         clearLine("white", 5)
  211.         indented("Page: ", 2, 5, "lightBlue")
  212.         page = read()
  213.         clearLine("white", 5)
  214.         rednet.broadcast(page, url)
  215.         local event = { rednet.receive(3) }
  216.         if not event[1] then -- 3
  217.             clearLine("white", 5)
  218.             centered("Failed to connect to "..url, 5, "red")
  219.         else
  220.             if not fs.exists(url) then -- 4
  221.                 fs.makeDir(url)
  222.             else
  223.                 if not fs.isDir(url) then -- 5
  224.                     fs.delete(url)
  225.                     fs.makeDir(url)
  226.                 end -- 5
  227.             end -- 4
  228.             local download = fs.open(url.."/"..page, "w")
  229.             page.write(event[2])
  230.             page.close()
  231.             clearLine("white", 5)
  232.             centered("Downloaded the page successfully!", 5, "green")
  233.             sleep(2)
  234.             main()
  235.         end -- 3
  236.     end -- 2
  237. end -- 1
  238.  
  239. function begin()
  240.     parallel.waitForAny(main, checkExit, requestPage)
  241. end
  242.  
  243. begin()
Advertisement
Add Comment
Please, Sign In to add comment