Advertisement
ROFLCopter64bit

EasyDev

Jun 26th, 2014
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.72 KB | None | 0 0
  1. local increase = 5
  2.  
  3. local function tConcat(t1,t2)
  4.     for i=1,#t2 do
  5.         t1[#t1+1] = t2[i]
  6.     end
  7.     return t1
  8. end
  9.  
  10. local tArgs = { ... }
  11. if #tArgs >= 1 then
  12.     if math.floor(tonumber(tArgs[1])) > 0 then --error checking
  13.             increase = math.floor(tonumber(tArgs[1]))
  14.     end
  15. end
  16.  
  17. local tFilesUsed = {} --preventing repetition
  18.  
  19. local tKeywords = {
  20.     ["and"] = true,
  21.     ["break"] = true,
  22.     ["do"] = true,
  23.     ["else"] = true,
  24.     ["elseif"] = true,
  25.     ["end"] = true,
  26.     ["false"] = true,
  27.     ["for"] = true,
  28.     ["function"] = true,
  29.     ["if"] = true,
  30.     ["in"] = true,
  31.     ["local"] = true,
  32.     ["nil"] = true,
  33.     ["not"] = true,
  34.     ["or"] = true,
  35.     ["repeat"] = true,
  36.     ["return"] = true,
  37.     ["then"] = true,
  38.     ["true"] = true,
  39.     ["until"] = true,
  40.     ["while"] = true
  41. }
  42.  
  43. local w,h = term.getSize()
  44. local X, Y = 1, 2
  45.  
  46. local tLines = {}
  47.  
  48. -- Colours
  49. local highlightColour, keywordColour, commentColour, textColour, bgColour
  50. if term.isColour() then
  51.     bgColour = colours.black
  52.     textColour = colours.white
  53.     highlightColour = colours.yellow
  54.     keywordColour = colours.blue
  55.     commentColour = colours.green
  56.     stringColour = colours.red
  57. else
  58.     bgColour = colours.black
  59.     textColour = colours.white
  60.     highlightColour = colours.white
  61.     keywordColour = colours.white
  62.     commentColour = colours.white
  63.     stringColour = colours.white
  64. end
  65.  
  66. local function tryWrite( sLine, regex, colour )
  67.     local match = string.match( sLine, regex )
  68.     if match then
  69.         if type(colour) == "number" then
  70.             term.setTextColour( colour )
  71.         else
  72.             term.setTextColour( colour(match) )
  73.         end
  74.         term.write( match )
  75.         term.setTextColour( textColour )
  76.         return string.sub( sLine, string.len(match) + 1 )
  77.     end
  78.     return nil
  79. end
  80.  
  81. local function writeHighlighted( sLine )
  82.     while string.len(sLine) > 0 do 
  83.         sLine =
  84.             tryWrite( sLine, "^%-%-%[%[.-%]%]", commentColour ) or
  85.             tryWrite( sLine, "^%-%-.*", commentColour ) or
  86.             tryWrite( sLine, "^\".-[^\\]\"", stringColour ) or
  87.             tryWrite( sLine, "^\'.-[^\\]\'", stringColour ) or
  88.             tryWrite( sLine, "^%[%[.-%]%]", stringColour ) or
  89.             tryWrite( sLine, "^[%w_]+", function( match )
  90.                 if tKeywords[ match ] then
  91.                     return keywordColour
  92.                 end
  93.                 return textColour
  94.             end ) or
  95.             tryWrite( sLine, "^[^%w_]", textColour )
  96.     end
  97. end
  98. local function redrawLine() --this is taken from edit, since I tried to do this myself but bios.lua kept throwing a fit
  99.     local sLine = tLines[Y]
  100.     --print(Y, ';')
  101.     term.setCursorPos( 1, math.min(h, Y) )
  102.     term.clearLine()
  103.     writeHighlighted( sLine )
  104. end
  105.  
  106. while true do
  107.     term.clear()
  108.     term.setCursorPos(1, 1)
  109.     if term.isColor() then
  110.         term.setTextColor(colors.yellow)
  111.     end
  112.    
  113.     tFiles = tConcat(fs.find('/rom/apis/*'), fs.find('/rom/programs/*'))
  114.    
  115.     if #tFilesUsed == #tFiles then
  116.         tFilesUsed = {}
  117.     end
  118.    
  119.     local i = 0
  120.     repeat
  121.         i = math.random(#tFiles)
  122.     until tFilesUsed[i] == nil
  123.    
  124.     local f = fs.open(tFiles[i], 'r')
  125.     --local f2 = fs.open(fs.combine('/rom/programs', tFiles[i]), 'r')
  126.    
  127.     --if f2 then f = f2 end
  128.     if f then --protecting against specific apis, such as turtle or advanced that is not in use
  129.         tLines = {}
  130.         Y = 1
  131.         X = 1
  132.         table.insert(tLines, 'Creating ' .. tFiles[i])
  133.         redrawLine()
  134.         Y = 2
  135.         table.insert(tLines, ' ')
  136.         redrawLine()   
  137.         local str = f.readAll()
  138.         f.close()
  139.         if term.isColor() then
  140.             term.setTextColor(colors.white)
  141.         end
  142.                
  143.         local pos = 1
  144.         local continue = true
  145.        
  146.         local currentStr = ''
  147.         local currentPos = 0
  148.         local linePos = 0
  149.         while str:sub(pos, pos + increase - 1) ~= '' and continue do
  150.             local sEvent, nKey = os.pullEvent()
  151.             if sEvent == 'key' then
  152.                 if nKey == 1 then --esc
  153.                     term.clear()
  154.                     term.setCursorPos(1, 1)
  155.                     if term.isColor() then
  156.                         term.setTextColor(colors.white)
  157.                     end
  158.                     return --exit
  159.                 elseif nKey == 14 or nKey == 211 then --backspace or delete
  160.                     --pretend this does something, i haven't figured out what to do
  161.                 elseif nKey == 28 then --enter
  162.                     continue = false --next file
  163.                 else
  164.                     for i = 1, increase do
  165.                         local c = str:sub(pos, pos)
  166.                         currentPos = currentPos + 1
  167.                         linePos = linePos + 1
  168.                        
  169.                         X = (currentPos % w - 1) + 1
  170.                         --Y = Y + math.floor(linePos / w)
  171.                        
  172.                         if c == '\n' or #tLines == 0 or math.floor(linePos / w) == 1 then
  173.                             table.insert(tLines, '')
  174.                             currentStr = ''
  175.                             currentPos = 0
  176.                             linePos = 0
  177.                             Y = Y + 1
  178.                             X = 1
  179.                            
  180.                             if Y >= h then print(' ') end
  181.                         else
  182.                             currentStr = currentStr .. c
  183.                             tLines[#tLines] = currentStr
  184.                             --print('fef')
  185.                             redrawLine()           
  186.                             --print('feef')
  187.                         end
  188.                         pos = pos + 1
  189.                                                
  190.                         --write(c)
  191.                     end
  192.                 end
  193.             end
  194.         end    
  195.     end
  196.     tFilesUsed[i] = tFiles[i] --adding it to the table
  197. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement