diff --git a/src/lua/runtime/iter.lua b/src/lua/runtime/iter.lua --- a/src/lua/runtime/iter.lua +++ b/src/lua/runtime/iter.lua @@ -8,57 +8,57 @@ ---@param n function ---@param t T ---@param i integer function Iter.new(n, t, i) - local self = {} - self.n = n - self.t = t - self.i = i + local self = {} + self.n = n + self.t = t + self.i = i - return setmetatable(self, Iter) + return setmetatable(self, Iter) end function Iter:to() - local final = {} + local final = {} - for k, v in self.n, self.t, self.i do - final[k] = v - end + for k, v in self.n, self.t, self.i do + final[k] = v + end - return final + return final end ---@generic K, V, R ---@param f fun(k: K, v: V): R function Iter:map(f) - local new = {} + local new = {} - for k, v in self.n, self.t, self.i do - local result = f(k, v) - if result ~= nil then - table.insert(new, result) - end - end + for k, v in self.n, self.t, self.i do + local result = f(k, v) + if result ~= nil then + table.insert(new, result) + end + end - return Iter.new(self.n, new, self.i) + return Iter.new(self.n, new, self.i) end ---@generic K, V, R ---@param f fun(k: K, v: V): R function Iter:filter(f) - for k, v in self.n, self.t, self.i do - if not f(k, v) then - table.remove(self.t, k) - end - end + for k, v in self.n, self.t, self.i do + if not f(k, v) then + table.remove(self.t, k) + end + end - return self + return self end ---@generic K, V ---@param f fun(k: K, v: V) function Iter:each(f) - for k, v in self.n, self.t, self.i do - f(k, v) - end + for k, v in self.n, self.t, self.i do + f(k, v) + end end ---@generic K, V, R @@ -66,102 +66,102 @@ ---@param init R ---@param f fun(acc: R, k: K, v: V): R? ---@return R function Iter:fold(init, f) - local acc = init - for k, v in self.n, self.t, self.i do - local result = f(acc, k, v) - if result ~= nil then - acc = result - end - end + local acc = init + for k, v in self.n, self.t, self.i do + local result = f(acc, k, v) + if result ~= nil then + acc = result + end + end - return acc + return acc end ---@generic K, V ---@param f fun(k: K, v: V): K, V function Iter:mapattrs(f) - local new = {} + local new = {} - for k, v in self.n, self.t, self.i do - local _k, _v = f(k, v) - if _k ~= nil then - new[_k] = _v - end - end + for k, v in self.n, self.t, self.i do + local _k, _v = f(k, v) + if _k ~= nil then + new[_k] = _v + end + end - return Iter.new(self.n, new, self.i) + return Iter.new(self.n, new, self.i) end ---@generic V ---@param f fun(a: V, b: V): boolean returns true when the first item is less --- than the second function Iter:sort(f) - local new = {} - for k, v in self.n, self.t, self.i do - new[k] = v - end + local new = {} + for k, v in self.n, self.t, self.i do + new[k] = v + end - table.sort(new, f) + table.sort(new, f) - return Iter.new(self.n, new, self.i) + return Iter.new(self.n, new, self.i) end ---@param n integer function Iter:skip(n) - self.i = (self.i or 1) + n + self.i = (self.i or 1) + n - return self + return self end ---@param j integer function Iter:take(j) - local new = {} + local new = {} - local i = 0 - for k, v in self.n, self.t, self.i do - i = i + 1 - if i > j then - break - end + local i = 0 + for k, v in self.n, self.t, self.i do + i = i + 1 + if i > j then + break + end - new[k] = v - end + new[k] = v + end - return Iter.new(self.n, new, self.i) + return Iter.new(self.n, new, self.i) end ---@param lst any[] function Iter.lst(lst) - return Iter.new(ipairs(lst)) + return Iter.new(ipairs(lst)) end ---@param tbl table function Iter.tbl(tbl) - return Iter.new(pairs(tbl)) + return Iter.new(pairs(tbl)) end ---@param str string function Iter.str(str) - local it = Iter.new( - setmetatable({ i = -1 }, { - __call = function(self, s, i) - if self.i == -1 then - self.i = i - end - local idx = self.i - self.i = self.i + 1 - if idx > #s then - self.i = -1 - return - end - return idx, s:sub(idx, idx) - end, - }), - str, - 1 - ) - local lst = it:to() - return Iter.lst(lst) + local it = Iter.new( + setmetatable({ i = -1 }, { + __call = function(self, s, i) + if self.i == -1 then + self.i = i + end + local idx = self.i + self.i = self.i + 1 + if idx > #s then + self.i = -1 + return + end + return idx, s:sub(idx, idx) + end, + }), + str, + 1 + ) + local lst = it:to() + return Iter.lst(lst) end return Iter diff --git a/src/lua/runtime/maivi.lua b/src/lua/runtime/maivi.lua --- a/src/lua/runtime/maivi.lua +++ b/src/lua/runtime/maivi.lua @@ -10,39 +10,39 @@ ---@param t table ---@param f fun(k: string): T ---@param mt metatable function maivi.defaulttable(t, f, mt) - local mt_ = { - __index = function(self, k) - local v = f(k) - rawset(self, k, v) - return rawget(self, k) - end, - } - if mt then - for k, v in pairs(mt) do - mt_[k] = v - end - end - return setmetatable(t, mt_) + local mt_ = { + __index = function(self, k) + local v = f(k) + rawset(self, k, v) + return rawget(self, k) + end, + } + if mt then + for k, v in pairs(mt) do + mt_[k] = v + end + end + return setmetatable(t, mt_) end ---@param str string ---@return string function maivi.escape(str) - local s = Iter.str(str) - :map(function(_, char) - if char == "<" then - return "<" - elseif char == ">" then - return ">" - elseif char == "&" then - return "&" - elseif char == '"' then - return """ - end - return char - end) - :to() - return table.concat(s) + local s = Iter.str(str) + :map(function(_, char) + if char == "<" then + return "<" + elseif char == ">" then + return ">" + elseif char == "&" then + return "&" + elseif char == '"' then + return """ + end + return char + end) + :to() + return table.concat(s) end -- maivi.str = maivi.str or {} @@ -82,99 +82,99 @@ ---@param args PropsOrChildren ---@return ParsedArgs function maivi.parse(args) - ---@type Props - local props = {} - ---@type Children - local children = {} - ---@type string[] - local boolean_props = {} + ---@type Props + local props = {} + ---@type Children + local children = {} + ---@type string[] + local boolean_props = {} - if type(args) == "table" then - for k, v in pairs(args) do - if type(k) == "number" then - table.insert(children, v) - elseif k == "_" then - for _, prop in ipairs(v) do - table.insert(boolean_props, prop) - end - elseif k == "data" then - for kdata, prop in ipairs(v) do - props["data-" .. kdata] = maivi.display.attr(prop) - end - else - props[k] = maivi.display.attr(v) - end - end - elseif args then - table.insert(children, args) - end + if type(args) == "table" then + for k, v in pairs(args) do + if type(k) == "number" then + table.insert(children, v) + elseif k == "_" then + for _, prop in ipairs(v) do + table.insert(boolean_props, prop) + end + elseif k == "data" then + for kdata, prop in ipairs(v) do + props["data-" .. kdata] = maivi.display.attr(prop) + end + else + props[k] = maivi.display.attr(v) + end + end + elseif args then + table.insert(children, args) + end - return { - props = props, - children = children, - boolean_props = boolean_props, - } + return { + props = props, + children = children, + boolean_props = boolean_props, + } end ---@param config? ElementConfig ---@return ElementConfig local function assign_config(config) - local default = { - close = true, - empty = false, - } + local default = { + close = true, + empty = false, + } - if config then - for k, v in pairs(config) do - default[k] = v - end - end + if config then + for k, v in pairs(config) do + default[k] = v + end + end - return default + return default end ---@param name string ---@param config? ElementConfig ---@return Element function maivi.elem(name, config) - local cfg = assign_config(config) - return function(args) - local str = "<" .. name - local parsed = maivi.parse(args) + local cfg = assign_config(config) + return function(args) + local str = "<" .. name + local parsed = maivi.parse(args) - for k, v in pairs(parsed.props) do - local s = maivi.display.attr(v) - if s then - s = maivi.escape(s) - local entry = k .. '="' .. s .. '"' - str = str .. " " .. entry - end - end + for k, v in pairs(parsed.props) do + local s = maivi.display.attr(v) + if s then + s = maivi.escape(s) + local entry = k .. '="' .. s .. '"' + str = str .. " " .. entry + end + end - for _, prop in ipairs(parsed.boolean_props) do - str = str .. " " .. prop - end + for _, prop in ipairs(parsed.boolean_props) do + str = str .. " " .. prop + end - if cfg.empty and #parsed.children == 0 then - str = str .. " />" - return str - end + if cfg.empty and #parsed.children == 0 then + str = str .. " />" + return str + end - str = str .. ">" + str = str .. ">" - for _, child in ipairs(parsed.children) do - local s = maivi.display.elem(child) - if s then - str = str .. s - end - end + for _, child in ipairs(parsed.children) do + local s = maivi.display.elem(child) + if s then + str = str .. s + end + end - if cfg.close then - str = str .. "" - end + if cfg.close then + str = str .. "" + end - return str - end + return str + end end -- components ================================================================= @@ -182,61 +182,61 @@ ---@param f Component ---@return Element function maivi.component(f) - return function(args) - local parsed = maivi.parse(args) - return maivi.display.elem(f(parsed.props, parsed.children)) - end + return function(args) + local parsed = maivi.parse(args) + return maivi.display.elem(f(parsed.props, parsed.children)) + end end ---@param f fun(slots: table): Component ---@return fun(page: Component): Component function maivi.layout(f) - return function(page) - return function(args) - local parsed = maivi.parse(args) - local slots = page(parsed.props, parsed.children) - return f(slots)(parsed.props, parsed.children) - end - end + return function(page) + return function(args) + local parsed = maivi.parse(args) + local slots = page(parsed.props, parsed.children) + return f(slots)(parsed.props, parsed.children) + end + end end -- modules ==================================================================== maivi.mod = setmetatable({ - graph = {}, + graph = {}, }, { - ---@param t table - ---@param modname string - __call = function(t, modname) - local info = debug.getinfo(2, "Sln") - local source = info.source - t.graph[source] = t.graph[source] or {} - table.insert(t.graph[source], modname) + ---@param t table + ---@param modname string + __call = function(t, modname) + local info = debug.getinfo(2, "Sln") + local source = info.source + t.graph[source] = t.graph[source] or {} + table.insert(t.graph[source], modname) - return function(...) - local ok, result = pcall(require, modname) - if not ok then - error( - ([[%s:%s error while loading module '%s':\t\n%s]]):format( - info.short_src, - info.currentline, - modname, - result - ) - ) - end - if type(result) ~= "function" then - error( - ([[%s:%s return value from module '%s' is not a function]]):format( - info.short_src, - info.currentline, - modname - ) - ) - end - return result(...) - end - end, + return function(...) + local ok, result = pcall(require, modname) + if not ok then + error( + ([[%s:%s error while loading module '%s':\t\n%s]]):format( + info.short_src, + info.currentline, + modname, + result + ) + ) + end + if type(result) ~= "function" then + error( + ([[%s:%s return value from module '%s' is not a function]]):format( + info.short_src, + info.currentline, + modname + ) + ) + end + return result(...) + end + end, }) return maivi diff --git a/stylua.toml b/stylua.toml new file mode 100644 --- /dev/null +++ b/stylua.toml @@ -0,0 +1,9 @@ +indent_type = "Spaces" +indent_width = 2 +column_width = 80 +quote_style = "AutoPreferDouble" +call_parentheses = "Always" +line_endings = "Unix" + +[sort_requires] +enabled = true