diff --git a/README.md b/README.md index 4ac9355..7f48b05 100644 --- a/README.md +++ b/README.md @@ -23,6 +23,7 @@ This port of Catppuccin is special because it was the first one and the one that - Handy CLI. - Extensible for many use cases. +- [Compile](https://github.com/catppuccin/nvim#Compile) user's configuration - Integrations with a bunch of plugins: - [Treesitter](https://github.com/tree-sitter/tree-sitter) - [Native LSP](https://github.com/neovim/nvim-lspconfig) @@ -86,6 +87,11 @@ There are already some sane defaults that you may like, however you can change t dim_inactive = false, transparent_background = false, term_colors = false, +compile = { + enable = false, + path = vim.fn.stdpath "cache" .. "/catppuccin", + suffix = "_compiled" +}, styles = { comments = { "italic" }, conditionals = { "italic" }, @@ -287,6 +293,42 @@ integration = { } ``` +### Compile + +Catppuccin is a highly customizable and configurable colorscheme. This does however come at the cost of complexity and execution time. + +Catppuccin can pre compute the results of your configuration and store the results in a compiled lua file. We use these precached values to set it's highlights. + +To enable compile enables it in setup function: + +```lua +compile = { + enable = true, + path = vim.fn.stdpath "cache" .. "/catppuccin", + suffix = "_compiled" +}, +``` +By default catppuccin writes the compiled results into the system's cache directory. + +Catppuccin provides these commands to work with the catppuccin compiler. + +```vim +:CatppuccinCompile # Create/update the compile file +:CatppuccinClean # Delete compiled file +``` + +It's recommended to add `:CatppuccinCompile` to run everytime you update your config. For example: + +``` +use { + "catppuccin/nvim", + as = "catppuccin", + run = ":CatppuccinCompile", +} +``` + +Acknowledge: [nightfox.nvim#compile](https://github.com/EdenEast/nightfox.nvim#compile) + ### Extra #### API diff --git a/lua/catppuccin/init.lua b/lua/catppuccin/init.lua index bcc7c93..1c439f2 100644 --- a/lua/catppuccin/init.lua +++ b/lua/catppuccin/init.lua @@ -1,7 +1,67 @@ local M = {} +local flavours = {"latte", "frappe", "macchiato", "mocha"} + +local command = vim.api.nvim_create_user_command + +command("Catppuccin", function(inp) + if not vim.tbl_contains(flavours, inp.args) then + local echo = require("catppuccin.utils.echo") + echo("Invalid flavour", "info") + return + end + vim.g.catppuccin_flavour = inp.args + vim.cmd "colorscheme catppuccin" +end, { + nargs = 1, + complete = function(line) + return vim.tbl_filter(function(val) + return vim.startswith(val, line) + end, flavours) + end +}) + +command("CatppuccinCompile", function() + require("catppuccin.utils.util").compile() +end, {}) + +command("CatppuccinClean", function() + require("catppuccin.utils.util").clean() +end, {}) + function M.load() - require("catppuccin.main").main("load") + local catppuccin = require("catppuccin") + + if catppuccin.before_loading ~= nil then + catppuccin.before_loading() + end + + local loaded = nil + local config = require("catppuccin.config").options + + if config.compile.enable == true then + 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 f ~= nil then + io.close(f) + vim.cmd("luafile " .. compiled_path) + loaded = true + end + end + + if not loaded then -- 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 + + vim.api.nvim_exec_autocmds("User", { pattern = "CatppuccinLoaded" }) end function M.setup(custom_opts) diff --git a/lua/catppuccin/lib/compiler.lua b/lua/catppuccin/lib/compiler.lua index c0a309a..9717faa 100644 --- a/lua/catppuccin/lib/compiler.lua +++ b/lua/catppuccin/lib/compiler.lua @@ -3,20 +3,19 @@ local M = {} -- 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 + 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, ", ")) + table.sort(list) + return fmt([[{ %s }]], table.concat(list, ", ")) end -function M.compile() - local theme = require("catppuccin.lib.mapper").apply() - local lines = { - [[ +function util.compile() + local theme = require("catppuccin.core.mapper").apply() + local lines = { [[ -- This file is autogenerated by CATPPUCCIN. -- Do not make changes directly to this file. @@ -24,12 +23,17 @@ vim.cmd("hi clear") if vim.fn.exists("syntax_on") then vim.cmd("syntax reset") end -vim.g.colors_name = "catppuccin"]], - } +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)) + if type(value) == "string" then + table.insert(lines, fmt('vim.o.%s = "%s"', property, value)) + elseif type(value) == "bool" then + table.insert(lines, fmt('vim.o.%s = %s', property, value)) + elseif type(value) == "table" then + table.insert(lines, fmt('vim.o.%s = %s', property, inspect(value))) + end end local tbl = vim.tbl_deep_extend("keep", theme.integrations, theme.base) tbl = vim.tbl_deep_extend("keep", custom_highlights, tbl) @@ -57,51 +61,19 @@ vim.g.colors_name = "catppuccin"]], end if config.term_colors then - local colors = { - "overlay0", - "red", - "green", - "yellow", - "blue", - "pink", - "sky", - "text", - "overlay1", - "red", - "green", - "yellow", - "blue", - "pink", - "sky", - "text", - } + 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) - ) - local file = io.open( - config.compile.path - .. (vim.loop.os_uname().sysname == "Windows" and "\\" or "/") - .. vim.g.catppuccin_flavour - .. config.compile.suffix - .. ".lua", - "w" - ) + os.execute(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 -function M.clean() +function util.clean() local config = require("catppuccin.config").options - local compiled_path = config.compile.path - .. (vim.loop.os_uname().sysname == "Windows" and "\\" or "/") - .. vim.g.catppuccin_flavour - .. config.compile.suffix - .. ".lua" + local compiled_path = config.compile.path .. (vim.loop.os_uname().sysname == 'Windows' and "\\" or "/") .. vim.g.catppuccin_flavour .. config.compile.suffix .. ".lua" os.remove(compiled_path) end - -return M diff --git a/lua/catppuccin/main.lua b/lua/catppuccin/main.lua deleted file mode 100644 index c4f1ca3..0000000 --- a/lua/catppuccin/main.lua +++ /dev/null @@ -1,67 +0,0 @@ -local M = {} - -local flavours = {"latte", "frappe", "macchiato", "mocha"} -local command = vim.api.nvim_create_user_command - -command("Catppuccin", function(inp) - vim.g.catppuccin_flavour = inp.args - vim.cmd "colorscheme catppuccin" -end, { - nargs = 1, - complete = function(line) - local builtin_list = flavours - return vim.tbl_filter(function(val) - return vim.startswith(val, line) - end, builtin_list) - end -}) - -command("CatppuccinCompile", function() - require("catppuccin.lib.compiler").compile() -end, {}) - -command("CatppuccinClean", function() - require("catppuccin.lib.compiler").clean() -end, {}) - -local function load() - local catppuccin = require("catppuccin") - - if catppuccin.before_loading ~= nil then - catppuccin.before_loading() - end - - local config = require("catppuccin.config").options - if config.compile.enable == true then - 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 f ~= nil then - io.close(f) - vim.cmd("luafile " .. compiled_path) - vim.api.nvim_exec_autocmds("User", { pattern = "CatppuccinLoaded" }) - return - end - end - -- colorscheme gets evaluated from mapper.lua - local theme = require("catppuccin.lib.mapper").apply() - local highlighter = require("catppuccin.lib.highlighter") - highlighter.load(theme) - - if catppuccin.after_loading ~= nil then - catppuccin.after_loading() - end - - vim.api.nvim_exec_autocmds("User", { pattern = "CatppuccinLoaded" }) -end - -function M.main(option) - option = option or "load" - - if option == "load" then - load() - else - print("catppuccin: option was not recognized") - end -end - -return M