forked from kmarkus/rFSM
-
Notifications
You must be signed in to change notification settings - Fork 2
/
rfsmpp.lua
120 lines (101 loc) · 3.27 KB
/
rfsmpp.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
--
-- Various pretty printing functions to make life easier
--
-- (C) 2010-2013 Markus Klotzbuecher <[email protected]>
-- (C) 2014-2020 Markus Klotzbuecher <[email protected]>
--
-- SPDX-License-Identifier: BSD-3-Clause
--
require("ansicolors")
require("utils")
require("rfsm")
local unpack, print, type, pairs, assert = unpack, print, type, pairs, assert
local table = table
local utils = utils
local string = string
local ac = ansicolors
local rfsm = rfsm
-- some shortcuts
local is_meta = rfsm.is_meta
local is_state = rfsm.is_state
local is_leaf = rfsm.is_leaf
local is_composite = rfsm.is_composite
local sta_mode = rfsm.sta_mode
local fsmobj_tochar = rfsm.fsmobj_tochar
module("rfsmpp")
local pad = 20
-- pretty print fsm
function fsm2str(fsm, ind)
local ind = ind or 1
local indstr = ' '
local res = {}
function __2colstr(s)
assert(s, "s not a state")
if s._mode == 'active' then
if is_leaf(s) then return ac.green .. ac.bright .. s._id .. ac.reset
else return ac.green .. s._id .. ac.reset end
elseif s._mode == 'done' then return ac.yellow .. s._id .. ac.reset
else return ac.red .. s._id .. ac.reset end
end
function __fsm_tostring(tab, res, ind)
for name,state in pairs(tab) do
if not is_meta(name) and is_state(state) then
res[#res+1] = string.rep(indstr, ind) .. __2colstr(state) .. '[' .. fsmobj_tochar(state) .. ']'
if is_leaf(state) then res[#res+1] = '\n' end
if is_composite(state) then
res[#res+1] = '\n'
__fsm_tostring(state, res, ind+1)
end
end
end
end
res[#res+1] = __2colstr(fsm) .. '\n'
__fsm_tostring(fsm, res, ind)
return table.concat(res, '')
end
--- Debug message colors.
local ctab = {
STATE_ENTER = ac.green,
STATE_EXIT = ac.red,
EFFECT = ac.yellow,
DOO = ac.blue,
EXEC_PATH = ac.cyan,
ERROR = ac.red .. ac.bright,
HIBERNATING = ac.magenta,
RAISED = ac.white .. ac.bright,
TIMEEVENT = ac.yellow .. ac.bright
}
function dbgcolorize(name, ...)
local str = ""
local args = { ... }
if name then str = ac.cyan .. ac.bright .. name .. ":" .. ac.reset .. '\t' end
-- convert nested tables to strings
ptab = utils.map(utils.tab2str, args)
col = ctab[ptab[1]]
if col ~= nil then
str = str.. col .. utils.rpad(ptab[1], pad) .. ac.reset .. table.concat(ptab, ' ', 2)
else
str = str .. utils.rpad(ptab[1], pad) .. table.concat(ptab, ' ', 2)
end
return str
end
--- Colorized fsm.dbg hook replacement.
function dbgcolor(name, ...)
print(dbgcolorize(name, ...))
end
--- Generate a configurable dbgcolor function.
-- @param name string name to prepend to printed message.
-- @param ftab table of the dbg ids to print.
-- @param defshow if false fields not mentioned in ftab are not shown. If true they are.
-- @param print_fcn a function actually used for printing. Defaults to print.
function gen_dbgcolor(name, ftab, defshow, print_fcn)
name = name or "<unnamed SM>"
ftab = ftab or {}
if defshow == nil then defshow = true end
if print_fcn == nil then print_fcn = print end
return function (tag, ...)
if ftab[tag] == true then print_fcn(dbgcolorize(name, tag, ...))
elseif ftab[tag] == false then return
else if defshow then print_fcn(dbgcolorize(name, tag, ...)) end end
end
end