Z1maV1

web

May 15th, 2022 (edited)
617
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 11.17 KB | None | 0 0
  1. local ex = require("/lib/exceptions")
  2. local str = require("/lib/string")
  3.  
  4. local url = ""
  5. local urlSplit = {}
  6. local isRefreshPressed = false
  7. local w
  8. local h
  9. local selectedLine = -1
  10. local serverId = -1
  11.  
  12. local redirectLines = {}
  13.  
  14. local page = ""
  15.  
  16. local formatColors = {
  17.     ["0"] = colors.white,
  18.     ["1"] = colors.orange,
  19.     ["2"] = colors.magenta,
  20.     ["3"] = colors.lightBlue,
  21.     ["4"] = colors.yellow,
  22.     ["5"] = colors.lime,
  23.     ["6"] = colors.pink,
  24.     ["7"] = colors.gray,
  25.     ["8"] = colors.lightGray,
  26.     ["9"] = colors.cyan,
  27.     ["a"] = colors.purple,
  28.     ["b"] = colors.blue,
  29.     ["c"] = colors.brown,
  30.     ["d"] = colors.green,
  31.     ["e"] = colors.red,
  32.     ["f"] = colors.black
  33. }
  34.  
  35. local request = {
  36.     ["method"] = "GET",
  37.     ["path"] = "",
  38.     ["body"] = {},
  39.     ["headers"] = {
  40.         ["User-Agent"] = "DiamondBrowser 0.0.1"
  41.     }
  42. }
  43.  
  44. local function resetColor()
  45.     term.setBackgroundColor(colors.black)
  46.     term.setTextColor(colors.white)
  47.     term.setCursorPos(1, 1)
  48. end
  49.  
  50. local function drawLowerBar()
  51.     term.setCursorPos(1, h)
  52.     term.setBackgroundColor(colors.lightGray)
  53.     term.setTextColor(colors.black)
  54.     term.clearLine()
  55.     term.setCursorPos(2, h)
  56.     term.setBackgroundColor(colors.gray)
  57.     term.setTextColor(colors.white)
  58.     term.write(url)
  59.     term.setBackgroundColor(colors.lightGray)
  60.     term.setTextColor(colors.black)
  61.     term.setCursorPos(w - 11, h)
  62.     if isRefreshPressed then
  63.         term.setBackgroundColor(colors.gray)
  64.         term.setTextColor(colors.white)
  65.         term.write("[ refresh ]")
  66.         term.setBackgroundColor(colors.lightGray)
  67.         term.setTextColor(colors.black)
  68.     else
  69.         term.write("  refresh  ")
  70.     end
  71. end
  72.  
  73. local function drawBox(x, y, width, height, color)
  74.     local originColor = term.getBackgroundColor()
  75.     ex.try(function ()
  76.         term.setBackgroundColor(color)
  77.         for i = x, x + width do
  78.             for j = y, y + height do
  79.                 term.setCursorPos(i, j)
  80.                 write(" ")
  81.             end
  82.         end
  83.     end, function(e)
  84.         ex.print_err(e)
  85.     end)
  86.     term.setBackgroundColor(originColor)
  87. end
  88.  
  89. local function drawLineSelector()
  90.     for i = 1, h - 1 do
  91.         term.setCursorPos(1, i)
  92.         if i == selectedLine then
  93.             term.setBackgroundColor(colors.gray)
  94.             term.setTextColor(colors.white)
  95.             term.write(">")
  96.             term.setBackgroundColor(colors.lightGray)
  97.             term.setTextColor(colors.black)
  98.         else
  99.             term.write(" ")
  100.         end
  101.     end
  102. end
  103.  
  104. local function drawAndHandleUrlInput()
  105.     drawBox(8, 7, 36, 6, colors.lightGray)
  106.    
  107.     drawBox(10, 10, 32, 0, colors.gray)
  108.  
  109.     term.setTextColor(colors.white)
  110.     term.setBackgroundColor(colors.gray)
  111.  
  112.     term.setCursorPos(10, 10)
  113.    
  114.     url = read()
  115.  
  116.     term.setTextColor(colors.black)
  117.     term.setBackgroundColor(colors.gray)
  118. end
  119.  
  120. --[[
  121.     Formatting will be done by simple tags
  122.     <b1> will effect background color
  123.     I'll call it <bN> tag, where N is a ket from formatColors table
  124.     There is also <fN> tag, which will effect on text color
  125.     there is more complex <lnk> tag that can be either <lnk loc:path/to/page>
  126.     or <lnk glb:website.com/path/to/page> and effects all line
  127. ]]
  128. local function formatAndDisplayPage(page)
  129.     term.setCursorPos(2, 1)
  130.     local bg = term.getBackgroundColor()
  131.     local fg = term.getTextColor()
  132.  
  133.     term.setBackgroundColor(colors.white)
  134.     term.setTextColor(colors.black)
  135.  
  136.     local toSkip = 0
  137.     local skip = false
  138.     local line = 1
  139.     for idx = 1, #page do
  140.         local char = page:sub(idx, idx)
  141.  
  142.         if skip then
  143.             if toSkip > 0 then
  144.                 toSkip = toSkip - 1
  145.                 goto continue
  146.             else
  147.                 skip = false
  148.             end
  149.         end
  150.  
  151.         if char == "\n" then
  152.             local _, y = term.getCursorPos()
  153.             term.setCursorPos(2, y + 1)
  154.             line = line + 1
  155.         else
  156.             if char == "<" then
  157.                 if page:sub(idx + 1, idx + 1) == "f" then
  158.                     local color = formatColors[page:sub(idx + 2, idx + 2)]
  159.                     if color ~= nil then
  160.                         term.setTextColor(color)
  161.                         toSkip = 3 -- count closing '>'
  162.                         skip = true
  163.                         goto continue
  164.                     end
  165.                 elseif page:sub(idx + 1, idx + 1) == "b" then
  166.                     local color = formatColors[page:sub(idx + 2, idx + 2)]
  167.                     if color ~= nil then
  168.                         term.setBackgroundColor(color)
  169.                         toSkip = 3 -- count closing '>'
  170.                         skip = true
  171.                         goto continue
  172.                     end
  173.                 -- stucture must be <lnk glb:someweb.com/path/to/page>
  174.                 -- or <lnk loc:path/to/page>
  175.                 elseif page:sub(idx + 1, idx + 3) == "lnk" then
  176.                     local isGlobal = page:sub(idx + 5, idx + 7) == "glb"
  177.                     local path = ""
  178.                     local amount = 0
  179.                     for i = idx + 9, #page do
  180.                         if page:sub(i, i) == ">" then
  181.                             break
  182.                         else
  183.                             path = path .. page:sub(i, i)
  184.                         end
  185.                         amount = amount + 1
  186.                     end
  187.                     redirectLines[line] = {path = path, isGlobal = isGlobal}
  188.                     toSkip = amount + 9
  189.                     skip = true
  190.                     goto continue
  191.                 end
  192.             end
  193.             term.write(char)
  194.         end
  195.  
  196.         ::continue::
  197.     end
  198.  
  199.     term.setBackgroundColor(bg)
  200.     term.setTextColor(fg)
  201. end
  202.  
  203. w, h = term.getSize()
  204.  
  205. -- set background
  206. term.setBackgroundColor(colors.white)
  207. term.clear()
  208.  
  209. drawLowerBar()
  210. drawLineSelector()
  211. drawAndHandleUrlInput()
  212.  
  213. rednet.open("top")
  214. urlSplit = str.split(url, "/")
  215. rednet.broadcast(urlSplit[1], "__ddns_lookup")
  216. _, msg = rednet.receive("__ddns_lookup", 5)
  217.  
  218. if msg == nil or msg == -1 then
  219.     drawLowerBar()
  220.     drawLineSelector()
  221.     term.setCursorPos(2, 1)
  222.     term.setTextColor(colors.black)
  223.  
  224.     term.write("Cannot resolve address! (Is url correct?)")
  225. else
  226.     serverId = msg
  227.     drawLowerBar()
  228.     drawLineSelector()
  229.     term.setCursorPos(2, 1)
  230.     term.setTextColor(colors.black)
  231.  
  232.     term.write("Connecting...")
  233.  
  234.     local req = request
  235.     if #urlSplit == 1 then
  236.         req["path"] = "/"
  237.     else
  238.         req["path"] = "/" .. table.concat(urlSplit, "/", 2, #urlSplit)
  239.     end
  240.     rednet.send(serverId, textutils.serialize(req), "http")
  241.     _, msg = rednet.receive("http", 5)
  242.     page = textutils.unserialize(msg)
  243.  
  244.     term.setBackgroundColor(colors.white)
  245.     term.clear()
  246.     drawLowerBar()
  247.     drawLineSelector()
  248.  
  249.     formatAndDisplayPage(page["body"])
  250. end
  251.  
  252. local function handleUserClicks()
  253.     while true do
  254.         local event, button, x, y = os.pullEvent("mouse_click")
  255.         if(event == "mouse_click") and button == 1 then
  256.             selectedLine = y
  257.             isRefreshPressed = false
  258.  
  259.             if y == h and x >= w - 12 and x <= w -1 then
  260.                 isRefreshPressed = true
  261.                 local req = request
  262.                 if #urlSplit == 1 then
  263.                     req["path"] = "/"
  264.                 else
  265.                     req["path"] = "/" .. table.concat(urlSplit, "/", 2, #urlSplit)
  266.                 end
  267.                 rednet.send(serverId, textutils.serialize(req), "http")
  268.                 _, msg = rednet.receive("http", 5)
  269.                 page = textutils.unserialize(msg)
  270.             elseif y == h and x >= 2 and x <=#url then
  271.                 drawAndHandleUrlInput()
  272.                 rednet.open("top")
  273.                 urlSplit = str.split(url, "/")
  274.                 rednet.broadcast(urlSplit[1], "__ddns_lookup")
  275.                 _, msg = rednet.receive("__ddns_lookup", 5)
  276.                
  277.                 if msg == nil or msg == -1 then
  278.                     page["body"] = "Cannot resolve address! (Is url correct?)"
  279.                 else
  280.                     serverId = msg
  281.                
  282.                     local req = request
  283.                     if #urlSplit == 1 then
  284.                         req["path"] = "/"
  285.                     else
  286.                         req["path"] = "/" .. table.concat(urlSplit, "/", 2, #urlSplit)
  287.                     end
  288.                     rednet.send(serverId, textutils.serialize(req), "http")
  289.                     _, msg = rednet.receive("http", 5)
  290.                     page = textutils.unserialize(msg)
  291.                 end
  292.             elseif redirectLines[y] ~= nil then
  293.                 if redirectLines[y].isGlobal then
  294.                     url = redirectLines[y].path
  295.                     rednet.open("top")
  296.                     urlSplit = str.split(url, "/")
  297.                     rednet.broadcast(urlSplit[1], "__ddns_lookup")
  298.                     _, msg = rednet.receive("__ddns_lookup", 5)
  299.                    
  300.                     if msg == nil or msg == -1 then
  301.                         page["body"] = "Cannot resolve address! (Is url correct?)"
  302.                     else
  303.                         serverId = msg
  304.                    
  305.                         local req = request
  306.                         if #urlSplit == 1 then
  307.                             req["path"] = "/"
  308.                         else
  309.                             req["path"] = "/" .. table.concat(urlSplit, "/", 2, #urlSplit)
  310.                         end
  311.                         rednet.send(serverId, textutils.serialize(req), "http")
  312.                         _, msg = rednet.receive("http", 5)
  313.                         page = textutils.unserialize(msg)
  314.                     end
  315.                 else
  316.                     local pathSplit = str.split(redirectLines[y].path, "/")
  317.                     for i = 1, #pathSplit do
  318.                         urlSplit[i + 1] = pathSplit[i]
  319.                     end
  320.                     url = urlSplit[1] .. "/" .. table.concat(pathSplit, "/")
  321.                     local req = request
  322.                     if #urlSplit == 1 then
  323.                         req["path"] = "/"
  324.                     else
  325.                         req["path"] = "/" .. table.concat(urlSplit, "/", 2, #urlSplit)
  326.                     end
  327.                     rednet.send(serverId, textutils.serialize(req), "http")
  328.                     _, msg = rednet.receive("http", 5)
  329.                     page = textutils.unserialize(msg)
  330.                 end
  331.             end
  332.                
  333.  
  334.             -- refresh
  335.             term.setBackgroundColor(colors.white)
  336.             term.clear()
  337.  
  338.             drawLowerBar()
  339.             drawLineSelector()
  340.             formatAndDisplayPage(page["body"])
  341.         end
  342.     end
  343. end
  344.  
  345. local function handleExit()
  346.     while true do
  347.         local event = os.pullEventRaw("terminate")
  348.         if(event == "terminate") then
  349.             rednet.close()
  350.             term.setBackgroundColor(colors.black)
  351.             term.setTextColor(colors.white)
  352.             term.clear()
  353.             break
  354.         end
  355.     end
  356. end
  357.  
  358. parallel.waitForAny(handleUserClicks, handleExit)
  359.  
  360. resetColor()
Advertisement
Add Comment
Please, Sign In to add comment