Sparta252

Saviho Edit

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