2011-08-29 01:04:36 +02:00
|
|
|
Console = { }
|
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
|
2011-08-29 00:07:22 +02:00
|
|
|
|
|
|
|
-- private variables
|
|
|
|
local consoleWidget
|
2011-08-20 22:30:41 +02:00
|
|
|
local logLocked = false
|
2011-08-21 03:01:46 +02:00
|
|
|
local commandEnv = createEnvironment()
|
2011-08-29 00:07:22 +02:00
|
|
|
local commandLineEdit
|
2011-08-29 01:04:36 +02:00
|
|
|
local consoleBuffer
|
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-08-29 00:07:22 +02:00
|
|
|
local function onKeyPress(widget, keyCode, keyChar, keyboardModifiers)
|
|
|
|
if keyboardModifiers == KeyboardNoModifier then
|
|
|
|
-- execute current command
|
|
|
|
if keyCode == KeyReturn or keyCode == keyEnter then
|
|
|
|
local currentCommand = commandLineEdit:getText()
|
2011-08-29 01:04:36 +02:00
|
|
|
Console.executeCommand(currentCommand)
|
2011-08-29 00:07:22 +02:00
|
|
|
commandLineEdit:clearText()
|
|
|
|
return true
|
|
|
|
-- navigate history up
|
|
|
|
elseif keyCode == KeyUp then
|
|
|
|
navigateCommand(1)
|
|
|
|
return true
|
|
|
|
-- navigate history down
|
|
|
|
elseif keyCode == KeyDown then
|
|
|
|
navigateCommand(-1)
|
|
|
|
return true
|
2011-08-28 23:32:24 +02:00
|
|
|
end
|
|
|
|
end
|
2011-08-29 00:07:22 +02:00
|
|
|
return false
|
|
|
|
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
|
2011-08-29 01:04:36 +02:00
|
|
|
Console.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
|
2011-08-29 01:04:36 +02:00
|
|
|
function Console.init()
|
2011-08-29 00:07:22 +02:00
|
|
|
consoleWidget = UI.loadAndDisplay("/console/console.otui")
|
|
|
|
consoleWidget:hide()
|
2011-08-29 21:35:58 +02:00
|
|
|
connect(consoleWidget, { onKeyPress = onKeyPress })
|
2011-08-29 00:07:22 +02:00
|
|
|
|
|
|
|
commandLineEdit = consoleWidget:getChildById('commandLineEdit')
|
2011-08-29 01:04:36 +02:00
|
|
|
consoleBuffer = consoleWidget:getChildById('consoleBuffer')
|
2011-08-29 00:07:22 +02:00
|
|
|
Logger.setOnLog(onLog)
|
2011-08-20 22:30:41 +02:00
|
|
|
Logger.fireOldMessages()
|
|
|
|
end
|
|
|
|
|
2011-08-29 01:04:36 +02:00
|
|
|
function Console.terminate()
|
2011-08-20 23:37:27 +02:00
|
|
|
Logger.setOnLog(nil)
|
2011-08-29 00:07:22 +02:00
|
|
|
consoleWidget:destroy()
|
|
|
|
commandLineEdit = nil
|
|
|
|
consoleWidget = nil
|
2011-08-20 22:30:41 +02:00
|
|
|
end
|
|
|
|
|
2011-08-29 01:04:36 +02:00
|
|
|
function Console.addLine(text, color)
|
2011-08-29 00:07:22 +02:00
|
|
|
-- create new line label
|
2011-08-29 01:04:36 +02:00
|
|
|
local numLines = consoleBuffer:getChildCount() + 1
|
2011-08-29 00:07:22 +02:00
|
|
|
local label = UILabel.create()
|
2011-08-29 01:04:36 +02:00
|
|
|
consoleBuffer:addChild(label)
|
2011-08-29 00:07:22 +02:00
|
|
|
label:setId('consoleLabel' .. numLines)
|
|
|
|
label:setStyle('ConsoleLabel')
|
|
|
|
label:setText(text)
|
|
|
|
label:setForegroundColor(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
|
2011-08-29 01:04:36 +02:00
|
|
|
consoleBuffer:getChildByIndex(1):destroy()
|
|
|
|
else
|
|
|
|
consoleBuffer:setHeight(consoleBuffer:getHeight() + LabelHeight)
|
|
|
|
consoleBuffer:updateParentLayout()
|
2011-08-29 00:07:22 +02:00
|
|
|
end
|
2011-08-20 22:30:41 +02:00
|
|
|
end
|
|
|
|
|
2011-08-29 01:04:36 +02:00
|
|
|
function Console.executeCommand(command)
|
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)
|
2011-08-29 00:07:22 +02:00
|
|
|
|
|
|
|
-- add command line
|
2011-08-29 01:04:36 +02:00
|
|
|
Console.addLine(">> " .. command, "#ffffff")
|
2011-08-29 00:07:22 +02:00
|
|
|
|
|
|
|
-- load command buffer
|
2011-08-21 03:01:46 +02:00
|
|
|
local func, err = loadstring(command, "@")
|
2011-08-29 00:07:22 +02:00
|
|
|
|
|
|
|
-- check for syntax errors
|
|
|
|
if not func then
|
2011-08-20 22:30:41 +02:00
|
|
|
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
|
|
|
|
Logger.log(LogError, 'command failed: ' .. ret)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|