Merge branch 'fixes' into main/gingo-test
[ryzomcore.git] / ryzom / common / data_common / r2 / r2_console.lua
blobda44a189c97bd6763ed4370f0552b6ab0bee57c4
1 -- console & printing related functions
3 ---------------
4 -- FUNCTIONS --
5 ---------------
7 ------------------------------------------------------------------------------------------------------------
8 -- alias for 'debugInfo'
9 log = debugInfo
12 ------------------------------------------------------------------------------------------------------------
13 -- Build color tag for use with debugInfo
14 function colorTag(r, g, b, a)
15 local function compToLetter(comp)
16 if comp == nil then
17 return 'F'
18 end
19 comp = math.floor(clamp(comp, 0, 255) / 16) + 1
20 --debugInfo("Comp = " .. tostring(comp))
21 return ComponentToLetter[comp]
22 end
23 return "@{" .. compToLetter(r) .. compToLetter(g) .. compToLetter(b) .. compToLetter(a) .. "}"
24 end
26 -------------------------------------------------------------------------------------------------
27 -- Display a string, splitting it when too long
28 function dumpSplittedString(str)
29 local splitSize = 50
30 local numParts = math.floor(string.len(str) / splitSize) + 1
31 for i = 0, numParts do
32 debugInfo(string.sub(str, i * splitSize, i * splitSize + splitSize - 1))
33 end
34 end
36 -------------------------------------------------------------------------------------------------
37 -- display debug info with warning color
38 function debugWarning(msg)
39 debugInfo(warningTag .. msg)
40 end
42 -------------------------------------------------------------------------------------------------
43 -- dump content of a lua object
44 function luaObject(obj, maxDepth)
45 dumpCallStack(0)
46 if runCommand == nil
47 then
48 r2.print(obj)
49 else
50 __tmpInstance = obj
51 runCommand("luaObject", "__tmpInstance", select(maxDepth, maxDepth, 10))
52 __tmpInstance = nil
53 end
54 end
56 -------------------------------------------------------------------------------------------------
57 -- dump content of a lua object (other version)
58 -- output : object with a "write" function that display the result. if nil, 'output' default to the global 'io'
60 function writeTable(t, output)
61 if output == nil then output = io end
62 function writeSpace(n)
63 for i = 1, n do
64 output.write("\t")
65 end
66 end
68 function writeTableR(t, n)
69 if (type(t) == "table")
70 then
72 output.write("{\n")
73 for key, value in pairs(t) do
74 if ( type(value) == "table")
75 then
76 writeSpace(n+1)
77 output.write (key)
78 output.write ("=")
79 writeTableR(value, n+1)
81 elseif (type(value) == "string")
82 then
83 value = "\"" ..value .. "\""
84 writeSpace(n+1)
85 output.write(key, "=", value,", \n")
86 elseif (type(value) == "number")
87 then
88 writeSpace(n+1)
89 output.write(key, "=", value,", \n")
90 end
91 end
92 writeSpace(n)
93 output.write("},\n");
94 end
95 end
97 writeTableR(t, 0)
98 end
101 --function loadTable(fileName)
103 -- local file = io.open(fileName, "r")
105 -- function loadTableR(file)
107 -- local line
108 -- while (line=file:read("*l")) ~= "}" then
110 -- if line == "{" then
111 -- loadTableR(file)
112 -- else
114 -- end
115 -- end
116 -- end
118 -- if file:read("*l") ~= "{" then
119 -- debugInfo("file doesn't store a valid table")
120 -- return
121 -- end
123 -- local resultT = loadTableR(file)
124 -- io.close(file)
125 -- return resultT
126 --end
129 -------------
130 -- STATICS --
131 -------------
134 ComponentToLetter = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' }
135 warningTag = colorTag(255, 127, 0)
137 ----------
138 -- INIT --
139 ----------
142 -- for vianney's tests : if debugInfo function wasn't registered externally, then use the standard 'print' instead
143 if debugInfo == nil then
144 debugInfo = r2.print
145 else
146 function print(a0, a1, a2, a3, a4, a5)
147 local result = ""
148 if a0 ~= nil then result = result .. tostring(a0) end
149 if a1 ~= nil then result = result .. " " ..tostring(a1) end
150 if a2 ~= nil then result = result .. " " ..tostring(a2) end
151 if a3 ~= nil then result = result .. " " ..tostring(a3) end
152 if a4 ~= nil then result = result .. " " ..tostring(a4) end
153 if a5 ~= nil then result = result .. " " ..tostring(a5) end
154 if result ~= nil then debugInfo(result) end
159 function gotoFile(name, line)
160 local path = fileLookup(name)
161 if path ~= "" then
162 local luaEditorPath = os.getenv("R2ED_LUA_EDITOR_PATH")
163 if luaEditorPath == nil then
164 debugInfo([[ Can't launch editor to edit lua file, please set ]] ..
165 [[ the environment variable R2ED_LUA_EDITOR_PATH ]] ..
166 [[ with the path of your editor. ]])
168 else
169 launchProgram(luaEditorPath, path .. "/" .. tostring(line))