feat(compile): Add compile support

dev
nullchilly 2 years ago
parent edd620668b
commit 2a235b712b

@ -4,6 +4,11 @@ local config = {}
config.options = { config.options = {
transparent_background = false, transparent_background = false,
term_colors = false, term_colors = false,
compile = {
enable = false,
path = vim.fn.stdpath "cache" .. "/catppuccin",
suffix = "_compiled"
},
dim_inactive = { dim_inactive = {
enable = false, enable = false,
shade = "dark", shade = "dark",

@ -1,6 +1,5 @@
local M = {} local M = {}
local utils = require("catppuccin.utils.util")
local flavours = {"latte", "frappe", "macchiato", "mocha"} local flavours = {"latte", "frappe", "macchiato", "mocha"}
function M.cli_flavour_completion() function M.cli_flavour_completion()
@ -14,12 +13,23 @@ local function load()
catppuccin.before_loading() catppuccin.before_loading()
end end
-- colorscheme gets evaluated from mapper.lua local config = require("catppuccin.config").options
local theme = require("catppuccin.core.mapper").apply() if config.compile.enable == true then
utils.load(theme) local compiled_path = config.compile.path .. (vim.loop.os_uname().sysname == 'Windows' and "\\" or "/") .. vim.g.catppuccin_flavour .. config.compile.suffix .. ".lua"
local f = io.open(compiled_path, "r")
if catppuccin.after_loading ~= nil then if f ~= nil then
catppuccin.after_loading() io.close(f)
vim.cmd("luafile " .. compiled_path)
end
else
-- colorscheme gets evaluated from mapper.lua
local theme = require("catppuccin.core.mapper").apply()
local utils = require("catppuccin.utils.util")
utils.load(theme)
if catppuccin.after_loading ~= nil then
catppuccin.after_loading()
end
end end
vim.api.nvim_exec_autocmds("User", { pattern = "CatppuccinLoaded" }) vim.api.nvim_exec_autocmds("User", { pattern = "CatppuccinLoaded" })

@ -99,4 +99,73 @@ function util.load(theme)
end end
end end
-- Credit: https://github.com/EdenEast/nightfox.nvim
local fmt = string.format
local function inspect(t)
local list = {}
for k, v in pairs(t) do
local q = type(v) == "string" and [["]] or ""
table.insert(list, fmt([[%s = %s%s%s]], k, q, v, q))
end
table.sort(list)
return fmt([[{ %s }]], table.concat(list, ", "))
end
function util.compile(theme)
if vim.fn.has "nvim-0.7" == 0 then
print "Compiling requires neovim 0.7"
end
local lines = { [[
-- This file is autogenerated by CATPPUCCIN.
-- Do not make changes directly to this file.
vim.cmd("hi clear")
if vim.fn.exists("syntax_on") then
vim.cmd("syntax reset")
end
vim.g.colors_name = "catppuccin"]] }
local config = require("catppuccin.config").options
local custom_highlights = config.custom_highlights
for property, value in pairs(theme.properties) do
table.insert(lines, fmt("vim.o.%s = %s", property, value))
end
local tbl = vim.tbl_deep_extend("keep", theme.integrations, theme.base)
tbl = vim.tbl_deep_extend("keep", custom_highlights, tbl)
for group, color in pairs(tbl) do
if color.link then
table.insert(lines, fmt([[vim.api.nvim_set_hl(0, "%s", { link = "%s" })]], group, color.link))
else
if color.style then
if color.style ~= "NONE" then
if type(color.style) == "table" then
for _, style in ipairs(color.style) do
color[style] = true
end
else
color[color.style] = true
end
end
end
color.style = nil
vim.api.nvim_set_hl(0, group, color)
table.insert(lines, fmt([[vim.api.nvim_set_hl(0, "%s", %s)]], group, inspect(color)))
end
end
if config.term_colors then
local colors = { "overlay0", "red", "green", "yellow", "blue", "pink", "sky", "text", "overlay1", "red", "green", "yellow", "blue", "pink", "sky", "text"}
for i = 0, 15 do
table.insert(lines, fmt('vim.g.terminal_color_%d = "%s"', i, theme.terminal[colors[i + 1]]))
end
end
os.execute(string.format("mkdir %s %s", vim.loop.os_uname().sysname == 'Windows' and "" or "-p", config.compile.path))
print(string.format("mkdir %s %s", vim.loop.os_uname().sysname == 'Windows' and "" or "-p", config.compile.path))
local file = io.open(config.compile.path .. (vim.loop.os_uname().sysname == 'Windows' and "\\" or "/") .. vim.g.catppuccin_flavour .. config.compile.suffix .. ".lua", "w")
file:write(table.concat(lines, "\n"))
file:close()
end
return util return util

Loading…
Cancel
Save