Guest User

rom/programs/edit

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