Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local version = 2
- --[[
- Edit Enhanced
- Original Code by Dan2000 - 772 Lines
- Refined by Deprimido ( Just Does Games ) - 1373 Lines (Version 2)
- Content:
- Version 1
- - [Advanced] Added Multi Color Support
- - [All] Added Ln Feature to go directly to a certain line
- - [All] Added Cl display to show the current letter they are on
- - [SOON] Added Settings menu to allow changing settings more freely
- - [All] Added a bar at the bottom of the screen for more style
- - [All] Changed the menu to display horizontal
- - [All] Changed menu text from "Press Ctrl to Access Menu" to a simple icon
- - [All] Clicking while the menu is open now closes it
- - [All] Fixed a Bug where 'edit <path>' command allowed for more than 1 args
- Version 2
- - [Advanced] Set Default Colors to the orginal Computercraft colors
- - [Advanced] Added Right-Click Menu (still in development)
- - [Advanced] Added clickable home buttons (soon to be clickable menus too)
- - [SOON] Settings Menu is still under development but has come near to being released soon!!!
- - [All] Changed the menu icon to something more visible
- - [All] Fixed bug where menu would flicker going at fast speeds
- - [All] Improved stability of the auto-update program
- - [All] Fixed a bug where the Ln cursor position moved slightly to the right
- - [All] Setting the Ln sets the cursor position at the end of that line
- - [All] Added setting to disable users from running programs
- ]]
- -- Get file to edit
- local tArgs = { ... }
- if #tArgs ~= 1 then
- print( "Usage: edit <path>" )
- return
- end
- term.setBackgroundColor( colours.black )
- term.clear()
- term.setCursorPos(1,1)
- local auto_update = false -- Set this to true or false to toggle updates
- local run_programs = true -- Allows users to run the program
- local __updated = false
- if auto_update then
- term.setTextColor( colours.white )
- term.setBackgroundColor( colours.black )
- term.setCursorPos(1,1)
- term.clear()
- print("Loading Updates...")
- term.setTextColor( colours.black )
- if fs.exists("tmp") then fs.delete("tmp") end
- shell.run("pastebin get ej8X7KiX tmp")
- local file = fs.open("tmp","r")
- local pVersion = file.readLine()
- file.close()
- fs.delete("tmp")
- if pVersion ~= version then
- if fs.exists("edit.lua") or fs.exists("edit") then
- fs.delete("edit")
- fs.delete("edit.lua")
- end
- shell.run("pastebin get m0uWTc4A edit.lua")
- shell.run("edit "..tArgs[1])
- __updated = true
- end
- term.setTextColor( colours.white )
- end
- term.setTextColor( colours.white )
- if __updated then return end
- -- PUT ANY ELSE PAST THIS TO AVOID SOFTLOCKING --
- -- Error checking
- local sPath = shell.resolve( tArgs[1] )
- --[[
- local bReadOnly = fs.isReadOnly( sPath )
- if fs.exists( sPath ) and fs.isDir( sPath ) then
- print( "Cannot edit a directory." )
- return
- end
- ]]--
- -- Create .lua files by default
- if not fs.exists( sPath ) and not string.find( sPath, "%." ) then
- local sExtension = settings.get("edit.default_extension", "" )
- if sExtension ~= "" and type( sExtension ) == "string" then
- sPath = sPath .. "." .. sExtension
- end
- end
- local x,y = 1,1
- local w,h = term.getSize()
- local scrollX, scrollY = 0,0
- local tLines = {}
- local bRunning = true
- -- Colours
- local highlightColour, keywordColour, commentColour, textColour, bgColour, stringColour, currentlyComp, barColour, menuBackgroundColor, menuTextColour, menuDeselectedColour, menuShadeColor, homeColor
- if term.isColour() then
- bgColour = colours.black
- textColour = colours.white
- highlightColour = colours.yellow
- keywordColour = colours.white
- commentColour = colours.green
- stringColour = colours.orange
- currentlyComp = colours.grey
- barColour = colours.blue
- menuBackgroundColor = colours.white
- menuTextColour = colours.black
- menuDeselectedColour = colours.grey
- menuShadeColor = colours.grey
- homeColor = colours.cyan
- else
- bgColour = colours.black
- textColour = colours.white
- highlightColour = colours.white
- keywordColour = colours.white
- commentColour = colours.white
- stringColour = colours.white
- currentlyComp = colours.lightGrey
- barColour = colours.white
- menuBackgroundColor = colours.white
- menuTextColour = colours.black
- menuDeselectedColour = colours.lightGrey
- menuShadeColor = colours.grey
- homeColor = colours.lightGrey
- end
- -- Menus
- local bMenu = false
- local _rM = false
- local nMenuItem = 1
- local tMenuItems = {}
- if not bReadOnly then
- table.insert( tMenuItems, "Save" )
- end
- if shell.openTab and run_programs then
- table.insert( tMenuItems, "Run" )
- end
- if peripheral.find( "printer" ) then
- table.insert( tMenuItems, "Print" )
- end
- table.insert( tMenuItems, "Exit" )
- table.insert( tMenuItems, "Settings" )
- local sStatus = " "
- if string.len( sStatus ) > w - 5 then
- sStatus = " "
- end
- local function load( _sPath )
- tLines = {}
- if fs.exists( _sPath ) then
- local file = io.open( _sPath, "r" )
- local sLine = file:read()
- while sLine do
- table.insert( tLines, sLine )
- sLine = file:read()
- end
- file:close()
- end
- if #tLines == 0 then
- table.insert( tLines, "" )
- end
- end
- local function save( _sPath )
- -- Create intervening folder
- local sDir = _sPath:sub(1, _sPath:len() - fs.getName(_sPath):len() )
- if not fs.exists( sDir ) then
- fs.makeDir( sDir )
- end
- -- Save
- local file = nil
- local function innerSave()
- file = fs.open( _sPath, "w" )
- if file then
- for n, sLine in ipairs( tLines ) do
- file.write( sLine .. "\n" )
- end
- else
- error( "Failed to open ".._sPath )
- end
- end
- local ok, err = pcall( innerSave )
- if file then
- file.close()
- end
- return ok, err
- end
- local tKeywords = {
- ["and"] = true,
- ["break"] = true,
- ["do"] = true,
- ["else"] = true,
- ["elseif"] = true,
- ["end"] = true,
- ["false"] = true,
- ["for"] = true,
- ["function"] = true,
- ["if"] = true,
- ["in"] = true,
- ["local"] = true,
- ["nil"] = true,
- ["not"] = true,
- ["or"] = true,
- ["repeat"] = true,
- ["return"] = true,
- ["then"] = true,
- ["true"] = true,
- ["until"]= true,
- ["while"] = true,
- }
- local function tryWrite( sLine, regex, colour )
- local match = string.match( sLine, regex )
- if match then
- if type(colour) == "number" then
- term.setTextColour( colour )
- else
- term.setTextColour( colour(match) )
- end
- term.write( match )
- term.setTextColour( textColour )
- return string.sub( sLine, string.len(match) + 1 )
- end
- return nil
- end
- local function writeHighlighted( sLine )
- while string.len(sLine) > 0 do
- sLine =
- tryWrite( sLine, "^%-%-%[%[.-%]%]", commentColour ) or
- tryWrite( sLine, "^%-%-.*", commentColour ) or
- tryWrite( sLine, "^\"\"", stringColour ) or
- tryWrite( sLine, "^\".-[^\\]\"", stringColour ) or
- tryWrite( sLine, "^\'\'", stringColour ) or
- tryWrite( sLine, "^\'.-[^\\]\'", stringColour ) or
- tryWrite( sLine, "^%[%[.-%]%]", stringColour ) or
- tryWrite( sLine, "^[%w_]+", function( match )
- if tKeywords[ match ] then
- return keywordColour
- end
- return textColour
- end ) or
- tryWrite( sLine, "^[^%w_]", textColour )
- end
- end
- local tCompletions
- local nCompletion
- local tCompleteEnv = _ENV
- local function complete( sLine )
- if settings.get( "edit.autocomplete" ) then
- local nStartPos = string.find( sLine, "[a-zA-Z0-9_%.:]+$" )
- if nStartPos then
- sLine = string.sub( sLine, nStartPos )
- end
- if #sLine > 0 then
- return textutils.complete( sLine, tCompleteEnv )
- end
- end
- return nil
- end
- local function recomplete()
- local sLine = tLines[y]
- if not bMenu and not bReadOnly and x == string.len(sLine) + 1 then
- tCompletions = complete( sLine )
- if tCompletions and #tCompletions > 0 then
- nCompletion = 1
- else
- nCompletion = nil
- end
- else
- tCompletions = nil
- nCompletion = nil
- end
- end
- local function writeCompletion( sLine )
- if nCompletion then
- local sCompletion = tCompletions[ nCompletion ]
- term.setTextColor( colours.white )
- term.setBackgroundColor( currentlyComp )
- term.write( sCompletion )
- term.setTextColor( textColour )
- term.setBackgroundColor( bgColour )
- end
- end
- local function redrawText()
- local cursorX, cursorY = x, y
- term.setBackgroundColor( bgColour )
- for y=1,h-1 do
- term.setCursorPos( 1 - scrollX, y )
- term.clearLine()
- local sLine = tLines[ y + scrollY ]
- if sLine ~= nil then
- writeHighlighted( sLine )
- if cursorY == y and cursorX == #sLine + 1 then
- writeCompletion()
- end
- end
- end
- term.setCursorPos( x - scrollX, y - scrollY )
- end
- local function redrawLine(_nY)
- local sLine = tLines[_nY]
- if sLine then
- term.setCursorPos( 1 - scrollX, _nY - scrollY )
- term.clearLine()
- writeHighlighted( sLine )
- if _nY == y and x == #sLine + 1 then
- writeCompletion()
- end
- term.setCursorPos( x - scrollX, _nY - scrollY )
- end
- end
- local function redrawMenu(reload)
- if reload == nil then reload = true end
- -- Clear line
- if reload then
- redrawText()
- term.setCursorPos( 1, h )
- local tmp = term.getBackgroundColor()
- term.setBackgroundColor( barColour )
- term.clearLine()
- -- Draw line numbers
- term.setCursorPos( w - string.len( "Cl "..x.." Ln "..y ) + 1, h )
- term.setTextColour( highlightColour )
- term.write( "Cl " )
- term.setTextColour( textColour )
- term.write( x )
- term.setTextColour( highlightColour )
- term.write( " Ln " )
- term.setTextColour( textColour )
- term.write( y )
- term.setCursorPos( 1,h )
- if sStatus == " " then
- term.setBackgroundColor( homeColor )
- end
- term.write( sStatus )
- end
- term.setCursorPos( 1, h )
- if bMenu then
- -- Draw menu
- if reload then
- term.setBackgroundColor( menuBackgroundColor )
- term.setCursorPos(1,h-#tMenuItems-2)
- local _x,_y = 0,0
- for i=1, #tMenuItems+2 do
- for i=1, 11 do
- write(" ")
- end
- term.setBackgroundColor( menuShadeColor )
- write(" ")
- term.setBackgroundColor( menuBackgroundColor )
- term.setCursorPos(1,h-#tMenuItems-2+i)
- end
- end
- term.setCursorPos(1,h-#tMenuItems-1)
- for nItem,sItem in pairs( tMenuItems ) do
- if nItem == nMenuItem then
- term.setTextColour( highlightColour )
- term.write( "[" )
- term.setTextColour( menuTextColour )
- term.write( sItem )
- term.setTextColour( highlightColour )
- term.write( "]" )
- term.setTextColour( menuTextColour )
- else
- term.setTextColour( menuDeselectedColour )
- term.write( " "..sItem.." " )
- end
- print("")
- end
- else
- -- Draw status
- term.setTextColour( highlightColour )
- if sStatus == " " then
- term.setBackgroundColor( homeColor )
- end
- -- Normal Menu
- term.write( sStatus )
- sStatus = " "
- term.setBackgroundColor( bgColour )
- term.setTextColour( textColour )
- end
- -- Reset cursor
- term.setCursorPos( x - scrollX, y - scrollY )
- end
- local function _menu()
- local _m_color_colors = {"white","orange","magenta","lightBlue","yellow","lime","pink","gray","lightGray","cyan","purple","blue","brown","green","red","black", "Back"}
- 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}
- local function _drawMenu(update)
- term.setTextColor( menuTextColour )
- if update then
- term.setBackgroundColor( bgColour )
- term.clear()
- term.setBackgroundColor( barColour )
- term.setCursorPos( 1,h )
- for i=1, w do
- write(" ")
- end
- term.setCursorPos( 1,4 )
- for i=1, h-1 do
- term.setCursorPos( w/2,i )
- write(" ")
- end
- end
- end
- _drawMenu(true)
- sleep(3)
- end
- local function setCursor( newX, newY )
- local oldX, oldY = x, y
- x, y = newX, newY
- local screenX = x - scrollX
- local screenY = y - scrollY
- local bRedraw = false
- if screenX < 1 then
- scrollX = x - 1
- screenX = 1
- bRedraw = true
- elseif screenX > w then
- scrollX = x - w
- screenX = w
- bRedraw = true
- end
- if screenY < 1 then
- scrollY = y - 1
- screenY = 1
- bRedraw = true
- elseif screenY > h-1 then
- scrollY = y - (h-1)
- screenY = h-1
- bRedraw = true
- end
- recomplete()
- if bRedraw then
- redrawText()
- elseif y ~= oldY then
- redrawLine( oldY )
- redrawLine( y )
- else
- redrawLine( y )
- end
- term.setCursorPos( screenX, screenY )
- redrawMenu()
- end
- local tMenuFuncs = {
- Save = function()
- if bReadOnly then
- sStatus = "Access denied"
- else
- local ok, err = save( sPath )
- if ok then
- sStatus="Saved to "..sPath
- else
- sStatus="Error saving to "..sPath
- end
- end
- redrawMenu()
- end,
- Print = function()
- local printer = peripheral.find( "printer" )
- if not printer then
- sStatus = "No printer attached"
- return
- end
- local nPage = 0
- local sName = fs.getName( sPath )
- if printer.getInkLevel() < 1 then
- sStatus = "Printer out of ink"
- return
- elseif printer.getPaperLevel() < 1 then
- sStatus = "Printer out of paper"
- return
- end
- local screenTerminal = term.current()
- local printerTerminal = {
- getCursorPos = printer.getCursorPos,
- setCursorPos = printer.setCursorPos,
- getSize = printer.getPageSize,
- write = printer.write,
- }
- printerTerminal.scroll = function()
- if nPage == 1 then
- printer.setPageTitle( sName.." (page "..nPage..")" )
- end
- while not printer.newPage() do
- if printer.getInkLevel() < 1 then
- sStatus = "Printer out of ink, please refill"
- elseif printer.getPaperLevel() < 1 then
- sStatus = "Printer out of paper, please refill"
- else
- sStatus = "Printer output tray full, please empty"
- end
- term.redirect( screenTerminal )
- redrawMenu()
- term.redirect( printerTerminal )
- local timer = os.startTimer(0.5)
- sleep(0.5)
- end
- nPage = nPage + 1
- if nPage == 1 then
- printer.setPageTitle( sName )
- else
- printer.setPageTitle( sName.." (page "..nPage..")" )
- end
- end
- bMenu = false
- term.redirect( printerTerminal )
- local ok, error = pcall( function()
- term.scroll()
- for n, sLine in ipairs( tLines ) do
- print( sLine )
- end
- end )
- term.redirect( screenTerminal )
- if not ok then
- print( error )
- end
- while not printer.endPage() do
- sStatus = "Printer output tray full, please empty"
- redrawMenu()
- sleep( 0.5 )
- end
- bMenu = true
- if nPage > 1 then
- sStatus = "Printed "..nPage.." Pages"
- else
- sStatus = "Printed 1 Page"
- end
- redrawMenu()
- end,
- Exit = function()
- bRunning = false
- end,
- Run = function()
- local sTempPath = "/.temp"
- local ok, err = save( sTempPath )
- if ok then
- local nTask = shell.openTab( sTempPath )
- if nTask then
- shell.switchTab( nTask )
- else
- sStatus="Error starting Task"
- end
- fs.delete( sTempPath )
- else
- sStatus="Error saving to "..sTempPath
- end
- redrawMenu()
- end,
- Settings = function()
- -- MENU FOR NEW SETTINGS --
- _menu()
- -- RESET --
- redrawMenu()
- redrawText()
- end,
- Save_and_exit = function()
- if bReadOnly then
- sStatus = "Access denied"
- else
- local ok, err = save( sPath )
- if ok then
- sStatus="Saved to "..sPath
- else
- sStatus="Error saving to "..sPath
- end
- end
- bRunning = false
- end,
- Go = function()
- term.setCursorBlink(true)
- redrawText()
- term.setCursorPos(w-string.len( "Ln "..y ), h)
- term.setBackgroundColor( barColour )
- term.setTextColor( colours.white )
- term.clearLine()
- for i=1, w-string.len( "Ln "..y )-1 do
- write(" ")
- end
- term.setCursorPos(w-string.len( "Ln " )-3, h)
- write("Ln ")
- local numbs = 0
- local numbsArray = {}
- while true do
- local sEvent, i = os.pullEvent() sleep(.00000000001)
- if sEvent == "key" then
- i = keys.getName(i)
- 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
- numbs = numbs + 1
- if i == "one" or i == "numPad1" then
- numbsArray[numbs] = 1
- elseif i == "two" or i == "numPad2" then
- numbsArray[numbs] = 2
- elseif i == "three" or i == "numPad3" then
- numbsArray[numbs] = 3
- elseif i == "four" or i == "numPad4" then
- numbsArray[numbs] = 4
- elseif i == "five" or i == "numPad5" then
- numbsArray[numbs] = 5
- elseif i == "six" or i == "numPad6" then
- numbsArray[numbs] = 6
- elseif i == "seven" or i == "numPad7" then
- numbsArray[numbs] = 7
- elseif i == "eight" or i == "numPad8" then
- numbsArray[numbs] = 8
- elseif i == "nine" or i == "numPad9" then
- numbsArray[numbs] = 9
- elseif i == "zero" or i == "numPad0" then
- numbsArray[numbs] = 0
- end
- elseif i == "enter" then
- break
- elseif i == "backspace" then
- if numbs > 0 then
- table.remove(numbsArray, #numbsArray)
- numbs = numbs - 1
- end
- elseif i == "rightCtrl" or i == "leftCtrl" then
- numbs = 0
- numbsArray = {}
- break
- end
- term.setCursorPos(w-string.len( "Ln "..y )+1, h)
- write(" ")
- term.setCursorPos(w-string.len( "Ln "..y )+1, h)
- for i=1, #numbsArray do
- write(numbsArray[i])
- end
- if numbs == 3 then sleep(.1) break end
- elseif sEvent == "mouse_click" then
- break
- end
- end
- if #numbsArray > 0 then
- numbs = ""
- for i=1, #numbsArray do
- numbs = numbs..tostring(numbsArray[i])
- end
- numbs = tonumber(numbs)
- local cx,cy = 1,1
- local newY = math.min( math.max( scrollY + cy, 1 ), #tLines )
- local newX = math.min( math.max( scrollX + cx, 1 ), string.len( tLines[newY] ) + 1 )
- if numbs > #tLines then
- setCursor(newX,#tLines)
- elseif numbs < 1 then
- setCursor(newX,1)
- else
- setCursor(newX,numbs)
- end
- local ocx,ocy = term.getCursorPos()
- local cx,cy = 999,ocy
- local newY = math.min( math.max( scrollY + cy, 1 ), #tLines )
- local newX = math.min( math.max( scrollX + cx, 1 ), string.len( tLines[newY] ) + 1 )
- setCursor(newX,newY)
- end
- end,
- Go_up = function()
- setCursor(1,1)
- end,
- Go_down = function()
- setCursor(1,#tLines)
- end,
- Clear_file = function()
- tLines = {} table.insert( tLines, "" ) setCursor(1,1)
- end
- }
- local function doMenuItem( _n )
- tMenuFuncs[tMenuItems[_n]]()
- if bMenu then
- bMenu = false
- term.setCursorBlink( true )
- end
- redrawMenu() redrawText()
- end
- -- Actual program functionality begins
- load(sPath)
- term.setBackgroundColour( bgColour )
- term.clear()
- term.setCursorPos(x,y)
- term.setCursorBlink( true )
- recomplete()
- redrawText()
- redrawMenu()
- local function acceptCompletion()
- if nCompletion then
- -- Append the completion
- local sCompletion = tCompletions[ nCompletion ]
- tLines[y] = tLines[y] .. sCompletion
- setCursor( x + string.len( sCompletion ), y )
- end
- end
- -- Handle input
- while bRunning do
- local sEvent, param, param2, param3 = os.pullEvent()
- if sEvent == "key" then
- local oldX, oldY = x, y
- if param == keys.up then
- -- Up
- if not bMenu then
- if nCompletion then
- -- Cycle completions
- nCompletion = nCompletion - 1
- if nCompletion < 1 then
- nCompletion = #tCompletions
- end
- redrawLine(y)
- elseif y > 1 then
- -- Move cursor up
- setCursor(
- math.min( x, string.len( tLines[y - 1] ) + 1 ),
- y - 1
- )
- end
- else
- -- Menu Up
- nMenuItem = nMenuItem - 1
- if nMenuItem < 1 then
- nMenuItem = #tMenuItems
- end
- redrawMenu(false)
- end
- elseif param == keys.down then
- -- Down
- if not bMenu then
- -- Move cursor down
- if nCompletion then
- -- Cycle completions
- nCompletion = nCompletion + 1
- if nCompletion > #tCompletions then
- nCompletion = 1
- end
- redrawLine(y)
- elseif y < #tLines then
- -- Move cursor down
- setCursor(
- math.min( x, string.len( tLines[y + 1] ) + 1 ),
- y + 1
- )
- end
- else
- -- Menu Down
- nMenuItem = nMenuItem + 1
- if nMenuItem > #tMenuItems then
- nMenuItem = 1
- end
- redrawMenu(false)
- end
- elseif param == keys.tab then
- -- Tab
- if not bMenu and not bReadOnly then
- if nCompletion and x == string.len(tLines[y]) + 1 then
- -- Accept autocomplete
- acceptCompletion()
- else
- -- Indent line
- local sLine = tLines[y]
- tLines[y] = string.sub(sLine,1,x-1) .. " " .. string.sub(sLine,x)
- setCursor( x + 4, y )
- end
- end
- elseif param == keys.pageUp then
- -- Page Up
- if not bMenu then
- -- Move up a page
- local newY
- if y - (h - 1) >= 1 then
- newY = y - (h - 1)
- else
- newY = 1
- end
- setCursor(
- math.min( x, string.len( tLines[newY] ) + 1 ),
- newY
- )
- end
- elseif param == keys.pageDown then
- -- Page Down
- if not bMenu then
- -- Move down a page
- local newY
- if y + (h - 1) <= #tLines then
- newY = y + (h - 1)
- else
- newY = #tLines
- end
- local newX = math.min( x, string.len( tLines[newY] ) + 1 )
- setCursor( newX, newY )
- end
- elseif param == keys.home then
- -- Home
- if not bMenu then
- -- Move cursor to the beginning
- if x > 1 then
- setCursor(1,y)
- end
- end
- elseif param == keys["end"] then
- -- End
- if not bMenu then
- -- Move cursor to the end
- local nLimit = string.len( tLines[y] ) + 1
- if x < nLimit then
- setCursor( nLimit, y )
- end
- end
- elseif param == keys.left then
- -- Left
- if not bMenu then
- if x > 1 then
- -- Move cursor left
- setCursor( x - 1, y )
- elseif x==1 and y>1 then
- setCursor( string.len( tLines[y-1] ) + 1, y - 1 )
- end
- else
- -- Move menu left
- nMenuItem = nMenuItem - 1
- if nMenuItem < 1 then
- nMenuItem = #tMenuItems
- end
- redrawMenu()
- end
- elseif param == keys.right then
- -- Right
- if not bMenu then
- local nLimit = string.len( tLines[y] ) + 1
- if x < nLimit then
- -- Move cursor right
- setCursor( x + 1, y )
- elseif nCompletion and x == string.len(tLines[y]) + 1 then
- -- Accept autocomplete
- acceptCompletion()
- elseif x==nLimit and y<#tLines then
- -- Go to next line
- setCursor( 1, y + 1 )
- end
- else
- -- Move menu right
- nMenuItem = nMenuItem + 1
- if nMenuItem > #tMenuItems then
- nMenuItem = 1
- end
- redrawMenu()
- end
- elseif param == keys.delete then
- -- Delete
- if not bMenu and not bReadOnly then
- local nLimit = string.len( tLines[y] ) + 1
- if x < nLimit then
- local sLine = tLines[y]
- tLines[y] = string.sub(sLine,1,x-1) .. string.sub(sLine,x+1)
- recomplete()
- redrawLine(y)
- elseif y<#tLines then
- tLines[y] = tLines[y] .. tLines[y+1]
- table.remove( tLines, y+1 )
- recomplete()
- redrawText()
- end
- end
- elseif param == keys.backspace then
- -- Backspace
- if not bMenu and not bReadOnly then
- if x > 1 then
- -- Remove character
- local sLine = tLines[y]
- if x > 4 and string.sub(sLine,x-4,x-1) == " " and not string.sub(sLine, 1, x - 1):find("%S") then
- tLines[y] = string.sub(sLine,1,x-5) .. string.sub(sLine,x)
- setCursor( x - 4, y )
- else
- tLines[y] = string.sub(sLine,1,x-2) .. string.sub(sLine,x)
- setCursor( x - 1, y )
- end
- elseif y > 1 then
- -- Remove newline
- local sPrevLen = string.len( tLines[y-1] )
- tLines[y-1] = tLines[y-1] .. tLines[y]
- table.remove( tLines, y )
- setCursor( sPrevLen + 1, y - 1 )
- redrawText()
- end
- end
- elseif param == keys.enter then
- -- Enter
- if not bMenu and not bReadOnly then
- -- Newline
- local sLine = tLines[y]
- local _,spaces=string.find(sLine,"^[ ]+")
- if not spaces then
- spaces=0
- end
- tLines[y] = string.sub(sLine,1,x-1)
- table.insert( tLines, y+1, string.rep(' ',spaces)..string.sub(sLine,x) )
- setCursor( spaces + 1, y + 1 )
- redrawText()
- elseif bMenu then
- -- Menu selection
- doMenuItem( nMenuItem )
- redrawText()
- end
- elseif param == keys.f1 or param == keys.leftCtrl or param == keys.rightCtrl or param == keys.rightAlt then
- -- Menu toggle
- bMenu = not bMenu
- if bMenu then
- term.setCursorBlink( false )
- else
- term.setCursorBlink( true )
- redrawText()
- end
- redrawMenu()
- end
- elseif sEvent == "char" then
- if not bMenu and not bReadOnly then
- -- Input text
- local sLine = tLines[y]
- tLines[y] = string.sub(sLine,1,x-1) .. param .. string.sub(sLine,x)
- setCursor( x + 1, y )
- elseif bMenu then
- -- Select menu items
- for n,sMenuItem in ipairs( tMenuItems ) do
- if string.lower(string.sub(sMenuItem,1,1)) == string.lower(param) then
- doMenuItem( n )
- break
- end
- end
- end
- elseif sEvent == "paste" then
- if not bReadOnly then
- -- Close menu if open
- if bMenu then
- bMenu = false
- term.setCursorBlink( true )
- redrawMenu()
- end
- -- Input text
- local sLine = tLines[y]
- tLines[y] = string.sub(sLine,1,x-1) .. param .. string.sub(sLine,x)
- setCursor( x + string.len( param ), y )
- end
- elseif sEvent == "mouse_click" then
- if not bMenu then
- if param == 1 then
- -- Left click
- local cx,cy = param2, param3
- if cx == 1 and cy == h then
- bMenu = not bMenu
- if bMenu then
- term.setCursorBlink( false )
- else
- term.setCursorBlink( true )
- redrawText()
- end
- redrawMenu()
- elseif cy < h then
- local newY = math.min( math.max( scrollY + cy, 1 ), #tLines )
- local newX = math.min( math.max( scrollX + cx, 1 ), string.len( tLines[newY] ) + 1 )
- setCursor( newX, newY )
- elseif cy == h and cx >= w-string.len( "Ln "..y )+1 then
- term.setCursorPos(w-string.len( "Ln "..y ), h)
- term.setBackgroundColor( barColour )
- term.setTextColor( colours.white )
- term.clearLine()
- for i=1, w-string.len( "Ln "..y )-1 do
- write(" ")
- end
- term.setCursorPos(w-string.len( "Ln " )-3, h)
- write("Ln ")
- local numbs = 0
- local numbsArray = {}
- while true do
- local sEvent, i = os.pullEvent() sleep(.00000000001)
- if sEvent == "key" then
- i = keys.getName(i)
- 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
- numbs = numbs + 1
- if i == "one" or i == "numPad1" then
- numbsArray[numbs] = 1
- elseif i == "two" or i == "numPad2" then
- numbsArray[numbs] = 2
- elseif i == "three" or i == "numPad3" then
- numbsArray[numbs] = 3
- elseif i == "four" or i == "numPad4" then
- numbsArray[numbs] = 4
- elseif i == "five" or i == "numPad5" then
- numbsArray[numbs] = 5
- elseif i == "six" or i == "numPad6" then
- numbsArray[numbs] = 6
- elseif i == "seven" or i == "numPad7" then
- numbsArray[numbs] = 7
- elseif i == "eight" or i == "numPad8" then
- numbsArray[numbs] = 8
- elseif i == "nine" or i == "numPad9" then
- numbsArray[numbs] = 9
- elseif i == "zero" or i == "numPad0" then
- numbsArray[numbs] = 0
- end
- elseif i == "enter" then
- break
- elseif i == "backspace" then
- if numbs > 0 then
- table.remove(numbsArray, #numbsArray)
- numbs = numbs - 1
- end
- elseif i == "rightCtrl" or i == "leftCtrl" then
- numbs = 0
- numbsArray = {}
- break
- end
- term.setCursorPos(w-string.len( "Ln "..y )+1, h)
- write(" ")
- term.setCursorPos(w-string.len( "Ln "..y )+1, h)
- for i=1, #numbsArray do
- write(numbsArray[i])
- end
- if numbs == 3 then sleep(.1) break end
- elseif sEvent == "mouse_click" then
- break
- end
- end
- if #numbsArray > 0 then
- numbs = ""
- for i=1, #numbsArray do
- numbs = numbs..tostring(numbsArray[i])
- end
- numbs = tonumber(numbs)
- local cx,cy = 1,1
- local newY = math.min( math.max( scrollY + cy, 1 ), #tLines )
- local newX = math.min( math.max( scrollX + cx, 1 ), string.len( tLines[newY] ) + 1 )
- if numbs > #tLines then
- setCursor(newX,#tLines)
- elseif numbs < 1 then
- setCursor(newX,1)
- else
- setCursor(newX,numbs)
- end
- local ocx,ocy = term.getCursorPos()
- local cx,cy = 999,ocy
- local newY = math.min( math.max( scrollY + cy, 1 ), #tLines )
- local newX = math.min( math.max( scrollX + cx, 1 ), string.len( tLines[newY] ) + 1 )
- setCursor(newX,newY)
- end
- redrawMenu()
- redrawText()
- end
- elseif param == 2 then
- term.setCursorBlink(false)
- -- Right click
- local _rMenu = {}
- local _rMenuMax
- local quad = 0
- local cx,cy = param2,param3
- --local newY = math.min( math.max( scrollY + cy, 1 ), #tLines )
- --local newX = math.min( math.max( scrollX + cx, 1 ), string.len( tLines[newY] ) + 1 )
- --setCursor( newX, newY )
- table.insert( _rMenu, "Go" )
- table.insert( _rMenu, "Go"..string.char(24) )
- table.insert( _rMenu, "Go"..string.char(25) )
- if not bReadOnly then
- table.insert( _rMenu, "Save" )
- table.insert( _rMenu, "Save and Exit" )
- end
- if shell.openTab and run_programs then
- table.insert( _rMenu, "Run" )
- end
- if peripheral.find( "printer" ) then
- table.insert( _rMenu, "Print" )
- end
- table.insert( _rMenu, "Exit" )
- _rMenuMax = 1
- local function tsearch(tbl, str)
- for i=1, #tbl do
- if tostring(tbl[i]) == tostring(str) then return i end
- end
- return false
- end
- local function runCommand(opt)
- opt = _rMenu[opt]
- if opt == "Go"..string.char(24) then
- -- Up
- tMenuFuncs["Go_up"]()
- elseif opt == "Go"..string.char(25) then
- -- Down
- tMenuFuncs["Go_down"]()
- elseif opt == "Save and Exit" then
- -- Save_and_exit
- tMenuFuncs["Save_and_exit"]()
- elseif opt == "Clear File" then
- tMenuFuncs["Clear_file"]()
- else
- tMenuFuncs[opt]()
- end
- end
- local function _updateMenu() -- Runs every time the menu's position changes
- if cx == 1 and cy == 1 then
- if tsearch( _rMenu, "Clear File" ) == false then
- table.insert( _rMenu, "Clear File" )
- end
- else
- if tsearch( _rMenu, "Clear File" ) ~= false then
- table.remove( _rMenu, tsearch( _rMenu, "Clear File" ) )
- end
- end
- for i=1, #_rMenu do
- if string.len(_rMenu[i]) > _rMenuMax then _rMenuMax = string.len(_rMenu[i]) end
- end
- end
- local function _displayMenu() -- Runs every time the menu needs a refresh
- term.setBackgroundColor( colours.white )
- term.setTextColor( colours.black )
- for i=1, #_rMenu do
- if cx < w-_rMenuMax then
- if cy > h-#_rMenu then
- term.setCursorPos(cx+1,cy-i)
- quad = 1
- else
- term.setCursorPos(cx+1,cy+i)
- quad = 4
- end
- else
- if cy > #_rMenu then
- term.setCursorPos(cx-_rMenuMax,cy-i)
- quad = 2
- else
- term.setCursorPos(cx-_rMenuMax,cy+i)
- quad = 3
- end
- end
- write(_rMenu[i])
- for i=1, _rMenuMax-string.len(_rMenu[i]) do
- write(" ")
- end
- print(" ")
- sleep(.005)
- end
- _rM = true
- end
- local _upd = true
- _updateMenu()
- while true do
- if _upd then _displayMenu() _upd = not _upd end
- local sEventt, paramx, paramy, paramz = os.pullEvent()
- if sEventt == "key" then
- if paramx == keys.leftCtrl or paramx == keys.rightCtrl then
- bMenu = true
- break
- end
- elseif sEventt == "mouse_click" then
- ocx,ocy = cx,cy
- cx,cy = paramy,paramz
- if paramx == 1 then
- -- left
- if cx > ocx and cx < _rMenuMax+ocx and cy > ocy and cy < ocy+_rMenuMax and quad == 4 then
- -- bottom right IV
- --error("IV: "..math.abs(ocy-cy))
- runCommand(math.abs(ocy-cy))
- elseif cx > ocx and cx < _rMenuMax+ocx and cy < ocy and cy > ocy-_rMenuMax and quad == 1 then
- -- top right I
- --error("I: "..math.abs(ocy-cy))
- runCommand(math.abs(ocy-cy))
- elseif cx < ocx and cx > _rMenuMax-ocx and cy < ocy and cy > ocy-_rMenuMax and quad == 2 then
- -- top left II
- --error("II: "..math.abs(ocy-cy))
- runCommand(math.abs(ocy-cy))
- elseif cx < ocx and cx > _rMenuMax-ocx and cy > ocy and cy < ocy+_rMenuMax and quad == 3 then
- -- bottom left III
- --error("III: "..math.abs(ocy-cy))
- runCommand(math.abs(ocy-cy))
- else
- local newY = math.min( math.max( scrollY + cy, 1 ), #tLines )
- local newX = math.min( math.max( scrollX + cx, 1 ), string.len( tLines[newY] ) + 1 )
- setCursor( newX, newY )
- end
- break
- elseif paramx == 2 then
- -- right
- cx,cy = paramy,paramz
- redrawText()
- _updateMenu() _upd = true
- end
- end
- end
- -- End
- term.setCursorPos(x,y)
- term.setCursorBlink(true)
- redrawText()
- redrawMenu()
- end
- else
- local cx,cy = param2, param3
- if cx == 1 and cy == h then
- bMenu = not bMenu
- redrawMenu()
- term.setCursorBlink(true)
- local newY = math.min( math.max( scrollY + cy, 1 ), #tLines )
- local newX = math.min( math.max( scrollX + cx, 1 ), string.len( tLines[newY] ) + 1 )
- setCursor( newX, newY )
- elseif cx <= 11 and cy > h-#tMenuItems-2 then
- --bMenu = not bMenu
- doMenuItem( cy-h+6 )
- redrawText()
- else
- bMenu = not bMenu
- redrawMenu()
- term.setCursorBlink(true)
- local newY = math.min( math.max( scrollY + cy, 1 ), #tLines )
- local newX = math.min( math.max( scrollX + cx, 1 ), string.len( tLines[newY] ) + 1 )
- setCursor( newX, newY )
- end
- end
- elseif sEvent == "mouse_scroll" then
- if not bMenu then
- if param == -1 then
- -- Scroll up
- if scrollY > 0 then
- -- Move cursor up
- scrollY = scrollY - 1
- redrawText()
- end
- elseif param == 1 then
- -- Scroll down
- local nMaxScroll = #tLines - (h-1)
- if scrollY < nMaxScroll then
- -- Move cursor down
- scrollY = scrollY + 1
- redrawText()
- end
- end
- end
- elseif sEvent == "mouse_drag" and not bMenu then
- -- DRAG WORKS
- elseif sEvent == "term_resize" then
- w,h = term.getSize()
- setCursor( x, y )
- redrawMenu()
- redrawText()
- end
- end
- -- Cleanup
- term.setBackgroundColor( colours.black )
- term.setTextColor( colours.white )
- term.clear()
- term.setCursorBlink( false )
- term.setCursorPos( 1, 1 )
Advertisement
Add Comment
Please, Sign In to add comment