Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --[[ Midday Navigator Ver. 1.0
- Created by Dimus
- No rights reserved ]]
- os.loadAPI('ip')
- if not term.isColour() then
- print( "Use advanced computer for view hypertext" )
- return
- end
- local w,h = term.getSize()
- local scrollX, scrollY = 0,0
- local tLines = {}
- local maxLen=0
- local bRunning = true
- -- Colours
- local highlightColour, textColour, bgColour, tagColour
- bgColour = colours.black
- textColour = colours.white
- highlightColour = colours.yellow
- tagColour = colours.cyan
- -- Menus
- local bMenu = false
- local nMenuItem = 1
- local tMenuItems = {"Back","Save","Exit"}
- local sTitle = ""
- local Site = ""
- local Host = ""
- local History = {}
- local objects={}
- local tags={}
- local function tagWork(tag)
- local tag_name=tag:match('[/%w_]+')
- if tags[tag_name] then
- local tag_params={}
- for k,v in tag:gmatch('(%w+)=([%w/_]+)') do tag_params[k]=v end
- tags[tag_name](tag_params)
- else
- term.write( '<'..tag..'>' )
- end
- end
- local function writeHighlighted( sLine )
- local tag,txt
- while string.len(sLine) > 0 do
- -- search tag
- tag,txt=sLine:match("^<(.-)>(.*)")
- while tag do
- sLine=txt
- tagWork( tag )
- tag,txt=sLine:match("^<(.-)>(.*)")
- end
- txt,tag=sLine:match("(.-)(<.->.*)")
- if txt then
- sLine=tag
- else
- txt,sLine=sLine,''
- end
- term.write( txt )
- end
- local lineEnd=term.getCursorPos()
- if lineEnd>maxLen then maxLen=lineEnd end
- end
- local function redrawText()
- maxLen=0
- objects={}
- term.setTextColour( textColour )
- for y=1,h-2 do
- term.setCursorPos( 1 - scrollX, y + 2 )
- term.clearLine()
- local sLine = tLines[ y + scrollY ]
- if sLine ~= nil then
- writeHighlighted( sLine )
- end
- end
- -- local x,y = 1 - scrollX, 3
- -- term.setCursorPos( x, y )
- -- term.clearLine()
- -- local sLine = tLines[ y + scrollY ]
- -- if sLine ~= nil then
- -- writeHighlighted( sLine )
- -- end
- -- x,y=term.getCursorPos
- end
- local function redrawMenu()
- term.setBackgroundColour( colours.blue )
- term.setCursorPos( 1, 1 )
- term.clearLine()
- local sLeft
- local nLeftColour, nLeftHighlight1, nLeftHighlight2
- if bMenu then
- local sMenu = ""
- for n,sItem in ipairs( tMenuItems ) do
- if n == nMenuItem then
- nLeftHighlight1 = sMenu:len() + 1
- nLeftHighlight2 = sMenu:len() + sItem:len() + 2
- end
- sMenu = sMenu.." "..sItem.." "
- end
- sLeft = sMenu
- nLeftColour = textColour
- else
- sLeft = sTitle
- if #sLeft>0 then sLeft=sLeft.." - " end
- sLeft = sLeft.."Midday Navigator"
- nLeftColour = highlightColour
- end
- if sLeft then
- term.setCursorPos( 1, 1 )
- term.setTextColour( nLeftColour )
- term.write(sLeft)
- if nLeftHighlight1 then
- term.setTextColour( highlightColour )
- term.setCursorPos( nLeftHighlight1, 1 )
- term.write( "[" )
- term.setCursorPos( nLeftHighlight2, 1 )
- term.write( "]" )
- end
- end
- term.setCursorPos( w-2 , 1 )
- term.write( "< " )
- term.setBackgroundColour( colours.red )
- term.write( "X" )
- term.setBackgroundColour( colours.white )
- term.setTextColour( colours.black )
- term.setCursorPos( 2, 2 )
- term.clearLine()
- term.write( Host..'/'..Site )
- term.setTextColour( textColour )
- term.setBackgroundColour( bgColour )
- end
- local function load(_sPath)
- if _sPath=='' or _sPath ==nil then redrawMenu() return end
- tLines = {}
- scrollX, scrollY = 0,0
- Host,Site=_sPath:match('(.-)/(.*)')
- if not Host then Host=_sPath Site='' end
- if Host=='' then
- local file = io.open( Site, "r" )
- if file then
- local sLine = file:read()
- while sLine do
- table.insert( tLines, sLine )
- sLine = file:read()
- end
- file:close()
- else
- table.insert( tLines, 'Can not open file "'..Site..'"' )
- end
- else
- if ip.open() then
- if ip.getIP() then
- ip.send(Host,Site)
- print(Host,' <',Site,'>')
- local addr,sLines
- -- repeat
- sLines,addr=ip.receive(3)
- if not addr then sLines='No ansver from server' end
- -- until addr==Host
- sLines=sLines..'\n'
- for sLine in sLines:gmatch('(.-)\n') do
- table.insert( tLines, sLine )
- end
- else
- table.insert( tLines, 'Can not connect to Craftnet' )
- end
- else
- table.insert( tLines, 'Can not open modem' )
- end
- end
- if #tLines == 0 then
- table.insert( tLines, "" )
- end
- if History[#History]~=_sPath then table.insert( History, _sPath ) end
- term.setBackgroundColour( bgColour )
- term.clear()
- term.setCursorBlink( false )
- redrawText()
- redrawMenu()
- end
- local tMenuFuncs = {
- Back=function()
- if #History>1 then
- History[#History]=nil
- load(History[#History])
- end
- end,
- Save=function()
- end,
- Exit=function()
- bRunning = false
- end
- }
- local function doMenuItem( _n )
- tMenuFuncs[tMenuItems[_n]]()
- bMenu = false
- end
- -- Tags
- tags['font']=function(arg)
- if arg.color and colours[arg.color] then
- term.setTextColour( colours[arg.color] )
- end
- if arg.bgcolor and colours[arg.bgcolor] then
- term.setBackgroundColour(colours[arg.bgcolor])
- end
- end
- tags['/font']=function()
- term.setTextColour( textColour )
- term.setBackgroundColour( bgColour )
- 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 then
- if x<=obj.x2 or y<obj.y2 then
- obj:work()
- return true
- end
- end
- end
- return false
- end
- local function href_work(obj)
- load(obj.href)
- end
- local function ref_work(obj)
- load(Host..'/'..obj.ref)
- end
- tags['a']=function(arg)
- if arg.href then
- term.setTextColour( tagColour )
- local href={check=line_check, work=href_work}
- href.x1, href.y1 = term.getCursorPos()
- href.href = arg.href
- table.insert(objects, href)
- end
- if arg.ref then
- term.setTextColour( tagColour )
- local ref={check=line_check, work=ref_work}
- ref.x1, ref.y1 = term.getCursorPos()
- ref.ref = arg.ref
- table.insert(objects, ref)
- end
- end
- tags['/a']=function()
- term.setTextColour( textColour )
- local href=objects[#objects]
- if href and not href.x2 then
- href.x2, href.y2 = term.getCursorPos()
- href.x2=href.x2 - 1
- end
- end
- --Key events
- local eventKey={}
- local eventMenu={}
- eventKey[keys.up]=function()
- -- Move screen up
- if scrollY > 0 then scrollY = scrollY - 1 end
- end
- eventKey[keys.down]=function()
- -- Move screen down
- if scrollY < #tLines - (h-1) then scrollY = scrollY + 1 end
- end
- eventKey[keys.pageUp]=function()
- -- Move screen up
- scrollY = scrollY - h - 1
- if scrollY<0 then scrollY=0 end
- end
- eventKey[keys.pageUp]=function()
- -- Move screen up
- scrollY = scrollY - h - 1
- if scrollY<0 then scrollY=0 end
- end
- eventKey[keys.pageUp]=function()
- -- Move screen up
- scrollY = scrollY - h - 1
- if scrollY<0 then scrollY=0 end
- end
- eventKey[keys.pageDown]=function()
- -- Move screen up
- scrollY = scrollY + h - 2
- if scrollY>#tLines-h+1 then scrollY=#tLines-h+1 end
- end
- eventKey[keys.home]=function()
- -- Move cursor to the beginning
- scrollX = 0
- end
- eventKey[keys["end"]]=function()
- -- Move cursor to the end
- if maxLen>w then scrollX = scrollX+maxLen-w end
- end
- eventKey[keys.left]=function()
- -- Move screen left
- if scrollX > 0 then scrollX = scrollX - 1 end
- end
- eventKey[keys.right]=function()
- -- Move screen right
- if maxLen > w then scrollX = scrollX + 1 end
- end
- eventKey[keys.leftCtrl]=function()
- -- Menu toggle
- bMenu = not bMenu
- redrawMenu()
- end
- eventKey[keys.rightCtrl]=eventKey[keys.leftCtrl]
- eventMenu[keys.left]=function()
- -- Move menu left
- nMenuItem = nMenuItem - 1
- if nMenuItem < 1 then nMenuItem = #tMenuItems end
- end
- eventMenu[keys.right]=function()
- -- Move menu right
- nMenuItem = nMenuItem + 1
- if nMenuItem > #tMenuItems then nMenuItem = 1 end
- end
- eventMenu[keys.enter]=function()
- -- Menu selection
- doMenuItem( nMenuItem )
- end
- eventMenu[keys.leftCtrl]=function()
- -- Menu toggle
- bMenu = not bMenu
- end
- eventMenu[keys.rightCtrl]=eventMenu[keys.leftCtrl]
- -- Actual program functionality begins
- -- Get file to view
- local tArgs = { ... }
- if #tArgs > 0 then
- local sPath = shell.resolve( tArgs[1] )
- load(sPath)
- end
- term.setBackgroundColour( bgColour )
- term.clear()
- term.setCursorBlink( false )
- redrawText()
- redrawMenu()
- -- Handle input
- while bRunning do
- local sEvent, param, param2, param3 = os.pullEvent()
- if sEvent == "key" then
- if bMenu then
- if eventMenu[param] then eventMenu[param]() redrawMenu() end
- else
- if eventKey[param] then eventKey[param]() redrawText() end
- end
- elseif sEvent == "mouse_click" then
- if not bMenu then
- if param == 1 then
- -- Left click
- local cx,cy = param2, param3
- if cy == 1 then
- if cx==w then tMenuFuncs.Exit() end
- if cx==w-2 then tMenuFuncs.Back() end
- elseif cy==2 then
- term.setBackgroundColour( colours.white )
- term.setTextColour( colours.black )
- term.setCursorPos( 2, 2 )
- term.clearLine()
- load(read(nil, History))
- else
- for i=1,#objects do
- if objects[i].check
- and objects[i].check(objects[i],cx,cy) then break end
- end
- end
- end
- end
- elseif sEvent == "mouse_scroll" then
- if not bMenu then
- if param == -1 then
- -- Scroll up
- if scrollY > 0 then
- scrollY = scrollY - 1
- redrawText()
- end
- elseif param == 1 then
- -- Scroll down
- local nMaxScroll = #tLines - (h-1)
- if scrollY < nMaxScroll then
- scrollY = scrollY + 1
- redrawText()
- end
- end
- end
- end
- end
- -- Cleanup
- term.clear()
- term.setCursorBlink( false )
- term.setCursorPos( 1, 1 )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement