diff --git a/runtime/share/mezzaluna/base_config.lua b/runtime/share/mezzaluna/base_config.lua new file mode 100644 index 0000000..30f845d --- /dev/null +++ b/runtime/share/mezzaluna/base_config.lua @@ -0,0 +1,285 @@ +local master = function() + local config = { + tag_count = 5, + } + + local ctx = { + master_ratio = 0.5, + tags = {}, + tag_id = 1 + } + + local tile_onscreen = function(tag_id, res) + if ctx.tags[tag_id].master == nil then + return + end + + if #ctx.tags[tag_id].stack == 0 then + mez.view.set_size(ctx.tags[tag_id].master, res.width, res.height) + mez.view.set_position(ctx.tags[tag_id].master, 0, 0) + else + mez.view.set_size(ctx.tags[tag_id].master, res.width * ctx.master_ratio, res.height) + mez.view.set_position(ctx.tags[tag_id].master, 0, 0) + + for i, stack_id in ipairs(ctx.tags[tag_id].stack) do + mez.view.set_size(stack_id, res.width * (1 - ctx.master_ratio), res.height / #ctx.tags[tag_id].stack) + mez.view.set_position(stack_id, res.width * ctx.master_ratio, (res.height / #ctx.tags[tag_id].stack * (i - 1))) + end + end + end + + local tile_offscreen = function(tag_id, res) + if ctx.tags[tag_id].master == nil then + return + end + + mez.view.set_position(ctx.tags[tag_id].master, 0, -res.height) + mez.view.set_size(ctx.tags[tag_id].master, res.width, res.height) + + for _, view in ipairs(ctx.tags[tag_id].stack) do + mez.view.set_position(view, 0, -res.height) + mez.view.set_size(view, res.width, res.height) + end + end + + local tile_all = function () + local res = mez.output.get_resolution(0) + + for id = 1,config.tag_count do + if id == ctx.tag_id then + tile_onscreen(id, res) + else + tile_offscreen(id, res) + end + end + end + + for i = 1,config.tag_count do + ctx.tags[#ctx.tags + 1] = { + stack = {}, + master = nil, + } + mez.input.add_keymap("alt", "" .. i, { + press = function () + ctx.tag_id = i + + if ctx.tags[i].master then + mez.view.set_focused(ctx.tags[i].master) + else + mez.view.set_focused(nil) + end + + tile_all() + end + }) + end + + mez.hook.add("ServerStartPost", { + callback = function() + print("ServerStartPost") + print("Doesn't do anything :(") + end + }) + + mez.hook.add("ViewMapPre", { + callback = function(v) + if ctx.tags[ctx.tag_id].master == nil then + ctx.tags[ctx.tag_id].master = v + else + table.insert(ctx.tags[ctx.tag_id].stack, #ctx.tags[ctx.tag_id].stack + 1, v) + end + + mez.view.set_focused(v) + + tile_all() + end + }) + + mez.hook.add("ViewUnmapPost", { + callback = function(v) + if v == ctx.tags[ctx.tag_id].master then + if #ctx.tags[ctx.tag_id].stack > 0 then + ctx.tags[ctx.tag_id].master = table.remove(ctx.tags[ctx.tag_id].stack, 1) + mez.view.set_focused(ctx.tags[ctx.tag_id].master) + else + ctx.tags[ctx.tag_id].master = nil + end + else + for i, id in ipairs(ctx.tags[ctx.tag_id].stack) do + if id == v then + if i == 1 then + mez.view.set_focused(ctx.tags[ctx.tag_id].master) + elseif i == #ctx.tags[ctx.tag_id].stack then + mez.view.set_focused(ctx.tags[ctx.tag_id].stack[i - 1]) + else + mez.view.set_focused(ctx.tags[ctx.tag_id].stack[i + 1]) + end + + table.remove(ctx.tags[ctx.tag_id].stack, i) + end + end + end + + tile_all() + end + }) + + mez.hook.add("ViewPointerMotion", { + callback = function (view_id, cursor_x, cursor_y) + mez.view.set_focused(view_id) + end + }) + + mez.input.add_keymap("alt", "p", { + press = function() + mez.api.spawn("wmenu-run") + end, + }) + + mez.input.add_keymap("alt", "b", { + press = function() + mez.api.spawn("swaybg -i ~/Images/wallpapers/void/gruv_void.png") + end, + }) + + mez.input.add_keymap("alt|shift", "Return", { + press = function() + mez.api.spawn("alacritty") + end, + }) + + mez.input.add_keymap("alt|shift", "C", { + press = function () + mez.view.close(0) + end + }) + + mez.input.add_keymap("alt|shift", "q", { + press = function () + mez.api.exit(); + end + }) + + mez.input.add_keymap("alt", "Return", { + press = function() + local focused = mez.view.get_focused_id() + + if focused == ctx.tags[ctx.tag_id].master then return end + + for i, id in ipairs(ctx.tags[ctx.tag_id].stack) do + if focused == id then + local t = ctx.tags[ctx.tag_id].master + ctx.tags[ctx.tag_id].master = ctx.tags[ctx.tag_id].stack[i] + ctx.tags[ctx.tag_id].stack[i] = t + end + end + + tile_all() + end, + }) + + mez.input.add_keymap("alt", "j", { + press = function () + local focused = mez.view.get_focused_id() + + if focused == ctx.tags[ctx.tag_id].master then + mez.view.set_focused(ctx.tags[ctx.tag_id].stack[1]) + elseif focused == ctx.tags[ctx.tag_id].stack[#ctx.tags[ctx.tag_id].stack] then + mez.view.set_focused(ctx.tags[ctx.tag_id].master) + else + for i, id in ipairs(ctx.tags[ctx.tag_id].stack) do + -- TODO: use table.find + if focused == id then + mez.view.set_focused(ctx.tags[ctx.tag_id].stack[i + 1]) + end + end + end + end + }) + + mez.input.add_keymap("alt", "k", { + press = function () + local focused = mez.view.get_focused_id() + + if focused == ctx.tags[ctx.tag_id].master then + mez.view.set_focused(ctx.tags[ctx.tag_id].stack[#ctx.tags[ctx.tag_id].stack]) + elseif focused == ctx.tags[ctx.tag_id].stack[1] then + mez.view.set_focused(ctx.tags[ctx.tag_id].master) + else + for i, id in ipairs(ctx.tags[ctx.tag_id].stack) do + -- TODO: use table.find + if focused == id then + mez.view.set_focused(ctx.tags[ctx.tag_id].stack[i - 1]) + end + end + end + end + }) + + mez.input.add_keymap("alt", "h", { + press = function() + if ctx.master_ratio > 0.15 then + ctx.master_ratio = ctx.master_ratio - 0.05 + tile_all() + end + end + }) + + mez.input.add_keymap("alt", "l", { + press = function() + if ctx.master_ratio < 0.85 then + ctx.master_ratio = ctx.master_ratio + 0.05 + tile_all() + end + end + }) + + mez.input.add_keymap("alt", "Tab", { + press = function () + local focused = mez.view.get_focused_id() + local all = mez.view.get_all_ids() + + for _, id in ipairs(all) do + if id ~= focused then + mez.view.set_focused(id) + return + end + end + end + }) + + for i = 1, 12 do + mez.input.add_keymap("ctrl|alt", "XF86Switch_VT_"..i, { + press = function() mez.api.change_vt(i) end + }) + end +end + +master() + +function print_table(tbl, indent, seen) + indent = indent or 0 + seen = seen or {} + + -- Prevent infinite loops from circular references + if seen[tbl] then + print(string.rep(" ", indent) .. "...(circular reference)") + return + end + seen[tbl] = true + + for key, value in pairs(tbl) do + local formatting = string.rep(" ", indent) .. tostring(key) .. ": " + + if type(value) == "table" then + print(formatting .. "{") + print_table(value, indent + 1, seen) + print(string.rep(" ", indent) .. "}") + elseif type(value) == "string" then + print(formatting .. '"' .. value .. '"') + else + print(formatting .. tostring(value)) + end + end +end + diff --git a/runtime/share/mezzaluna/init.lua b/runtime/share/mezzaluna/init.lua index cfbe04f..0c3e593 100644 --- a/runtime/share/mezzaluna/init.lua +++ b/runtime/share/mezzaluna/init.lua @@ -7,291 +7,10 @@ if not env_conf then env_conf = mez.fs.joinpath(env_conf, ".config") end +-- allow loading files in the runtime directory +package.path = package.path..";"..mez.fs.joinpath(mez.path.runtime, "?.lua") +mez.inspect = require("inspect").inspect + +mez.path.base_config = mez.fs.joinpath(mez.path.runtime, "base_config.lua") mez.path.config = mez.fs.joinpath(env_conf, "mez", "init.lua") package.path = package.path..";"..mez.fs.joinpath(env_conf, "mez", "lua", "?.lua") - -local master = function() - local config = { - tag_count = 5, - } - - local ctx = { - master_ratio = 0.5, - tags = {}, - tag_id = 1 - } - - local tile_onscreen = function(tag_id, res) - if ctx.tags[tag_id].master == nil then - return - end - - if #ctx.tags[tag_id].stack == 0 then - mez.view.set_size(ctx.tags[tag_id].master, res.width, res.height) - mez.view.set_position(ctx.tags[tag_id].master, 0, 0) - else - mez.view.set_size(ctx.tags[tag_id].master, res.width * ctx.master_ratio, res.height) - mez.view.set_position(ctx.tags[tag_id].master, 0, 0) - - for i, stack_id in ipairs(ctx.tags[tag_id].stack) do - mez.view.set_size(stack_id, res.width * (1 - ctx.master_ratio), res.height / #ctx.tags[tag_id].stack) - mez.view.set_position(stack_id, res.width * ctx.master_ratio, (res.height / #ctx.tags[tag_id].stack * (i - 1))) - end - end - end - - local tile_offscreen = function(tag_id, res) - if ctx.tags[tag_id].master == nil then - return - end - - mez.view.set_position(ctx.tags[tag_id].master, 0, -res.height) - mez.view.set_size(ctx.tags[tag_id].master, res.width, res.height) - - for _, view in ipairs(ctx.tags[tag_id].stack) do - mez.view.set_position(view, 0, -res.height) - mez.view.set_size(view, res.width, res.height) - end - end - - local tile_all = function () - local res = mez.output.get_resolution(0) - - for id = 1,config.tag_count do - if id == ctx.tag_id then - tile_onscreen(id, res) - else - tile_offscreen(id, res) - end - end - end - - for i = 1,config.tag_count do - ctx.tags[#ctx.tags + 1] = { - stack = {}, - master = nil, - } - mez.input.add_keymap("alt", "" .. i, { - press = function () - ctx.tag_id = i - - if ctx.tags[i].master then - mez.view.set_focused(ctx.tags[i].master) - else - mez.view.set_focused(nil) - end - - tile_all() - end - }) - end - - mez.hook.add("ServerStartPost", { - callback = function() - print("ServerStartPost") - print("Doesn't do anything :(") - end - }) - - mez.hook.add("ViewMapPre", { - callback = function(v) - if ctx.tags[ctx.tag_id].master == nil then - ctx.tags[ctx.tag_id].master = v - else - table.insert(ctx.tags[ctx.tag_id].stack, #ctx.tags[ctx.tag_id].stack + 1, v) - end - - mez.view.set_focused(v) - - tile_all() - end - }) - - mez.hook.add("ViewUnmapPost", { - callback = function(v) - if v == ctx.tags[ctx.tag_id].master then - if #ctx.tags[ctx.tag_id].stack > 0 then - ctx.tags[ctx.tag_id].master = table.remove(ctx.tags[ctx.tag_id].stack, 1) - mez.view.set_focused(ctx.tags[ctx.tag_id].master) - else - ctx.tags[ctx.tag_id].master = nil - end - else - for i, id in ipairs(ctx.tags[ctx.tag_id].stack) do - if id == v then - if i == 1 then - mez.view.set_focused(ctx.tags[ctx.tag_id].master) - elseif i == #ctx.tags[ctx.tag_id].stack then - mez.view.set_focused(ctx.tags[ctx.tag_id].stack[i - 1]) - else - mez.view.set_focused(ctx.tags[ctx.tag_id].stack[i + 1]) - end - - table.remove(ctx.tags[ctx.tag_id].stack, i) - end - end - end - - tile_all() - end - }) - - mez.hook.add("ViewPointerMotion", { - callback = function (view_id, cursor_x, cursor_y) - mez.view.set_focused(view_id) - end - }) - - mez.input.add_keymap("alt", "p", { - press = function() - mez.api.spawn("wmenu-run") - end, - }) - - mez.input.add_keymap("alt", "b", { - press = function() - mez.api.spawn("swaybg -i ~/Images/wallpapers/void/gruv_void.png") - end, - }) - - mez.input.add_keymap("alt|shift", "Return", { - press = function() - mez.api.spawn("alacritty") - end, - }) - - mez.input.add_keymap("alt|shift", "C", { - press = function () - mez.view.close(0) - end - }) - - mez.input.add_keymap("alt|shift", "q", { - press = function () - mez.api.exit(); - end - }) - - mez.input.add_keymap("alt", "Return", { - press = function() - local focused = mez.view.get_focused_id() - - if focused == ctx.tags[ctx.tag_id].master then return end - - for i, id in ipairs(ctx.tags[ctx.tag_id].stack) do - if focused == id then - local t = ctx.tags[ctx.tag_id].master - ctx.tags[ctx.tag_id].master = ctx.tags[ctx.tag_id].stack[i] - ctx.tags[ctx.tag_id].stack[i] = t - end - end - - tile_all() - end, - }) - - mez.input.add_keymap("alt", "j", { - press = function () - local focused = mez.view.get_focused_id() - - if focused == ctx.tags[ctx.tag_id].master then - mez.view.set_focused(ctx.tags[ctx.tag_id].stack[1]) - elseif focused == ctx.tags[ctx.tag_id].stack[#ctx.tags[ctx.tag_id].stack] then - mez.view.set_focused(ctx.tags[ctx.tag_id].master) - else - for i, id in ipairs(ctx.tags[ctx.tag_id].stack) do - -- TODO: use table.find - if focused == id then - mez.view.set_focused(ctx.tags[ctx.tag_id].stack[i + 1]) - end - end - end - end - }) - - mez.input.add_keymap("alt", "k", { - press = function () - local focused = mez.view.get_focused_id() - - if focused == ctx.tags[ctx.tag_id].master then - mez.view.set_focused(ctx.tags[ctx.tag_id].stack[#ctx.tags[ctx.tag_id].stack]) - elseif focused == ctx.tags[ctx.tag_id].stack[1] then - mez.view.set_focused(ctx.tags[ctx.tag_id].master) - else - for i, id in ipairs(ctx.tags[ctx.tag_id].stack) do - -- TODO: use table.find - if focused == id then - mez.view.set_focused(ctx.tags[ctx.tag_id].stack[i - 1]) - end - end - end - end - }) - - mez.input.add_keymap("alt", "h", { - press = function() - if ctx.master_ratio > 0.15 then - ctx.master_ratio = ctx.master_ratio - 0.05 - tile_all() - end - end - }) - - mez.input.add_keymap("alt", "l", { - press = function() - if ctx.master_ratio < 0.85 then - ctx.master_ratio = ctx.master_ratio + 0.05 - tile_all() - end - end - }) - - mez.input.add_keymap("alt", "Tab", { - press = function () - local focused = mez.view.get_focused_id() - local all = mez.view.get_all_ids() - - for _, id in ipairs(all) do - if id ~= focused then - mez.view.set_focused(id) - return - end - end - end - }) - - for i = 1, 12 do - mez.input.add_keymap("ctrl|alt", "XF86Switch_VT_"..i, { - press = function() mez.api.change_vt(i) end - }) - end -end - -master() - -function print_table(tbl, indent, seen) - indent = indent or 0 - seen = seen or {} - - -- Prevent infinite loops from circular references - if seen[tbl] then - print(string.rep(" ", indent) .. "...(circular reference)") - return - end - seen[tbl] = true - - for key, value in pairs(tbl) do - local formatting = string.rep(" ", indent) .. tostring(key) .. ": " - - if type(value) == "table" then - print(formatting .. "{") - print_table(value, indent + 1, seen) - print(string.rep(" ", indent) .. "}") - elseif type(value) == "string" then - print(formatting .. '"' .. value .. '"') - else - print(formatting .. tostring(value)) - end - end -end - diff --git a/runtime/share/mezzaluna/inspect.lua b/runtime/share/mezzaluna/inspect.lua new file mode 100644 index 0000000..f727d6f --- /dev/null +++ b/runtime/share/mezzaluna/inspect.lua @@ -0,0 +1,355 @@ +local _tl_compat; if (tonumber((_VERSION or ''):match('[%d.]*$')) or 0) < 5.3 then local p, m = pcall(require, 'compat53.module'); if p then _tl_compat = m end end; local math = _tl_compat and _tl_compat.math or math; local string = _tl_compat and _tl_compat.string or string; local table = _tl_compat and _tl_compat.table or table; local type = type +local inspect = { Options = {} } + +inspect._VERSION = 'inspect.lua 3.1.0' +inspect._URL = 'http://github.com/kikito/inspect.lua' +inspect._DESCRIPTION = 'human-readable representations of tables' +inspect._LICENSE = [[ + MIT LICENSE + + Copyright (c) 2022 Enrique GarcĂ­a Cota + + Permission is hereby granted, free of charge, to any person obtaining a + copy of this software and associated documentation files (the + "Software"), to deal in the Software without restriction, including + without limitation the rights to use, copy, modify, merge, publish, + distribute, sublicense, and/or sell copies of the Software, and to + permit persons to whom the Software is furnished to do so, subject to + the following conditions: + + The above copyright notice and this permission notice shall be included + in all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS + OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY + CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, + TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE + SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +]] +inspect.KEY = setmetatable({}, { __tostring = function() return 'inspect.KEY' end }) +inspect.METATABLE = setmetatable({}, { __tostring = function() return 'inspect.METATABLE' end }) + +local tostring = tostring +local rep = string.rep +local match = string.match +local char = string.char +local gsub = string.gsub +local fmt = string.format + +local _rawget +if rawget then + _rawget = rawget +else + _rawget = function(t, k) return t[k] end +end + +local function rawpairs(t) + return next, t, nil +end + + + +local function smartQuote(str) + if match(str, '"') and not match(str, "'") then + return "'" .. str .. "'" + end + return '"' .. gsub(str, '"', '\\"') .. '"' +end + + +local shortControlCharEscapes = { + ["\a"] = "\\a", ["\b"] = "\\b", ["\f"] = "\\f", ["\n"] = "\\n", + ["\r"] = "\\r", ["\t"] = "\\t", ["\v"] = "\\v", ["\127"] = "\\127", +} +local longControlCharEscapes = { ["\127"] = "\127" } +for i = 0, 31 do + local ch = char(i) + if not shortControlCharEscapes[ch] then + shortControlCharEscapes[ch] = "\\" .. i + longControlCharEscapes[ch] = fmt("\\%03d", i) + end +end + +local function escape(str) + return (gsub(gsub(gsub(str, "\\", "\\\\"), + "(%c)%f[0-9]", longControlCharEscapes), + "%c", shortControlCharEscapes)) +end + +local luaKeywords = { + ['and'] = true, + ['break'] = true, + ['do'] = true, + ['else'] = true, + ['elseif'] = true, + ['end'] = true, + ['false'] = true, + ['for'] = true, + ['function'] = true, + ['goto'] = true, + ['if'] = true, + ['in'] = true, + ['local'] = true, + ['nil'] = true, + ['not'] = true, + ['or'] = true, + ['repeat'] = true, + ['return'] = true, + ['then'] = true, + ['true'] = true, + ['until'] = true, + ['while'] = true, +} + +local function isIdentifier(str) + return type(str) == "string" and + not not str:match("^[_%a][_%a%d]*$") and + not luaKeywords[str] +end + +local flr = math.floor +local function isSequenceKey(k, sequenceLength) + return type(k) == "number" and + flr(k) == k and + 1 <= (k) and + k <= sequenceLength +end + +local defaultTypeOrders = { + ['number'] = 1, ['boolean'] = 2, ['string'] = 3, ['table'] = 4, + ['function'] = 5, ['userdata'] = 6, ['thread'] = 7, +} + +local function sortKeys(a, b) + local ta, tb = type(a), type(b) + + + if ta == tb and (ta == 'string' or ta == 'number') then + return (a) < (b) + end + + local dta = defaultTypeOrders[ta] or 100 + local dtb = defaultTypeOrders[tb] or 100 + + + return dta == dtb and ta < tb or dta < dtb +end + +local function getKeys(t) + + local seqLen = 1 + while _rawget(t, seqLen) ~= nil do + seqLen = seqLen + 1 + end + seqLen = seqLen - 1 + + local keys, keysLen = {}, 0 + for k in rawpairs(t) do + if not isSequenceKey(k, seqLen) then + keysLen = keysLen + 1 + keys[keysLen] = k + end + end + table.sort(keys, sortKeys) + return keys, keysLen, seqLen +end + +local function countCycles(x, cycles) + if type(x) == "table" then + if cycles[x] then + cycles[x] = cycles[x] + 1 + else + cycles[x] = 1 + for k, v in rawpairs(x) do + countCycles(k, cycles) + countCycles(v, cycles) + end + countCycles(getmetatable(x), cycles) + end + end +end + +local function makePath(path, a, b) + local newPath = {} + local len = #path + for i = 1, len do newPath[i] = path[i] end + + newPath[len + 1] = a + newPath[len + 2] = b + + return newPath +end + + +local function processRecursive(process, + item, + path, + visited) + if item == nil then return nil end + if visited[item] then return visited[item] end + + local processed = process(item, path) + if type(processed) == "table" then + local processedCopy = {} + visited[item] = processedCopy + local processedKey + + for k, v in rawpairs(processed) do + processedKey = processRecursive(process, k, makePath(path, k, inspect.KEY), visited) + if processedKey ~= nil then + processedCopy[processedKey] = processRecursive(process, v, makePath(path, processedKey), visited) + end + end + + local mt = processRecursive(process, getmetatable(processed), makePath(path, inspect.METATABLE), visited) + if type(mt) ~= 'table' then mt = nil end + setmetatable(processedCopy, mt) + processed = processedCopy + end + return processed +end + +local function puts(buf, str) + buf.n = buf.n + 1 + buf[buf.n] = str +end + + + +local Inspector = {} + + + + + + + + + + +local Inspector_mt = { __index = Inspector } + +local function tabify(inspector) + puts(inspector.buf, inspector.newline .. rep(inspector.indent, inspector.level)) +end + +function Inspector:getId(v) + local id = self.ids[v] + local ids = self.ids + if not id then + local tv = type(v) + id = (ids[tv] or 0) + 1 + ids[v], ids[tv] = id, id + end + return tostring(id) +end + +function Inspector:putValue(v) + local buf = self.buf + local tv = type(v) + if tv == 'string' then + puts(buf, smartQuote(escape(v))) + elseif tv == 'number' or tv == 'boolean' or tv == 'nil' or + tv == 'cdata' or tv == 'ctype' then + puts(buf, tostring(v)) + elseif tv == 'table' and not self.ids[v] then + local t = v + + if t == inspect.KEY or t == inspect.METATABLE then + puts(buf, tostring(t)) + elseif self.level >= self.depth then + puts(buf, '{...}') + else + if self.cycles[t] > 1 then puts(buf, fmt('<%d>', self:getId(t))) end + + local keys, keysLen, seqLen = getKeys(t) + + puts(buf, '{') + self.level = self.level + 1 + + for i = 1, seqLen + keysLen do + if i > 1 then puts(buf, ',') end + if i <= seqLen then + puts(buf, ' ') + self:putValue(t[i]) + else + local k = keys[i - seqLen] + tabify(self) + if isIdentifier(k) then + puts(buf, k) + else + puts(buf, "[") + self:putValue(k) + puts(buf, "]") + end + puts(buf, ' = ') + self:putValue(t[k]) + end + end + + local mt = getmetatable(t) + if type(mt) == 'table' then + if seqLen + keysLen > 0 then puts(buf, ',') end + tabify(self) + puts(buf, ' = ') + self:putValue(mt) + end + + self.level = self.level - 1 + + if keysLen > 0 or type(mt) == 'table' then + tabify(self) + elseif seqLen > 0 then + puts(buf, ' ') + end + + puts(buf, '}') + end + + else + puts(buf, fmt('<%s %d>', tv, self:getId(v))) + end +end + + + + +function inspect.inspect(root, options) + options = options or {} + + local depth = options.depth or (math.huge) + local newline = options.newline or '\n' + local indent = options.indent or ' ' + local process = options.process + + if process then + root = processRecursive(process, root, {}, {}) + end + + local cycles = {} + countCycles(root, cycles) + + local inspector = setmetatable({ + buf = { n = 0 }, + ids = {}, + cycles = cycles, + depth = depth, + level = 0, + newline = newline, + indent = indent, + }, Inspector_mt) + + inspector:putValue(root) + + return table.concat(inspector.buf) +end + +setmetatable(inspect, { + __call = function(_, root, options) + return inspect.inspect(root, options) + end, +}) + +return inspect diff --git a/src/RemoteLua.zig b/src/RemoteLua.zig index fadb433..d5e5f92 100644 --- a/src/RemoteLua.zig +++ b/src/RemoteLua.zig @@ -37,6 +37,9 @@ pub fn create(client: *wl.Client, version: u32, id: u32) !void { errdefer node.L.deinit(); node.L.openLibs(); Lua.openLibs(node.L); + Lua.loadRuntimeDir(node.L) catch |err| if (err == error.LuaRuntime) { + std.log.warn("{s}", .{try node.L.toString(-1)}); + }; // TODO: replace stdout and stderr with buffers we can send to the clients server.remote_lua_clients.prepend(&node.node); diff --git a/src/lua/Bridge.zig b/src/lua/Bridge.zig index f8d4153..8ba0c1d 100644 --- a/src/lua/Bridge.zig +++ b/src/lua/Bridge.zig @@ -1,25 +1,25 @@ const Bridge = @This(); const std = @import("std"); -const Lua = @import("Lua.zig"); +const zlua = @import("zlua"); const gpa = std.heap.c_allocator; -pub fn getNestedField(L: *Lua, path: []u8) bool { +pub fn getNestedField(L: *zlua.Lua, path: []u8) bool { var tokens = std.mem.tokenizeScalar(u8, path, '.'); var first = true; while (tokens.next()) |token| { const tok = gpa.dupeZ(u8, token) catch return false; if (first) { - _ = L.state.getGlobal(tok) catch return false; + _ = L.getGlobal(tok) catch return false; first = false; } else { - _ = L.state.getField(-1, tok); - L.state.remove(-2); + _ = L.getField(-1, tok); + L.remove(-2); } - if (L.state.isNil(-1)) { + if (L.isNil(-1)) { return false; } } diff --git a/src/lua/Lua.zig b/src/lua/Lua.zig index aabb353..3b2c23a 100644 --- a/src/lua/Lua.zig +++ b/src/lua/Lua.zig @@ -16,33 +16,60 @@ const gpa = std.heap.c_allocator; state: *zlua.Lua, -fn loadRuntimeDir(self: *Lua) !void { - const tmppath = try std.fs.path.join(gpa, &[_][]const u8{ +pub fn loadRuntimeDir(self: *zlua.Lua) !void { + const path_dir = try std.fs.path.joinZ(gpa, &[_][]const u8{ config.runtime_path_prefix, "share", "mezzaluna", + }); + defer gpa.free(path_dir); + + { + _ = try self.getGlobal("mez"); + _ = self.getField(-1, "path"); + defer self.pop(2); + _ = self.pushString(path_dir); + self.setField(-2, "runtime"); + } + + const path_full = try std.fs.path.joinZ(gpa, &[_][]const u8{ + path_dir, "init.lua", }); - const path = try gpa.dupeZ(u8, tmppath); + defer gpa.free(path_full); - self.state.doFile(path) catch { - const err = try self.state.toString(-1); + self.doFile(path_full) catch { + const err = try self.toString(-1); std.log.debug("Failed to run lua file: {s}", .{err}); }; } -fn loadConfigDir(self: *Lua) !void { +fn loadBaseConfig(self: *zlua.Lua) !void { + const lua_path = "mez.path.base_config"; + if (!Bridge.getNestedField(self, @constCast(lua_path[0..]))) { + std.log.err("Base config path not found. is your runtime dir setup?", .{}); + return; + } + const path = self.toString(-1) catch |err| { + std.log.err("Failed to pop the base config path from the lua stack. {}", .{err}); + return; + }; + self.pop(-1); + try self.doFile(path); +} + +fn loadConfigDir(self: *zlua.Lua) !void { const lua_path = "mez.path.config"; if (!Bridge.getNestedField(self, @constCast(lua_path[0..]))) { std.log.err("Config path not found. is your runtime dir setup?", .{}); return; } - const path = self.state.toString(-1) catch |err| { + const path = self.toString(-1) catch |err| { std.log.err("Failed to pop the config path from the lua stack. {}", .{err}); return; }; - self.state.pop(-1); - try self.state.doFile(path); + self.pop(-1); + try self.doFile(path); } pub fn openLibs(self: *zlua.Lua) void { @@ -93,16 +120,16 @@ pub fn init(self: *Lua) !void { openLibs(self.state); - loadRuntimeDir(self) catch |err| { - if (err == error.LuaRuntime) { - std.log.warn("{s}", .{try self.state.toString(-1)}); - } + loadRuntimeDir(self.state) catch |err| if (err == error.LuaRuntime) { + std.log.warn("{s}", .{try self.state.toString(-1)}); }; - loadConfigDir(self) catch |err| { - if (err == error.LuaRuntime) { - std.log.warn("{s}", .{try self.state.toString(-1)}); - } + loadBaseConfig(self.state) catch |err| if (err == error.LuaRuntime) { + std.log.warn("{s}", .{try self.state.toString(-1)}); + }; + + loadConfigDir(self.state) catch |err| if (err == error.LuaRuntime) { + std.log.warn("{s}", .{try self.state.toString(-1)}); }; std.log.debug("Loaded lua", .{});