v2.7.0 - Added a serialization library for table and strings, and automatic output in the Lua shell

This commit is contained in:
Ponali
2025-07-23 10:46:05 +02:00
parent 68e5b70273
commit bf4c1af9ef
5 changed files with 139 additions and 5 deletions
+6 -2
View File
@@ -1,7 +1,7 @@
local agcfg = {
["halyde"] = {
["maindir"] = "",
["version"] = "2.6.0",
["version"] = "2.7.0",
["description"] = "A universal, customizable and feature-packed operating system for OpenComputers.",
["directories"] = {
"halyde/apps",
@@ -12,7 +12,11 @@ local agcfg = {
"halyde/lib",
"halyde",
"home",
"mnt"
"mnt",
"tmp",
"special/eeprom",
"special/drive"
"special"
},
["files"] = {
"init.lua",
+3 -1
View File
@@ -17,7 +17,9 @@ while true do
return
else
local function runCommand()
assert(load(loadedLibraries .. command))()
local func = load(loadedLibraries.."return "..command,"=stdin") or load(loadedLibraries..command,"=stdin")
local res = {assert(func)()}
if res and (type(res[1])~="nil" or type(res[2])~="nil") then print(table.unpack(res)) end
end
local result, reason = xpcall(runCommand, function(errMsg)
return errMsg .. "\n\n" .. debug.traceback()
+1 -1
View File
@@ -1,7 +1,7 @@
local loadfile = ...
local filesystem = loadfile("/halyde/lib/filesystem.lua")(loadfile)
_G._OSVERSION = "Halyde 2.6.0"
_G._OSVERSION = "Halyde 2.7.0"
_G._OSLOGO = ""
local handle, tmpdata = filesystem.open("/halyde/config/oslogo.ans", "r"), nil
repeat
+4 -1
View File
@@ -1,3 +1,4 @@
local serialize = import("serialize")
local unicode = import("unicode")
local event = import("event")
--local keyboard = import("keyboard")
@@ -209,7 +210,9 @@ function _G.print(...)
local args = {...}
local stringArgs = {}
for _, arg in pairs(args) do
if tostring(arg) then
if type(arg)=="table" then
table.insert(stringArgs, serialize.table(arg,true))
elseif tostring(arg) then
table.insert(stringArgs, tostring(arg))
end
end
+125
View File
@@ -0,0 +1,125 @@
local serialize = {}
function serialize.string(str)
return '"'..str:gsub("[%z\1-\31\34\92\127-\159]",function(c)
local byte = c:byte()
if byte== 7 then return "\\a" end
if byte== 8 then return "\\b" end
if byte== 9 then return "\\t" end
if byte==10 then return "\\n" end
if byte==11 then return "\\v" end
if byte==12 then return "\\f" end
if byte==13 then return "\\r" end
if byte==34 then return "\\\"" end
if byte==92 then return "\\\\" end
return string.format("\\x%02x",byte)
end)..'"'
end
function serialize.table(tbl,colors,stack)
stack = table.copy(stack or {})
table.insert(stack,tbl)
local keyAmount = 0
local keyNumber = true
local out = ""
local first = true
for key,val in pairs(tbl) do
if not first then out=out..",\n" end
first=false
out=out.." "
if type(key)=="string" then
if key:match("^[%a_][%w_]*$") then
out=out..key.."="
else
out=out..'['..serialize.string(key)..']='
end
else
out=out.."["..tostring(key).."]="
end
if type(key)~="number" then
keyNumber=false
end
local success,reason = pcall(function()
local valStr = ""
if type(val)=="table" then
if #stack>4 or table.find(stack,val) then
valStr="..."
else
valStr=serialize.table(val,colors,stack)
end
elseif type(val)=="string" then
local lines = {}
for line in (val.."\n"):gmatch("([^\n]*)\n") do table.insert(lines,line) end
if #lines[#lines]==0 then
lines[#lines]=nil
lines[#lines]=lines[#lines].."\n"
end
for i=1,#lines do
if i<#lines then
lines[i]=serialize.string(lines[i].."\n")
else
lines[i]=serialize.string(lines[i])
end
end
valStr=table.concat(lines," ..\n ")
else
valStr=tostring(val)
end
local lines = {}
for line in (valStr.."\n"):gmatch("([^\n]*)\n") do table.insert(lines,line) end
out=out..table.concat(lines,"\n ")
lines = nil
keyAmount=keyAmount+1
end)
if not success then
if colors then out=out.."\x1b[91m" end
out=out.."["..tostring(reason).."]"
if colors then out=out.."\x1b[39m" end
end
coroutine.yield()
end
local metatbl = getmetatable(tbl)
local metakeys = {}
if type(metatbl)=="table" then
for i,v in pairs(metatbl) do
keyNumber=false
table.insert(metakeys,i)
end
end
if #metakeys>0 then
out=out.."\n "
if colors then out=out.."\x1b[92m" end
if table.find(metakeys,"__tostring") then
out=out.."tostring: "..serialize.string(tostring(tbl)).."\n "
table.remove(metakeys,table.find(metakeys,"__tostring"))
end
out=out..table.concat(metakeys,", ")
if colors then out=out.."\x1b[39m" end
end
if keyAmount==0 then return "{}" end
if keyNumber then
-- fix strings not being serialised
local vals = {}
for _,v in pairs(tbl) do
if #vals>=5 and #stack>1 then
table.insert(vals,"...")
break
end
if type(v)=="table" then
table.insert(vals,serialize.table(v,colors,stack))
elseif type(v)=="string" then
table.insert(vals,serialize.string(v))
else
table.insert(vals,tostring(v))
end
end
return "{"..table.concat(vals,", ").."}"
end
return "{\n"..out.."\n}"
end
return serialize