kanso.nvim/lua/kanso/utils.lua
Webhooked 90088255f9 Add EditorConfig and standardize code formatting
Adds an EditorConfig file to maintain consistent coding styles across editors. Reformats code with consistent indentation using spaces instead of tabs. Makes minor color palette adjustments.
2025-05-26 11:01:00 +02:00

60 lines
1.7 KiB
Lua

local M = {}
local PATH_SEP = vim.loop.os_uname().version:match("Windows") and "\\" or "/"
local get_compiled_path = function(theme)
return table.concat({ vim.fn.stdpath("state"), "kanso", theme .. "_compiled.lua" }, PATH_SEP)
end
---@return string theme
function M.get_theme_from_bg_opt()
local config = require("kanso").config
return config.theme[vim.o.background] or config.theme.default
end
---@param theme string
---@param highlights table
---@param termcolors table
function M.compile(theme, highlights, termcolors)
vim.loop.fs_mkdir(vim.fn.stdpath("state") .. PATH_SEP .. "kanso", 448)
local fname = get_compiled_path(theme)
local file, err = io.open(fname, "wb")
if not file or err then
vim.notify("Kanso: Error writing " .. fname .. ":\n" .. err, vim.log.levels.ERROR)
return
end
local lines = {
"require'kanso'.compiled = string.dump(function()",
"local g = vim.g",
"local nvim_set_hl = vim.api.nvim_set_hl",
}
local inspect = vim.inspect
for hl, spec in pairs(highlights) do
if next(spec) then
table.insert(lines, ('nvim_set_hl(0, "%s", %s)'):format(hl, inspect(spec):gsub("%s", "")))
end
for i, tcolor in ipairs(termcolors) do
table.insert(lines, ('g["terminal_color_%d"] = "%s"'):format(i - 1, tcolor))
end
end
table.insert(lines, "end)")
local blob = table.concat(lines, "\n")
assert(loadstring(blob, "=(compile)"))()
file:write(require("kanso").compiled)
file:close()
end
---@param theme string
---@return boolean status
function M.load_compiled(theme)
local f = loadfile(get_compiled_path(theme))
if f then
f()
return true
end
return false
end
return M