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") if keyboardModifiers == KeyboardNoModifier then
console:hide() -- execute current command
console.onKeyPress = function(self, keyCode, keyChar, keyboardModifiers) if keyCode == KeyReturn or keyCode == keyEnter then
if keyboardModifiers == KeyboardNoModifier then local currentCommand = commandLineEdit:getText()
if keyCode == KeyReturn or keyCode == keyEnter then executeCommand(currentCommand)
local commandLineEdit = console:getChildById('commandLineEdit') commandLineEdit:clearText()
local command = commandLineEdit:getText() return true
Console.executeCommand(command) -- navigate history up
commandLineEdit:clearText() elseif keyCode == KeyUp then
return true navigateCommand(1)
elseif keyCode == KeyUp then return true
Console.navigateCommand(1) -- navigate history down
return true elseif keyCode == KeyDown then
elseif keyCode == KeyDown then navigateCommand(-1)
Console.navigateCommand(-1) return true
return true
end
end end
return false
end end
return false
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
end local label = UILabel.create()
consoleWidget:insertChild(-2, label)
label:setId('consoleLabel' .. numLines)
label:setStyle('ConsoleLabel')
label:setText(text)
label:setForegroundColor(color)
function Console.hide() -- delete old lines if needed
console.parent:unlockChild(console) if numLines > MaxLogLines then
console.visible = false consoleWidget:getChildByIndex(1):destroy()
end
function Console.executeCommand(command)
currentHistoryIndex = 0
table.insert(commandHistory, command)
Console.addLine(">> " .. command, "#ffffff")
local func, err = loadstring(command, "@")
if func then
setfenv(func, commandEnv)
local ok, ret = pcall(func)
if ok then
if ret then
print(ret)
end
else
Logger.log(LogError, 'command failed: ' .. ret)
end
else
Logger.log(LogError, 'incorrect lua syntax: ' .. err:sub(5))
end end
end end
function executeCommand(command)
-- reset current history index
currentHistoryIndex = 0
-- add new command to history
table.insert(commandHistory, command)
-- add command line
addLine(">> " .. command, "#ffffff")
-- load command buffer
local func, err = loadstring(command, "@")
-- 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)
-- 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

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()