KinoftheFlames

Untitled

Sep 20th, 2012
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.63 KB | None | 0 0
  1. --get filepath
  2. local arg = { ... }
  3. if #arg == 0 then
  4.     print("Syntax: editor <path>")
  5.     return
  6. end
  7.  
  8. --constants
  9. local TIMER_INTERVAL = .1 --updates the screen this often (in seconds)
  10. local X_MIN, Y_MIN = 5, 1 --min values for file display
  11. local X_MAX, Y_MAX = term.getSize() --max values for file display
  12.  
  13. --flags
  14. local bDebug = true --display debugging info if true
  15. local bInsert = true --insert text if true, override text if false
  16.  
  17. --variables
  18. local sFilepath = arg[1] --path to the file being editted
  19. local tFile = { } --table of lines for the file being editted
  20. local xFile, yFile = 1, 1 --the location of the cursor in the text file
  21. local xScroll, yScroll = 0, 0 --amount file is scrolled on screen
  22. local sDebugText = "" --debug text shown if debugging is on
  23.  
  24. --load a file from hdd into editor
  25. function load()
  26.     --error handling file
  27.     if fs.exists(sFilepath) then
  28.         if fs.isDir(sFilepath) then
  29.             print("Cannot read directory as a file.")
  30.             error() --exit program
  31.         else
  32.             --load file
  33.             local file = fs.open(sFilepath, "r")
  34.             while true do
  35.                 local line = file.readLine()
  36.                 if line == nil then
  37.                     break end
  38.                
  39.                 table.insert(tFile, line)
  40.             end
  41.             file.close()
  42.         end
  43.     else
  44.         table.insert(tFile, "")
  45.     end
  46. end
  47.  
  48. --intialization
  49. function intialize()
  50.     load() --load file
  51.    
  52.     --setup screen
  53.     term.setCursorPos(X_MIN,Y_MIN)
  54.     term.setCursorBlink(true)
  55.     draw()
  56. end
  57.  
  58. --moves cursor around
  59. function shiftCursor(xShift,yShift)
  60.     local x,y = term.getCursorPos()
  61.    
  62.     --handle left/right movement
  63.     if xFile == 1 and xShift < 0 then --if at start of line and moving left, move to end of above line (if exists)
  64.         if yFile ~= 1 then
  65.             xFile = #tFile[yFile-1] + 1
  66.             yFile = yFile - 1
  67.             x = X_MIN + xFile - 1 + xScroll
  68.             y = y - 1
  69.         end
  70.     elseif xFile > #tFile[yFile] and xShift > 0 then --if at end of line and moving right, move to start of next line (if exists)
  71.         if yFile ~= #tFile then
  72.             xFile = 1
  73.             yFile = yFile + 1
  74.             x = X_MIN - xScroll
  75.             y = y + 1
  76.         end
  77.     else --regular movement
  78.         xFile = xFile + xShift
  79.         x = x + xShift
  80.     end
  81.    
  82.     --handle up/down movement
  83.     if yFile == 1 and yShift < 0 then --if at first line and moving up, move to start of line
  84.         xFile = 1
  85.         x = X_MIN - xScroll
  86.     elseif yFile == #tFile and yShift > 0 then --if at last line and moving down, move to end of line
  87.         xFile = #tFile[yFile] + 1
  88.         x = X_MIN + xFile - 1 - xScroll
  89.     else --regular movement
  90.         yFile = yFile + yShift
  91.         y = y + yShift
  92.        
  93.         --if the cursor is past the end of the last character after having moved, move it to the last character
  94.         if xFile > #tFile[yFile] then
  95.             xFile = 1 + #tFile[yFile]
  96.             x = X_MIN + #tFile[yFile] - xScroll
  97.         end
  98.     end
  99.    
  100.     sDebugText = sDebugText .. " MIN/MAX:"..X_MIN..","..X_MAX
  101.    
  102.     --set cursor pos
  103.     term.setCursorPos(x, y)
  104. end
  105.  
  106. --move cursor to the start of the line
  107. function shiftCursorHome()
  108.     local x, y = term.getCursorPos()
  109.     xFile = 1
  110.     term.setCursorPos(X_MIN - xScroll, y)
  111. end
  112.  
  113. --move cursor to the end of the line (end of text)
  114. function shiftCursorEnd()
  115.     local x, y = term.getCursorPos()
  116.     xFile = 1 + #tFile[yFile]
  117.     term.setCursorPos(X_MIN + #tFile[yFile] - xScroll, y)
  118. end
  119.  
  120. --save editor's text to a file
  121. function save()
  122.     file = fs.open(sFilepath, "w")
  123.     for i,line in ipairs(tFile) do
  124.         file.writeLine(line) end
  125.     file.close()
  126. end
  127.  
  128. function write(sText)
  129.     tFile[yFile] = string.sub(tFile[yFile], 1, xFile-1) .. sText .. string.sub(tFile[yFile], xFile)
  130.     shiftCursor(1,0)
  131. end
  132.  
  133. --display debug info in bottom right
  134. function drawDebug(sText)
  135.     local x,y = term.getCursorPos()
  136.    
  137.     term.setCursorPos(X_MAX + 1 - #sText, Y_MAX) --set cursor at bottom rightmost while still showing all text
  138.     term.write(sText)
  139.    
  140.     term.setCursorPos(x, y)
  141. end
  142.  
  143. --scrolls the screen with the cursor
  144. function scroll()
  145.     local x,y = term.getCursorPos()
  146.    
  147.     if y > Y_MAX then
  148.         yScroll = yScroll + (y - Y_MAX)
  149.         y = Y_MAX
  150.     elseif y < Y_MIN then
  151.         yScroll = yScroll - (Y_MIN - y)
  152.         y = Y_MIN
  153.     end
  154.     if x > X_MAX then
  155.         xScroll = xScroll + (x - X_MAX)
  156.         x = X_MAX
  157.     elseif x < X_MIN then
  158.         xScroll = xScroll - (X_MIN - x)
  159.         x = X_MIN
  160.     end
  161.    
  162.     sDebugText = sDebugText .. " Scroll:"..xScroll..","..yScroll
  163.    
  164.     term.setCursorPos(x, y)
  165. end
  166.  
  167. --redraws entire screen
  168. function draw()
  169.     local x,y = term.getCursorPos()
  170.     term.clear()
  171.    
  172.     --draw line numbers
  173.     for i=Y_MIN,Y_MAX do
  174.         if tFile[i+yScroll] ~= nil then
  175.             term.setCursorPos(1, i)
  176.             if i+yScroll < 100 then term.write(" ") end
  177.             if i+yScroll < 10 then term.write(" ") end
  178.             term.write(i + yScroll)
  179.         end
  180.     end
  181.    
  182.     --draw line number seperator
  183.     for i=Y_MIN,Y_MAX do
  184.         term.setCursorPos(X_MIN-1, i)
  185.         term.write("|")
  186.     end
  187.    
  188.     --draw file text
  189.     --TODO stop draw at last visible line for code effeciency
  190.     for i=Y_MIN,Y_MAX do
  191.         if tFile[i+yScroll] ~= nil then
  192.             term.setCursorPos(X_MIN, i)
  193.             term.write(string.sub(tFile[i+yScroll], 1 + xScroll, X_MAX - X_MIN + 1 + xScroll))
  194.         end
  195.     end
  196.    
  197.     --draw debug
  198.     if bDebug then
  199.         drawDebug(sDebugText) end
  200.     term.setCursorPos(x, y)
  201. end
  202.  
  203. --main loop
  204. function run()
  205.     while true do
  206.         sDebugText = ""
  207.         --wait for key input
  208.         local e, p1 = os.pullEvent()
  209.        
  210.         if e == "char" then --handles character input
  211.             write(p1)
  212.         elseif e == "key" then -- handles non-character input
  213.             --DONT USE F2, F10, F11, ALT, ESC
  214.            
  215.             --functional keys
  216.             if p1 == keys.f1 then -- F1 - menu
  217.                
  218.             elseif p1 == keys.f3 then -- F3 - save
  219.                 save()
  220.                 sDebugText = "SAVED"
  221.             elseif p1 == keys.f4 then -- F4 - exit
  222.                 term.clear()
  223.                 term.setCursorPos(1,1)
  224.                 return
  225.            
  226.             --arrow keys
  227.             elseif p1 == keys.up then
  228.                 shiftCursor(0, -1)
  229.             elseif p1 == keys.down then
  230.                 shiftCursor(0, 1)
  231.             elseif p1 == keys.left then
  232.                 shiftCursor(-1, 0)
  233.             elseif p1 == keys.right then
  234.                 shiftCursor(1, 0)
  235.            
  236.             --backspace, delete
  237.             elseif p1 == keys.backspace then
  238.                 if not (yFile == 1 and xFile == 1) then
  239.                     if xFile == 1 then --if at the start of a line, concatenate line above with this line
  240.                         shiftCursor(0,-1)
  241.                         shiftCursorEnd()
  242.                        
  243.                         tFile[yFile] = tFile[yFile] .. tFile[yFile+1] --concat lines
  244.                         table.remove(tFile, yFile+1) --remove second line
  245.                     else --otherwise just delete one character
  246.                         shiftCursor(-1,0)
  247.                         tFile[yFile] = string.sub(tFile[yFile], 1, xFile-1) .. string.sub(tFile[yFile], xFile+1)
  248.                     end
  249.                 end
  250.             elseif p1 == keys.delete then --delete
  251.                 if not (yFile == #tFile and xFile == #tFile[yFile] + 1) then
  252.                     if xFile > #tFile[yFile] then --if cursor is at the end of the line, concatenate with line below
  253.                         tFile[yFile] = tFile[yFile] .. tFile[yFile+1] --concat lines
  254.                         table.remove(tFile, yFile+1) --remove second line
  255.                     else --otherwise just delete one character
  256.                         tFile[yFile] = string.sub(tFile[yFile], 1, xFile-1) .. string.sub(tFile[yFile], xFile+1)
  257.                     end
  258.                 end
  259.            
  260.             --enter, home, end
  261.             elseif p1 == keys.enter then
  262.                 if xFile <= #tFile[yFile] then --if cursor is between characters
  263.                     table.insert(tFile, yFile+1, string.sub(tFile[yFile], xFile)) --move remainder of characters to a new line
  264.                     tFile[yFile] = string.sub(tFile[yFile], 1, xFile-1) --and delete the moved characters from the previous line
  265.                 else --cursor at the end of the line
  266.                     table.insert(tFile, yFile+1, "")
  267.                 end
  268.                 shiftCursorHome()
  269.                 shiftCursor(0,1)
  270.             elseif p1 == keys.home then
  271.                 shiftCursorHome()
  272.             elseif p1 == keys["end"] then
  273.                 shiftCursorEnd()
  274.             elseif p1 == keys.home then
  275.                 shiftCursorHome()
  276.             end
  277.         end
  278.        
  279.         local x,y = term.getCursorPos()
  280.         sDebugText = sDebugText .. " File:"..xFile..","..yFile .." Scrn:"..x..","..y
  281.         scroll()
  282.         draw()
  283.     end
  284. end
  285.  
  286. intialize()
  287. run()
Advertisement
Add Comment
Please, Sign In to add comment