Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local w, h = term.getSize()
- local buffer = window.create(term.current(), 1, 1, w, h)
- local oldTerm = term.redirect(buffer)
- local succ, mess = pcall(function()
- rednet.open("top")
- local history = {}
- local currentSite = 1
- local siteElements = {}
- local programElements = {}
- local onSite = false
- local moveToSite
- serverId = 0
- local getFileFromServer = function(site, serverIp)
- local serverIp = serverIp
- rednet.broadcast(site, "get")
- local siteData = {
- "Error 404",
- "",
- "Site does not exist"
- }
- local startTime = os.clock()
- local timer = os.startTimer(0.5)
- while true do
- local event, senderId, message, protocol = os.pullEventRaw()
- if event == "rednet_message" then
- if protocol == "send"..site and not serverIp or (senderId == serverIp) then
- local serverIp = serverId
- siteData = textutils.unserialize(message)
- break
- end
- elseif event == "timer" then
- break
- end
- end
- os.cancelTimer(timer)
- return siteData, serverIp
- end
- local loadSite
- local newElement = function(elementTab)
- return function(argTab)
- return setmetatable(
- argTab,
- {
- __index = elementTab
- }
- )
- end
- end
- local lastPage = function()
- if history[currentSite-1] then
- currentSite = currentSite - 1
- siteElements, serverIp = loadSite(getFileFromServer(history[currentSite]))
- end
- end
- local nextPage = function()
- if history[currentSite+1] then
- currentSite = currentSite + 1
- siteElements, serverIp = loadSite(getFileFromServer(history[currentSite]))
- end
- end
- local uiElements = {
- button = newElement({
- mouse_click = function(self, button, x, y)
- if self.t then error("test") end
- if x>=self.x and y>=self.y then
- if x<self.x+self.w and y<self.y+self.h then
- if self.clickedFunc then self:clickedFunc() end
- end
- end
- end,
- draw = function(self)
- if self.text then
- self.text:draw()
- end
- end
- }),
- text = newElement({
- draw = function(self)
- term.setCursorPos(self.x, self.y)
- term.setTextColor(self.color)
- if self.backgroundColor then
- term.setBackgroundColor(self.backgroundColor)
- else
- term.setBackgroundColor(colors.white)
- end
- term.write(self.text)
- end
- }),
- image = newElement({
- draw = function(self)
- for lineNum = 1, #self.image do
- local line = self.image[lineNum]
- term.setCursorPos(self.x, lineNum+self.y-1)
- for x = 1, #line do
- local char = string.sub(line, x, x)
- if char == " " then
- local xpos, ypos = term.getCursorPos()
- term.setCursorPos(xpos+1, ypos)
- else
- term.blit(char, char, char)
- end
- end
- end
- end
- }),
- script = newElement({}),
- box = newElement({
- draw = function(self)
- paintutils.drawFilledBox(self.x, self.y, self.x+self.w, self.y+self.h, self.color)
- end
- }),
- line = newElement({
- draw = function(self)
- paintutils.drawLine(self.x, self.y, self.x2, self.y2, self.color)
- end
- })
- }
- local elements = {
- image = function(path)
- local image = getFileFromServer(path)
- local x, y = term.getCursorPos()
- term.setCursorPos(x, y+#image)
- return uiElements.image({image = image, x = x, y = y})
- end,
- text = function(text)
- local x, y = term.getCursorPos()
- term.setCursorPos(x, y+1)
- return uiElements.text({
- text = text, color = term.getTextColor(), x = x, y = y
- })
- end,
- set = function(x, y)
- term.setCursorPos(tonumber(x), tonumber(y))
- end,
- move = function(mx, my)
- local x, y = term.getCursorPos()
- term.setCursorPos(x+mx, y+(my or 0))
- end,
- link = function(path, text)
- local x, y = term.getCursorPos()
- local text = text or path
- term.setCursorPos(x, y+1)
- return {
- uiElements.text({
- text = text, color = colors.blue, x = x, y = y
- }),
- uiElements.button({
- w = #text, h = 1,
- clickedFunc = function(self)
- moveToSite(path)
- end,
- x = x, y = y
- })
- }
- end,
- color = function(color)
- term.setTextColor(colors[color])
- end,
- script = function(path)
- local script = getFileFromServer(path)
- local scriptString = ""
- for lineNum = 1, #script do
- scriptString = scriptString .. script[lineNum].."\n"
- end
- local x, y = term.getCursorPos()
- local returnElement = uiElements.script({
- x = x, y = y,
- path = path,
- serverId = serverId
- })
- local succ
- local func, mess = load(scriptString, path, "t", setmetatable(returnElement,{__index = _G}))
- if func then
- succ, mess = pcall(func)
- if succ then
- return returnElement
- end
- end
- return uiElements.text({
- x = 1, y = h-1,
- text = mess, color = colors.red
- })
- end,
- box = function(w, h)
- local x, y = term.getCursorPos()
- return uiElements.box({
- x = x, y = y, w = w, h = h, color = term.getTextColor()
- })
- end,
- line = function(x2, y2)
- local x, y = term.getCursorPos()
- return uiElements.line({
- x = x, y = y, x2 = x+x2, y2 = y+y2, color = term.getTextColor()
- })
- end
- }
- local newElements = function(tab)
- return setmetatable(tab,
- {
- __index = {
- draw = function(self)
- for elementNum = 1, #self do
- local element = self[elementNum]
- if element[1] then
- self.draw(element)
- else
- if element.draw then
- element:draw()
- end
- end
- end
- end,
- scroll = function(self, move, hard)
- for i = 1, #self do
- if self[i][1] then
- self.scroll(self[i], move, self[i].movable or hard)
- else
- if self[i].y and (self[i].moveable or hard) then
- self[i].y = self[i].y - move
- end
- end
- end
- end,
- update = function(self, event, ...)
- if event == "mouse_scroll" then
- if self.scroll then
- self:scroll(({...})[1])
- end
- end
- for elementNum = 1, #self do
- local element = self[elementNum]
- if element[1] then
- self.update(element, event, ...)
- else
- if element.update then
- element:update(event, ...)
- end
- if element[event] then
- element[event](element, ...)
- end
- end
- end
- end
- }
- }
- )
- end
- local searchElements = newElements({movable=true})
- loadSite = function(siteData)
- local siteElements = newElements({})
- term.setCursorPos(2, 2)
- for lineNum = 1, #siteData do
- local line = siteData[lineNum]
- local element = string.match(line, "^%S+")
- local arguments = {}
- if string.sub(line, 1, 1) == " " then
- element = "text"
- arguments = {string.sub(line, 2)}
- elseif not element then
- element = "text"
- arguments = {line}
- else
- for arg in string.gmatch(string.sub(line, #element+2), "[^,]+") do
- table.insert(arguments, arg)
- end
- end
- if elements[element] then
- table.insert(siteElements, elements[element](table.unpack(arguments)))
- end
- end
- siteElements:scroll(-1, true)
- return siteElements
- end
- moveToSite = function(site)
- for i = currentSite+1, #history do
- table.remove(history, i)
- end
- currentSite = currentSite + 1
- table.insert(history, site)
- siteElements, serverId = loadSite(getFileFromServer(site))
- end
- local search = function(str)
- if str ~= "" then
- searchElements = {}
- onSite = false
- rednet.broadcast(str, "find")
- local results = {}
- os.startTimer(0.1)
- while true do
- local event, var1, var2, var3 = os.pullEventRaw()
- if event == "rednet_message" and var3 == "findResult" then
- local siteResults = textutils.unserialize(var2)
- for i = 1, #siteResults do
- table.insert(results, siteResults[i])
- end
- elseif event == "timer" then
- break
- end
- end
- local sortedResults = table.sort(
- results,
- function(list1, list2)
- return list1.num>list2.num
- end
- )
- local y = 4
- for i = 1, #results do
- if results[i].num > 0 then
- local name = string.match(results[i].page, "[^/]*$")
- if name == "website" then
- name = string.match(results[i].page, "[^'mc.'']%a*.sol")
- end
- name = string.upper(string.sub(name, 1, 1))..string.sub(name, 2)
- local siteText = getFileFromServer(results[i].page)
- local tipText = ""
- local altTipText = ""
- for i = 1, #siteText do
- if string.sub(siteText[i], 1, 1) == " " then
- local line = string.sub(siteText[i], 2)
- tipText = string.match(line, ".*"..str..".*") or tipText
- altTipText = string.match(string.lower(line), ".*"..string.lower(str)..".*") or altTipText
- end
- end
- if tipText == "" then tipText = altTipText end
- table.insert(
- searchElements,
- {
- movable = true,
- uiElements.box({
- x = 3, y = y, w = 40, h = 2, color = colors.lightGray
- }),
- uiElements.button({
- x = 3, y = y, w = 40, h = 3, clickedFunc = function()
- searchElements = newElements({})
- onSite = true
- moveToSite(results[i].page)
- end
- }),
- uiElements.text({
- x = 3, y = y,
- text = name,
- color = colors.black,
- backgroundColor = colors.lightGray
- }),
- uiElements.text({
- x = 3, y = y+1,
- text = results[i].page,
- color = colors.gray,
- backgroundColor = colors.lightGray
- }),
- uiElements.text({
- x = 3, y = y+2,
- text = string.sub(tipText, 1, 41),
- color = colors.gray,
- backgroundColor = colors.lightGray
- })
- }
- )
- y = y + 4
- end
- end
- table.insert(searchElements,
- uiElements.text({
- x=1, y = 2, color = colors.gray,
- text = "Search had "..#searchElements.." results",
- }))
- searchElements = newElements(searchElements)
- end
- end
- programElements = newElements({
- uiElements.line({
- x = 1, y = 1, x2 = w, y2 = 1, color = colors.gray
- }),
- uiElements.button({
- x = 2, y = 1, w = 3, h = 1,
- text = uiElements.text({
- x = 2, y = 1, text = " < ", color = colors.gray, backgroundColor = colors.lightGray
- }),
- clickedFunc = function()
- lastPage()
- end
- }),
- uiElements.button({
- x = 6, y = 1, w = 3, h = 1,
- text = uiElements.text({
- x = 6, y = 1, text = " > ", color = colors.gray, backgroundColor = colors.lightGray
- }),
- clickedFunc = function()
- nextPage()
- end
- }),
- uiElements.button({
- x = 11, y = 1, w = 20, h = 1,
- text = uiElements.text({
- x = 11, y = 1, text = "search... ", color = colors.gray, backgroundColor = colors.lightGray
- }),
- clickedFunc = function(self)
- term.setCursorPos(self.x, self.y)
- term.setBackgroundColor(colors.lightGray)
- term.setTextColor(colors.black)
- search(io.read())
- end
- }),
- uiElements.button({
- x = w, y = 1, h = 1, w = 1,
- clickedFunc = function(self)
- term.setBackgroundColor(colors.black)
- term.clear()
- term.setCursorPos(1, 1)
- error("", 0)
- end,
- text = uiElements.text({
- x = w, y = 1, text = "X", color = colors.orange, backgroundColor = colors.red
- }),
- })
- })
- while true do
- buffer.setVisible(false)
- term.setBackgroundColor(colors.white)
- term.clear()
- if onSite then
- siteElements:draw()
- else
- searchElements:draw()
- end
- programElements:draw()
- buffer.setVisible(true)
- local event, var1, var2, var3 = os.pullEventRaw()
- if event == "mouse_scroll" then
- if onSite then
- siteElements:scroll(var1, true)
- else
- programElements:scroll(var1)
- end
- end
- if event == "key" then
- local key = keys.getName(var1)
- if key == "left" then
- lastPage()
- elseif key == "right" then
- nextPage()
- end
- if onSite and key == "r" then
- --moveToSite(history[currentSite])
- end
- end
- programElements:update(event, var1, var2, var3)
- if onSite then
- siteElements:update(event, var1, var2, var3)
- else
- searchElements:update(event, var1, var2, var3)
- end
- end
- end)
- term.redirect(oldTerm)
- buffer.setVisible(false)
- term.setBackgroundColor(colors.black)
- term.setTextColor(colors.red)
- term.clear()
- term.setCursorPos(1, 1)
- print(mess)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement