2011-08-29 01:04:36 +02:00
|
|
|
-- configs
|
2013-02-28 22:39:27 +01:00
|
|
|
local LogColors = { [LogDebug] = 'pink',
|
|
|
|
[LogInfo] = 'white',
|
2011-08-29 01:04:36 +02:00
|
|
|
[LogWarning] = 'yellow',
|
|
|
|
[LogError] = 'red' }
|
2013-03-01 01:35:14 +01:00
|
|
|
local MaxLogLines = 128
|
2012-01-15 16:13:22 +01:00
|
|
|
local MaxHistory = 1000
|
2011-08-29 00:07:22 +02:00
|
|
|
|
2013-02-28 22:39:27 +01:00
|
|
|
local oldenv = getfenv(0)
|
|
|
|
setfenv(0, _G)
|
|
|
|
commandEnv = runinsandbox('commands')
|
|
|
|
setfenv(0, oldenv)
|
|
|
|
|
2011-08-29 00:07:22 +02:00
|
|
|
-- private variables
|
2012-03-26 20:33:00 +02:00
|
|
|
local terminalWindow
|
2012-01-07 18:36:58 +01:00
|
|
|
local terminalButton
|
2011-08-20 22:30:41 +02:00
|
|
|
local logLocked = false
|
2012-03-31 15:43:01 +02:00
|
|
|
local commandTextEdit
|
2012-01-07 18:36:58 +01:00
|
|
|
local terminalBuffer
|
2011-08-28 23:32:24 +02:00
|
|
|
local commandHistory = { }
|
|
|
|
local currentHistoryIndex = 0
|
2013-03-01 00:10:36 +01:00
|
|
|
local poped = false
|
|
|
|
local oldPos
|
|
|
|
local oldSize
|
|
|
|
local firstShown = false
|
2013-03-01 01:35:14 +01:00
|
|
|
local flushEvent
|
|
|
|
local cachedLines = {}
|
|
|
|
local disabled = false
|
2011-08-20 22:30:41 +02:00
|
|
|
|
2011-08-29 00:07:22 +02:00
|
|
|
-- private functions
|
|
|
|
local function navigateCommand(step)
|
2011-08-28 23:32:24 +02:00
|
|
|
local numCommands = #commandHistory
|
|
|
|
if numCommands > 0 then
|
|
|
|
currentHistoryIndex = math.min(math.max(currentHistoryIndex + step, 0), numCommands)
|
|
|
|
if currentHistoryIndex > 0 then
|
|
|
|
local command = commandHistory[numCommands - currentHistoryIndex + 1]
|
2012-03-31 15:43:01 +02:00
|
|
|
commandTextEdit:setText(command)
|
2013-01-25 12:44:15 +01:00
|
|
|
commandTextEdit:setCursorPos(-1)
|
2011-08-28 23:32:24 +02:00
|
|
|
else
|
2012-03-31 15:43:01 +02:00
|
|
|
commandTextEdit:clearText()
|
2011-08-28 23:32:24 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-11-01 19:32:48 +01:00
|
|
|
local function completeCommand()
|
2012-03-31 15:43:01 +02:00
|
|
|
local cursorPos = commandTextEdit:getCursorPos()
|
2011-11-01 19:32:48 +01:00
|
|
|
if cursorPos == 0 then return end
|
|
|
|
|
2012-03-31 15:43:01 +02:00
|
|
|
local commandBegin = commandTextEdit:getText():sub(1, cursorPos)
|
2011-11-01 19:32:48 +01:00
|
|
|
local possibleCommands = {}
|
|
|
|
|
|
|
|
-- create a list containing all globals
|
|
|
|
local allVars = table.copy(_G)
|
|
|
|
table.merge(allVars, commandEnv)
|
|
|
|
|
|
|
|
-- match commands
|
|
|
|
for k,v in pairs(allVars) do
|
2011-12-07 01:31:55 +01:00
|
|
|
if k:sub(1, cursorPos) == commandBegin then
|
2011-11-01 19:32:48 +01:00
|
|
|
table.insert(possibleCommands, k)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
-- complete command with one match
|
|
|
|
if #possibleCommands == 1 then
|
2012-03-31 15:43:01 +02:00
|
|
|
commandTextEdit:setText(possibleCommands[1])
|
2013-01-25 12:44:15 +01:00
|
|
|
commandTextEdit:setCursorPos(-1)
|
2011-11-01 19:32:48 +01:00
|
|
|
-- show command matches
|
|
|
|
elseif #possibleCommands > 0 then
|
|
|
|
print('>> ' .. commandBegin)
|
|
|
|
|
|
|
|
-- expand command
|
|
|
|
local expandedComplete = commandBegin
|
|
|
|
local done = false
|
|
|
|
while not done do
|
|
|
|
cursorPos = #commandBegin+1
|
|
|
|
if #possibleCommands[1] < cursorPos then
|
|
|
|
break
|
|
|
|
end
|
2011-12-07 01:31:55 +01:00
|
|
|
expandedComplete = commandBegin .. possibleCommands[1]:sub(cursorPos, cursorPos)
|
2011-11-01 19:32:48 +01:00
|
|
|
for i,v in ipairs(possibleCommands) do
|
2011-12-07 01:31:55 +01:00
|
|
|
if v:sub(1, #expandedComplete) ~= expandedComplete then
|
2011-11-01 19:32:48 +01:00
|
|
|
done = true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
if not done then
|
|
|
|
commandBegin = expandedComplete
|
|
|
|
end
|
|
|
|
end
|
2012-03-31 15:43:01 +02:00
|
|
|
commandTextEdit:setText(commandBegin)
|
2013-01-25 12:44:15 +01:00
|
|
|
commandTextEdit:setCursorPos(-1)
|
2011-11-01 19:32:48 +01:00
|
|
|
|
|
|
|
for i,v in ipairs(possibleCommands) do
|
|
|
|
print(v)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-01-09 19:45:28 +01:00
|
|
|
local function doCommand()
|
2012-03-31 15:43:01 +02:00
|
|
|
local currentCommand = commandTextEdit:getText()
|
2013-01-18 23:39:11 +01:00
|
|
|
executeCommand(currentCommand)
|
2012-02-06 20:19:47 +01:00
|
|
|
|
2012-03-31 15:43:01 +02:00
|
|
|
if commandTextEdit then
|
|
|
|
commandTextEdit:clearText()
|
2012-02-06 20:19:47 +01:00
|
|
|
end
|
2012-01-09 19:45:28 +01:00
|
|
|
return true
|
2011-08-29 00:07:22 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
local function onLog(level, message, time)
|
2013-03-01 01:35:14 +01:00
|
|
|
if disabled then return end
|
2011-08-29 00:07:22 +02:00
|
|
|
-- avoid logging while reporting logs (would cause a infinite loop)
|
|
|
|
if logLocked then return end
|
|
|
|
|
|
|
|
logLocked = true
|
2013-01-18 23:39:11 +01:00
|
|
|
addLine(message, LogColors[level])
|
2011-08-29 00:07:22 +02:00
|
|
|
logLocked = false
|
|
|
|
end
|
2011-08-20 22:30:41 +02:00
|
|
|
|
2011-08-29 00:07:22 +02:00
|
|
|
-- public functions
|
2013-01-18 23:39:11 +01:00
|
|
|
function init()
|
|
|
|
terminalWindow = g_ui.displayUI('terminal')
|
2012-03-26 20:33:00 +02:00
|
|
|
terminalWindow:setVisible(false)
|
|
|
|
|
2013-03-01 00:10:36 +01:00
|
|
|
terminalWindow.onDoubleClick = popWindow
|
2011-08-29 00:07:22 +02:00
|
|
|
|
2013-01-18 23:39:11 +01:00
|
|
|
terminalButton = modules.client_topmenu.addLeftButton('terminalButton', tr('Terminal') .. ' (Ctrl + T)', '/images/topbuttons/terminal', toggle)
|
|
|
|
g_keyboard.bindKeyDown('Ctrl+T', toggle)
|
2012-01-07 18:36:58 +01:00
|
|
|
|
2012-06-26 00:13:30 +02:00
|
|
|
commandHistory = g_settings.getList('terminal-history')
|
2012-01-15 16:13:22 +01:00
|
|
|
|
2012-03-31 15:43:01 +02:00
|
|
|
commandTextEdit = terminalWindow:getChildById('commandTextEdit')
|
2012-06-26 00:13:30 +02:00
|
|
|
g_keyboard.bindKeyPress('Up', function() navigateCommand(1) end, commandTextEdit)
|
|
|
|
g_keyboard.bindKeyPress('Down', function() navigateCommand(-1) end, commandTextEdit)
|
2013-02-28 22:39:27 +01:00
|
|
|
g_keyboard.bindKeyPress('Ctrl+C',
|
|
|
|
function()
|
|
|
|
if commandTextEdit:hasSelection() or not terminalSelectText:hasSelection() then return false end
|
|
|
|
g_window.setClipboardText(terminalSelectText:getSelection())
|
|
|
|
return true
|
|
|
|
end, commandTextEdit)
|
2012-06-26 00:13:30 +02:00
|
|
|
g_keyboard.bindKeyDown('Tab', completeCommand, commandTextEdit)
|
|
|
|
g_keyboard.bindKeyDown('Enter', doCommand, commandTextEdit)
|
2013-01-18 23:39:11 +01:00
|
|
|
g_keyboard.bindKeyDown('Escape', hide, terminalWindow)
|
2012-01-07 18:36:58 +01:00
|
|
|
|
2013-03-01 01:35:14 +01:00
|
|
|
terminalBuffer = terminalWindow:getChildById('terminalBuffer')
|
|
|
|
terminalSelectText = terminalWindow:getChildById('terminalSelectText')
|
2013-03-01 00:10:36 +01:00
|
|
|
terminalSelectText.onDoubleClick = popWindow
|
2013-03-01 01:35:14 +01:00
|
|
|
terminalSelectText.onMouseWheel = function(a,b,c) terminalBuffer:onMouseWheel(b,c) end
|
|
|
|
terminalBuffer.onScrollChange = function(self, value) terminalSelectText:setTextVirtualOffset(value) end
|
2013-02-28 22:39:27 +01:00
|
|
|
|
2012-02-20 03:27:08 +01:00
|
|
|
g_logger.setOnLog(onLog)
|
|
|
|
g_logger.fireOldMessages()
|
2011-08-20 22:30:41 +02:00
|
|
|
end
|
|
|
|
|
2013-01-18 23:39:11 +01:00
|
|
|
function terminate()
|
2012-06-26 00:13:30 +02:00
|
|
|
g_settings.setList('terminal-history', commandHistory)
|
2013-03-01 00:10:36 +01:00
|
|
|
|
2013-03-01 01:35:14 +01:00
|
|
|
removeEvent(flushEvent)
|
|
|
|
|
2013-03-01 00:10:36 +01:00
|
|
|
if poped then
|
|
|
|
oldPos = terminalWindow:getPosition()
|
|
|
|
oldSize = terminalWindow:getSize()
|
|
|
|
end
|
|
|
|
local settings = {
|
|
|
|
size = oldSize,
|
|
|
|
pos = oldPos,
|
|
|
|
poped = poped
|
|
|
|
}
|
|
|
|
g_settings.setNode('terminal-window', settings)
|
|
|
|
|
2012-06-26 00:13:30 +02:00
|
|
|
g_keyboard.unbindKeyDown('Ctrl+T')
|
2012-02-20 03:27:08 +01:00
|
|
|
g_logger.setOnLog(nil)
|
2012-03-26 20:33:00 +02:00
|
|
|
terminalWindow:destroy()
|
2013-02-01 05:32:15 +01:00
|
|
|
terminalButton:destroy()
|
2012-01-07 18:36:58 +01:00
|
|
|
commandEnv = nil
|
|
|
|
end
|
|
|
|
|
2013-01-18 23:39:11 +01:00
|
|
|
function hideButton()
|
|
|
|
terminalButton:hide()
|
|
|
|
end
|
|
|
|
|
2013-03-01 00:10:36 +01:00
|
|
|
function popWindow()
|
|
|
|
if poped then
|
|
|
|
oldPos = terminalWindow:getPosition()
|
|
|
|
oldSize = terminalWindow:getSize()
|
|
|
|
terminalWindow:fill('parent')
|
|
|
|
terminalWindow:setOn(false)
|
|
|
|
terminalWindow:getChildById('bottomResizeBorder'):disable()
|
|
|
|
terminalWindow:getChildById('rightResizeBorder'):disable()
|
|
|
|
terminalWindow:getChildById('titleBar'):hide()
|
|
|
|
terminalWindow:getChildById('terminalScroll'):setMarginTop(0)
|
|
|
|
terminalWindow:getChildById('terminalScroll'):setMarginBottom(0)
|
|
|
|
terminalWindow:getChildById('terminalScroll'):setMarginRight(0)
|
|
|
|
poped = false
|
|
|
|
else
|
|
|
|
terminalWindow:breakAnchors()
|
|
|
|
terminalWindow:setOn(true)
|
|
|
|
local size = oldSize or { width = g_window.getWidth()/2, height = g_window.getHeight()/2 }
|
|
|
|
terminalWindow:setSize(size)
|
|
|
|
local pos = oldPos or { x = (g_window.getWidth() - terminalWindow:getWidth())/2, y = (g_window.getHeight() - terminalWindow:getHeight())/2 }
|
|
|
|
terminalWindow:setPosition(pos)
|
|
|
|
terminalWindow:getChildById('bottomResizeBorder'):enable()
|
|
|
|
terminalWindow:getChildById('rightResizeBorder'):enable()
|
|
|
|
terminalWindow:getChildById('titleBar'):show()
|
|
|
|
terminalWindow:getChildById('terminalScroll'):setMarginTop(18)
|
|
|
|
terminalWindow:getChildById('terminalScroll'):setMarginBottom(1)
|
|
|
|
terminalWindow:getChildById('terminalScroll'):setMarginRight(1)
|
|
|
|
terminalWindow:bindRectToParent()
|
|
|
|
poped = true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-01-18 23:39:11 +01:00
|
|
|
function toggle()
|
2012-03-26 20:33:00 +02:00
|
|
|
if terminalWindow:isVisible() then
|
2013-01-18 23:39:11 +01:00
|
|
|
hide()
|
2012-01-07 21:00:07 +01:00
|
|
|
else
|
2013-03-01 00:10:36 +01:00
|
|
|
if not firstShow then
|
|
|
|
local settings = g_settings.getNode('terminal-window')
|
|
|
|
if settings then
|
|
|
|
if settings.size then oldSize = size end
|
|
|
|
if settings.pos then oldPos = settings.pos end
|
|
|
|
if settings.poped then popWindow() end
|
|
|
|
end
|
|
|
|
firstShown = true
|
|
|
|
end
|
2013-01-18 23:39:11 +01:00
|
|
|
show()
|
2012-01-07 21:00:07 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-01-18 23:39:11 +01:00
|
|
|
function show()
|
2012-03-26 20:33:00 +02:00
|
|
|
terminalWindow:show()
|
|
|
|
terminalWindow:raise()
|
|
|
|
terminalWindow:focus()
|
2011-08-20 22:30:41 +02:00
|
|
|
end
|
|
|
|
|
2013-01-18 23:39:11 +01:00
|
|
|
function hide()
|
2012-03-26 20:33:00 +02:00
|
|
|
terminalWindow:hide()
|
2012-01-07 18:36:58 +01:00
|
|
|
end
|
|
|
|
|
2013-01-24 17:01:28 +01:00
|
|
|
function disable()
|
|
|
|
terminalButton:hide()
|
|
|
|
g_keyboard.unbindKeyDown('Ctrl+T')
|
2013-03-01 01:35:14 +01:00
|
|
|
disabled = true
|
2013-01-24 17:01:28 +01:00
|
|
|
end
|
|
|
|
|
2013-03-01 01:35:14 +01:00
|
|
|
function flushLines()
|
|
|
|
local numLines = terminalBuffer:getChildCount() + #cachedLines
|
|
|
|
local fulltext = terminalSelectText:getText()
|
|
|
|
|
|
|
|
for _,line in pairs(cachedLines) do
|
|
|
|
-- delete old lines if needed
|
|
|
|
if numLines > MaxLogLines then
|
|
|
|
local len = #terminalBuffer:getChildByIndex(1):getText()
|
|
|
|
terminalBuffer:getChildByIndex(1):destroy()
|
|
|
|
fulltext = string.sub(fulltext, len)
|
|
|
|
end
|
|
|
|
|
|
|
|
local label = g_ui.createWidget('TerminalLabel', terminalBuffer)
|
|
|
|
label:setId('terminalLabel' .. numLines)
|
|
|
|
label:setText(line.text)
|
|
|
|
label:setColor(line.color)
|
|
|
|
|
|
|
|
fulltext = fulltext .. '\n' .. line.text
|
2011-08-29 00:07:22 +02:00
|
|
|
end
|
2012-04-27 06:54:14 +02:00
|
|
|
|
2013-03-01 01:35:14 +01:00
|
|
|
terminalSelectText:setText(fulltext)
|
|
|
|
|
|
|
|
cachedLines = {}
|
|
|
|
flushEvent = nil
|
|
|
|
end
|
|
|
|
|
|
|
|
function addLine(text, color)
|
|
|
|
if not flushEvent then
|
|
|
|
flushEvent = scheduleEvent(flushLines, 10)
|
|
|
|
end
|
2013-02-28 22:39:27 +01:00
|
|
|
|
2013-03-01 01:35:14 +01:00
|
|
|
table.insert(cachedLines, {text=text, color=color})
|
2011-08-20 22:30:41 +02:00
|
|
|
end
|
|
|
|
|
2013-01-18 23:39:11 +01:00
|
|
|
function executeCommand(command)
|
2012-01-09 19:45:28 +01:00
|
|
|
if command == nil or #command == 0 then return end
|
2012-02-02 01:10:55 +01:00
|
|
|
|
2013-03-01 00:10:36 +01:00
|
|
|
-- add command line
|
|
|
|
addLine("> " .. command, "#ffffff")
|
2012-02-02 01:10:55 +01:00
|
|
|
|
2013-03-01 00:10:36 +01:00
|
|
|
-- reset current history index
|
|
|
|
currentHistoryIndex = 0
|
2013-02-28 22:39:27 +01:00
|
|
|
|
2013-03-01 00:10:36 +01:00
|
|
|
-- add new command to history
|
|
|
|
if #commandHistory == 0 or commandHistory[#commandHistory] ~= command then
|
|
|
|
table.insert(commandHistory, command)
|
|
|
|
if #commandHistory > MaxHistory then
|
|
|
|
table.remove(commandHistory, 1)
|
2013-02-28 22:39:27 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-11-01 19:32:48 +01:00
|
|
|
-- detect and convert commands with simple syntax
|
|
|
|
local realCommand
|
2012-03-18 14:34:39 +01:00
|
|
|
if string.sub(command, 1, 1) == '=' then
|
|
|
|
realCommand = 'print(' .. string.sub(command,2) .. ')'
|
2011-11-01 19:32:48 +01:00
|
|
|
else
|
|
|
|
realCommand = command
|
|
|
|
end
|
|
|
|
|
2013-03-01 00:10:36 +01:00
|
|
|
local func, err = loadstring(realCommand, "@")
|
2011-08-29 00:07:22 +02:00
|
|
|
|
2013-03-01 00:10:36 +01:00
|
|
|
-- detect terminal commands
|
|
|
|
if not func then
|
|
|
|
local command_name = command:match('^([%w_]+)[%s]*.*')
|
|
|
|
if command_name then
|
|
|
|
local args = string.split(command:match('^[%w]+[%s]*(.*)'), ' ')
|
|
|
|
if commandEnv[command_name] and type(commandEnv[command_name]) == 'function' then
|
|
|
|
func = function() modules.client_terminal.commandEnv[command_name](unpack(args)) end
|
|
|
|
elseif command_name == command then
|
|
|
|
addLine('ERROR: command not found', 'red')
|
|
|
|
return
|
|
|
|
end
|
2012-05-01 08:49:48 +02:00
|
|
|
end
|
2012-01-15 16:13:22 +01:00
|
|
|
end
|
2011-08-29 00:07:22 +02:00
|
|
|
|
2013-03-01 00:10:36 +01:00
|
|
|
-- check for syntax errors
|
2011-08-29 00:07:22 +02:00
|
|
|
if not func then
|
2013-03-01 00:10:36 +01:00
|
|
|
addLine('ERROR: incorrect lua syntax: ' .. err:sub(5), 'red')
|
|
|
|
return
|
2011-08-20 22:30:41 +02:00
|
|
|
end
|
2011-08-29 00:07:22 +02:00
|
|
|
|
|
|
|
-- setup func env to commandEnv
|
|
|
|
setfenv(func, commandEnv)
|
|
|
|
|
|
|
|
-- execute the command
|
2013-03-01 00:10:36 +01:00
|
|
|
local ok, ret = pcall(func)
|
|
|
|
if ok then
|
|
|
|
-- if the command returned a value, print it
|
|
|
|
if ret then print(ret) end
|
|
|
|
else
|
|
|
|
addLine('ERROR: command failed: ' .. ret, 'red')
|
|
|
|
end
|
2013-02-28 22:39:27 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
function clear()
|
|
|
|
terminalBuffer:destroyChildren()
|
|
|
|
terminalSelectText:setText('')
|
2011-08-29 00:07:22 +02:00
|
|
|
end
|