use public/private semantics for lua console class

This commit is contained in:
Eduardo Bart 2011-08-28 19:07:22 -03:00
parent 8aadea2a96
commit 66703e4b07
2 changed files with 103 additions and 99 deletions

View File

@ -1,59 +1,25 @@
Console = { } Console = createEnvironment()
setfenv(1, Console)
local console -- public variables
LogColors = { [LogInfo] = 'white',
[LogWarning] = 'yellow',
[LogError] = 'red' }
MaxLogLines = 80
-- private variables
local consoleWidget
local logLocked = false local logLocked = false
local commandEnv = createEnvironment() local commandEnv = createEnvironment()
local maxLines = 80 local commandLineEdit
local numLines = 0
local commandHistory = { } local commandHistory = { }
local currentHistoryIndex = 0 local currentHistoryIndex = 0
function Console.onLog(level, message, time) -- private functions
-- avoid logging while reporting logs (would cause a infinite loop) local function navigateCommand(step)
if not logLocked then
logLocked = true
local color
if level == LogDebug then
color = '#5555ff'
elseif level == LogInfo then
color = '#5555ff'
elseif level == LogWarning then
color = '#ffff00'
else
color = '#ff0000'
end
if level ~= LogDebug then
Console.addLine(message, color)
end
logLocked = false
end
end
function Console.addLine(text, color)
-- create new label
local label = UILabel.create()
console:insertChild(-2, label)
label:setId('consoleLabel' .. numLines)
label:setText(text)
label:setForegroundColor(color)
label:setStyle('ConsoleLabel')
numLines = numLines + 1
if numLines > maxLines then
local firstLabel = console:getChildByIndex(1)
firstLabel:destroy()
end
end
function Console.navigateCommand(step)
local numCommands = #commandHistory local numCommands = #commandHistory
if numCommands > 0 then if numCommands > 0 then
currentHistoryIndex = math.min(math.max(currentHistoryIndex + step, 0), numCommands) currentHistoryIndex = math.min(math.max(currentHistoryIndex + step, 0), numCommands)
local commandLineEdit = console:getChildById('commandLineEdit')
if currentHistoryIndex > 0 then if currentHistoryIndex > 0 then
local command = commandHistory[numCommands - currentHistoryIndex + 1] local command = commandHistory[numCommands - currentHistoryIndex + 1]
commandLineEdit:setText(command) commandLineEdit:setText(command)
@ -63,64 +29,102 @@ function Console.navigateCommand(step)
end end
end end
function Console.create() local function onKeyPress(widget, keyCode, keyChar, keyboardModifiers)
console = UI.loadAndDisplay("/console/console.otui")
console:hide()
console.onKeyPress = function(self, keyCode, keyChar, keyboardModifiers)
if keyboardModifiers == KeyboardNoModifier then if keyboardModifiers == KeyboardNoModifier then
-- execute current command
if keyCode == KeyReturn or keyCode == keyEnter then if keyCode == KeyReturn or keyCode == keyEnter then
local commandLineEdit = console:getChildById('commandLineEdit') local currentCommand = commandLineEdit:getText()
local command = commandLineEdit:getText() executeCommand(currentCommand)
Console.executeCommand(command)
commandLineEdit:clearText() commandLineEdit:clearText()
return true return true
-- navigate history up
elseif keyCode == KeyUp then elseif keyCode == KeyUp then
Console.navigateCommand(1) navigateCommand(1)
return true return true
-- navigate history down
elseif keyCode == KeyDown then elseif keyCode == KeyDown then
Console.navigateCommand(-1) navigateCommand(-1)
return true return true
end end
end end
return false return false
end end
Logger.setOnLog(Console.onLog) 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
addLine(message, LogColors[level])
logLocked = false
end
-- public functions
function init()
consoleWidget = UI.loadAndDisplay("/console/console.otui")
consoleWidget:hide()
consoleWidget.onKeyPress = onKeyPress
commandLineEdit = consoleWidget:getChildById('commandLineEdit')
Logger.setOnLog(onLog)
Logger.fireOldMessages() Logger.fireOldMessages()
end end
function Console.destroy() function terminate()
Logger.setOnLog(nil) Logger.setOnLog(nil)
console:destroy() consoleWidget:destroy()
console = nil commandLineEdit = nil
consoleWidget = nil
end end
function Console.show() function addLine(text, color)
console.parent:lockChild(console) -- create new line label
console.visible = true local numLines = consoleWidget:getChildCount() - 2
local label = UILabel.create()
consoleWidget:insertChild(-2, label)
label:setId('consoleLabel' .. numLines)
label:setStyle('ConsoleLabel')
label:setText(text)
label:setForegroundColor(color)
-- delete old lines if needed
if numLines > MaxLogLines then
consoleWidget:getChildByIndex(1):destroy()
end
end end
function Console.hide() function executeCommand(command)
console.parent:unlockChild(console) -- reset current history index
console.visible = false
end
function Console.executeCommand(command)
currentHistoryIndex = 0 currentHistoryIndex = 0
-- add new command to history
table.insert(commandHistory, command) table.insert(commandHistory, command)
Console.addLine(">> " .. command, "#ffffff")
-- add command line
addLine(">> " .. command, "#ffffff")
-- load command buffer
local func, err = loadstring(command, "@") local func, err = loadstring(command, "@")
if func then
-- check for syntax errors
if not func then
Logger.log(LogError, 'incorrect lua syntax: ' .. err:sub(5))
return
end
-- setup func env to commandEnv
setfenv(func, commandEnv) setfenv(func, commandEnv)
-- execute the command
local ok, ret = pcall(func) local ok, ret = pcall(func)
if ok then if ok then
if ret then -- if the command returned a value, print it
print(ret) if ret then print(ret) end
end
else else
Logger.log(LogError, 'command failed: ' .. ret) Logger.log(LogError, 'command failed: ' .. ret)
end end
else
Logger.log(LogError, 'incorrect lua syntax: ' .. err:sub(5))
end
end end

View File

@ -10,8 +10,8 @@ Module
onLoad: | onLoad: |
require 'console' require 'console'
Console.create() Console.init()
return true return true
onUnload: | onUnload: |
Console.destroy() Console.terminate()