Advertisement
ZNZNCOOP

Browser

Apr 15th, 2014
215
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 10.13 KB | None | 0 0
  1. --[[ Midday Navigator Ver. 1.0
  2.      Created by Dimus
  3.      No rights reserved ]]
  4.  
  5. os.loadAPI('ip')
  6.  
  7. if not term.isColour() then
  8.     print( "Use advanced computer for view hypertext" )
  9.     return
  10. end
  11.  
  12. local w,h = term.getSize()
  13. local scrollX, scrollY = 0,0
  14.  
  15. local tLines = {}
  16. local maxLen=0
  17. local bRunning = true
  18.  
  19. -- Colours
  20. local highlightColour, textColour, bgColour, tagColour
  21.     bgColour = colours.black
  22.     textColour = colours.white
  23.     highlightColour = colours.yellow
  24.     tagColour = colours.cyan
  25.  
  26. -- Menus
  27. local bMenu = false
  28. local nMenuItem = 1
  29. local tMenuItems = {"Back","Save","Exit"}
  30. local sTitle = ""
  31. local Site = ""
  32. local Host = ""
  33. local History = {}
  34. local objects={}
  35.  
  36. local tags={}
  37.  
  38. local function tagWork(tag)
  39.     local tag_name=tag:match('[/%w_]+')
  40.     if tags[tag_name] then
  41.         local tag_params={}
  42.         for k,v in tag:gmatch('(%w+)=([%w/_]+)') do tag_params[k]=v end
  43.         tags[tag_name](tag_params)
  44.     else
  45.         term.write( '<'..tag..'>' )
  46.     end
  47. end
  48.  
  49. local function writeHighlighted( sLine )
  50.     local tag,txt
  51.     while string.len(sLine) > 0 do
  52.         -- search tag
  53.         tag,txt=sLine:match("^<(.-)>(.*)")
  54.         while tag do
  55.             sLine=txt
  56.             tagWork( tag )
  57.             tag,txt=sLine:match("^<(.-)>(.*)")
  58.         end
  59.         txt,tag=sLine:match("(.-)(<.->.*)")
  60.         if txt then
  61.             sLine=tag
  62.         else
  63.             txt,sLine=sLine,''
  64.         end
  65.         term.write( txt )
  66.     end
  67.     local lineEnd=term.getCursorPos()
  68.     if lineEnd>maxLen then maxLen=lineEnd end
  69. end
  70.  
  71. local function redrawText()
  72.     maxLen=0
  73.     objects={}
  74.     term.setTextColour( textColour )
  75.     for y=1,h-2 do
  76.         term.setCursorPos( 1 - scrollX, y + 2 )
  77.         term.clearLine()
  78.  
  79.         local sLine = tLines[ y + scrollY ]
  80.         if sLine ~= nil then
  81.             writeHighlighted( sLine )
  82.         end
  83.     end
  84. --    local x,y = 1 - scrollX, 3
  85. --  term.setCursorPos( x, y )
  86. --  term.clearLine()
  87. --  local sLine = tLines[ y + scrollY ]
  88. --  if sLine ~= nil then
  89. --      writeHighlighted( sLine )
  90. --  end
  91. --    x,y=term.getCursorPos
  92.  
  93. end
  94.  
  95. local function redrawMenu()
  96.     term.setBackgroundColour( colours.blue )
  97.     term.setCursorPos( 1, 1 )
  98.     term.clearLine()
  99.  
  100.     local sLeft
  101.     local nLeftColour, nLeftHighlight1, nLeftHighlight2
  102.     if bMenu then
  103.         local sMenu = ""
  104.         for n,sItem in ipairs( tMenuItems ) do
  105.             if n == nMenuItem then
  106.                 nLeftHighlight1 = sMenu:len() + 1
  107.                 nLeftHighlight2 = sMenu:len() + sItem:len() + 2
  108.             end
  109.             sMenu = sMenu.." "..sItem.." "
  110.         end
  111.         sLeft = sMenu
  112.         nLeftColour = textColour
  113.     else
  114.         sLeft = sTitle
  115.         if #sLeft>0 then sLeft=sLeft.." - " end
  116.         sLeft = sLeft.."Midday Navigator"
  117.         nLeftColour = highlightColour
  118.     end
  119.    
  120.     if sLeft then
  121.         term.setCursorPos( 1, 1 )
  122.         term.setTextColour( nLeftColour )
  123.         term.write(sLeft)      
  124.         if nLeftHighlight1 then
  125.             term.setTextColour( highlightColour )
  126.             term.setCursorPos( nLeftHighlight1, 1 )
  127.             term.write( "[" )
  128.             term.setCursorPos( nLeftHighlight2, 1 )
  129.             term.write( "]" )
  130.         end
  131.     end
  132.  
  133.     term.setCursorPos( w-2 , 1 )
  134.     term.write( "< " )
  135.     term.setBackgroundColour( colours.red )
  136.     term.write( "X" )
  137.  
  138.     term.setBackgroundColour( colours.white )
  139.     term.setTextColour( colours.black )
  140.     term.setCursorPos( 2, 2 )
  141.     term.clearLine()
  142.     term.write( Host..'/'..Site )
  143.  
  144.     term.setTextColour( textColour )
  145.     term.setBackgroundColour( bgColour )
  146. end
  147.  
  148. local function load(_sPath)
  149.     if _sPath=='' or _sPath ==nil then redrawMenu() return end
  150.     tLines = {}
  151.     scrollX, scrollY = 0,0
  152.     Host,Site=_sPath:match('(.-)/(.*)')
  153.     if not Host then Host=_sPath Site='' end
  154.     if Host=='' then
  155.         local file = io.open( Site, "r" )
  156.         if file then
  157.           local sLine = file:read()
  158.           while sLine do
  159.             table.insert( tLines, sLine )
  160.             sLine = file:read()
  161.           end
  162.           file:close()
  163.         else
  164.             table.insert( tLines, 'Can not open file "'..Site..'"' )
  165.         end
  166.     else
  167.         if ip.open() then
  168.           if ip.getIP() then
  169.             ip.send(Host,Site)
  170.             print(Host,' <',Site,'>')
  171.             local addr,sLines
  172. --            repeat
  173.               sLines,addr=ip.receive(3)
  174.               if not addr then sLines='No ansver from server' end
  175. --            until addr==Host
  176.             sLines=sLines..'\n'
  177.             for sLine in sLines:gmatch('(.-)\n') do
  178.               table.insert( tLines, sLine )
  179.             end
  180.           else
  181.             table.insert( tLines, 'Can not connect to Craftnet' )
  182.           end
  183.         else
  184.             table.insert( tLines, 'Can not open modem' )
  185.         end
  186.     end
  187.    
  188.     if #tLines == 0 then
  189.         table.insert( tLines, "" )
  190.     end
  191.     if History[#History]~=_sPath then table.insert( History, _sPath ) end
  192.     term.setBackgroundColour( bgColour )
  193.     term.clear()
  194.     term.setCursorBlink( false )
  195.     redrawText()
  196.     redrawMenu()
  197. end
  198.  
  199. local tMenuFuncs = {
  200.     Back=function()
  201.         if #History>1 then
  202.             History[#History]=nil
  203.             load(History[#History])
  204.         end
  205.     end,
  206.     Save=function()
  207.     end,
  208.     Exit=function()
  209.         bRunning = false
  210.     end
  211. }
  212.  
  213. local function doMenuItem( _n )
  214.     tMenuFuncs[tMenuItems[_n]]()
  215.     bMenu = false
  216. end
  217.  
  218. -- Tags
  219. tags['font']=function(arg)
  220.     if arg.color and colours[arg.color] then
  221.       term.setTextColour( colours[arg.color] )
  222.     end
  223.     if arg.bgcolor and colours[arg.bgcolor] then
  224.       term.setBackgroundColour(colours[arg.bgcolor])
  225.     end
  226. end
  227. tags['/font']=function()
  228.     term.setTextColour( textColour )
  229.     term.setBackgroundColour( bgColour )
  230. end
  231.  
  232. local function line_check(obj,x,y)
  233.     if y>=obj.y1 and y<=obj.y2 then
  234.         if x>=obj.x1 or y>obj.y1 then
  235.             if x<=obj.x2 or y<obj.y2 then
  236.                 obj:work()
  237.                 return true
  238.             end
  239.         end
  240.     end
  241.     return false
  242. end
  243. local function href_work(obj)
  244.   load(obj.href)
  245. end
  246. local function ref_work(obj)
  247.   load(Host..'/'..obj.ref)
  248. end
  249. tags['a']=function(arg)
  250.     if arg.href then
  251.       term.setTextColour( tagColour )
  252.       local href={check=line_check, work=href_work}
  253.       href.x1, href.y1 = term.getCursorPos()
  254.       href.href = arg.href
  255.       table.insert(objects, href)
  256.     end
  257.     if arg.ref then
  258.       term.setTextColour( tagColour )
  259.       local ref={check=line_check, work=ref_work}
  260.       ref.x1, ref.y1 = term.getCursorPos()
  261.       ref.ref = arg.ref
  262.       table.insert(objects, ref)
  263.     end
  264. end
  265. tags['/a']=function()
  266.     term.setTextColour( textColour )
  267.     local href=objects[#objects]
  268.     if href and not href.x2 then
  269.       href.x2, href.y2 = term.getCursorPos()
  270.       href.x2=href.x2 - 1
  271.     end
  272. end
  273.  
  274. --Key events
  275. local eventKey={}
  276. local eventMenu={}
  277. eventKey[keys.up]=function()
  278. -- Move screen up
  279.     if scrollY > 0 then scrollY = scrollY - 1 end
  280. end
  281. eventKey[keys.down]=function()
  282. -- Move screen down
  283.     if scrollY < #tLines - (h-1) then   scrollY = scrollY + 1 end
  284. end
  285. eventKey[keys.pageUp]=function()
  286. -- Move screen up
  287.     scrollY = scrollY - h - 1
  288.     if scrollY<0 then scrollY=0 end
  289. end
  290. eventKey[keys.pageUp]=function()
  291. -- Move screen up
  292.     scrollY = scrollY - h - 1
  293.     if scrollY<0 then scrollY=0 end
  294. end
  295. eventKey[keys.pageUp]=function()
  296. -- Move screen up
  297.     scrollY = scrollY - h - 1
  298.     if scrollY<0 then scrollY=0 end
  299. end
  300. eventKey[keys.pageDown]=function()
  301. -- Move screen up
  302.     scrollY = scrollY + h - 2
  303.     if scrollY>#tLines-h+1 then scrollY=#tLines-h+1 end
  304. end
  305. eventKey[keys.home]=function()
  306. -- Move cursor to the beginning
  307.     scrollX = 0
  308. end
  309. eventKey[keys["end"]]=function()
  310. -- Move cursor to the end
  311.     if maxLen>w then scrollX = scrollX+maxLen-w end
  312. end
  313. eventKey[keys.left]=function()
  314. -- Move screen left
  315.     if scrollX > 0 then scrollX = scrollX - 1 end
  316. end
  317. eventKey[keys.right]=function()
  318. -- Move screen right
  319.     if maxLen > w then scrollX = scrollX + 1 end
  320. end
  321. eventKey[keys.leftCtrl]=function()
  322. -- Menu toggle
  323.     bMenu = not bMenu
  324.     redrawMenu()
  325. end
  326. eventKey[keys.rightCtrl]=eventKey[keys.leftCtrl]
  327.  
  328. eventMenu[keys.left]=function()
  329. -- Move menu left
  330.     nMenuItem = nMenuItem - 1
  331.     if nMenuItem < 1 then nMenuItem = #tMenuItems end
  332. end
  333. eventMenu[keys.right]=function()
  334. -- Move menu right
  335.     nMenuItem = nMenuItem + 1
  336.     if nMenuItem > #tMenuItems then nMenuItem = 1 end
  337. end
  338. eventMenu[keys.enter]=function()
  339. -- Menu selection
  340.     doMenuItem( nMenuItem )
  341. end
  342. eventMenu[keys.leftCtrl]=function()
  343. -- Menu toggle
  344.     bMenu = not bMenu
  345. end
  346. eventMenu[keys.rightCtrl]=eventMenu[keys.leftCtrl]
  347.  
  348. -- Actual program functionality begins
  349. -- Get file to view
  350. local tArgs = { ... }
  351. if #tArgs > 0 then
  352.     local sPath = shell.resolve( tArgs[1] )
  353.     load(sPath)
  354. end
  355.  
  356. term.setBackgroundColour( bgColour )
  357. term.clear()
  358. term.setCursorBlink( false )
  359.  
  360. redrawText()
  361. redrawMenu()
  362. -- Handle input
  363. while bRunning do
  364.     local sEvent, param, param2, param3 = os.pullEvent()
  365.     if sEvent == "key" then
  366.       if bMenu then
  367.         if eventMenu[param] then eventMenu[param]() redrawMenu() end
  368.       else
  369.         if eventKey[param] then  eventKey[param]() redrawText() end
  370.       end
  371.  
  372.     elseif sEvent == "mouse_click" then
  373.         if not bMenu then
  374.             if param == 1 then
  375.                 -- Left click
  376.                 local cx,cy = param2, param3
  377.                 if cy == 1 then
  378.                     if cx==w then tMenuFuncs.Exit() end
  379.                     if cx==w-2 then tMenuFuncs.Back() end
  380.                 elseif cy==2 then
  381.                     term.setBackgroundColour( colours.white )
  382.                     term.setTextColour( colours.black )
  383.                     term.setCursorPos( 2, 2 )
  384.                     term.clearLine()
  385.                     load(read(nil, History))
  386.                 else
  387.                     for i=1,#objects do
  388.                       if objects[i].check
  389.                       and objects[i].check(objects[i],cx,cy) then break end
  390.                     end
  391.                 end
  392.             end
  393.         end
  394.        
  395.     elseif sEvent == "mouse_scroll" then
  396.         if not bMenu then
  397.             if param == -1 then
  398.                 -- Scroll up
  399.                 if scrollY > 0 then
  400.                     scrollY = scrollY - 1
  401.                     redrawText()
  402.                 end
  403.            
  404.             elseif param == 1 then
  405.                 -- Scroll down
  406.                 local nMaxScroll = #tLines - (h-1)
  407.                 if scrollY < nMaxScroll then
  408.                     scrollY = scrollY + 1
  409.                     redrawText()
  410.                 end
  411.                
  412.             end
  413.         end
  414.     end
  415. end
  416.  
  417. -- Cleanup
  418. term.clear()
  419. term.setCursorBlink( false )
  420. term.setCursorPos( 1, 1 )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement