KaoSDlanor

Lua Console

Feb 6th, 2014
244
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.07 KB | None | 0 0
  1. function print(sText,...)
  2.     table.insert(tLines,tostring(sText))
  3.     if ... then print(...) end
  4. end
  5.  
  6. function trim(sText,maxlen)
  7.     local tSegments={}
  8.     while #sText>maxlen do
  9.         local start1,fin1=1,0
  10.         while true do
  11.             local start2,fin2=sText:find("%s+",fin1+1)
  12.             if not start2 or fin2>=maxlen then
  13.                 break
  14.             else
  15.                 start1,fin1=start2,fin2
  16.             end
  17.         end
  18.         table.insert(tSegments,sText:sub(1,start1==1 and maxlen or start1-1))
  19.         sText=sText:sub(start1==1 and maxlen+1 or start1):gsub("^%s+","")
  20.     end
  21.     table.insert(tSegments,sText)
  22.     return tSegments
  23. end
  24.  
  25. function dispPrint(t,sAppend,offsetx,offsety)
  26.     if not t then
  27.         tTemp={}
  28.     end
  29.     local maxlen=love.graphics.getWidth()/7
  30.     for i=1,#tLines do
  31.         for sLine in string.gmatch(tostring(tLines[i]),"[^\n]+") do
  32.             local tSegments=trim(sLine,maxlen)
  33.             for i=1,#tSegments do
  34.                 table.insert(tTemp,tSegments[i])
  35.             end
  36.         end
  37.     end
  38.     if not t then
  39.         love.graphics.print(table.concat(tTemp,"\n",math.max(1,math.floor(#tTemp-love.graphics.getHeight()/12)+3))..(sAppend or ""),offsetx or 0,offsety or 0)
  40.     end
  41. end
  42.  
  43. function love.load()
  44.     love.window.setTitle("KaoConsole")
  45.     bConsoleActive=true
  46.     love.keyboard.setKeyRepeat(0.5,0.05)
  47.     nShowCursor=0
  48.     tLines={}
  49.     love.graphics.setFont(love.graphics.newFont("lucon.ttf",12))
  50.     nElapsed=0
  51.     tHist_MT={__index=function(self,index) return tCMDHistory[index] end}
  52.     tTempHistory=setmetatable({},tHist_MT)
  53.     tCMDHistory={""}
  54.     nAtHistory=#tCMDHistory
  55.     lastkey=""
  56.     sCMD=""
  57.     nCursorPos=#sCMD+1
  58.     sCMDOutput=""
  59. end
  60.  
  61. function love.update(nSec)
  62.     nElapsed=nElapsed+nSec
  63.     nShowCursor=nShowCursor-nSec
  64. end
  65.  
  66. local function execString(sCMD)
  67.     func,output=loadstring("return "..sCMD)
  68.     if not func then func,output=loadstring(sCMD) end
  69.     if type(func)=="function" then
  70.         setfenv(func,getfenv())
  71.         output={pcall(func)}
  72.         print(unpack(output,2))
  73.     else
  74.         print(output)
  75.     end
  76. end
  77.  
  78. local function addStr(sText)
  79.     nShowCursor=1
  80.     sCMD=(nCursorPos>1 and sCMD:sub(1,nCursorPos-1) or "")..sText..sCMD:sub(nCursorPos)
  81.     nCursorPos=nCursorPos+#sText
  82. end
  83.  
  84. function love.textinput(sText)
  85.     addStr(sText)
  86. end
  87.  
  88. function love.keypressed(key)
  89.     if bConsoleActive then
  90.         lastkey=key
  91.         if key=="v" and love.keyboard.isDown("lctrl","rctrl") then --ctrl+v
  92.             addStr(love.system.getClipboardText())
  93.         elseif key=="backspace" then --backspace
  94.             nShowCursor=1
  95.             sCMD=(nCursorPos>2 and sCMD:sub(1,nCursorPos-2) or "")..sCMD:sub(nCursorPos)
  96.             nCursorPos=math.max(nCursorPos-1,1)
  97.         elseif key=="delete" then --delete
  98.             nShowCursor=1
  99.             sCMD=(nCursorPos>2 and sCMD:sub(1,nCursorPos-1) or "")..sCMD:sub(nCursorPos+1)
  100.         elseif key=="left" then --left arrow
  101.  
  102.             if love.keyboard.isDown("lctrl","rctrl") then --if control is held then jump to the previous word
  103.                 local start1,fin1=1,0
  104.                 while true do
  105.                     local start2,fin2=sCMD:find("%w+",fin1+1)
  106.                     if not start2 or fin2>=nCursorPos then
  107.                         nCursorPos=start1
  108.                         break
  109.                     else
  110.                         start1,fin1=start2,fin2
  111.                     end
  112.                 end
  113.             else --move the cursor 1 to the left
  114.                 nShowCursor=1
  115.                 nCursorPos=math.max(nCursorPos-1,1)
  116.             end
  117.  
  118.         elseif key=="right" then --right arrow
  119.  
  120.             if love.keyboard.isDown("lctrl","rctrl") then --if control is held then jump to the next word
  121.                 local start,fin=0,0
  122.                 while true do
  123.                     start,fin=sCMD:find("%w+",fin+1)
  124.                     if not start or start>nCursorPos then
  125.                         nCursorPos=start or #sCMD+1
  126.                         break
  127.                     end
  128.                 end
  129.             else --move the cursor 1 to the right
  130.                 nShowCursor=1
  131.                 nCursorPos=math.min(nCursorPos+1,#sCMD+1)
  132.             end
  133.  
  134.         elseif key=="up" then --up arrow
  135.             nShowCursor=1
  136.             tTempHistory[nAtHistory]=sCMD
  137.             nAtHistory=math.max(nAtHistory-1,1)
  138.             sCMD=tTempHistory[nAtHistory]
  139.             nCursorPos=#sCMD+1
  140.         elseif key=="down" then --down arrow
  141.             nShowCursor=1
  142.             tTempHistory[nAtHistory]=sCMD
  143.             nAtHistory=math.min(nAtHistory+1,math.max(#tCMDHistory,#tTempHistory))
  144.             sCMD=tTempHistory[nAtHistory]
  145.             nCursorPos=#sCMD+1
  146.         elseif key=="home" then
  147.             nShowCursor=1
  148.             nCursorPos=1
  149.         elseif key=="end" then
  150.             nShowCursor=1
  151.             nCursorPos=#sCMD+1
  152.         elseif key=="return" or key=="kpenter" then --enter
  153.             nShowCursor=1
  154.             print("> "..sCMD)
  155.             tCMDHistory[tCMDHistory[nAtHistory]=="" and nAtHistory or #tCMDHistory]=sCMD
  156.             execString(sCMD)
  157.             sCMD=""
  158.             nCursorPos=1
  159.             if tCMDHistory[#tCMDHistory]~="" then
  160.                 table.insert(tCMDHistory,"")
  161.             end
  162.             nAtHistory=#tCMDHistory
  163.             tTempHistory=setmetatable({},tHist_MT)
  164.         end
  165.     end
  166. end
  167.  
  168. function love.draw()
  169.     if bConsoleActive then
  170.         love.graphics.print("CMD History #"..nAtHistory.."/"..#tCMDHistory.."     "..lastkey,0,0)
  171.         local sCMDDisp=table.concat(trim((#tLines>0 and "\n" or "").."> "..(nCursorPos>1 and sCMD:sub(1,nCursorPos-1) or "")..((nShowCursor>0 or math.floor(nElapsed*2)%2==0) and "|" or sCMD:sub(nCursorPos,nCursorPos))..sCMD:sub(nCursorPos+1),love.graphics.getWidth()/7),"\n")
  172.         dispPrint(nil,sCMDDisp,0,12)
  173.         --love.graphics.print(lastkey.."\n"..table.concat(tCMDHistory,"\n").."\n"..(nCursorPos>#sCMD and sCMD or (nCursorPos>1 and sCMD:sub(1,nCursorPos-1) or "").." "..sCMD:sub(nCursorPos)).."\n\n"..tostring(sCMDOutput),0,0)
  174.     end
  175. end
Advertisement
Add Comment
Please, Sign In to add comment