KinoftheFlames

Untitled

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