2012-01-07 18:36:58 +01:00
|
|
|
Terminal = { }
|
2011-08-21 03:01:46 +02:00
|
|
|
|
2011-08-29 01:04:36 +02:00
|
|
|
-- configs
|
|
|
|
local LogColors = { [LogInfo] = 'white',
|
|
|
|
[LogWarning] = 'yellow',
|
|
|
|
[LogError] = 'red' }
|
|
|
|
local MaxLogLines = 80
|
|
|
|
local LabelHeight = 16
|
2012-01-15 16:13:22 +01:00
|
|
|
local MaxHistory = 1000
|
2011-08-29 00:07:22 +02:00
|
|
|
|
|
|
|
-- private variables
|
2012-01-07 18:36:58 +01:00
|
|
|
local terminalWidget
|
|
|
|
local terminalButton
|
2011-08-20 22:30:41 +02:00
|
|
|
local logLocked = false
|
2012-01-05 02:28:29 +01:00
|
|
|
local commandEnv = newenv()
|
2011-08-29 00:07:22 +02:00
|
|
|
local commandLineEdit
|
2012-01-07 18:36:58 +01:00
|
|
|
local terminalBuffer
|
2011-08-28 23:32:24 +02:00
|
|
|
local commandHistory = { }
|
|
|
|
local currentHistoryIndex = 0
|
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]
|
|
|
|
commandLineEdit:setText(command)
|
|
|
|
else
|
|
|
|
commandLineEdit:clearText()
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-11-01 19:32:48 +01:00
|
|
|
local function completeCommand()
|
|
|
|
local cursorPos = commandLineEdit:getCursorPos()
|
|
|
|
if cursorPos == 0 then return end
|
|
|
|
|
2011-12-07 01:31:55 +01:00
|
|
|
local commandBegin = commandLineEdit: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
|
|
|
|
commandLineEdit:setText(possibleCommands[1])
|
|
|
|
-- 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
|
|
|
|
commandLineEdit:setText(commandBegin)
|
|
|
|
|
|
|
|
for i,v in ipairs(possibleCommands) do
|
|
|
|
print(v)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-01-09 19:45:28 +01:00
|
|
|
local function doCommand()
|
|
|
|
local currentCommand = commandLineEdit:getText()
|
|
|
|
Terminal.executeCommand(currentCommand)
|
2012-02-06 20:19:47 +01:00
|
|
|
|
|
|
|
if commandLineEdit then
|
|
|
|
commandLineEdit:clearText()
|
|
|
|
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)
|
|
|
|
-- debug messages are ignored
|
|
|
|
if level == LogDebug then return end
|
|
|
|
|
|
|
|
-- avoid logging while reporting logs (would cause a infinite loop)
|
|
|
|
if logLocked then return end
|
|
|
|
|
|
|
|
logLocked = true
|
2012-01-07 18:36:58 +01:00
|
|
|
Terminal.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
|
2012-01-07 18:36:58 +01:00
|
|
|
function Terminal.init()
|
|
|
|
terminalWidget = displayUI('terminal.otui')
|
|
|
|
terminalWidget:setVisible(false)
|
2011-08-29 00:07:22 +02:00
|
|
|
|
2012-02-20 03:27:08 +01:00
|
|
|
terminalButton = TopMenu.addLeftButton('terminalButton', 'Terminal (Ctrl + T)', 'terminal.png', Terminal.toggle)
|
2012-02-06 05:39:52 +01:00
|
|
|
Keyboard.bindKeyDown('Ctrl+T', Terminal.toggle)
|
2012-01-07 18:36:58 +01:00
|
|
|
|
2012-01-15 16:13:22 +01:00
|
|
|
commandHistory = Settings.getList('terminal-history')
|
|
|
|
|
2012-01-07 18:36:58 +01:00
|
|
|
commandLineEdit = terminalWidget:getChildById('commandLineEdit')
|
2012-02-20 03:27:08 +01:00
|
|
|
Keyboard.bindKeyPress('Up', function() navigateCommand(1) end, commandLineEdit)
|
|
|
|
Keyboard.bindKeyPress('Down', function() navigateCommand(-1) end, commandLineEdit)
|
2012-02-06 05:39:52 +01:00
|
|
|
Keyboard.bindKeyDown('Tab', completeCommand, commandLineEdit)
|
|
|
|
Keyboard.bindKeyDown('Enter', doCommand, commandLineEdit)
|
2012-02-20 03:27:08 +01:00
|
|
|
Keyboard.bindKeyDown('Escape', Terminal.hide, terminalWidget)
|
2012-01-07 18:36:58 +01:00
|
|
|
|
|
|
|
terminalBuffer = terminalWidget:getChildById('terminalBuffer')
|
2012-02-20 03:27:08 +01:00
|
|
|
g_logger.setOnLog(onLog)
|
|
|
|
g_logger.fireOldMessages()
|
2011-08-20 22:30:41 +02:00
|
|
|
end
|
|
|
|
|
2012-01-07 18:36:58 +01:00
|
|
|
function Terminal.terminate()
|
2012-01-15 16:13:22 +01:00
|
|
|
Settings.setList('terminal-history', commandHistory)
|
2012-02-06 05:39:52 +01:00
|
|
|
Keyboard.unbindKeyDown('Ctrl+T')
|
2012-02-20 03:27:08 +01:00
|
|
|
g_logger.setOnLog(nil)
|
2012-01-07 18:36:58 +01:00
|
|
|
terminalButton:destroy()
|
|
|
|
terminalButton = nil
|
2011-08-29 00:07:22 +02:00
|
|
|
commandLineEdit = nil
|
2012-01-07 18:36:58 +01:00
|
|
|
terminalBuffer = nil
|
|
|
|
terminalWidget:destroy()
|
|
|
|
terminalWidget = nil
|
|
|
|
commandEnv = nil
|
2012-02-07 01:41:53 +01:00
|
|
|
Terminal = nil
|
2012-01-07 18:36:58 +01:00
|
|
|
end
|
|
|
|
|
2012-01-07 21:00:07 +01:00
|
|
|
function Terminal.toggle()
|
|
|
|
if terminalWidget:isVisible() then
|
|
|
|
Terminal.hide()
|
|
|
|
else
|
|
|
|
Terminal.show()
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-01-07 18:36:58 +01:00
|
|
|
function Terminal.show()
|
|
|
|
terminalWidget:show()
|
2012-02-20 03:27:08 +01:00
|
|
|
terminalWidget:raise()
|
|
|
|
terminalWidget:focus()
|
2011-08-20 22:30:41 +02:00
|
|
|
end
|
|
|
|
|
2012-01-07 18:36:58 +01:00
|
|
|
function Terminal.hide()
|
|
|
|
terminalWidget:hide()
|
|
|
|
end
|
|
|
|
|
|
|
|
function Terminal.addLine(text, color)
|
2011-08-29 00:07:22 +02:00
|
|
|
-- create new line label
|
2012-01-07 18:36:58 +01:00
|
|
|
local numLines = terminalBuffer:getChildCount() + 1
|
|
|
|
local label = createWidget('TerminalLabel', terminalBuffer)
|
|
|
|
label:setId('terminalLabel' .. numLines)
|
2011-08-29 00:07:22 +02:00
|
|
|
label:setText(text)
|
2012-01-09 19:45:28 +01:00
|
|
|
label:setColor(color)
|
2011-08-20 22:30:41 +02:00
|
|
|
|
2011-08-29 00:07:22 +02:00
|
|
|
-- delete old lines if needed
|
|
|
|
if numLines > MaxLogLines then
|
2012-01-07 18:36:58 +01:00
|
|
|
terminalBuffer:getChildByIndex(1):destroy()
|
2011-08-29 01:04:36 +02:00
|
|
|
else
|
2012-01-07 18:36:58 +01:00
|
|
|
terminalBuffer:setHeight(terminalBuffer:getHeight() + LabelHeight)
|
2011-08-29 00:07:22 +02:00
|
|
|
end
|
2011-08-20 22:30:41 +02:00
|
|
|
end
|
|
|
|
|
2012-01-07 18:36:58 +01:00
|
|
|
function Terminal.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
|
|
|
|
|
|
|
logLocked = true
|
2012-02-20 03:27:08 +01:00
|
|
|
g_logger.log(LogInfo, '>> ' .. command)
|
2012-02-02 01:10:55 +01:00
|
|
|
logLocked = false
|
|
|
|
|
2011-11-01 19:32:48 +01:00
|
|
|
-- detect and convert commands with simple syntax
|
|
|
|
local realCommand
|
|
|
|
if commandEnv[command] then
|
|
|
|
if type(commandEnv[command]) == "function" then
|
|
|
|
realCommand = command .. '()'
|
|
|
|
else
|
|
|
|
realCommand = 'print(' .. command .. ')'
|
|
|
|
end
|
|
|
|
else
|
|
|
|
realCommand = command
|
|
|
|
end
|
|
|
|
|
2011-08-29 00:07:22 +02:00
|
|
|
-- reset current history index
|
2011-08-28 23:32:24 +02:00
|
|
|
currentHistoryIndex = 0
|
2011-08-29 00:07:22 +02:00
|
|
|
|
|
|
|
-- add new command to history
|
2011-08-28 23:32:24 +02:00
|
|
|
table.insert(commandHistory, command)
|
2012-01-15 16:13:22 +01:00
|
|
|
if #commandHistory > MaxHistory then
|
|
|
|
table.remove(commandHistory, 1)
|
|
|
|
end
|
2011-08-29 00:07:22 +02:00
|
|
|
|
|
|
|
-- add command line
|
2012-01-07 18:36:58 +01:00
|
|
|
Terminal.addLine(">> " .. command, "#ffffff")
|
2011-08-29 00:07:22 +02:00
|
|
|
|
|
|
|
-- load command buffer
|
2011-11-01 19:32:48 +01:00
|
|
|
local func, err = loadstring(realCommand, "@")
|
2011-08-29 00:07:22 +02:00
|
|
|
|
|
|
|
-- check for syntax errors
|
|
|
|
if not func then
|
2012-02-20 03:27:08 +01:00
|
|
|
g_logger.log(LogError, 'incorrect lua syntax: ' .. err:sub(5))
|
2011-08-29 00:07:22 +02:00
|
|
|
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
|
|
|
|
local ok, ret = pcall(func)
|
|
|
|
if ok then
|
|
|
|
-- if the command returned a value, print it
|
|
|
|
if ret then print(ret) end
|
|
|
|
else
|
2012-02-20 03:27:08 +01:00
|
|
|
g_logger.log(LogError, 'command failed: ' .. ret)
|
2011-08-29 00:07:22 +02:00
|
|
|
end
|
|
|
|
end
|