Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function print(sText,...)
- table.insert(tLines,tostring(sText))
- if ... then print(...) end
- end
- function trim(sText,maxlen)
- local tSegments={}
- while #sText>maxlen do
- local start1,fin1=1,0
- while true do
- local start2,fin2=sText:find("%s+",fin1+1)
- if not start2 or fin2>=maxlen then
- break
- else
- start1,fin1=start2,fin2
- end
- end
- table.insert(tSegments,sText:sub(1,start1==1 and maxlen or start1-1))
- sText=sText:sub(start1==1 and maxlen+1 or start1):gsub("^%s+","")
- end
- table.insert(tSegments,sText)
- return tSegments
- end
- function dispPrint(t,sAppend,offsetx,offsety)
- if not t then
- tTemp={}
- end
- local maxlen=love.graphics.getWidth()/7
- for i=1,#tLines do
- for sLine in string.gmatch(tostring(tLines[i]),"[^\n]+") do
- local tSegments=trim(sLine,maxlen)
- for i=1,#tSegments do
- table.insert(tTemp,tSegments[i])
- end
- end
- end
- if not t then
- 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)
- end
- end
- function love.load()
- love.window.setTitle("KaoConsole")
- bConsoleActive=true
- love.keyboard.setKeyRepeat(0.5,0.05)
- nShowCursor=0
- tLines={}
- love.graphics.setFont(love.graphics.newFont("lucon.ttf",12))
- nElapsed=0
- tHist_MT={__index=function(self,index) return tCMDHistory[index] end}
- tTempHistory=setmetatable({},tHist_MT)
- tCMDHistory={""}
- nAtHistory=#tCMDHistory
- lastkey=""
- sCMD=""
- nCursorPos=#sCMD+1
- sCMDOutput=""
- end
- function love.update(nSec)
- nElapsed=nElapsed+nSec
- nShowCursor=nShowCursor-nSec
- end
- local function execString(sCMD)
- func,output=loadstring("return "..sCMD)
- if not func then func,output=loadstring(sCMD) end
- if type(func)=="function" then
- setfenv(func,getfenv())
- output={pcall(func)}
- print(unpack(output,2))
- else
- print(output)
- end
- end
- local function addStr(sText)
- nShowCursor=1
- sCMD=(nCursorPos>1 and sCMD:sub(1,nCursorPos-1) or "")..sText..sCMD:sub(nCursorPos)
- nCursorPos=nCursorPos+#sText
- end
- function love.textinput(sText)
- addStr(sText)
- end
- function love.keypressed(key)
- if bConsoleActive then
- lastkey=key
- if key=="v" and love.keyboard.isDown("lctrl","rctrl") then --ctrl+v
- addStr(love.system.getClipboardText())
- elseif key=="backspace" then --backspace
- nShowCursor=1
- sCMD=(nCursorPos>2 and sCMD:sub(1,nCursorPos-2) or "")..sCMD:sub(nCursorPos)
- nCursorPos=math.max(nCursorPos-1,1)
- elseif key=="delete" then --delete
- nShowCursor=1
- sCMD=(nCursorPos>2 and sCMD:sub(1,nCursorPos-1) or "")..sCMD:sub(nCursorPos+1)
- elseif key=="left" then --left arrow
- if love.keyboard.isDown("lctrl","rctrl") then --if control is held then jump to the previous word
- local start1,fin1=1,0
- while true do
- local start2,fin2=sCMD:find("%w+",fin1+1)
- if not start2 or fin2>=nCursorPos then
- nCursorPos=start1
- break
- else
- start1,fin1=start2,fin2
- end
- end
- else --move the cursor 1 to the left
- nShowCursor=1
- nCursorPos=math.max(nCursorPos-1,1)
- end
- elseif key=="right" then --right arrow
- if love.keyboard.isDown("lctrl","rctrl") then --if control is held then jump to the next word
- local start,fin=0,0
- while true do
- start,fin=sCMD:find("%w+",fin+1)
- if not start or start>nCursorPos then
- nCursorPos=start or #sCMD+1
- break
- end
- end
- else --move the cursor 1 to the right
- nShowCursor=1
- nCursorPos=math.min(nCursorPos+1,#sCMD+1)
- end
- elseif key=="up" then --up arrow
- nShowCursor=1
- tTempHistory[nAtHistory]=sCMD
- nAtHistory=math.max(nAtHistory-1,1)
- sCMD=tTempHistory[nAtHistory]
- nCursorPos=#sCMD+1
- elseif key=="down" then --down arrow
- nShowCursor=1
- tTempHistory[nAtHistory]=sCMD
- nAtHistory=math.min(nAtHistory+1,math.max(#tCMDHistory,#tTempHistory))
- sCMD=tTempHistory[nAtHistory]
- nCursorPos=#sCMD+1
- elseif key=="home" then
- nShowCursor=1
- nCursorPos=1
- elseif key=="end" then
- nShowCursor=1
- nCursorPos=#sCMD+1
- elseif key=="return" or key=="kpenter" then --enter
- nShowCursor=1
- print("> "..sCMD)
- tCMDHistory[tCMDHistory[nAtHistory]=="" and nAtHistory or #tCMDHistory]=sCMD
- execString(sCMD)
- sCMD=""
- nCursorPos=1
- if tCMDHistory[#tCMDHistory]~="" then
- table.insert(tCMDHistory,"")
- end
- nAtHistory=#tCMDHistory
- tTempHistory=setmetatable({},tHist_MT)
- end
- end
- end
- function love.draw()
- if bConsoleActive then
- love.graphics.print("CMD History #"..nAtHistory.."/"..#tCMDHistory.." "..lastkey,0,0)
- 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")
- dispPrint(nil,sCMDDisp,0,12)
- --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)
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment