JustDoesGames

edit Enhanced

Nov 11th, 2019
358
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 40.29 KB | None | 0 0
  1. local version = 2
  2. --[[
  3.  
  4.  
  5. Edit Enhanced
  6.  
  7.  
  8. Original Code  by  Dan2000 - 772 Lines
  9. Refined  by  Deprimido ( Just Does Games ) - 1373 Lines (Version 2)
  10.  
  11.  
  12. Content:
  13.  
  14.         Version 1
  15.  - [Advanced] Added Multi Color Support
  16.  - [All] Added Ln Feature to go directly to a certain line
  17.  - [All] Added Cl display to show the current letter they are on
  18.  - [SOON] Added Settings menu to allow changing settings more freely
  19.  - [All] Added a bar at the bottom of the screen for more style
  20.  - [All] Changed the menu to display horizontal
  21.  - [All] Changed menu text from "Press Ctrl to Access Menu" to a simple icon
  22.  - [All] Clicking while the menu is open now closes it
  23.  - [All] Fixed a Bug where 'edit <path>' command allowed for more than 1 args
  24.  
  25.         Version 2
  26.  - [Advanced] Set Default Colors to the orginal Computercraft colors
  27.  - [Advanced] Added Right-Click Menu (still in development)
  28.  - [Advanced] Added clickable home buttons (soon to be clickable menus too)
  29.  - [SOON] Settings Menu is still under development but has come near to being released soon!!!
  30.  - [All] Changed the menu icon to something more visible
  31.  - [All] Fixed bug where menu would flicker going at fast speeds
  32.  - [All] Improved stability of the auto-update program
  33.  - [All] Fixed a bug where the Ln cursor position moved slightly to the right
  34.  - [All] Setting the Ln sets the cursor position at the end of that line
  35.  - [All] Added setting to disable users from running programs
  36.  
  37.  
  38. ]]
  39.  
  40. -- Get file to edit
  41. local tArgs = { ... }
  42. if #tArgs ~= 1 then
  43.     print( "Usage: edit <path>" )
  44.     return
  45. end
  46.  
  47. term.setBackgroundColor( colours.black )
  48. term.clear()
  49. term.setCursorPos(1,1)
  50.  
  51.  
  52. local auto_update = false -- Set this to true or false to toggle updates
  53. local run_programs = true -- Allows users to run the program
  54.  
  55.  
  56. local __updated = false
  57. if auto_update then
  58.     term.setTextColor( colours.white )
  59.     term.setBackgroundColor( colours.black )
  60.     term.setCursorPos(1,1)
  61.     term.clear()
  62.     print("Loading Updates...")
  63.  
  64.     term.setTextColor( colours.black )
  65.     if fs.exists("tmp") then fs.delete("tmp") end
  66.     shell.run("pastebin get ej8X7KiX tmp")
  67.     local file = fs.open("tmp","r")
  68.     local pVersion = file.readLine()
  69.     file.close()
  70.     fs.delete("tmp")
  71.     if pVersion ~= version then
  72.         if fs.exists("edit.lua") or fs.exists("edit") then
  73.             fs.delete("edit")
  74.             fs.delete("edit.lua")
  75.         end
  76.         shell.run("pastebin get m0uWTc4A edit.lua")
  77.         shell.run("edit "..tArgs[1])
  78.         __updated = true
  79.     end
  80.     term.setTextColor( colours.white )
  81. end
  82.  
  83. term.setTextColor( colours.white )
  84.  
  85. if __updated then return end
  86.  
  87.  
  88.  
  89. -- PUT ANY ELSE PAST THIS TO AVOID SOFTLOCKING --
  90.  
  91. -- Error checking
  92.  
  93. local sPath = shell.resolve( tArgs[1] )
  94. --[[
  95. local bReadOnly = fs.isReadOnly( sPath )
  96. if fs.exists( sPath ) and fs.isDir( sPath ) then
  97.     print( "Cannot edit a directory." )
  98.     return
  99. end
  100. ]]--
  101.  
  102. -- Create .lua files by default
  103. if not fs.exists( sPath ) and not string.find( sPath, "%." ) then
  104.     local sExtension = settings.get("edit.default_extension", "" )
  105.     if sExtension ~= "" and type( sExtension ) == "string" then
  106.         sPath = sPath .. "." .. sExtension
  107.     end
  108. end
  109.  
  110. local x,y = 1,1
  111. local w,h = term.getSize()
  112. local scrollX, scrollY = 0,0
  113.  
  114. local tLines = {}
  115. local bRunning = true
  116.  
  117. -- Colours
  118. local highlightColour, keywordColour, commentColour, textColour, bgColour, stringColour, currentlyComp, barColour, menuBackgroundColor, menuTextColour, menuDeselectedColour, menuShadeColor, homeColor
  119. if term.isColour() then
  120.     bgColour = colours.black
  121.     textColour = colours.white
  122.     highlightColour = colours.yellow
  123.     keywordColour = colours.white
  124.     commentColour = colours.green
  125.     stringColour = colours.orange
  126.     currentlyComp = colours.grey
  127.     barColour = colours.blue
  128.     menuBackgroundColor = colours.white
  129.     menuTextColour = colours.black
  130.     menuDeselectedColour = colours.grey
  131.     menuShadeColor = colours.grey
  132.     homeColor = colours.cyan
  133. else
  134.     bgColour = colours.black
  135.     textColour = colours.white
  136.     highlightColour = colours.white
  137.     keywordColour = colours.white
  138.     commentColour = colours.white
  139.     stringColour = colours.white
  140.     currentlyComp = colours.lightGrey
  141.     barColour = colours.white
  142.     menuBackgroundColor = colours.white
  143.     menuTextColour = colours.black
  144.     menuDeselectedColour = colours.lightGrey
  145.     menuShadeColor = colours.grey
  146.     homeColor = colours.lightGrey
  147. end
  148.  
  149. -- Menus
  150. local bMenu = false
  151. local _rM = false
  152. local nMenuItem = 1
  153. local tMenuItems = {}
  154. if not bReadOnly then
  155.     table.insert( tMenuItems, "Save" )
  156. end
  157. if shell.openTab and run_programs then
  158.     table.insert( tMenuItems, "Run" )
  159. end
  160. if peripheral.find( "printer" ) then
  161.     table.insert( tMenuItems, "Print" )
  162. end
  163. table.insert( tMenuItems, "Exit" )
  164. table.insert( tMenuItems, "Settings" )
  165.  
  166. local sStatus = " "
  167. if string.len( sStatus ) > w - 5 then
  168.     sStatus = " "
  169. end
  170.  
  171. local function load( _sPath )
  172.     tLines = {}
  173.     if fs.exists( _sPath ) then
  174.         local file = io.open( _sPath, "r" )
  175.         local sLine = file:read()
  176.         while sLine do
  177.             table.insert( tLines, sLine )
  178.             sLine = file:read()
  179.         end
  180.         file:close()
  181.     end
  182.  
  183.     if #tLines == 0 then
  184.         table.insert( tLines, "" )
  185.     end
  186. end
  187.  
  188. local function save( _sPath )
  189.     -- Create intervening folder
  190.     local sDir = _sPath:sub(1, _sPath:len() - fs.getName(_sPath):len() )
  191.     if not fs.exists( sDir ) then
  192.         fs.makeDir( sDir )
  193.     end
  194.  
  195.     -- Save
  196.     local file = nil
  197.     local function innerSave()
  198.         file = fs.open( _sPath, "w" )
  199.         if file then
  200.             for n, sLine in ipairs( tLines ) do
  201.                 file.write( sLine .. "\n" )
  202.             end
  203.         else
  204.             error( "Failed to open ".._sPath )
  205.         end
  206.     end
  207.  
  208.     local ok, err = pcall( innerSave )
  209.     if file then
  210.         file.close()
  211.     end
  212.     return ok, err
  213. end
  214.  
  215. local tKeywords = {
  216.     ["and"] = true,
  217.     ["break"] = true,
  218.     ["do"] = true,
  219.     ["else"] = true,
  220.     ["elseif"] = true,
  221.     ["end"] = true,
  222.     ["false"] = true,
  223.     ["for"] = true,
  224.     ["function"] = true,
  225.     ["if"] = true,
  226.     ["in"] = true,
  227.     ["local"] = true,
  228.     ["nil"] = true,
  229.     ["not"] = true,
  230.     ["or"] = true,
  231.     ["repeat"] = true,
  232.     ["return"] = true,
  233.     ["then"] = true,
  234.     ["true"] = true,
  235.     ["until"]= true,
  236.     ["while"] = true,
  237. }
  238.  
  239. local function tryWrite( sLine, regex, colour )
  240.     local match = string.match( sLine, regex )
  241.     if match then
  242.         if type(colour) == "number" then
  243.             term.setTextColour( colour )
  244.         else
  245.             term.setTextColour( colour(match) )
  246.         end
  247.         term.write( match )
  248.         term.setTextColour( textColour )
  249.         return string.sub( sLine, string.len(match) + 1 )
  250.     end
  251.     return nil
  252. end
  253.  
  254. local function writeHighlighted( sLine )
  255.     while string.len(sLine) > 0 do
  256.         sLine =
  257.             tryWrite( sLine, "^%-%-%[%[.-%]%]", commentColour ) or
  258.             tryWrite( sLine, "^%-%-.*", commentColour ) or
  259.             tryWrite( sLine, "^\"\"", stringColour ) or
  260.             tryWrite( sLine, "^\".-[^\\]\"", stringColour ) or
  261.             tryWrite( sLine, "^\'\'", stringColour ) or
  262.             tryWrite( sLine, "^\'.-[^\\]\'", stringColour ) or
  263.             tryWrite( sLine, "^%[%[.-%]%]", stringColour ) or
  264.             tryWrite( sLine, "^[%w_]+", function( match )
  265.                 if tKeywords[ match ] then
  266.                     return keywordColour
  267.                 end
  268.                 return textColour
  269.             end ) or
  270.             tryWrite( sLine, "^[^%w_]", textColour )
  271.     end
  272. end
  273.  
  274. local tCompletions
  275. local nCompletion
  276.  
  277. local tCompleteEnv = _ENV
  278. local function complete( sLine )
  279.     if settings.get( "edit.autocomplete" ) then
  280.         local nStartPos = string.find( sLine, "[a-zA-Z0-9_%.:]+$" )
  281.         if nStartPos then
  282.             sLine = string.sub( sLine, nStartPos )
  283.         end
  284.         if #sLine > 0 then
  285.             return textutils.complete( sLine, tCompleteEnv )
  286.         end
  287.     end
  288.     return nil
  289. end
  290.  
  291. local function recomplete()
  292.     local sLine = tLines[y]
  293.     if not bMenu and not bReadOnly and x == string.len(sLine) + 1 then
  294.         tCompletions = complete( sLine )
  295.         if tCompletions and #tCompletions > 0 then
  296.             nCompletion = 1
  297.         else
  298.             nCompletion = nil
  299.         end
  300.     else
  301.         tCompletions = nil
  302.         nCompletion = nil
  303.     end
  304. end
  305.  
  306. local function writeCompletion( sLine )
  307.     if nCompletion then
  308.         local sCompletion = tCompletions[ nCompletion ]
  309.         term.setTextColor( colours.white )
  310.         term.setBackgroundColor( currentlyComp )
  311.         term.write( sCompletion )
  312.         term.setTextColor( textColour )
  313.         term.setBackgroundColor( bgColour )
  314.     end
  315. end
  316.  
  317. local function redrawText()
  318.     local cursorX, cursorY = x, y
  319.     term.setBackgroundColor( bgColour )
  320.     for y=1,h-1 do
  321.         term.setCursorPos( 1 - scrollX, y )
  322.         term.clearLine()
  323.  
  324.         local sLine = tLines[ y + scrollY ]
  325.         if sLine ~= nil then
  326.             writeHighlighted( sLine )
  327.             if cursorY == y and cursorX == #sLine + 1 then
  328.                 writeCompletion()
  329.             end
  330.         end
  331.     end
  332.     term.setCursorPos( x - scrollX, y - scrollY )
  333. end
  334.  
  335. local function redrawLine(_nY)
  336.     local sLine = tLines[_nY]
  337.     if sLine then
  338.         term.setCursorPos( 1 - scrollX, _nY - scrollY )
  339.         term.clearLine()
  340.         writeHighlighted( sLine )
  341.         if _nY == y and x == #sLine + 1 then
  342.             writeCompletion()
  343.         end
  344.         term.setCursorPos( x - scrollX, _nY - scrollY )
  345.     end
  346. end
  347.  
  348. local function redrawMenu(reload)
  349.     if reload == nil then reload = true end
  350.  
  351.     -- Clear line
  352.     if reload then
  353.         redrawText()
  354.         term.setCursorPos( 1, h )
  355.         local tmp = term.getBackgroundColor()
  356.         term.setBackgroundColor( barColour )
  357.         term.clearLine()
  358.  
  359.  
  360.         -- Draw line numbers
  361.         term.setCursorPos( w - string.len( "Cl "..x.." Ln "..y ) + 1, h )
  362.         term.setTextColour( highlightColour )
  363.  
  364.         term.write( "Cl " )
  365.         term.setTextColour( textColour )
  366.         term.write( x )
  367.  
  368.         term.setTextColour( highlightColour )
  369.         term.write( " Ln " )
  370.         term.setTextColour( textColour )
  371.         term.write( y )
  372.         term.setCursorPos( 1,h )
  373.         if sStatus == " " then
  374.         term.setBackgroundColor( homeColor )
  375.         end
  376.         term.write( sStatus )
  377.     end
  378.  
  379.     term.setCursorPos( 1, h )
  380.     if bMenu then
  381.         -- Draw menu
  382.         if reload then
  383.             term.setBackgroundColor( menuBackgroundColor )
  384.             term.setCursorPos(1,h-#tMenuItems-2)
  385.             local _x,_y = 0,0
  386.             for i=1, #tMenuItems+2 do
  387.                 for i=1, 11 do
  388.                     write(" ")
  389.                 end
  390.                 term.setBackgroundColor( menuShadeColor )
  391.                 write(" ")
  392.                 term.setBackgroundColor( menuBackgroundColor )
  393.                 term.setCursorPos(1,h-#tMenuItems-2+i)
  394.             end
  395.         end
  396.  
  397.  
  398.         term.setCursorPos(1,h-#tMenuItems-1)
  399.         for nItem,sItem in pairs( tMenuItems ) do
  400.             if nItem == nMenuItem then
  401.                 term.setTextColour( highlightColour )
  402.                 term.write( "[" )
  403.                 term.setTextColour( menuTextColour )
  404.                 term.write( sItem )
  405.                 term.setTextColour( highlightColour )
  406.                 term.write( "]" )
  407.                 term.setTextColour( menuTextColour )
  408.             else
  409.                 term.setTextColour( menuDeselectedColour )
  410.                 term.write( " "..sItem.." " )
  411.             end
  412.             print("")
  413.         end
  414.     else
  415.         -- Draw status
  416.         term.setTextColour( highlightColour )
  417.         if sStatus == " " then
  418.         term.setBackgroundColor( homeColor )
  419.         end
  420.  
  421.         -- Normal Menu
  422.         term.write( sStatus )
  423.         sStatus = " "
  424.  
  425.         term.setBackgroundColor( bgColour )
  426.         term.setTextColour( textColour )
  427.     end
  428.  
  429.     -- Reset cursor
  430.     term.setCursorPos( x - scrollX, y - scrollY )
  431. end
  432.  
  433.  
  434.  
  435.  
  436.  
  437.  
  438. local function _menu()
  439.     local _m_color_colors = {"white","orange","magenta","lightBlue","yellow","lime","pink","gray","lightGray","cyan","purple","blue","brown","green","red","black", "Back"}
  440.     local _m_color_colors_raw = {colours.white, colours.orange, colours.magenta, colours.lightBlue, colours.yellow, colours.lime, colours.pink, colours.grey, colours.lightGrey, colours.cyan, colours.purple, colours.blue, colours.brown, colours.green, colours.red, colours.black}
  441.  
  442.     local function _drawMenu(update)
  443.         term.setTextColor( menuTextColour )
  444.         if update then
  445.             term.setBackgroundColor( bgColour )
  446.             term.clear()
  447.             term.setBackgroundColor( barColour )
  448.             term.setCursorPos( 1,h )
  449.             for i=1, w do
  450.                 write(" ")
  451.             end
  452.             term.setCursorPos( 1,4 )
  453.             for i=1, h-1 do
  454.                 term.setCursorPos( w/2,i )
  455.                 write(" ")
  456.             end
  457.         end
  458.     end
  459.     _drawMenu(true)
  460.     sleep(3)
  461.  
  462. end
  463.  
  464.  
  465.  
  466.  
  467.  
  468.  
  469. local function setCursor( newX, newY )
  470.     local oldX, oldY = x, y
  471.     x, y = newX, newY
  472.     local screenX = x - scrollX
  473.     local screenY = y - scrollY
  474.  
  475.     local bRedraw = false
  476.     if screenX < 1 then
  477.         scrollX = x - 1
  478.         screenX = 1
  479.         bRedraw = true
  480.     elseif screenX > w then
  481.         scrollX = x - w
  482.         screenX = w
  483.         bRedraw = true
  484.     end
  485.  
  486.     if screenY < 1 then
  487.         scrollY = y - 1
  488.         screenY = 1
  489.         bRedraw = true
  490.     elseif screenY > h-1 then
  491.         scrollY = y - (h-1)
  492.         screenY = h-1
  493.         bRedraw = true
  494.     end
  495.  
  496.     recomplete()
  497.     if bRedraw then
  498.         redrawText()
  499.     elseif y ~= oldY then
  500.         redrawLine( oldY )
  501.         redrawLine( y )
  502.     else
  503.         redrawLine( y )
  504.     end
  505.     term.setCursorPos( screenX, screenY )
  506.  
  507.     redrawMenu()
  508. end
  509.  
  510.  
  511.  
  512. local tMenuFuncs = {
  513.     Save = function()
  514.         if bReadOnly then
  515.             sStatus = "Access denied"
  516.         else
  517.             local ok, err = save( sPath )
  518.             if ok then
  519.                 sStatus="Saved to "..sPath
  520.             else
  521.                 sStatus="Error saving to "..sPath
  522.             end
  523.         end
  524.         redrawMenu()
  525.     end,
  526.     Print = function()
  527.         local printer = peripheral.find( "printer" )
  528.         if not printer then
  529.             sStatus = "No printer attached"
  530.             return
  531.         end
  532.  
  533.         local nPage = 0
  534.         local sName = fs.getName( sPath )
  535.         if printer.getInkLevel() < 1 then
  536.             sStatus = "Printer out of ink"
  537.             return
  538.         elseif printer.getPaperLevel() < 1 then
  539.             sStatus = "Printer out of paper"
  540.             return
  541.         end
  542.  
  543.         local screenTerminal = term.current()
  544.         local printerTerminal = {
  545.             getCursorPos = printer.getCursorPos,
  546.             setCursorPos = printer.setCursorPos,
  547.             getSize = printer.getPageSize,
  548.             write = printer.write,
  549.         }
  550.         printerTerminal.scroll = function()
  551.             if nPage == 1 then
  552.                 printer.setPageTitle( sName.." (page "..nPage..")" )
  553.             end
  554.  
  555.             while not printer.newPage()    do
  556.                 if printer.getInkLevel() < 1 then
  557.                     sStatus = "Printer out of ink, please refill"
  558.                 elseif printer.getPaperLevel() < 1 then
  559.                     sStatus = "Printer out of paper, please refill"
  560.                 else
  561.                     sStatus = "Printer output tray full, please empty"
  562.                 end
  563.  
  564.                 term.redirect( screenTerminal )
  565.                 redrawMenu()
  566.                 term.redirect( printerTerminal )
  567.  
  568.                 local timer = os.startTimer(0.5)
  569.                 sleep(0.5)
  570.             end
  571.  
  572.             nPage = nPage + 1
  573.             if nPage == 1 then
  574.                 printer.setPageTitle( sName )
  575.             else
  576.                 printer.setPageTitle( sName.." (page "..nPage..")" )
  577.             end
  578.         end
  579.  
  580.         bMenu = false
  581.         term.redirect( printerTerminal )
  582.         local ok, error = pcall( function()
  583.             term.scroll()
  584.             for n, sLine in ipairs( tLines ) do
  585.                 print( sLine )
  586.             end
  587.         end )
  588.         term.redirect( screenTerminal )
  589.         if not ok then
  590.             print( error )
  591.         end
  592.  
  593.         while not printer.endPage() do
  594.             sStatus = "Printer output tray full, please empty"
  595.             redrawMenu()
  596.             sleep( 0.5 )
  597.         end
  598.         bMenu = true
  599.  
  600.         if nPage > 1 then
  601.             sStatus = "Printed "..nPage.." Pages"
  602.         else
  603.             sStatus = "Printed 1 Page"
  604.         end
  605.         redrawMenu()
  606.     end,
  607.     Exit = function()
  608.         bRunning = false
  609.     end,
  610.     Run = function()
  611.         local sTempPath = "/.temp"
  612.         local ok, err = save( sTempPath )
  613.         if ok then
  614.             local nTask = shell.openTab( sTempPath )
  615.             if nTask then
  616.                 shell.switchTab( nTask )
  617.             else
  618.                 sStatus="Error starting Task"
  619.             end
  620.             fs.delete( sTempPath )
  621.         else
  622.             sStatus="Error saving to "..sTempPath
  623.         end
  624.         redrawMenu()
  625.     end,
  626.  
  627.  
  628.  
  629.     Settings = function()
  630.         -- MENU FOR NEW SETTINGS --
  631.         _menu()
  632.         -- RESET --
  633.         redrawMenu()
  634.         redrawText()
  635.     end,
  636.     Save_and_exit = function()
  637.         if bReadOnly then
  638.             sStatus = "Access denied"
  639.         else
  640.             local ok, err = save( sPath )
  641.             if ok then
  642.                 sStatus="Saved to "..sPath
  643.             else
  644.                 sStatus="Error saving to "..sPath
  645.             end
  646.         end
  647.         bRunning = false
  648.     end,
  649.     Go = function()
  650.                     term.setCursorBlink(true)
  651.                     redrawText()
  652.                     term.setCursorPos(w-string.len( "Ln "..y ), h)
  653.                     term.setBackgroundColor( barColour )
  654.                     term.setTextColor( colours.white )
  655.                     term.clearLine()
  656.                     for i=1, w-string.len( "Ln "..y )-1 do
  657.                         write(" ")
  658.                     end
  659.                     term.setCursorPos(w-string.len( "Ln " )-3, h)
  660.                     write("Ln ")
  661.                     local numbs = 0
  662.                     local numbsArray = {}
  663.                     while true do
  664.                         local sEvent, i = os.pullEvent() sleep(.00000000001)
  665.                         if sEvent == "key" then
  666.                             i = keys.getName(i)
  667.                             if i == "one" or i == "two" or i == "three" or i == "four" or i == "five" or i == "six" or i == "seven" or i == "eight" or i == "nine" or i == "zero" or i == "numPad1"  or i == "numPad2" or i == "numPad3" or i == "numPad4" or i == "numPad5" or i == "numPad6" or i == "numPad7" or i == "numPad8" or i == "numPad9" or i == "numPad0" then
  668.                                 numbs = numbs + 1
  669.                                 if i == "one" or i == "numPad1" then
  670.                                     numbsArray[numbs] = 1
  671.                                 elseif i == "two" or i == "numPad2" then
  672.                                     numbsArray[numbs] = 2
  673.                                 elseif i == "three" or i == "numPad3" then
  674.                                     numbsArray[numbs] = 3
  675.                                 elseif i == "four" or i == "numPad4" then
  676.                                     numbsArray[numbs] = 4
  677.                                 elseif i == "five" or i == "numPad5" then
  678.                                     numbsArray[numbs] = 5
  679.                                 elseif i == "six" or i == "numPad6" then
  680.                                     numbsArray[numbs] = 6
  681.                                 elseif i == "seven" or i == "numPad7" then
  682.                                     numbsArray[numbs] = 7
  683.                                 elseif i == "eight" or i == "numPad8" then
  684.                                     numbsArray[numbs] = 8
  685.                                 elseif i == "nine" or i == "numPad9" then
  686.                                     numbsArray[numbs] = 9
  687.                                 elseif i == "zero" or i == "numPad0" then
  688.                                     numbsArray[numbs] = 0
  689.                                 end
  690.                             elseif i == "enter" then
  691.                                 break
  692.                             elseif i == "backspace" then
  693.                                 if numbs > 0 then
  694.                                     table.remove(numbsArray, #numbsArray)
  695.                                     numbs = numbs - 1
  696.                                 end
  697.                             elseif i == "rightCtrl" or i == "leftCtrl" then
  698.                                 numbs = 0
  699.                                 numbsArray = {}
  700.                                 break
  701.                             end
  702.                             term.setCursorPos(w-string.len( "Ln "..y )+1, h)
  703.                             write("   ")
  704.                             term.setCursorPos(w-string.len( "Ln "..y )+1, h)
  705.                             for i=1, #numbsArray do
  706.                                 write(numbsArray[i])
  707.                             end
  708.                             if numbs == 3 then sleep(.1) break end
  709.                         elseif sEvent == "mouse_click" then
  710.                             break
  711.                         end
  712.                     end
  713.                     if #numbsArray > 0 then
  714.                         numbs = ""
  715.                         for i=1, #numbsArray do
  716.                             numbs = numbs..tostring(numbsArray[i])
  717.                         end
  718.                         numbs = tonumber(numbs)
  719.                         local cx,cy = 1,1
  720.                         local newY = math.min( math.max( scrollY + cy, 1 ), #tLines )
  721.                         local newX = math.min( math.max( scrollX + cx, 1 ), string.len( tLines[newY] ) + 1 )
  722.                         if numbs > #tLines then
  723.                             setCursor(newX,#tLines)
  724.                         elseif numbs < 1 then
  725.                             setCursor(newX,1)
  726.                         else
  727.                             setCursor(newX,numbs)
  728.                         end
  729.  
  730.                         local ocx,ocy = term.getCursorPos()
  731.                         local cx,cy = 999,ocy
  732.                         local newY = math.min( math.max( scrollY + cy, 1 ), #tLines )
  733.                         local newX = math.min( math.max( scrollX + cx, 1 ), string.len( tLines[newY] ) + 1 )
  734.                         setCursor(newX,newY)
  735.                     end
  736.     end,
  737.     Go_up = function()
  738.         setCursor(1,1)
  739.     end,
  740.     Go_down = function()
  741.         setCursor(1,#tLines)
  742.     end,
  743.     Clear_file = function()
  744.         tLines = {} table.insert( tLines, "" ) setCursor(1,1)
  745.     end
  746. }
  747.  
  748. local function doMenuItem( _n )
  749.     tMenuFuncs[tMenuItems[_n]]()
  750.     if bMenu then
  751.         bMenu = false
  752.         term.setCursorBlink( true )
  753.     end
  754.     redrawMenu() redrawText()
  755. end
  756.  
  757.  
  758.  
  759.  
  760. -- Actual program functionality begins
  761. load(sPath)
  762.  
  763. term.setBackgroundColour( bgColour )
  764. term.clear()
  765. term.setCursorPos(x,y)
  766. term.setCursorBlink( true )
  767.  
  768. recomplete()
  769. redrawText()
  770. redrawMenu()
  771.  
  772. local function acceptCompletion()
  773.     if nCompletion then
  774.         -- Append the completion
  775.         local sCompletion = tCompletions[ nCompletion ]
  776.         tLines[y] = tLines[y] .. sCompletion
  777.         setCursor( x + string.len( sCompletion ), y )
  778.     end
  779. end
  780.  
  781. -- Handle input
  782. while bRunning do
  783.     local sEvent, param, param2, param3 = os.pullEvent()
  784.     if sEvent == "key" then
  785.         local oldX, oldY = x, y
  786.         if param == keys.up then
  787.             -- Up
  788.             if not bMenu then
  789.                 if nCompletion then
  790.                     -- Cycle completions
  791.                     nCompletion = nCompletion - 1
  792.                     if nCompletion < 1 then
  793.                         nCompletion = #tCompletions
  794.                     end
  795.                     redrawLine(y)
  796.  
  797.                 elseif y > 1 then
  798.                     -- Move cursor up
  799.                     setCursor(
  800.                         math.min( x, string.len( tLines[y - 1] ) + 1 ),
  801.                         y - 1
  802.                     )
  803.                 end
  804.             else
  805.                 -- Menu Up
  806.                 nMenuItem = nMenuItem - 1
  807.                 if nMenuItem < 1 then
  808.                     nMenuItem = #tMenuItems
  809.                 end
  810.                 redrawMenu(false)
  811.             end
  812.  
  813.         elseif param == keys.down then
  814.             -- Down
  815.             if not bMenu then
  816.                 -- Move cursor down
  817.                 if nCompletion then
  818.                     -- Cycle completions
  819.                     nCompletion = nCompletion + 1
  820.                     if nCompletion > #tCompletions then
  821.                         nCompletion = 1
  822.                     end
  823.                     redrawLine(y)
  824.  
  825.                 elseif y < #tLines then
  826.                     -- Move cursor down
  827.                     setCursor(
  828.                         math.min( x, string.len( tLines[y + 1] ) + 1 ),
  829.                         y + 1
  830.                     )
  831.                 end
  832.             else
  833.                 -- Menu Down
  834.                 nMenuItem = nMenuItem + 1
  835.                 if nMenuItem > #tMenuItems then
  836.                     nMenuItem = 1
  837.                 end
  838.                 redrawMenu(false)
  839.             end
  840.  
  841.         elseif param == keys.tab then
  842.             -- Tab
  843.             if not bMenu and not bReadOnly then
  844.                 if nCompletion and x == string.len(tLines[y]) + 1 then
  845.                     -- Accept autocomplete
  846.                     acceptCompletion()
  847.                 else
  848.                     -- Indent line
  849.                     local sLine = tLines[y]
  850.                     tLines[y] = string.sub(sLine,1,x-1) .. "    " .. string.sub(sLine,x)
  851.                     setCursor( x + 4, y )
  852.                 end
  853.             end
  854.  
  855.         elseif param == keys.pageUp then
  856.             -- Page Up
  857.             if not bMenu then
  858.                 -- Move up a page
  859.                 local newY
  860.                 if y - (h - 1) >= 1 then
  861.                     newY = y - (h - 1)
  862.                 else
  863.                     newY = 1
  864.                 end
  865.                 setCursor(
  866.                     math.min( x, string.len( tLines[newY] ) + 1 ),
  867.                     newY
  868.                 )
  869.             end
  870.  
  871.         elseif param == keys.pageDown then
  872.             -- Page Down
  873.             if not bMenu then
  874.                 -- Move down a page
  875.                 local newY
  876.                 if y + (h - 1) <= #tLines then
  877.                     newY = y + (h - 1)
  878.                 else
  879.                     newY = #tLines
  880.                 end
  881.                 local newX = math.min( x, string.len( tLines[newY] ) + 1 )
  882.                 setCursor( newX, newY )
  883.             end
  884.  
  885.         elseif param == keys.home then
  886.             -- Home
  887.             if not bMenu then
  888.                 -- Move cursor to the beginning
  889.                 if x > 1 then
  890.                     setCursor(1,y)
  891.                 end
  892.             end
  893.  
  894.         elseif param == keys["end"] then
  895.             -- End
  896.             if not bMenu then
  897.                 -- Move cursor to the end
  898.                 local nLimit = string.len( tLines[y] ) + 1
  899.                 if x < nLimit then
  900.                     setCursor( nLimit, y )
  901.                 end
  902.             end
  903.  
  904.         elseif param == keys.left then
  905.             -- Left
  906.             if not bMenu then
  907.                 if x > 1 then
  908.                     -- Move cursor left
  909.                     setCursor( x - 1, y )
  910.                 elseif x==1 and y>1 then
  911.                     setCursor( string.len( tLines[y-1] ) + 1, y - 1 )
  912.                 end
  913.             else
  914.                 -- Move menu left
  915.                 nMenuItem = nMenuItem - 1
  916.                 if nMenuItem < 1 then
  917.                     nMenuItem = #tMenuItems
  918.                 end
  919.                 redrawMenu()
  920.             end
  921.  
  922.         elseif param == keys.right then
  923.             -- Right
  924.             if not bMenu then
  925.                 local nLimit = string.len( tLines[y] ) + 1
  926.                 if x < nLimit then
  927.                     -- Move cursor right
  928.                     setCursor( x + 1, y )
  929.                 elseif nCompletion and x == string.len(tLines[y]) + 1 then
  930.                     -- Accept autocomplete
  931.                     acceptCompletion()
  932.                 elseif x==nLimit and y<#tLines then
  933.                     -- Go to next line
  934.                     setCursor( 1, y + 1 )
  935.                 end
  936.             else
  937.                 -- Move menu right
  938.                 nMenuItem = nMenuItem + 1
  939.                 if nMenuItem > #tMenuItems then
  940.                     nMenuItem = 1
  941.                 end
  942.                 redrawMenu()
  943.             end
  944.  
  945.         elseif param == keys.delete then
  946.             -- Delete
  947.             if not bMenu and not bReadOnly then
  948.                 local nLimit = string.len( tLines[y] ) + 1
  949.                 if x < nLimit then
  950.                     local sLine = tLines[y]
  951.                     tLines[y] = string.sub(sLine,1,x-1) .. string.sub(sLine,x+1)
  952.                     recomplete()
  953.                     redrawLine(y)
  954.                 elseif y<#tLines then
  955.                     tLines[y] = tLines[y] .. tLines[y+1]
  956.                     table.remove( tLines, y+1 )
  957.                     recomplete()
  958.                     redrawText()
  959.                 end
  960.             end
  961.  
  962.         elseif param == keys.backspace then
  963.             -- Backspace
  964.             if not bMenu and not bReadOnly then
  965.                 if x > 1 then
  966.                     -- Remove character
  967.                     local sLine = tLines[y]
  968.                     if x > 4 and string.sub(sLine,x-4,x-1) == "    " and not string.sub(sLine, 1, x - 1):find("%S") then
  969.                         tLines[y] = string.sub(sLine,1,x-5) .. string.sub(sLine,x)
  970.                         setCursor( x - 4, y )
  971.                     else
  972.                         tLines[y] = string.sub(sLine,1,x-2) .. string.sub(sLine,x)
  973.                         setCursor( x - 1, y )
  974.                     end
  975.                 elseif y > 1 then
  976.                     -- Remove newline
  977.                     local sPrevLen = string.len( tLines[y-1] )
  978.                     tLines[y-1] = tLines[y-1] .. tLines[y]
  979.                     table.remove( tLines, y )
  980.                     setCursor( sPrevLen + 1, y - 1 )
  981.                     redrawText()
  982.                 end
  983.             end
  984.  
  985.         elseif param == keys.enter then
  986.             -- Enter
  987.             if not bMenu and not bReadOnly then
  988.                 -- Newline
  989.                 local sLine = tLines[y]
  990.                 local _,spaces=string.find(sLine,"^[ ]+")
  991.                 if not spaces then
  992.                     spaces=0
  993.                 end
  994.                 tLines[y] = string.sub(sLine,1,x-1)
  995.                 table.insert( tLines, y+1, string.rep(' ',spaces)..string.sub(sLine,x) )
  996.                 setCursor( spaces + 1, y + 1 )
  997.                 redrawText()
  998.  
  999.             elseif bMenu then
  1000.                 -- Menu selection
  1001.                 doMenuItem( nMenuItem )
  1002.                 redrawText()
  1003.  
  1004.             end
  1005.  
  1006.         elseif param == keys.f1 or param == keys.leftCtrl or param == keys.rightCtrl or param == keys.rightAlt then
  1007.             -- Menu toggle
  1008.             bMenu = not bMenu
  1009.             if bMenu then
  1010.                 term.setCursorBlink( false )
  1011.             else
  1012.                 term.setCursorBlink( true )
  1013.                 redrawText()
  1014.             end
  1015.             redrawMenu()
  1016.  
  1017.         end
  1018.  
  1019.     elseif sEvent == "char" then
  1020.         if not bMenu and not bReadOnly then
  1021.             -- Input text
  1022.             local sLine = tLines[y]
  1023.             tLines[y] = string.sub(sLine,1,x-1) .. param .. string.sub(sLine,x)
  1024.             setCursor( x + 1, y )
  1025.  
  1026.         elseif bMenu then
  1027.             -- Select menu items
  1028.             for n,sMenuItem in ipairs( tMenuItems ) do
  1029.                 if string.lower(string.sub(sMenuItem,1,1)) == string.lower(param) then
  1030.                     doMenuItem( n )
  1031.                     break
  1032.                 end
  1033.             end
  1034.         end
  1035.  
  1036.     elseif sEvent == "paste" then
  1037.         if not bReadOnly then
  1038.             -- Close menu if open
  1039.             if bMenu then
  1040.                 bMenu = false
  1041.                 term.setCursorBlink( true )
  1042.                 redrawMenu()
  1043.             end
  1044.             -- Input text
  1045.             local sLine = tLines[y]
  1046.             tLines[y] = string.sub(sLine,1,x-1) .. param .. string.sub(sLine,x)
  1047.             setCursor( x + string.len( param ), y )
  1048.         end
  1049.  
  1050.     elseif sEvent == "mouse_click" then
  1051.         if not bMenu then
  1052.             if param == 1 then
  1053.                 -- Left click
  1054.                 local cx,cy = param2, param3
  1055.                 if cx == 1 and cy == h then
  1056.                     bMenu = not bMenu
  1057.                     if bMenu then
  1058.                         term.setCursorBlink( false )
  1059.                     else
  1060.                         term.setCursorBlink( true )
  1061.                         redrawText()
  1062.                     end
  1063.                     redrawMenu()
  1064.                 elseif cy < h then
  1065.                     local newY = math.min( math.max( scrollY + cy, 1 ), #tLines )
  1066.                     local newX = math.min( math.max( scrollX + cx, 1 ), string.len( tLines[newY] ) + 1 )
  1067.                     setCursor( newX, newY )
  1068.                 elseif cy == h and cx >= w-string.len( "Ln "..y )+1 then
  1069.                     term.setCursorPos(w-string.len( "Ln "..y ), h)
  1070.                     term.setBackgroundColor( barColour )
  1071.                     term.setTextColor( colours.white )
  1072.                     term.clearLine()
  1073.                     for i=1, w-string.len( "Ln "..y )-1 do
  1074.                         write(" ")
  1075.                     end
  1076.                     term.setCursorPos(w-string.len( "Ln " )-3, h)
  1077.                     write("Ln ")
  1078.                     local numbs = 0
  1079.                     local numbsArray = {}
  1080.                     while true do
  1081.                         local sEvent, i = os.pullEvent() sleep(.00000000001)
  1082.                         if sEvent == "key" then
  1083.                             i = keys.getName(i)
  1084.                             if i == "one" or i == "two" or i == "three" or i == "four" or i == "five" or i == "six" or i == "seven" or i == "eight" or i == "nine" or i == "zero" or i == "numPad1"  or i == "numPad2" or i == "numPad3" or i == "numPad4" or i == "numPad5" or i == "numPad6" or i == "numPad7" or i == "numPad8" or i == "numPad9" or i == "numPad0" then
  1085.                                 numbs = numbs + 1
  1086.                                 if i == "one" or i == "numPad1" then
  1087.                                     numbsArray[numbs] = 1
  1088.                                 elseif i == "two" or i == "numPad2" then
  1089.                                     numbsArray[numbs] = 2
  1090.                                 elseif i == "three" or i == "numPad3" then
  1091.                                     numbsArray[numbs] = 3
  1092.                                 elseif i == "four" or i == "numPad4" then
  1093.                                     numbsArray[numbs] = 4
  1094.                                 elseif i == "five" or i == "numPad5" then
  1095.                                     numbsArray[numbs] = 5
  1096.                                 elseif i == "six" or i == "numPad6" then
  1097.                                     numbsArray[numbs] = 6
  1098.                                 elseif i == "seven" or i == "numPad7" then
  1099.                                     numbsArray[numbs] = 7
  1100.                                 elseif i == "eight" or i == "numPad8" then
  1101.                                     numbsArray[numbs] = 8
  1102.                                 elseif i == "nine" or i == "numPad9" then
  1103.                                     numbsArray[numbs] = 9
  1104.                                 elseif i == "zero" or i == "numPad0" then
  1105.                                     numbsArray[numbs] = 0
  1106.                                 end
  1107.                             elseif i == "enter" then
  1108.                                 break
  1109.                             elseif i == "backspace" then
  1110.                                 if numbs > 0 then
  1111.                                     table.remove(numbsArray, #numbsArray)
  1112.                                     numbs = numbs - 1
  1113.                                 end
  1114.                             elseif i == "rightCtrl" or i == "leftCtrl" then
  1115.                                 numbs = 0
  1116.                                 numbsArray = {}
  1117.                                 break
  1118.                             end
  1119.                             term.setCursorPos(w-string.len( "Ln "..y )+1, h)
  1120.                             write("   ")
  1121.                             term.setCursorPos(w-string.len( "Ln "..y )+1, h)
  1122.                             for i=1, #numbsArray do
  1123.                                 write(numbsArray[i])
  1124.                             end
  1125.                             if numbs == 3 then sleep(.1) break end
  1126.                         elseif sEvent == "mouse_click" then
  1127.                             break
  1128.                         end
  1129.                     end
  1130.                     if #numbsArray > 0 then
  1131.                         numbs = ""
  1132.                         for i=1, #numbsArray do
  1133.                             numbs = numbs..tostring(numbsArray[i])
  1134.                         end
  1135.                         numbs = tonumber(numbs)
  1136.                         local cx,cy = 1,1
  1137.                         local newY = math.min( math.max( scrollY + cy, 1 ), #tLines )
  1138.                         local newX = math.min( math.max( scrollX + cx, 1 ), string.len( tLines[newY] ) + 1 )
  1139.                         if numbs > #tLines then
  1140.                             setCursor(newX,#tLines)
  1141.                         elseif numbs < 1 then
  1142.                             setCursor(newX,1)
  1143.                         else
  1144.                             setCursor(newX,numbs)
  1145.                         end
  1146.  
  1147.                         local ocx,ocy = term.getCursorPos()
  1148.                         local cx,cy = 999,ocy
  1149.                         local newY = math.min( math.max( scrollY + cy, 1 ), #tLines )
  1150.                         local newX = math.min( math.max( scrollX + cx, 1 ), string.len( tLines[newY] ) + 1 )
  1151.                         setCursor(newX,newY)
  1152.                     end
  1153.                     redrawMenu()
  1154.                     redrawText()
  1155.                 end
  1156.             elseif param == 2 then
  1157.                 term.setCursorBlink(false)
  1158.                 -- Right click
  1159.                 local _rMenu = {}
  1160.                 local _rMenuMax
  1161.                 local quad = 0
  1162.  
  1163.                 local cx,cy = param2,param3
  1164.                 --local newY = math.min( math.max( scrollY + cy, 1 ), #tLines )
  1165.                 --local newX = math.min( math.max( scrollX + cx, 1 ), string.len( tLines[newY] ) + 1 )
  1166.                 --setCursor( newX, newY )
  1167.  
  1168.                 table.insert( _rMenu, "Go" )
  1169.                 table.insert( _rMenu, "Go"..string.char(24) )
  1170.                 table.insert( _rMenu, "Go"..string.char(25) )
  1171.                 if not bReadOnly then
  1172.                     table.insert( _rMenu, "Save" )
  1173.                     table.insert( _rMenu, "Save and Exit" )
  1174.                 end
  1175.                 if shell.openTab and run_programs then
  1176.                     table.insert( _rMenu, "Run" )
  1177.                 end
  1178.                 if peripheral.find( "printer" ) then
  1179.                     table.insert( _rMenu, "Print" )
  1180.                 end
  1181.                 table.insert( _rMenu, "Exit" )
  1182.                 _rMenuMax = 1
  1183.  
  1184.  
  1185.                 local function tsearch(tbl, str)
  1186.                     for i=1, #tbl do
  1187.                         if tostring(tbl[i]) == tostring(str) then return i end
  1188.                     end
  1189.                     return false
  1190.                 end
  1191.  
  1192.                 local function runCommand(opt)
  1193.                     opt = _rMenu[opt]
  1194.                     if opt == "Go"..string.char(24) then
  1195.                         -- Up
  1196.                         tMenuFuncs["Go_up"]()
  1197.                     elseif opt == "Go"..string.char(25) then
  1198.                         -- Down
  1199.                         tMenuFuncs["Go_down"]()
  1200.                     elseif opt == "Save and Exit" then
  1201.                         -- Save_and_exit
  1202.                         tMenuFuncs["Save_and_exit"]()
  1203.                     elseif opt == "Clear File" then
  1204.                         tMenuFuncs["Clear_file"]()
  1205.                     else
  1206.                         tMenuFuncs[opt]()
  1207.                     end
  1208.                 end
  1209.  
  1210.  
  1211.                 local function _updateMenu() -- Runs every time the menu's position changes
  1212.                     if cx == 1 and cy == 1 then
  1213.                         if tsearch( _rMenu, "Clear File" ) == false then
  1214.                             table.insert( _rMenu, "Clear File" )
  1215.                         end
  1216.                     else
  1217.                         if tsearch( _rMenu, "Clear File" ) ~= false then
  1218.                             table.remove( _rMenu, tsearch( _rMenu, "Clear File" ) )
  1219.                         end
  1220.                     end
  1221.                     for i=1, #_rMenu do
  1222.                         if string.len(_rMenu[i]) > _rMenuMax then _rMenuMax = string.len(_rMenu[i]) end
  1223.                     end
  1224.                 end
  1225.  
  1226.                 local function _displayMenu() -- Runs every time the menu needs a refresh
  1227.                     term.setBackgroundColor( colours.white )
  1228.                     term.setTextColor( colours.black )
  1229.                     for i=1, #_rMenu do
  1230.                         if cx < w-_rMenuMax then
  1231.                             if cy > h-#_rMenu then
  1232.                                 term.setCursorPos(cx+1,cy-i)
  1233.                                 quad = 1
  1234.                             else
  1235.                                 term.setCursorPos(cx+1,cy+i)
  1236.                                 quad = 4
  1237.                             end
  1238.                         else
  1239.                             if cy > #_rMenu then
  1240.                                 term.setCursorPos(cx-_rMenuMax,cy-i)
  1241.                                 quad = 2
  1242.                             else
  1243.                                 term.setCursorPos(cx-_rMenuMax,cy+i)
  1244.                                 quad = 3
  1245.                             end
  1246.                         end
  1247.                         write(_rMenu[i])
  1248.                         for i=1, _rMenuMax-string.len(_rMenu[i]) do
  1249.                             write(" ")
  1250.                         end
  1251.                         print(" ")
  1252.                         sleep(.005)
  1253.                     end
  1254.                     _rM = true
  1255.                 end
  1256.  
  1257.                 local _upd = true
  1258.                 _updateMenu()
  1259.  
  1260.                 while true do
  1261.                     if _upd then _displayMenu() _upd = not _upd end
  1262.                     local sEventt, paramx, paramy, paramz = os.pullEvent()
  1263.                     if sEventt == "key" then
  1264.                         if paramx == keys.leftCtrl or paramx == keys.rightCtrl then
  1265.                             bMenu = true
  1266.                             break
  1267.                         end
  1268.                     elseif sEventt == "mouse_click" then
  1269.                         ocx,ocy = cx,cy
  1270.                         cx,cy = paramy,paramz
  1271.                         if paramx == 1 then
  1272.                             -- left
  1273.                             if cx > ocx and cx < _rMenuMax+ocx and cy > ocy and cy < ocy+_rMenuMax and quad == 4 then
  1274.                                 -- bottom right IV
  1275.                                 --error("IV: "..math.abs(ocy-cy))
  1276.                                 runCommand(math.abs(ocy-cy))
  1277.                             elseif cx > ocx and cx < _rMenuMax+ocx and cy < ocy and cy > ocy-_rMenuMax and quad == 1 then
  1278.                                 -- top right I
  1279.                                 --error("I: "..math.abs(ocy-cy))
  1280.                                 runCommand(math.abs(ocy-cy))
  1281.                             elseif cx < ocx and cx > _rMenuMax-ocx and cy < ocy and cy > ocy-_rMenuMax and quad == 2 then
  1282.                                 -- top left II
  1283.                                 --error("II: "..math.abs(ocy-cy))
  1284.                                 runCommand(math.abs(ocy-cy))
  1285.                             elseif cx < ocx and cx > _rMenuMax-ocx and cy > ocy and cy < ocy+_rMenuMax and quad == 3 then
  1286.                                 -- bottom left III
  1287.                                 --error("III: "..math.abs(ocy-cy))
  1288.                                 runCommand(math.abs(ocy-cy))
  1289.                             else
  1290.                                 local newY = math.min( math.max( scrollY + cy, 1 ), #tLines )
  1291.                                 local newX = math.min( math.max( scrollX + cx, 1 ), string.len( tLines[newY] ) + 1 )
  1292.                                 setCursor( newX, newY )
  1293.                             end
  1294.                             break
  1295.                         elseif paramx == 2 then
  1296.                             -- right
  1297.                             cx,cy = paramy,paramz
  1298.                             redrawText()
  1299.                             _updateMenu() _upd = true
  1300.                         end
  1301.                     end
  1302.                 end
  1303.  
  1304.                 -- End
  1305.                 term.setCursorPos(x,y)
  1306.                 term.setCursorBlink(true)
  1307.                 redrawText()
  1308.                 redrawMenu()
  1309.             end
  1310.         else
  1311.             local cx,cy = param2, param3
  1312.             if cx == 1 and cy == h then
  1313.                 bMenu = not bMenu
  1314.                 redrawMenu()
  1315.  
  1316.                 term.setCursorBlink(true)
  1317.  
  1318.                 local newY = math.min( math.max( scrollY + cy, 1 ), #tLines )
  1319.                 local newX = math.min( math.max( scrollX + cx, 1 ), string.len( tLines[newY] ) + 1 )
  1320.                 setCursor( newX, newY )
  1321.             elseif cx <= 11 and cy > h-#tMenuItems-2 then
  1322.                 --bMenu = not bMenu
  1323.                 doMenuItem( cy-h+6 )
  1324.                 redrawText()
  1325.             else
  1326.                 bMenu = not bMenu
  1327.                 redrawMenu()
  1328.  
  1329.                 term.setCursorBlink(true)
  1330.  
  1331.                 local newY = math.min( math.max( scrollY + cy, 1 ), #tLines )
  1332.                 local newX = math.min( math.max( scrollX + cx, 1 ), string.len( tLines[newY] ) + 1 )
  1333.                 setCursor( newX, newY )
  1334.             end
  1335.         end
  1336.  
  1337.     elseif sEvent == "mouse_scroll" then
  1338.         if not bMenu then
  1339.             if param == -1 then
  1340.                 -- Scroll up
  1341.                 if scrollY > 0 then
  1342.                     -- Move cursor up
  1343.                     scrollY = scrollY - 1
  1344.                     redrawText()
  1345.                 end
  1346.  
  1347.             elseif param == 1 then
  1348.                 -- Scroll down
  1349.                 local nMaxScroll = #tLines - (h-1)
  1350.                 if scrollY < nMaxScroll then
  1351.                     -- Move cursor down
  1352.                     scrollY = scrollY + 1
  1353.                     redrawText()
  1354.                 end
  1355.  
  1356.             end
  1357.         end
  1358.     elseif sEvent == "mouse_drag" and not bMenu then
  1359.         -- DRAG WORKS
  1360.     elseif sEvent == "term_resize" then
  1361.         w,h = term.getSize()
  1362.         setCursor( x, y )
  1363.         redrawMenu()
  1364.         redrawText()
  1365.  
  1366.     end
  1367. end
  1368.  
  1369. -- Cleanup
  1370. term.setBackgroundColor( colours.black )
  1371. term.setTextColor( colours.white )
  1372. term.clear()
  1373. term.setCursorBlink( false )
  1374. term.setCursorPos( 1, 1 )
Advertisement
Add Comment
Please, Sign In to add comment