local setmetatable = require "__gc" local MLObj = {} MLObj.Trigger = {} MLObj.Trigger._mt = {} MLObj.Trigger._mt.__index = MLObj.Trigger MLObj.Trigger._mt.__gc = function(s) s:delete() end function MLObj.Trigger:delete() assert(not rawget(self,"_mt"),":delete() is meant only for cloned objects") killTrigger(self.triggerID) end function MLObj.Trigger:enable() assert(not rawget(self,"_mt"),":enable() is meant only for cloned objects") enableTrigger(self.triggerID) self.enabled = true end function MLObj.Trigger:disable() assert(not rawget(self,"_mt"),":disable() is meant only for cloned objects") disableTrigger(self.triggerID) self.enabled = false end function MLObj.Trigger:isEnabled() assert(not rawget(self,"_mt"),":isEnabled() is meant only for cloned objects") return self.enabled end function MLObj.Trigger:new(triggerDef) assert(rawget(self,"_mt"), ":new() is only supposed to be called from Trigger prototype") assert(triggerDef,"Trigger definition is not provided") assert(triggerDef.triggerType,"Trigger type is not given in the trigger definition") assert(type(triggerDef.triggerType) == "string", "Trigger type must be a string") local triggerID -- no assert of triggerDef.expireAfter, if that is not defined (i.e. evaluates to nil) then trigger repeats forever assert(triggerDef.code, "Code to execute upon trigger was not given in the trigger definition") if triggerDef.triggerType =="REGEX" then assert(triggerDef.regex, "REGEX trigger definition does not provide regex pattern to match") triggerID = tempRegexTrigger(triggerDef.regex, triggerDef.code, triggerDef.expireAfter) elseif triggerDef.triggerType =="SUBSTRING" then assert(triggerDef.substring,"SUBSTRING trigger definition does not provide substring to match") triggerID = tempTrigger(triggerDef.substring, triggerDef.code, triggerDef.expireAfter) elseif triggerDef.triggerType =="PROMPT" then triggerID = tempPromptTrigger(triggerDef.code, triggerDef.expireAfter) elseif triggerDef.trigtype =="LINE" then assert(triggerDef.from,"LINE trigger definition does not give provide from parameter") assert(triggerDef.howMany,"LINE trigger definition does not give provide howMany parameter") triggerID = tempLineTrigger(triggerDef.from, triggerDef.howMany, triggerDef.code, triggerDef.expireAfter) elseif triggerDef.triggerType =="EXACT" then assert(triggerDef.exactLine, "EXACT trigger definition does not provide the line to match") triggerID = tempExactMatchTrigger(triggerDef.exactLine, triggerDef.code, triggerDef.expireAfter) -- elseif triggerDef.triggerType =="COMPLEX" then -- assert(triggerDef.regex) -- local multiLine = assert(triggerDef.multiLine) -- local fgColor = assert(triggerDef.fgColor) -- local bgColor = assert(triggerDef.bgColor) -- local filter = assert(triggerDef.filter) -- local matchAll = assert(triggerDef.matchAll) -- local hiliteFgColor = assert(triggerDef.hiliteFgColor) -- local hiliteBgColor = assert(triggerDef.hiliteBgColor) -- local soundFile = assert(triggerDef.soundFile) -- local fireLength = assert(triggerDef.fireLength) -- local lineDelta = assert(triggerDef.lineDelta) -- triggerID = tempComplexRegexTrigger(triggerDef.name, triggerDef.regex, code, multiLine, fgColor, bgColor, -- filter, matchAll, hiliteFgColor, hiliteBgColor, soundFile, -- fireLength, lineDelta, expireAfter) elseif triggerDef.triggerType =="HEAD" then assert(triggerDef.head, "HEAD trigger definition does not provide the beginning of the line (head parameter) to match") triggerID = tempBeginOfLineTrigger(triggerDef.head, triggerDef.code, triggerDef.expireAfter) elseif triggerDef.triggerType =="COLOR" then assert(triggerDef.fgColor,"COLOR trigger definition does not provide background color to match") assert(triggerDef.bgColor,"COLOR trigger definition does not provide foreground color to match") triggerID = tempAnsiColorTrigger(triggerDef.fgColor, triggerDef.bgColor, triggerDef.code, triggerDef.expireAfter) else error("Unsupported trigger type.") end local s = {} if triggerID then s.triggerID = triggerID s.enabled = true return setmetatable(s,self._mt) end end -- -- -- Timer objects -- -- MLObj.Timer = {} MLObj.Timer._mt = {} MLObj.Timer._mt.__index = MLObj.Timer MLObj.Timer._mt.__gc = function(s) s:delete() end function MLObj.Timer:delete() assert(not rawget(self,"_mt"),":delete() is meant only for cloned objects") disableTimer(self.timerID) -- overkill killTimer(self.timerID) end function MLObj.Timer:enable() assert(not rawget(self,"_mt"),":enable() is meant only for cloned objects") enableTimer(self.timerID) self.enabled = true end function MLObj.Timer:disable() assert(not rawget(self,"_mt"),":disable() is meant only for cloned objects") disableTimer(self.timerID) self.enabled = false end function MLObj.Timer:isEnabled() assert(not rawget(self,"_mt"),":isEnabled() is meant only for cloned objects") return self.enabled end function MLObj.Timer:new(timerDef) assert(timerDef.interval, "Timer interval information is not provided in the Timer object definition") assert(type(timerDef.interval)=="number", "Timer interval is not a number") assert(timerDef.code,"No code to execute is provided in the Timer object definition") local timerID = tempTimer(timerDef.interval, timerDef.code, timerDef.repeating) if timerID then local s = {} s.timerID = timerID s.enabled = true return setmetatable(s,self._mt) end end -- -- -- Event objects -- -- MLObj.Event = {} MLObj.Event._mt = {} MLObj.Event._mt.__index = MLObj.Event MLObj.Event._mt.__gc = function(s) s:delete() end function MLObj.Event:delete() assert(not rawget(self,"_mt"),":delete() is meant only for cloned objects") killAnonymousEventHandler(self.eventID) end function MLObj.Event:new(eventDef) assert(eventDef.name, "Event name information is not provided in the Event object definition") assert(type(eventDef.name)=="string", "Event name is not a string") assert(eventDef.code,"No code to execute is provided in the Event object definition") local eventID = registerAnonymousEventHandler(eventDef.name, eventDef.code, eventDef.oneshot) if eventID then local s = {} s.eventID = eventID return setmetatable(s,self._mt) end end -- -- -- Stopwatch objects -- -- MLObj.Stopwatch = {} MLObj.Stopwatch._mt = {} MLObj.Stopwatch._mt.__index = MLObj.Stopwatch MLObj.Stopwatch._mt.__gc = function(s) s:delete() end function MLObj.Stopwatch:delete() assert(not rawget(self,"_mt"),":delete() is meant only for cloned objects") deleteStopWatch(self.stopwatchID) end function MLObj.Stopwatch:start() startStopWatch( self.stopwatchID ) end function MLObj.Stopwatch:stop() stopStopWatch( self.stopwatchID ) end function MLObj.Stopwatch:reset() resetStopWatch( self.stopwatchID ) end function MLObj.Stopwatch:getTime() return getStopWatchTime( self.stopwatchID ) end function MLObj.Stopwatch:adjustBy(seconds) assert(seconds,":adjustBy(seconds), the required argument is missing") assert(type(seconds) == "number",":adjustBy(seconds), argument is not a number") adjustStopWatch( self.stopwatchID, seconds ) end function MLObj.Stopwatch:new(stopwatchDef) local stopwatchID = createStopWatch(false) if stopwatchID then local s = {} s.stopwatchID = stopwatchID return setmetatable(s,self._mt) end end return MLObj