Make prpw a library, so it can be used by other Lua scripts

This commit is contained in:
Fierelier 2024-08-12 02:42:07 +02:00
parent 8144b3063f
commit cb0c120e7e
2 changed files with 87 additions and 85 deletions

85
lib/prpw.lua Normal file
View File

@ -0,0 +1,85 @@
math.randomseed(os.time())
env = {}
function env.loop()
local command = env.stringSplit(io.read()," ",3,"\\")
env.run(command)
print("")
end
function env.run(command)
local func = loadfile(basepath .. "/cmd/" ..command[1].. ".lua")
func = func()
env.rtn = func(command)
return env.rtn
end
function env.xmlFindTags(xml,tag)
local rtn = {}
for i,v in ipairs(xml["children"]) do
if v.tag == tag then
table.insert(rtn,v)
end
end
return rtn
end
function env.stringDate(d)
return d.year .. "." ..env.padNumber(d.month,2).. "." ..env.padNumber(d.day,2).. "_" ..env.padNumber(d.hour,2).. ":" ..env.padNumber(d.min,2).. ":" ..env.padNumber(d.sec,2)
end
function env.makeUUID()
local template ='xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'
return string.gsub(template, '[xy]', function (c)
local v = (c == 'x') and math.random(0, 0x0f) or math.random(8, 0x0b)
return string.format('%x', v)
end)
end
function env.stringSplit(str, sep, limit, esc)
local length = string.len(str)
local index = 1
local c
local escaped = false
local entry = ""
local rtn = {}
local rtnLength = 0
while index <= length do
c = string.sub(str,index,index)
if escaped == false then
if esc ~= nil then
if c == esc then
escaped = true
else
if c == sep then
if limit > -1 and rtnLength < limit then
table.insert(rtn,entry)
entry = ""
rtnLength = rtnLength + 1
else
entry = entry .. c
end
else
entry = entry .. c
end
end
end
else
entry = entry .. c
end
index = index + 1
end
table.insert(rtn,entry)
return rtn
end
function env.padNumber(nr,len)
nr = tostring(nr)
local curLen = string.len(nr)
while curLen < len do
nr = "0" .. nr
curLen = curLen + 1
end
return nr
end

87
prpw
View File

@ -1,10 +1,9 @@
#!/usr/bin/env lua #!/usr/bin/env lua
local basepath = (debug.getinfo(1, "S").source:sub(2):match("(.*[/\\])") or "./"):sub(1,-2) basepath = (debug.getinfo(1, "S").source:sub(2):match("(.*[/\\])") or "./"):sub(1,-2)
env = {}
local function main(arg) local function main(arg)
package.path = basepath .. "/lib/?.lua;" .. basepath .. "/lib/?/main.lua;" .. package.path package.path = basepath .. "/lib/?.lua;" .. basepath .. "/lib/?/main.lua;" .. package.path
math.randomseed(os.time()) require("prpw")
env.run({"autorun"}) env.run({"autorun"})
if #arg > 0 then if #arg > 0 then
@ -22,86 +21,4 @@ local function main(arg)
end end
end end
function env.loop()
local command = env.stringSplit(io.read()," ",3,"\\")
env.run(command)
print("")
end
function env.run(command)
local func = loadfile(basepath .. "/cmd/" ..command[1].. ".lua")
func = func()
env.rtn = func(command)
end
function env.xmlFindTags(xml,tag)
local rtn = {}
for i,v in ipairs(xml["children"]) do
if v.tag == tag then
table.insert(rtn,v)
end
end
return rtn
end
function env.stringDate(d)
return d.year .. "." ..env.padNumber(d.month,2).. "." ..env.padNumber(d.day,2).. "_" ..env.padNumber(d.hour,2).. ":" ..env.padNumber(d.min,2).. ":" ..env.padNumber(d.sec,2)
end
function env.makeUUID()
local template ='xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'
return string.gsub(template, '[xy]', function (c)
local v = (c == 'x') and math.random(0, 0x0f) or math.random(8, 0x0b)
return string.format('%x', v)
end)
end
function env.stringSplit(str, sep, limit, esc)
local length = string.len(str)
local index = 1
local c
local escaped = false
local entry = ""
local rtn = {}
local rtnLength = 0
while index <= length do
c = string.sub(str,index,index)
if escaped == false then
if esc ~= nil then
if c == esc then
escaped = true
else
if c == sep then
if limit > -1 and rtnLength < limit then
table.insert(rtn,entry)
entry = ""
rtnLength = rtnLength + 1
else
entry = entry .. c
end
else
entry = entry .. c
end
end
end
else
entry = entry .. c
end
index = index + 1
end
table.insert(rtn,entry)
return rtn
end
function env.padNumber(nr,len)
nr = tostring(nr)
local curLen = string.len(nr)
while curLen < len do
nr = "0" .. nr
curLen = curLen + 1
end
return nr
end
main(arg) main(arg)