From: Olav Bakke Svendsen Date: Tue, 26 Nov 2024 23:46:10 +0000 (+0100) Subject: Uploaded new libraries X-Git-Tag: catalyst X-Git-Url: http://git.olavbs.no/?a=commitdiff_plain;h=30dbb9c84ab40e2b3f1e81fddcdc2ba57a22708a;p=cc.git Uploaded new libraries --- diff --git a/lib/control.lua b/lib/control.lua new file mode 100644 index 0000000..c6f89ec --- /dev/null +++ b/lib/control.lua @@ -0,0 +1,15 @@ +local control = {} + +control.forever = function(f) + return function() +-- while true do print( "YES" ); f() end + f() + return control.forever(f)() + end +end + +control.simultaneously = function(t) + parallel.waitForAny(table.unpack(t)) +end + +return control diff --git a/lib/import.lua b/lib/import.lua new file mode 100644 index 0000000..9a56443 --- /dev/null +++ b/lib/import.lua @@ -0,0 +1,30 @@ +-- lib/import.lua +-- +-- import returns some or all definitions from file +-- global_import defines some or all definitions from file globally +-- +-- examples: +-- local lib = import "library.lua" {} -- same as local lib = dofile "library.lua" +-- local lib = import "library.lua" { "foo", "bar", } -- defines lib.foo and lib.bar +-- import_global "module.lua" { "baz" } -- defines baz globally +-- mod = import_global "module.lua" { "baz" } -- defines mod.baz globally + +import = function(path) + return function(list) + local m = dofile(path) + if #list == 0 then + return m + else + local d = {} + for _, k in ipairs(list) do d[k] = m[k] end + return d + end + end +end + + +global_import = function(path) + return function(list) + for k, v in pairs(import(path)(list)) do _G[k] = v end + end +end diff --git a/lib/log.lua b/lib/log.lua new file mode 100644 index 0000000..ae65734 --- /dev/null +++ b/lib/log.lua @@ -0,0 +1,51 @@ +local log = {} + +-- printing +-- log.enable_printing = false +-- log.print_format = function() end +-- log.print = function(...) print(...) end + + +-- inserting +log.on_insert = nil -- function to run on entry on insertion +log.insert = function(self, status, a) + local entry = { epoch = os.epoch("local")/1000, status = status, entry = a } + table.insert(self, 1, entry) + if self.on_insert then self.on_insert(entry) end + return entry +end +log.ok = function(self, str) return self:insert("ok", str) end +log.warn = function(self, str) return self:insert("warn", str) end +log.err = function(self, str) return self:insert("err", str) end + +log.pwarn = function(self, warning, f, ...) + local ok, err = pcall(f, ...) + if not ok then self:warn(warning..": "..err) end + return ok +end + +log.perr = function(self, warning, f, ...) + local ok, err = pcall(f, ...) + if not ok then self:err(warning..": "..err) end + return ok +end + +log.queue_event = function(event) + return function(entry) + os.queueEvent(event, entry.epoch, entry.status, entry.entry) + end +end + + +-- viewing +log.view = function(self, i) + if i then + self.print(self.print_format(self[i])) + else + for _,t in ipairs(self) do + self.print(self.print_format(t)) + end + end +end + +return log