Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local ex = require("/lib/exceptions")
- local str = require("/lib/string")
- local url = ""
- local urlSplit = {}
- local isRefreshPressed = false
- local w
- local h
- local selectedLine = -1
- local serverId = -1
- local redirectLines = {}
- local page = ""
- local formatColors = {
- ["0"] = colors.white,
- ["1"] = colors.orange,
- ["2"] = colors.magenta,
- ["3"] = colors.lightBlue,
- ["4"] = colors.yellow,
- ["5"] = colors.lime,
- ["6"] = colors.pink,
- ["7"] = colors.gray,
- ["8"] = colors.lightGray,
- ["9"] = colors.cyan,
- ["a"] = colors.purple,
- ["b"] = colors.blue,
- ["c"] = colors.brown,
- ["d"] = colors.green,
- ["e"] = colors.red,
- ["f"] = colors.black
- }
- local request = {
- ["method"] = "GET",
- ["path"] = "",
- ["body"] = {},
- ["headers"] = {
- ["User-Agent"] = "DiamondBrowser 0.0.1"
- }
- }
- local function resetColor()
- term.setBackgroundColor(colors.black)
- term.setTextColor(colors.white)
- term.setCursorPos(1, 1)
- end
- local function drawLowerBar()
- term.setCursorPos(1, h)
- term.setBackgroundColor(colors.lightGray)
- term.setTextColor(colors.black)
- term.clearLine()
- term.setCursorPos(2, h)
- term.setBackgroundColor(colors.gray)
- term.setTextColor(colors.white)
- term.write(url)
- term.setBackgroundColor(colors.lightGray)
- term.setTextColor(colors.black)
- term.setCursorPos(w - 11, h)
- if isRefreshPressed then
- term.setBackgroundColor(colors.gray)
- term.setTextColor(colors.white)
- term.write("[ refresh ]")
- term.setBackgroundColor(colors.lightGray)
- term.setTextColor(colors.black)
- else
- term.write(" refresh ")
- end
- end
- local function drawBox(x, y, width, height, color)
- local originColor = term.getBackgroundColor()
- ex.try(function ()
- term.setBackgroundColor(color)
- for i = x, x + width do
- for j = y, y + height do
- term.setCursorPos(i, j)
- write(" ")
- end
- end
- end, function(e)
- ex.print_err(e)
- end)
- term.setBackgroundColor(originColor)
- end
- local function drawLineSelector()
- for i = 1, h - 1 do
- term.setCursorPos(1, i)
- if i == selectedLine then
- term.setBackgroundColor(colors.gray)
- term.setTextColor(colors.white)
- term.write(">")
- term.setBackgroundColor(colors.lightGray)
- term.setTextColor(colors.black)
- else
- term.write(" ")
- end
- end
- end
- local function drawAndHandleUrlInput()
- drawBox(8, 7, 36, 6, colors.lightGray)
- drawBox(10, 10, 32, 0, colors.gray)
- term.setTextColor(colors.white)
- term.setBackgroundColor(colors.gray)
- term.setCursorPos(10, 10)
- url = read()
- term.setTextColor(colors.black)
- term.setBackgroundColor(colors.gray)
- end
- --[[
- Formatting will be done by simple tags
- <b1> will effect background color
- I'll call it <bN> tag, where N is a ket from formatColors table
- There is also <fN> tag, which will effect on text color
- there is more complex <lnk> tag that can be either <lnk loc:path/to/page>
- or <lnk glb:website.com/path/to/page> and effects all line
- ]]
- local function formatAndDisplayPage(page)
- term.setCursorPos(2, 1)
- local bg = term.getBackgroundColor()
- local fg = term.getTextColor()
- term.setBackgroundColor(colors.white)
- term.setTextColor(colors.black)
- local toSkip = 0
- local skip = false
- local line = 1
- for idx = 1, #page do
- local char = page:sub(idx, idx)
- if skip then
- if toSkip > 0 then
- toSkip = toSkip - 1
- goto continue
- else
- skip = false
- end
- end
- if char == "\n" then
- local _, y = term.getCursorPos()
- term.setCursorPos(2, y + 1)
- line = line + 1
- else
- if char == "<" then
- if page:sub(idx + 1, idx + 1) == "f" then
- local color = formatColors[page:sub(idx + 2, idx + 2)]
- if color ~= nil then
- term.setTextColor(color)
- toSkip = 3 -- count closing '>'
- skip = true
- goto continue
- end
- elseif page:sub(idx + 1, idx + 1) == "b" then
- local color = formatColors[page:sub(idx + 2, idx + 2)]
- if color ~= nil then
- term.setBackgroundColor(color)
- toSkip = 3 -- count closing '>'
- skip = true
- goto continue
- end
- -- stucture must be <lnk glb:someweb.com/path/to/page>
- -- or <lnk loc:path/to/page>
- elseif page:sub(idx + 1, idx + 3) == "lnk" then
- local isGlobal = page:sub(idx + 5, idx + 7) == "glb"
- local path = ""
- local amount = 0
- for i = idx + 9, #page do
- if page:sub(i, i) == ">" then
- break
- else
- path = path .. page:sub(i, i)
- end
- amount = amount + 1
- end
- redirectLines[line] = {path = path, isGlobal = isGlobal}
- toSkip = amount + 9
- skip = true
- goto continue
- end
- end
- term.write(char)
- end
- ::continue::
- end
- term.setBackgroundColor(bg)
- term.setTextColor(fg)
- end
- w, h = term.getSize()
- -- set background
- term.setBackgroundColor(colors.white)
- term.clear()
- drawLowerBar()
- drawLineSelector()
- drawAndHandleUrlInput()
- rednet.open("top")
- urlSplit = str.split(url, "/")
- rednet.broadcast(urlSplit[1], "__ddns_lookup")
- _, msg = rednet.receive("__ddns_lookup", 5)
- if msg == nil or msg == -1 then
- drawLowerBar()
- drawLineSelector()
- term.setCursorPos(2, 1)
- term.setTextColor(colors.black)
- term.write("Cannot resolve address! (Is url correct?)")
- else
- serverId = msg
- drawLowerBar()
- drawLineSelector()
- term.setCursorPos(2, 1)
- term.setTextColor(colors.black)
- term.write("Connecting...")
- local req = request
- if #urlSplit == 1 then
- req["path"] = "/"
- else
- req["path"] = "/" .. table.concat(urlSplit, "/", 2, #urlSplit)
- end
- rednet.send(serverId, textutils.serialize(req), "http")
- _, msg = rednet.receive("http", 5)
- page = textutils.unserialize(msg)
- term.setBackgroundColor(colors.white)
- term.clear()
- drawLowerBar()
- drawLineSelector()
- formatAndDisplayPage(page["body"])
- end
- local function handleUserClicks()
- while true do
- local event, button, x, y = os.pullEvent("mouse_click")
- if(event == "mouse_click") and button == 1 then
- selectedLine = y
- isRefreshPressed = false
- if y == h and x >= w - 12 and x <= w -1 then
- isRefreshPressed = true
- local req = request
- if #urlSplit == 1 then
- req["path"] = "/"
- else
- req["path"] = "/" .. table.concat(urlSplit, "/", 2, #urlSplit)
- end
- rednet.send(serverId, textutils.serialize(req), "http")
- _, msg = rednet.receive("http", 5)
- page = textutils.unserialize(msg)
- elseif y == h and x >= 2 and x <=#url then
- drawAndHandleUrlInput()
- rednet.open("top")
- urlSplit = str.split(url, "/")
- rednet.broadcast(urlSplit[1], "__ddns_lookup")
- _, msg = rednet.receive("__ddns_lookup", 5)
- if msg == nil or msg == -1 then
- page["body"] = "Cannot resolve address! (Is url correct?)"
- else
- serverId = msg
- local req = request
- if #urlSplit == 1 then
- req["path"] = "/"
- else
- req["path"] = "/" .. table.concat(urlSplit, "/", 2, #urlSplit)
- end
- rednet.send(serverId, textutils.serialize(req), "http")
- _, msg = rednet.receive("http", 5)
- page = textutils.unserialize(msg)
- end
- elseif redirectLines[y] ~= nil then
- if redirectLines[y].isGlobal then
- url = redirectLines[y].path
- rednet.open("top")
- urlSplit = str.split(url, "/")
- rednet.broadcast(urlSplit[1], "__ddns_lookup")
- _, msg = rednet.receive("__ddns_lookup", 5)
- if msg == nil or msg == -1 then
- page["body"] = "Cannot resolve address! (Is url correct?)"
- else
- serverId = msg
- local req = request
- if #urlSplit == 1 then
- req["path"] = "/"
- else
- req["path"] = "/" .. table.concat(urlSplit, "/", 2, #urlSplit)
- end
- rednet.send(serverId, textutils.serialize(req), "http")
- _, msg = rednet.receive("http", 5)
- page = textutils.unserialize(msg)
- end
- else
- local pathSplit = str.split(redirectLines[y].path, "/")
- for i = 1, #pathSplit do
- urlSplit[i + 1] = pathSplit[i]
- end
- url = urlSplit[1] .. "/" .. table.concat(pathSplit, "/")
- local req = request
- if #urlSplit == 1 then
- req["path"] = "/"
- else
- req["path"] = "/" .. table.concat(urlSplit, "/", 2, #urlSplit)
- end
- rednet.send(serverId, textutils.serialize(req), "http")
- _, msg = rednet.receive("http", 5)
- page = textutils.unserialize(msg)
- end
- end
- -- refresh
- term.setBackgroundColor(colors.white)
- term.clear()
- drawLowerBar()
- drawLineSelector()
- formatAndDisplayPage(page["body"])
- end
- end
- end
- local function handleExit()
- while true do
- local event = os.pullEventRaw("terminate")
- if(event == "terminate") then
- rednet.close()
- term.setBackgroundColor(colors.black)
- term.setTextColor(colors.white)
- term.clear()
- break
- end
- end
- end
- parallel.waitForAny(handleUserClicks, handleExit)
- resetColor()
Advertisement
Add Comment
Please, Sign In to add comment