2025-05-06 12:02:29 +02:00
|
|
|
local M = {}
|
2025-07-25 11:00:04 +02:00
|
|
|
local PATH_SEP = vim.uv.os_uname().version:match("Windows") and "\\" or "/"
|
2025-05-06 12:02:29 +02:00
|
|
|
|
|
|
|
|
local get_compiled_path = function(theme)
|
2025-05-26 11:01:00 +02:00
|
|
|
return table.concat({ vim.fn.stdpath("state"), "kanso", theme .. "_compiled.lua" }, PATH_SEP)
|
2025-05-06 12:02:29 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
---@return string theme
|
|
|
|
|
function M.get_theme_from_bg_opt()
|
|
|
|
|
local config = require("kanso").config
|
2025-07-25 11:00:04 +02:00
|
|
|
return config.background[vim.o.background] or config.theme
|
2025-05-06 12:02:29 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
---@param theme string
|
|
|
|
|
---@param highlights table
|
|
|
|
|
---@param termcolors table
|
|
|
|
|
function M.compile(theme, highlights, termcolors)
|
2025-07-25 11:00:04 +02:00
|
|
|
vim.uv.fs_mkdir(vim.fn.stdpath("state") .. PATH_SEP .. "kanso", 448)
|
2025-05-06 12:02:29 +02:00
|
|
|
|
|
|
|
|
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
|