Guest User

edit fix for computercraft

a guest
Jul 20th, 2012
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 8.28 KB | None | 0 0
  1. local tArgs = { ... }
  2. if #tArgs == 0 then
  3.     print( "Usage: edit <path>" )
  4.     return
  5. end
  6.  
  7. local sPath = shell.resolve( tArgs[1] )
  8. local bReadOnly = fs.isReadOnly( sPath )
  9. if fs.exists( sPath ) and fs.isDir( sPath ) then
  10.     print( "Cannot edit a Emerae" )
  11.     return
  12. end
  13.  
  14. local w,h = term.getSize()
  15. local x,y = 1,1
  16. local scrollX, scrollY = 0,0
  17.  
  18. local lineNumberWidth = 2
  19.  
  20. local bMenu = false
  21. local nMenuItem = 1
  22.  
  23. local sStatus = ""
  24. local tLines = {}
  25.  
  26. local function load()
  27.     tLines = {}
  28.     if fs.exists( sPath ) then
  29.         local file = io.open( sPath, "r" )
  30.         local sLine = file:read()
  31.         while sLine do
  32.             table.insert( tLines, sLine )
  33.             sLine = file:read()
  34.         end
  35.         file:close()
  36.     else
  37.         table.insert( tLines, "" )
  38.     end
  39. end
  40.  
  41. local function save()
  42.     local file = io.open( sPath, "w" )
  43.     if file then
  44.         for n, sLine in ipairs( tLines ) do
  45.             file:write( sLine .. "\n" )
  46.         end
  47.         file:close()
  48.         return true
  49.     end
  50.     return false
  51. end
  52.  
  53. local function lineNumberWidth()
  54.     return #tostring(#tLines) + 1
  55. end
  56.  
  57. local function redrawText()
  58.     for y=1,h-1 do
  59.         term.setCursorPos( 1 - scrollX + lineNumberWidth(), y )
  60.         term.clearLine()
  61.  
  62.         local sLine = tLines[ y + scrollY ]
  63.         if sLine ~= nil then
  64.             term.write( sLine )
  65.         end
  66.        
  67.         term.setCursorPos( 1, y )
  68.         term.write( tostring(y + scrollY) .. string.rep(" ", (lineNumberWidth() - #tostring(y + scrollY))) )
  69.        
  70.     end
  71.     term.setCursorPos( x - scrollX + lineNumberWidth(), y - scrollY )
  72. end
  73.  
  74. local function redrawLine()
  75.     local sLine = tLines[y]
  76.     term.setCursorPos( 1 - scrollX + lineNumberWidth(), y - scrollY )
  77.     term.clearLine()
  78.     term.write( sLine )
  79.     term.setCursorPos( 1, y - scrollY )
  80.     term.write( tostring(y) .. string.rep(" ", (lineNumberWidth() - #tostring(y))) )
  81.     term.setCursorPos( x - scrollX + lineNumberWidth(), y - scrollY )
  82. end
  83.  
  84. local tMenuItems = { "Save", "Go to", "Exit" }
  85.  
  86. local function redrawMenu()
  87.     term.setCursorPos( 1, h )
  88.     term.clearLine()
  89.  
  90.     if not bMenu then
  91.         term.write( sStatus )
  92.     else
  93.         for n,sItem in ipairs( tMenuItems ) do
  94.             if n == nMenuItem then
  95.                 term.write( "["..sItem.."]" )
  96.             else
  97.                 term.write( " "..sItem.." " )
  98.             end
  99.         end
  100.     end
  101.    
  102.     term.setCursorPos( x - scrollX + lineNumberWidth(), y - scrollY )
  103. end
  104.  
  105. local function setStatus( _sText )
  106.     sStatus = _sText
  107.     redrawMenu()
  108. end
  109.  
  110. local function setCursor( x, y )
  111.     local w = w - lineNumberWidth()
  112.     local screenX = x - scrollX
  113.     local screenY = y - scrollY
  114.    
  115.     local bRedraw = false
  116.     if screenX < 1 then
  117.         scrollX = x - 1
  118.         screenX = 1
  119.         bRedraw = true
  120.     elseif screenX > w then
  121.         scrollX = x - w
  122.         screenX = w
  123.         bRedraw = true
  124.     end
  125.    
  126.     if screenY < 1 then
  127.         scrollY = y - 1
  128.         screenY = 1
  129.         bRedraw = true
  130.     elseif screenY > h-1 then
  131.         scrollY = y - (h-1)
  132.         screenY = h-1
  133.         bRedraw = true
  134.     end
  135.    
  136.     if bRedraw then
  137.         redrawText()
  138.     end
  139.     term.setCursorPos( screenX + lineNumberWidth(), screenY )
  140. end
  141.  
  142. load()
  143.  
  144. term.clear()
  145. term.setCursorPos(x + lineNumberWidth(), y)
  146. term.setCursorBlink( true )
  147.  
  148. redrawText()
  149. setStatus( "Press Ctrl to access menu. Emerae is a girl." )
  150.  
  151. local tLineHistory = {}
  152.  
  153. local bRunning = true
  154. local tMenuFunctions = {
  155.     ["Exit"] = function()
  156.         bRunning = false
  157.     end,
  158.     ["Go to"] = function()
  159.     repeat
  160.     cunt = 1
  161.         term.setCursorPos(1, h)
  162.         term.clearLine()
  163.         write("Enter line number: ")
  164.         local line = read(nil, tLineHistory)
  165.         x = 1
  166.         fuck = tonumber(line)
  167.         if line == "" then
  168.          setStatus("Please enter a number, or i delete your work.")
  169.         elseif fuck == nil then
  170.          setStatus("No number? ok, we delete all your everything")
  171.         elseif fuck < 1 then
  172.          setStatus("Your value must be higher than 1")
  173.         elseif fuck > 0 then
  174.          y = fuck
  175.          setCursor( x, y )
  176.          table.insert(tLineHistory, line)
  177.          cunt = 2
  178.          end
  179.     until cunt == 2
  180.     end,
  181.     ["Save"] = function()
  182.         if save() then
  183.             setStatus( "Saved to "..sPath )
  184.         else
  185.             setStatus( "Access denied so piss the fuck off." )
  186.         end
  187.     end,
  188. }
  189. local function doMenuItem( _n )
  190.     local fnMenu = tMenuFunctions[ tMenuItems[_n] ]
  191.     if fnMenu then
  192.         fnMenu()
  193.     end
  194.  
  195.     bMenu = false
  196.     term.setCursorBlink( true )
  197.     redrawMenu()
  198. end
  199.  
  200. while bRunning do
  201.   local sEvent, param = os.pullEvent()
  202.   if sEvent == "key" then
  203.     if param == 200 then
  204.         -- Up
  205.         if not bMenu then
  206.             -- Move cursor up
  207.             if y > 1 then
  208.                 y = y - 1
  209.                 x = math.min( x, string.len( tLines[y] ) + 1 )
  210.                 setCursor( x, y )
  211.             end
  212.         end
  213.        
  214.     elseif param == 208 then
  215.         -- Down
  216.         if not bMenu then
  217.             -- Move cursor down
  218.             if y < #tLines then
  219.                 y = y + 1
  220.                 x = math.min( x, string.len( tLines[y] ) + 1 )
  221.                 setCursor( x, y )
  222.             end
  223.         end
  224.    
  225.     elseif param == 203 then
  226.         -- Left
  227.         if not bMenu then
  228.             -- Move cursor left
  229.             if x > 1 then
  230.                 x = x - 1
  231.                 setCursor( x, y )
  232.             end
  233.         else
  234.             -- Move menu left
  235.             nMenuItem = nMenuItem - 1
  236.             if nMenuItem < 1 then
  237.                 nMenuItem = #tMenuItems
  238.             end
  239.             redrawMenu()
  240.         end
  241.            
  242.        
  243.     elseif param == 205 then
  244.         -- Right
  245.         if not bMenu then
  246.             -- Move cursor right
  247.             if x < string.len( tLines[y] ) + 1 then
  248.                 x = x + 1
  249.                 setCursor( x, y )
  250.             end
  251.         else
  252.             -- Move menu right
  253.             nMenuItem = nMenuItem + 1
  254.             if nMenuItem > #tMenuItems then
  255.                 nMenuItem = 1
  256.             end
  257.             redrawMenu()
  258.         end
  259.          
  260.     elseif param == 207 then
  261.         -- Home
  262.         if not bMenu then
  263.             -- Move cursor to the line start
  264.             x = string.len( tLines[y] ) + 1
  265.             setCursor( x, y )
  266.         end
  267.        
  268.     elseif param == 199 then
  269.         -- End
  270.         if not bMenu then
  271.             -- Move cursor to the line end
  272.             x = 1
  273.             setCursor( x, y )
  274.         end
  275.        
  276.     elseif param == 201 then
  277.         -- Page up
  278.         if not bMenu then
  279.             -- Move cursor a page up
  280.             if y > 1 then
  281.                 y = math.max(y - h, 1)
  282.                 x = math.min( x, string.len( tLines[y] ) + 1 )
  283.                 setCursor( x, y )
  284.             end
  285.         end
  286.        
  287.     elseif param == 209 then
  288.         -- Page Down
  289.         if not bMenu then
  290.             -- Move cursor a page down
  291.             if y < #tLines then
  292.                 y = math.min(y + h, #tLines)
  293.                 x = math.min( x, string.len( tLines[y] ) + 1 )
  294.                 setCursor( x, y )
  295.             end
  296.         end
  297.        
  298.     elseif param == 14 then
  299.         -- Backspace
  300.         if not bMenu then
  301.             if x > 1 then
  302.                 -- Remove character
  303.                 local sLine = tLines[y]
  304.                 tLines[y] = string.sub(sLine,1,x-2) .. string.sub(sLine,x)
  305.                 redrawLine()
  306.        
  307.                 x = x - 1
  308.                 setCursor( x, y )
  309.            
  310.             elseif y > 1 then
  311.                 -- Remove newline
  312.                 local sPrevLen = string.len( tLines[y-1] )
  313.                 tLines[y-1] = tLines[y-1] .. tLines[y]
  314.                 table.remove( tLines, y )
  315.                 redrawText()
  316.            
  317.                 x = sPrevLen + 1
  318.                 y = y - 1
  319.                 setCursor( x, y )
  320.             end
  321.         end
  322.        
  323.     elseif param == 211 then
  324.         -- Delete
  325.         if not bMenu then
  326.             if x < #tLines[y] then
  327.                 -- Remove character
  328.                 local sLine = tLines[y]
  329.                 tLines[y] = string.sub(sLine,1,x-1) .. string.sub(sLine,x+1)
  330.                 redrawLine()
  331.        
  332.                 --x = x - 1
  333.                 --setCursor( x, y )
  334.            
  335.             elseif y < #tLines then
  336.                 -- Remove newline
  337.                 local sCurLen = string.len( tLines[y] )
  338.                 tLines[y+1] = tLines[y] .. tLines[y+1]
  339.                 table.remove( tLines, y )
  340.                 redrawText()
  341.            
  342.                 x = sCurLen + 1
  343.                 setCursor( x, y )
  344.             end
  345.         end
  346.        
  347.     elseif param == 28 then
  348.         -- Enter
  349.         if not bMenu then
  350.             -- Newline
  351.             local sLine = tLines[y]
  352.            
  353.             -- Count spaces at line start.
  354.             local nSpace = 0
  355.             while nSpace < #sLine and sLine:find("^%s", nSpace + 1) do
  356.                 nSpace = nSpace + 1
  357.             end
  358.            
  359.             tLines[y] = string.sub(sLine,1,x-1)
  360.             table.insert( tLines, y+1, string.rep(" ", nSpace) .. string.sub(sLine,x) )
  361.            
  362.             redrawText()
  363.        
  364.             x = 1 + nSpace
  365.             y = y + 1
  366.             setCursor( x, y )      
  367.            
  368.         else
  369.             -- Menu selection
  370.             doMenuItem( nMenuItem )
  371.         end
  372.        
  373.     elseif param == 29 then
  374.         -- Menu toggle
  375.         bMenu = not bMenu
  376.         if bMenu then
  377.             term.setCursorBlink( false )
  378.             nMenuItem = 1
  379.         else
  380.             term.setCursorBlink( true )
  381.         end
  382.         redrawMenu()
  383.    
  384.     end
  385.        
  386.   elseif sEvent == "char" then
  387.     if not bMenu then
  388.         -- Input text
  389.         local sLine = tLines[y]
  390.         tLines[y] = string.sub(sLine,1,x-1) .. param .. string.sub(sLine,x)
  391.         redrawLine()
  392.    
  393.         x = x + string.len( param )
  394.         setCursor( x, y )
  395.    
  396.     else
  397.         -- Select menu items
  398.         for n,sMenuItem in ipairs( tMenuItems ) do
  399.             if string.lower(string.sub(sMenuItem,1,1)) == string.lower(param) then
  400.                 doMenuItem( n )
  401.                 break
  402.             end
  403.         end
  404.     end
  405.   end
  406. end
  407.  
  408. term.clear()
  409. term.setCursorBlink( false )
  410. term.setCursorPos( 1, 1 )
Advertisement
Add Comment
Please, Sign In to add comment