Advertisement
Guest User

edit+

a guest
Nov 25th, 2015
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 18.26 KB | None | 0 0
  1. -- Get file to edit
  2. local tArgs = { ... }
  3. if #tArgs == 0 then
  4.     print( "Usage: edit+ <path> [line]" )
  5.     return
  6. end
  7.  
  8.  
  9. -- Error checking
  10. local sPath = shell.resolve( tArgs[1] )
  11. local bReadOnly = fs.isReadOnly( sPath )
  12. if fs.exists( sPath ) and fs.isDir( sPath ) then
  13.     print( "Cannot edit a directory." )
  14.     return
  15. end
  16.  
  17. local x,y = 1,1
  18. local w,h = term.getSize()
  19. local scrollX, scrollY = 0,0
  20.  
  21. local tLines = {}
  22. local bRunning = true
  23.  
  24. if tArgs[2] then
  25.   y = tonumber(tArgs[2])
  26. end
  27.  
  28. -- Colours
  29. local highlightColour, keywordColour, commentColour, textColour, bgColour
  30. if term.isColour() then
  31.     bgColour = colors.white
  32.     textColour = colors.black
  33.     highlightColour = colors.lightBlue
  34.     keywordColour = colors.lightGray
  35.     commentColour = colors.lime
  36.     stringColour = colors.blue
  37. else
  38.     bgColour = colours.black
  39.     textColour = colours.white
  40.     highlightColour = colours.white
  41.     keywordColour = colours.white
  42.     commentColour = colours.white
  43.     stringColour = colours.white
  44. end
  45.  
  46. -- Menus
  47. local bMenu = false
  48. local nMenuItem = 1
  49. local tMenuItems = {}
  50. if not bReadOnly then
  51.     table.insert( tMenuItems, "Save" )
  52. end
  53. if shell.openTab then
  54.     table.insert( tMenuItems, "Run" )
  55. end
  56. if peripheral.find( "printer" ) then
  57.     table.insert( tMenuItems, "Print" )
  58. end
  59. table.insert( tMenuItems, "Exit" )
  60.  
  61. local sStatus = "Press Ctrl to access menu"
  62. if string.len( sStatus ) > w - 5 then
  63.     sStatus = "Press Ctrl for menu"
  64. end
  65.  
  66. local function load( _sPath )
  67.     tLines = {}
  68.     if fs.exists( _sPath ) then
  69.         local file = io.open( _sPath, "r" )
  70.         local sLine = file:read()
  71.         while sLine do
  72.             table.insert( tLines, sLine )
  73.             sLine = file:read()
  74.         end
  75.         file:close()
  76.     end
  77.    
  78.     if #tLines == 0 then
  79.         table.insert( tLines, "" )
  80.     end
  81. end
  82.  
  83. local function save( _sPath )
  84.     -- Create intervening folder
  85.     local sDir = _sPath:sub(1, _sPath:len() - fs.getName(_sPath):len() )
  86.     if not fs.exists( sDir ) then
  87.         fs.makeDir( sDir )
  88.     end
  89.  
  90.     -- Save
  91.     local file = nil
  92.     local function innerSave()
  93.         file = fs.open( _sPath, "w" )
  94.         if file then
  95.             for n, sLine in ipairs( tLines ) do
  96.                 file.write( sLine .. "\n" )
  97.             end
  98.         else
  99.             error( "Failed to open ".._sPath )
  100.         end
  101.     end
  102.    
  103.     local ok, err = pcall( innerSave )
  104.     if file then
  105.         file.close()
  106.     end
  107.     return ok, err
  108. end
  109.  
  110. local tKeywords = {
  111.     ["and"] = true,
  112.     ["break"] = true,
  113.     ["do"] = true,
  114.     ["else"] = true,
  115.     ["elseif"] = true,
  116.     ["end"] = true,
  117.     ["false"] = true,
  118.     ["for"] = true,
  119.     ["function"] = true,
  120.     ["if"] = true,
  121.     ["in"] = true,
  122.     ["local"] = true,
  123.     ["nil"] = true,
  124.     ["not"] = true,
  125.     ["or"] = true,
  126.     ["repeat"] = true,
  127.     ["return"] = true,
  128.     ["then"] = true,
  129.     ["true"] = true,
  130.     ["until"]= true,
  131.     ["while"] = true,
  132. }
  133.  
  134. local function tryWrite( sLine, regex, colour )
  135.     local match = string.match( sLine, regex )
  136.     if match then
  137.         if type(colour) == "number" then
  138.             term.setTextColour( colour )
  139.         else
  140.             term.setTextColour( colour(match) )
  141.         end
  142.         term.write( match )
  143.         term.setTextColour( textColour )
  144.         return string.sub( sLine, string.len(match) + 1 )
  145.     end
  146.     return nil
  147. end
  148.  
  149. local function writeHighlighted( sLine )
  150.     while string.len(sLine) > 0 do 
  151.         sLine =
  152.             tryWrite( sLine, "^%-%-%[%[.-%]%]", commentColour ) or
  153.             tryWrite( sLine, "^%-%-.*", commentColour ) or
  154.             tryWrite( sLine, "^\".-[^\\]\"", stringColour ) or
  155.             tryWrite( sLine, "^\'.-[^\\]\'", stringColour ) or
  156.             tryWrite( sLine, "^%[%[.-%]%]", stringColour ) or
  157.             tryWrite( sLine, "^[%w_]+", function( match )
  158.                 if tKeywords[ match ] then
  159.                     return keywordColour
  160.                 end
  161.                 return textColour
  162.             end ) or
  163.             tryWrite( sLine, "^[^%w_]", textColour )
  164.     end
  165. end
  166.  
  167. local tCompletions
  168. local nCompletion
  169.  
  170. local tCompleteEnv = _ENV
  171. local function complete( sLine )
  172.     local nStartPos = string.find( sLine, "[a-zA-Z0-9_%.]+$" )
  173.     if nStartPos then
  174.         sLine = string.sub( sLine, nStartPos )
  175.     end
  176.     if #sLine > 0 then
  177.         return textutils.complete( sLine, tCompleteEnv )
  178.     end
  179.     return nil
  180. end
  181.  
  182. local function recomplete()
  183.     local sLine = tLines[y]
  184.     if not bMenu and not bReadOnly and x == string.len(sLine) + 1 then
  185.         tCompletions = complete( sLine )
  186.         if tCompletions and #tCompletions > 0 then
  187.             nCompletion = 1
  188.         else
  189.             nCompletion = nil
  190.         end
  191.     else
  192.         tCompletions = nil
  193.         nCompletion = nil
  194.     end
  195. end
  196.  
  197. local function writeCompletion( sLine )
  198.     if nCompletion then
  199.         local sCompletion = tCompletions[ nCompletion ]
  200.         term.setTextColor( colors.gray )
  201.         term.setBackgroundColor( colors.lightGray )
  202.         term.write( sCompletion )
  203.         term.setTextColor( textColour )
  204.         term.setBackgroundColor( bgColour )
  205.     end
  206. end
  207.  
  208. local function redrawText()
  209.     local cursorX, cursorY = x, y
  210.     for y=1,h-1 do
  211.         term.setCursorPos( 1 - scrollX, y )
  212.         term.clearLine()
  213.  
  214.         local sLine = tLines[ y + scrollY ]
  215.         if sLine ~= nil then
  216.             writeHighlighted( sLine )
  217.             if cursorY == y and cursorX == #sLine + 1 then
  218.                 writeCompletion()
  219.             end
  220.         end
  221.     end
  222.     term.setCursorPos( x - scrollX, y - scrollY )
  223. end
  224.  
  225. local function redrawLine(_nY)
  226.     local sLine = tLines[_nY]
  227.     if sLine then
  228.         term.setCursorPos( 1 - scrollX, _nY - scrollY )
  229.         term.clearLine()
  230.         writeHighlighted( sLine )
  231.         if _nY == y and x == #sLine + 1 then
  232.             writeCompletion()
  233.         end
  234.         term.setCursorPos( x - scrollX, _nY - scrollY )
  235.     end
  236. end
  237.  
  238. local function redrawMenu()
  239.     -- Clear line
  240.     term.setCursorPos( 1, h )
  241.     term.clearLine()
  242.  
  243.     -- Draw line numbers
  244.     term.setCursorPos( w - string.len( "Ln "..y ) + 1, h )
  245.     term.setTextColour( highlightColour )
  246.     term.write( "Ln " )
  247.     term.setTextColour( textColour )
  248.     term.write( y )
  249.  
  250.     term.setCursorPos( 1, h )
  251.     if bMenu then
  252.         -- Draw menu
  253.         term.setTextColour( textColour )
  254.         for nItem,sItem in pairs( tMenuItems ) do
  255.             if nItem == nMenuItem then
  256.                 term.setTextColour( highlightColour )
  257.                 term.write( "[" )
  258.                 term.setTextColour( textColour )
  259.                 term.write( sItem )
  260.                 term.setTextColour( highlightColour )
  261.                 term.write( "]" )
  262.                 term.setTextColour( textColour )
  263.             else
  264.                 term.write( " "..sItem.." " )
  265.             end
  266.         end
  267.     else
  268.         -- Draw status
  269.         term.setTextColour( highlightColour )
  270.         term.write( sStatus )
  271.         term.setTextColour( textColour )
  272.     end
  273.  
  274.     -- Reset cursor
  275.     term.setCursorPos( x - scrollX, y - scrollY )
  276. end
  277.  
  278. local tMenuFuncs = {
  279.     Save = function()
  280.         if bReadOnly then
  281.             sStatus = "Access denied"
  282.         else
  283.             local ok, err = save( sPath )
  284.             if ok then
  285.                 sStatus="Saved to "..sPath
  286.             else
  287.                 sStatus="Error saving to "..sPath
  288.             end
  289.         end
  290.         redrawMenu()
  291.     end,
  292.     Print = function()
  293.         local printer = peripheral.find( "printer" )
  294.         if not printer then
  295.             sStatus = "No printer attached"
  296.             return
  297.         end
  298.  
  299.         local nPage = 0
  300.         local sName = fs.getName( sPath )
  301.         if printer.getInkLevel() < 1 then
  302.             sStatus = "Printer out of ink"
  303.             return
  304.         elseif printer.getPaperLevel() < 1 then
  305.             sStatus = "Printer out of paper"
  306.             return
  307.         end
  308.  
  309.         local screenTerminal = term.current()
  310.         local printerTerminal = {
  311.             getCursorPos = printer.getCursorPos,
  312.             setCursorPos = printer.setCursorPos,
  313.             getSize = printer.getPageSize,
  314.             write = printer.write,
  315.         }
  316.         printerTerminal.scroll = function()
  317.             if nPage == 1 then
  318.                 printer.setPageTitle( sName.." (page "..nPage..")" )           
  319.             end
  320.            
  321.             while not printer.newPage() do
  322.                 if printer.getInkLevel() < 1 then
  323.                     sStatus = "Printer out of ink, please refill"
  324.                 elseif printer.getPaperLevel() < 1 then
  325.                     sStatus = "Printer out of paper, please refill"
  326.                 else
  327.                     sStatus = "Printer output tray full, please empty"
  328.                 end
  329.    
  330.                 term.redirect( screenTerminal )
  331.                 redrawMenu()
  332.                 term.redirect( printerTerminal )
  333.                
  334.                 local timer = os.startTimer(0.5)
  335.                 sleep(0.5)
  336.             end
  337.  
  338.             nPage = nPage + 1
  339.             if nPage == 1 then
  340.                 printer.setPageTitle( sName )
  341.             else
  342.                 printer.setPageTitle( sName.." (page "..nPage..")" )
  343.             end
  344.         end
  345.        
  346.         bMenu = false
  347.         term.redirect( printerTerminal )
  348.         local ok, error = pcall( function()
  349.             term.scroll()
  350.             for n, sLine in ipairs( tLines ) do
  351.                 print( sLine )
  352.             end
  353.         end )
  354.         term.redirect( screenTerminal )
  355.         if not ok then
  356.             print( error )
  357.         end
  358.        
  359.         while not printer.endPage() do
  360.             sStatus = "Printer output tray full, please empty"
  361.             redrawMenu()
  362.             sleep( 0.5 )
  363.         end
  364.         bMenu = true
  365.            
  366.         if nPage > 1 then
  367.             sStatus = "Printed "..nPage.." Pages"
  368.         else
  369.             sStatus = "Printed 1 Page"
  370.         end
  371.         redrawMenu()
  372.     end,
  373.     Exit = function()
  374.         bRunning = false
  375.     end,
  376.     Run = function()
  377.         local sTempPath = "/.temp"
  378.         local ok, err = save( sTempPath )
  379.         if ok then
  380.             local nTask = shell.openTab( sTempPath )
  381.             if nTask then
  382.                 shell.switchTab( nTask )
  383.             else
  384.                 sStatus="Error starting Task"
  385.             end
  386.             fs.delete( sTempPath )
  387.         else
  388.             sStatus="Error saving to "..sTempPath
  389.         end
  390.         redrawMenu()
  391.     end
  392. }
  393.  
  394. local function doMenuItem( _n )
  395.     tMenuFuncs[tMenuItems[_n]]()
  396.     if bMenu then
  397.         bMenu = false
  398.         term.setCursorBlink( true )
  399.     end
  400.     redrawMenu()
  401. end
  402.  
  403. local function setCursor( newX, newY )
  404.     local oldX, oldY = x, y
  405.     x, y = newX, newY
  406.     local screenX = x - scrollX
  407.     local screenY = y - scrollY
  408.    
  409.     local bRedraw = false
  410.     if screenX < 1 then
  411.         scrollX = x - 1
  412.         screenX = 1
  413.         bRedraw = true
  414.     elseif screenX > w then
  415.         scrollX = x - w
  416.         screenX = w
  417.         bRedraw = true
  418.     end
  419.    
  420.     if screenY < 1 then
  421.         scrollY = y - 1
  422.         screenY = 1
  423.         bRedraw = true
  424.     elseif screenY > h-1 then
  425.         scrollY = y - (h-1)
  426.         screenY = h-1
  427.         bRedraw = true
  428.     end
  429.  
  430.     recomplete()
  431.     if bRedraw then
  432.         redrawText()
  433.     elseif y ~= oldY then
  434.         redrawLine( oldY )
  435.         redrawLine( y )
  436.     else
  437.         redrawLine( y )
  438.     end
  439.     term.setCursorPos( screenX, screenY )
  440.  
  441.     redrawMenu()
  442. end
  443.  
  444. -- Actual program functionality begins
  445. redrawText()
  446. load(sPath)
  447.  
  448. term.setBackgroundColour( bgColour )
  449. term.clear()
  450. term.setCursorPos(x,y)
  451. term.setCursorBlink( true )
  452.  
  453. recomplete()
  454. redrawText()
  455. redrawMenu()
  456.  
  457. local function acceptCompletion()
  458.     if nCompletion then
  459.         -- Find the common prefix of all the other suggestions which start with the same letter as the current one
  460.         local sCompletion = tCompletions[ nCompletion ]
  461.         local sFirstLetter = string.sub( sCompletion, 1, 1 )
  462.         local sCommonPrefix = sCompletion
  463.         for n=1,#tCompletions do
  464.             local sResult = tCompletions[n]
  465.             if n ~= nCompletion and string.find( sResult, sFirstLetter, 1, true ) == 1 then
  466.                 while #sCommonPrefix > 1 do
  467.                     if string.find( sResult, sCommonPrefix, 1, true ) == 1 then
  468.                         break
  469.                     else
  470.                         sCommonPrefix = string.sub( sCommonPrefix, 1, #sCommonPrefix - 1 )
  471.                     end
  472.                 end
  473.             end
  474.         end
  475.  
  476.         -- Append this string
  477.         tLines[y] = tLines[y] .. sCommonPrefix
  478.         setCursor( x + string.len( sCommonPrefix ), y )
  479.     end
  480. end
  481.  
  482. -- Handle input
  483. while bRunning do
  484.     local sEvent, param, param2, param3 = os.pullEvent()
  485.     if sEvent == "key" then
  486.         local oldX, oldY = x, y
  487.         if param == keys.up then
  488.             -- Up
  489.             if not bMenu then
  490.                 if nCompletion then
  491.                     -- Cycle completions
  492.                     nCompletion = nCompletion - 1
  493.                     if nCompletion < 1 then
  494.                         nCompletion = #tCompletions
  495.                     end
  496.                     redrawLine(y)
  497.  
  498.                 elseif y > 1 then
  499.                     -- Move cursor up
  500.                     setCursor(
  501.                         math.min( x, string.len( tLines[y - 1] ) + 1 ),
  502.                         y - 1
  503.                     )
  504.                 end
  505.             end
  506.  
  507.         elseif param == keys.down then
  508.             -- Down
  509.             if not bMenu then
  510.                 -- Move cursor down
  511.                 if nCompletion then
  512.                     -- Cycle completions
  513.                     nCompletion = nCompletion + 1
  514.                     if nCompletion > #tCompletions then
  515.                         nCompletion = 1
  516.                     end
  517.                     redrawLine(y)
  518.  
  519.                 elseif y < #tLines then
  520.                     -- Move cursor down
  521.                     setCursor(
  522.                         math.min( x, string.len( tLines[y + 1] ) + 1 ),
  523.                         y + 1
  524.                     )
  525.                 end
  526.             end
  527.  
  528.         elseif param == keys.tab then
  529.             -- Tab
  530.             if not bMenu and not bReadOnly then
  531.                 if nCompletion and x == string.len(tLines[y]) + 1 then
  532.                     -- Accept autocomplete
  533.                     acceptCompletion()
  534.                 else
  535.                     -- Indent line
  536.                     local sLine = tLines[y]
  537.                     tLines[y] = string.sub(sLine,1,x-1) .. "  " .. string.sub(sLine,x)
  538.                     setCursor( x + 2, y )
  539.                 end
  540.             end
  541.  
  542.         elseif param == keys.pageUp then
  543.             -- Page Up
  544.             if not bMenu then
  545.                 -- Move up a page
  546.                 local newY
  547.                 if y - (h - 1) >= 1 then
  548.                     newY = y - (h - 1)
  549.                 else
  550.                     newY = 1
  551.                 end
  552.                 setCursor(
  553.                     math.min( x, string.len( tLines[newY] ) + 1 ),
  554.                     newY
  555.                 )
  556.             end
  557.  
  558.         elseif param == keys.pageDown then
  559.             -- Page Down
  560.             if not bMenu then
  561.                 -- Move down a page
  562.                 local newY
  563.                 if y + (h - 1) <= #tLines then
  564.                     newY = y + (h - 1)
  565.                 else
  566.                     newY = #tLines
  567.                 end
  568.                 local newX = math.min( x, string.len( tLines[newY] ) + 1 )
  569.                 setCursor( newX, newY )
  570.             end
  571.  
  572.         elseif param == keys.home then
  573.             -- Home
  574.             if not bMenu then
  575.                 -- Move cursor to the beginning
  576.                 if x > 1 then
  577.                     setCursor(1,y)
  578.                 end
  579.             end
  580.  
  581.         elseif param == keys["end"] then
  582.             -- End
  583.             if not bMenu then
  584.                 -- Move cursor to the end
  585.                 local nLimit = string.len( tLines[y] ) + 1
  586.                 if x < nLimit then
  587.                     setCursor( nLimit, y )
  588.                 end
  589.             end
  590.  
  591.         elseif param == keys.left then
  592.             -- Left
  593.             if not bMenu then
  594.                 if x > 1 then
  595.                     -- Move cursor left
  596.                     setCursor( x - 1, y )
  597.                 elseif x==1 and y>1 then
  598.                     setCursor( string.len( tLines[y-1] ) + 1, y - 1 )
  599.                 end
  600.             else
  601.                 -- Move menu left
  602.                 nMenuItem = nMenuItem - 1
  603.                 if nMenuItem < 1 then
  604.                     nMenuItem = #tMenuItems
  605.                 end
  606.                 redrawMenu()
  607.             end
  608.  
  609.         elseif param == keys.right then
  610.             -- Right
  611.             if not bMenu then
  612.                 local nLimit = string.len( tLines[y] ) + 1
  613.                 if x < nLimit then
  614.                     -- Move cursor right
  615.                     setCursor( x + 1, y )
  616.                 elseif nCompletion and x == string.len(tLines[y]) + 1 then
  617.                     -- Accept autocomplete
  618.                     acceptCompletion()
  619.                 elseif x==nLimit and y<#tLines then
  620.                     -- Go to next line
  621.                     setCursor( 1, y + 1 )
  622.                 end
  623.             else
  624.                 -- Move menu right
  625.                 nMenuItem = nMenuItem + 1
  626.                 if nMenuItem > #tMenuItems then
  627.                     nMenuItem = 1
  628.                 end
  629.                 redrawMenu()
  630.             end
  631.  
  632.         elseif param == keys.delete then
  633.             -- Delete
  634.             if not bMenu and not bReadOnly then
  635.                 local nLimit = string.len( tLines[y] ) + 1
  636.                 if x < nLimit then
  637.                     local sLine = tLines[y]
  638.                     tLines[y] = string.sub(sLine,1,x-1) .. string.sub(sLine,x+1)
  639.                     recomplete()
  640.                     redrawLine(y)
  641.                 elseif y<#tLines then
  642.                     tLines[y] = tLines[y] .. tLines[y+1]
  643.                     table.remove( tLines, y+1 )
  644.                     recomplete()
  645.                     redrawText()
  646.                 end
  647.             end
  648.  
  649.         elseif param == keys.backspace then
  650.             -- Backspace
  651.             if not bMenu and not bReadOnly then
  652.                 if x > 1 then
  653.                     -- Remove character
  654.                     local sLine = tLines[y]
  655.                     tLines[y] = string.sub(sLine,1,x-2) .. string.sub(sLine,x)
  656.                     setCursor( x - 1, y )
  657.                 elseif y > 1 then
  658.                     -- Remove newline
  659.                     local sPrevLen = string.len( tLines[y-1] )
  660.                     tLines[y-1] = tLines[y-1] .. tLines[y]
  661.                     table.remove( tLines, y )
  662.                     setCursor( sPrevLen + 1, y - 1 )
  663.                     redrawText()
  664.                 end
  665.             end
  666.  
  667.         elseif param == keys.enter then
  668.             -- Enter
  669.             if not bMenu and not bReadOnly then
  670.                 -- Newline
  671.                 local sLine = tLines[y]
  672.                 local _,spaces=string.find(sLine,"^[ ]+")
  673.                 if not spaces then
  674.                     spaces=0
  675.                 end
  676.                 tLines[y] = string.sub(sLine,1,x-1)
  677.                 table.insert( tLines, y+1, string.rep(' ',spaces)..string.sub(sLine,x) )
  678.                 setCursor( spaces + 1, y + 1 )
  679.                 redrawText()
  680.  
  681.             elseif bMenu then
  682.                 -- Menu selection
  683.                 doMenuItem( nMenuItem )
  684.  
  685.             end
  686.  
  687.         elseif param == keys.leftCtrl or param == keys.rightCtrl or param == keys.rightAlt then
  688.             -- Menu toggle
  689.             bMenu = not bMenu
  690.             if bMenu then
  691.                 term.setCursorBlink( false )
  692.             else
  693.                 term.setCursorBlink( true )
  694.             end
  695.             redrawMenu()
  696.  
  697.         end
  698.        
  699.     elseif sEvent == "char" then
  700.         if not bMenu and not bReadOnly then
  701.             -- Input text
  702.             local sLine = tLines[y]
  703.             tLines[y] = string.sub(sLine,1,x-1) .. param .. string.sub(sLine,x)
  704.             setCursor( x + 1, y )
  705.  
  706.         elseif bMenu then
  707.             -- Select menu items
  708.             for n,sMenuItem in ipairs( tMenuItems ) do
  709.                 if string.lower(string.sub(sMenuItem,1,1)) == string.lower(param) then
  710.                     doMenuItem( n )
  711.                     break
  712.                 end
  713.             end
  714.         end
  715.  
  716.     elseif sEvent == "paste" then
  717.         if not bMenu and not bReadOnly then
  718.             -- Input text
  719.             local sLine = tLines[y]
  720.             tLines[y] = string.sub(sLine,1,x-1) .. param .. string.sub(sLine,x)
  721.             setCursor( x + string.len( param ), y )
  722.         end
  723.        
  724.     elseif sEvent == "mouse_click" then
  725.         if not bMenu then
  726.             if param == 1 then
  727.                 -- Left click
  728.                 local cx,cy = param2, param3
  729.                 if cy < h then
  730.                     local newY = math.min( math.max( scrollY + cy, 1 ), #tLines )
  731.                     local newX = math.min( math.max( scrollX + cx, 1 ), string.len( tLines[newY] ) + 1 )
  732.                     setCursor( newX, newY )
  733.                 end
  734.             end
  735.         end
  736.        
  737.     elseif sEvent == "mouse_scroll" then
  738.         if not bMenu then
  739.             if param == -1 then
  740.                 -- Scroll up
  741.                 if scrollY > 0 then
  742.                     -- Move cursor up
  743.                     scrollY = scrollY - 1
  744.                     redrawText()
  745.                 end
  746.            
  747.             elseif param == 1 then
  748.                 -- Scroll down
  749.                 local nMaxScroll = #tLines - (h-1)
  750.                 if scrollY < nMaxScroll then
  751.                     -- Move cursor down
  752.                     scrollY = scrollY + 1
  753.                     redrawText()
  754.                 end
  755.                
  756.             end
  757.         end
  758.  
  759.     elseif sEvent == "term_resize" then
  760.         w,h = term.getSize()
  761.         setCursor( x, y )
  762.         redrawMenu()
  763.         redrawText()
  764.  
  765.     end
  766. end
  767.  
  768. -- Cleanup
  769. term.clear()
  770. term.setCursorBlink( false )
  771. term.setCursorPos( 1, 1 )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement