Advertisement
Guest User

demo1

a guest
Feb 22nd, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.79 KB | None | 0 0
  1. --ad3aUsVw
  2. string.rpad = function(str, len, char)
  3.     return str..string.rep(char,len-#str)
  4. end
  5.  
  6.  
  7.  
  8. y = colors.yellow
  9. b = colors.lightBlue;
  10. o = colors.orange
  11. trigtarget = {" and"," or"," no"," function"," local"," do","if","else","elseif"," then ","while","repeat","until","print","end"}
  12. trigresults = {y,y,y,o,o,y,y,y,y,y,y,b,o,b,o}
  13. y = "4";
  14. b = "9"
  15. o = "1"
  16. qText = ""
  17. qFore = ""
  18. qBack = ""
  19. cBackgroundColor = "f" -- current black background
  20. cForegroundColor = "0" -- current white foreground
  21. trigblitcode = {y,y,y,o,o,y,y,y,y,y,y,b,o,b,o}
  22. args = {...}
  23. HEADER_SIZE = 2;
  24. function round(num, numDecimalPlaces)
  25.     local mult = 10^(numDecimalPlaces or 0)
  26.     return math.floor(num*mult + 0.5)/mult;
  27. end
  28. function isDigit(c)
  29.     local p = string.byte(c)
  30.     return p >= 48 and p<= 57;
  31. end
  32. function genFontTable(ML)
  33.     local isString = false;
  34.     local comment = false;
  35.     local cidx = 1
  36.     local vp = "";
  37.     fonts = {}
  38.     while (cidx <= #ML) do
  39.         if(ML:sub(cidx,cidx) == "\"" or ML:sub(cidx,cidx) == "\'") then
  40.             isString = not isString
  41.         end
  42.         if(ML:sub(cidx-1,cidx) == "--") then
  43.             comment = true;
  44.         end
  45.        
  46.         if(comment) then
  47.             fonts[cidx] = "d"
  48.         elseif(isString or ML:sub(cidx,cidx) == "\"" or ML:sub(cidx,cidx) == "\'") then
  49.             fonts[cidx] = "e"
  50.         elseif(isDigit(ML:sub(cidx,cidx))) then
  51.             fonts[cidx] = "3"
  52.         elseif(ML:sub(cidx,cidx) == "#") then
  53.             fonts[cidx] = "3"
  54.         else
  55.             local foundmatch = false;
  56.             for i = 1,#trigtarget do
  57.                 if(ML:sub(cidx-#trigtarget[i]+1,cidx) == trigtarget[i]) then
  58.                 --print("matched: "..trigtarget[i])
  59.                     foundmatch = true;
  60.                     for overwriteindex=cidx-#trigtarget[i]+1,cidx do
  61.                         fonts[overwriteindex] = trigblitcode[i];
  62.                     end
  63.                 end
  64.             end
  65.             if(foundmatch == false) then
  66.                 fonts[cidx] = "0";
  67.             end
  68.         end
  69.         cidx = cidx+1;
  70.        
  71.     end
  72.     return fonts;
  73. end
  74. function qWrite(car)
  75.     qText = qText..car
  76.     qFore = qFore..cForegroundColor;
  77.     qBack = qBack..cBackgroundColor;
  78. end
  79. function qClear()
  80.     qText = ""
  81.     qFore = ""
  82.     qBack = ""
  83. end
  84. function qPush(mon,vvq)
  85.     mon.setCursorPos(1,vvq)
  86.     mon.blit(qText,qFore,qBack)
  87.     qClear()
  88. end
  89. function renderProgram(name,cline,ccol,cdata,mon,linestoRender)
  90.     srend = os.time()
  91.     -- render on advapi?
  92.     program = split(cdata,"\n")
  93.     w,l = mon.getSize()
  94.     offsetrender = 0
  95.     l = l-3; -- keep 3 for debug space.
  96.     if(ccol > w-3) then
  97.         offsetrender = ccol-w+3
  98.     end
  99.     start_line = math.max(0,cline-l+4);
  100.     mon.setTextColor(colors.white)
  101.     mon.setBackgroundColor(colors.black)
  102.     mon.setCursorPos(1,1)
  103.     --mon.write("Current File: ");
  104.     mon.setTextColor(colors.red);
  105.     mon.write(name);
  106.     mon.setTextColor(colors.white)
  107.     local paddedline = string.format("%03d", cline);
  108.     local paddedcol =  string.format("%03d",ccol);
  109.     mon.write("[Line "..paddedline.."] [Col"..paddedcol.."]")  
  110.     for current_line = 0,linestoRender do
  111.         local c = current_line + start_line+1;
  112.         if(program[c] ~= nil) then
  113.             toDisplay = program[c]:sub(offsetrender,offsetrender+w);
  114.         else
  115.             toDisplay = string.rep(" ",w)
  116.  
  117.         end
  118.         mon.setTextColor(colors.white)
  119.         cForegroundColor = "0"
  120.        
  121.         --mon.setCursorPos(1,c+HEADER_SIZE-start_line);
  122.         if(c ~= cline) then
  123.             --mon.setBackgroundColor(colors.black)
  124.             --mon.setTextColor(colors.white)
  125.             cBackgroundColor = "f"
  126.    --print(toDisplay)
  127.    dtx = string.rpad(toDisplay,w+1," ");
  128.             fonttable = genFontTable(dtx);
  129.             for font_index=1,#dtx do
  130.                 cForegroundColor = fonttable[font_index]
  131.                 qWrite(dtx:sub(font_index,font_index))
  132.             end
  133.         else
  134.             local fonttable = genFontTable(toDisplay);
  135.             for disp = 0,w do
  136.                 r = toDisplay:sub(disp+1,disp+1);
  137.                 renderunderscore = false
  138.                 if(offsetrender + disp+1 == ccol) then
  139.                     if(r == " ") then
  140.                         cBackgroundColor = "d"
  141.                     else
  142.                         cForegroundColor = "d"
  143.                     end
  144.                     renderunderscore = true    
  145.                 else
  146.                     if(fonttable[disp+1] ~= nil) then
  147.                         cForegroundColor = fonttable[disp + 1]
  148.                     else
  149.                         cForegroundColor = "0"
  150.                     end
  151.                     cBackgroundColor = "f"
  152.                 end
  153.                
  154.                 if(#toDisplay:sub(disp+1,disp+1)== 1) then
  155.                     qWrite(toDisplay:sub(disp+1,disp+1));
  156.                 elseif((renderunderscore == true)and (#toDisplay:sub(disp+1,disp+1) == 0)) then
  157.                     qWrite("_");
  158.                 elseif (#toDisplay:sub(disp+1,disp+1) == 0) then
  159.                     qWrite(" ")
  160.                 else
  161.                 end
  162.              end
  163.         end
  164.        
  165.         qPush(mon,c+HEADER_SIZE-start_line);
  166.     end
  167.     --mon.setCursorPos(1,l+1)
  168.     --mon.setTextColor(colors.blue);
  169.     --mon.write("Debug: "..w.." "..l.." "..offsetrender.." "..start_line)
  170.  erend = round(os.time()-srend,4)
  171.  mon.setCursorPos(35,1)
  172.  mon.setTextColor(colors.green)
  173.  mon.write(erend.." on "..#cdata.."B") 
  174. end
  175.  
  176.  
  177. function split(str, on)
  178.   local ret = {};
  179.   local s, e;
  180.   while str:find(on) do
  181.     s, e = str:find(on);
  182.     table.insert(ret, str:sub(0, s - 1));
  183.     str = str:sub(e + 1);
  184.   end
  185.   table.insert(ret, str);
  186.   return ret;
  187. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement