Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --------------------------------------------------
- -- Midday Navigator. Браузер для сети OpenNet --
- -- Автор: Zer0Galaxy --
- --------------------------------------------------
- gpu = require("component").gpu
- kbd = require("component").keyboard
- computer=require("computer")
- fs = require("filesystem")
- text = require("text")
- term = require("term")
- wlen = require("unicode").wlen
- event= require("event")
- on = require("opennet")
- local cursorX, cursorY = 1, 1
- local ShiftX = 0
- local ShiftY = 0
- local Left,Top = 1, 3
- local WinW, WinH = gpu.getResolution()
- WinH=WinH-Top+1
- local Caption="Midday Navigator 1.0"
- local Site=""
- local txColour = 0xFFFFFF
- local bgColour = 0x000000
- local tagColour = 0x0080FF
- local History = {}
- local object={}
- function winhead()
- gpu.setForeground(0xFFFFFF)
- gpu.setBackground(0x0000FF)
- gpu.fill(1,1, WinW,1, " ") gpu.set(2,1, Caption)
- gpu.setBackground(0x00FF00) gpu.set(WinW-8,1, " S ")
- gpu.setBackground(0x0080FF) gpu.set(WinW-5,1, " < ")
- gpu.setBackground(0xFF0000) gpu.set(WinW-2,1, " X ")
- gpu.setForeground(0x000000)
- gpu.setBackground(0xFFFFFF)
- gpu.fill(1,2, WinW,1, " ")
- gpu.set(2,2, Site)
- gpu.setForeground(txColour)
- gpu.setBackground(bgColour)
- end
- function winwrite(value)
- value = text.detab(tostring(value))
- if wlen(value) == 0 then
- return
- end
- local line, nl
- repeat
- if cursorY > WinH then return end
- line, value, nl = text.wrap(value, WinW - (cursorX - 2), WinW)
- if cursorY>=1 then gpu.set(cursorX+Left-1-ShiftX, cursorY+Top-1, line) end
- cursorX = cursorX + wlen(line)
- if nl or (cursorX > WinW) then
- cursorX = 1
- cursorY = cursorY + 1
- end
- until not value
- end
- function winclear()
- object={}
- gpu.fill(Left, Top, WinW, WinH, " ")
- cursorX, cursorY = 1, 1
- end
- tags={}
- tags['html']=function(arg)
- if arg.caption then Caption=arg.caption winhead() end
- local param=tonumber(arg.color)
- if param then txColour = param gpu.setForeground(param) end
- param=tonumber(arg.bgcolor)
- if param then bgColour = param gpu.setBackground(param) winclear() end
- end
- tags['font']=function(arg)
- local param=tonumber(arg.color)
- if param then gpu.setForeground(param) end
- param=tonumber(arg.bgcolor)
- if param then gpu.setBackground(param) end
- end
- tags['/font']=function()
- gpu.setForeground( txColour )
- gpu.setBackground( bgColour )
- end
- tags['cr']=function()
- -- if cursorX>1 then
- cursorY=cursorY+1
- cursorX=1
- -- end
- end
- tags['---']=function()
- if cursorX>1 then cursorY=cursorY+1 cursorX=1 end
- winwrite(string.rep('─',WinW))
- end
- tags['===']=function()
- if cursorX>1 then cursorY=cursorY+1 cursorX=1 end
- winwrite(string.rep('═',WinW))
- end
- local function line_check(obj,x,y)
- if y>=obj.y1 and y<=obj.y2 then
- if (x>=obj.x1 or y>obj.y1) and (x<=obj.x2 or y<obj.y2) then return true end
- end
- end
- local function bar_check(obj,x,y)
- return y>=obj.y1 and y<=obj.y2 and x>=obj.x1 and x<=obj.x2
- end
- local function ref_work(obj) load(obj.ref) end
- tags['a']=function(arg)
- if arg.ref then
- table.insert(object, {check=line_check,
- x1=cursorX+Left-1-ShiftX, y1=cursorY+Top-1,
- work=ref_work, ref = arg.ref,
- col=gpu.getForeground()})
- end
- local color=tonumber(arg.color) or tagColour
- gpu.setForeground(color)
- end
- tags['/a']=function()
- local ref=object[#object]
- if ref and not ref.x2 then
- gpu.setForeground(ref.col or txColour)
- ref.x2, ref.y2 = cursorX+Left-2-ShiftX, cursorY+Top-1
- end
- end
- function tagWork(tag)
- local name=tag:match('%S+')
- if tags[name] then
- local params={}
- for k,v in tag:gmatch('(%w+)=([^%s"]+)') do params[k]=v end
- for k,v in tag:gmatch('(%w+)="([^"]+)"') do params[k]=v end
- tags[name](params)
- else
- winwrite( '<'..tag..'>' )
- end
- end
- function winline(line)
- if line then
- cursorY=line.Y-ShiftY
- cursorX=line.X
- local sLine=line.text
- while string.len(sLine) > 0 do
- local p1,p2
- p1=sLine:find("<")
- if p1 then p2=sLine:find(">",p1) end
- if p2 then
- winwrite(sLine:sub(1,p1-1))
- tagWork(sLine:sub(p1+1,p2-1))
- sLine=sLine:sub(p2+1)
- else
- winwrite(sLine)
- sLine=""
- end
- end
- if cursorY <= WinH then return true end
- end
- end
- function htmltext()
- winclear()
- local line=1
- for i=#lines,1,-1 do
- if lines[i].Y<=ShiftY then line=i break end
- end
- while winline(lines[line]) do
- line=line+1
- if lines[line] then lines[line].Y=ShiftY+cursorY lines[line].X=cursorX end
- end
- end
- function codetext()
- winclear()
- for i=1,WinH do
- if lines[i+ShiftY] then gpu.set(1-ShiftX,i+Top-1,lines[i+ShiftY].text)
- else break end
- end
- end
- wintext=htmltext
- function winshift(shX,shY)
- ShiftX=ShiftX+shX
- ShiftY=ShiftY+shY
- if ShiftX<0 then ShiftX=0 end
- if ShiftY<0 then ShiftY=0 end
- wintext()
- end
- function load(sPath)
- if sPath=='' or sPath==nil or sPath=="\n" then winhead() return end
- if sPath:sub(-1)=="\n" then sPath=sPath:sub(1,-2) end
- lines={}
- ShiftX=0
- ShiftY=0
- Caption="Midday Navigator 1.0"
- txColour = 0xFFFFFF
- bgColour = 0x000000
- if sPath:sub(1,1)=="." then
- while sPath:sub(1,1)=="." do
- Site=Site:match("(.+)/.*") or Site
- sPath=sPath:sub(2)
- end
- Site=Site..sPath
- else
- Site=sPath
- end
- local host,doc=Site:match('(.-)/(.*)')
- if not host then host=Site doc=nil end
- if host=="" then
- local file=io.open(Site,"r")
- if file then
- for line in file:lines() do
- lines[#lines+1]={X=1,Y=math.huge,text=line}
- end
- file:close()
- else
- lines[1]={X=5,Y=1,text="<html>Файл <font color=0xFF0000>"..Site.."</font> не найден"}
- end
- else
- local MyIP,err=on.getIP()
- if MyIP then
- on.send(host,"get",doc)
- local adr,com,text
- repeat
- adr,com,text=on.receive(3)
- until com=="get" or not com
- if com=="get" then
- text=tostring(text)
- local line=1
- while #text>0 do
- local p=text:find("\n")
- if p then
- lines[line],text={X=1,Y=math.huge,text=text:sub(1,p-1)}, text:sub(p+1)
- else
- lines[line],text={X=1,Y=math.huge,text=text}, ""
- end
- line=line+1
- end
- else
- if adr then
- lines[1]={X=1,Y=math.huge,text="Ответ от узла "..tostring(adr)}
- lines[2]={X=1,Y=math.huge,text=tostring(text)}
- else
- lines[1]={X=1,Y=math.huge,text="Таймаут ожидания"}
- end
- end
- else
- lines[1]={X=5,Y=1,text="<html>Ошибка подключения к сети OpenNet: <font color=0xFF0000>"..err.."</font>"}
- end
- end
- wintext=codetext
- if lines[1] then
- lines[1].Y=1
- if string.find(lines[1].text,"<%s*html.*>") then wintext=htmltext end
- end
- if History[#History]~=Site then table.insert( History, Site ) end
- winhead()
- wintext()
- end
- local work=true
- function read()
- gpu.setForeground( 0x000000 )
- gpu.setBackground( 0xFFFFFF )
- term.setCursor( 1, 2 )
- term.clearLine()
- term.setCursor( 2, 2 )
- load(term.read(History,false))
- end
- function save()
- if kbd then
- gpu.setForeground( 0x000000 )
- gpu.setBackground( 0xFFFFFF )
- term.setCursor( 1, 2 ) term.clearLine()
- term.setCursor( 2, 2 )
- term.write("Safe as: ")
- computer.pushSignal("key_down",kbd.address,0,200)
- local Name=term.read({"/download/"..Site:match("[^/]*$")},false):sub(1,-2)
- if Name~="" then
- if not fs.exists("download") then fs.makeDirectory("download") end
- local file=io.open(Name,"w")
- if file then
- for i=1,#lines-1 do file:write(lines[i].text.."\n") end
- file:write(lines[#lines].text)
- file:close()
- else
- term.clearLine()
- term.setCursor( 2, 2 )
- term.write("File "..Name.." can't be saved")
- computer.beep()
- os.sleep(2)
- end
- end
- winhead()
- end
- end
- function back()
- if #History>1 then
- History[#History]=nil
- load(History[#History])
- end
- end
- function quit()
- gpu.setForeground( 0xFFFFFF )
- gpu.setBackground( 0x000000 )
- term.clear()
- work=false
- end
- do
- local param=...
- if param then load(param) else winhead() read() end
- end
- local Sh=false
- while work do
- local ev, _, X, Y, S=event.pull()
- if ev=="scroll" and Y>=Top then
- if Sh then winshift(-5*S,0)
- else winshift(0,-3*S) end
- end
- if ev=="key_down" then
- if Y==208 then winshift(0,3) end
- if Y==200 then winshift(0,-3) end
- if Y==203 then winshift(-5,0) end
- if Y==205 then winshift(5,0) end
- if Y==29 then Sh=true end
- if Y==15 then read() end
- if Sh and Y==31 then save() end
- if Y==14 then back() end
- if Sh and Y==17 then quit() end
- end
- if ev=="key_up" and Y==29 then Sh=false end
- if ev=="touch" and S==0 then
- if Y==1 then
- if X>=WinW-8 and X<=WinW-6 then save() end
- if X>=WinW-5 and X<=WinW-3 then back() end
- if X>=WinW-2 then quit() end
- elseif Y==2 then read()
- else
- for i=1,#object do
- if object[i]:check(X,Y) then object[i]:work() break end
- end
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement