Merge pull request #184 from nullchilly/cleanup

fix(command): after_loading hook & set properties type & packer run hook & update readme
dev
Pocco81 2 years ago committed by GitHub
commit 2b7b75cc72
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -23,6 +23,7 @@ This port of Catppuccin is special because it was the first one and the one that
- Handy CLI. - Handy CLI.
- Extensible for many use cases. - Extensible for many use cases.
- [Compile](https://github.com/catppuccin/nvim#Compile) user's configuration
- Integrations with a bunch of plugins: - Integrations with a bunch of plugins:
- [Treesitter](https://github.com/tree-sitter/tree-sitter) - [Treesitter](https://github.com/tree-sitter/tree-sitter)
- [Native LSP](https://github.com/neovim/nvim-lspconfig) - [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, dim_inactive = false,
transparent_background = false, transparent_background = false,
term_colors = false, term_colors = false,
compile = {
enable = false,
path = vim.fn.stdpath "cache" .. "/catppuccin",
suffix = "_compiled"
},
styles = { styles = {
comments = { "italic" }, comments = { "italic" },
conditionals = { "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 ### Extra
#### API #### API

@ -1,7 +1,67 @@
local M = {} 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() 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 end
function M.setup(custom_opts) function M.setup(custom_opts)

@ -13,10 +13,9 @@ local function inspect(t)
return fmt([[{ %s }]], table.concat(list, ", ")) return fmt([[{ %s }]], table.concat(list, ", "))
end end
function M.compile() function util.compile()
local theme = require("catppuccin.lib.mapper").apply() local theme = require("catppuccin.core.mapper").apply()
local lines = { local lines = { [[
[[
-- This file is autogenerated by CATPPUCCIN. -- This file is autogenerated by CATPPUCCIN.
-- Do not make changes directly to this file. -- Do not make changes directly to this file.
@ -24,12 +23,17 @@ vim.cmd("hi clear")
if vim.fn.exists("syntax_on") then if vim.fn.exists("syntax_on") then
vim.cmd("syntax reset") vim.cmd("syntax reset")
end end
vim.g.colors_name = "catppuccin"]], vim.g.colors_name = "catppuccin"]] }
}
local config = require("catppuccin.config").options local config = require("catppuccin.config").options
local custom_highlights = config.custom_highlights local custom_highlights = config.custom_highlights
for property, value in pairs(theme.properties) do 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 end
local tbl = vim.tbl_deep_extend("keep", theme.integrations, theme.base) local tbl = vim.tbl_deep_extend("keep", theme.integrations, theme.base)
tbl = vim.tbl_deep_extend("keep", custom_highlights, tbl) tbl = vim.tbl_deep_extend("keep", custom_highlights, tbl)
@ -57,51 +61,19 @@ vim.g.colors_name = "catppuccin"]],
end end
if config.term_colors then if config.term_colors then
local colors = { local colors = { "overlay0", "red", "green", "yellow", "blue", "pink", "sky", "text", "overlay1", "red", "green", "yellow", "blue", "pink", "sky", "text"}
"overlay0",
"red",
"green",
"yellow",
"blue",
"pink",
"sky",
"text",
"overlay1",
"red",
"green",
"yellow",
"blue",
"pink",
"sky",
"text",
}
for i = 0, 15 do for i = 0, 15 do
table.insert(lines, fmt('vim.g.terminal_color_%d = "%s"', i, theme.terminal[colors[i + 1]])) table.insert(lines, fmt('vim.g.terminal_color_%d = "%s"', i, theme.terminal[colors[i + 1]]))
end end
end end
os.execute( os.execute(string.format("mkdir %s %s", vim.loop.os_uname().sysname == 'Windows' and "" or "-p", config.compile.path))
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")
)
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:write(table.concat(lines, "\n"))
file:close() file:close()
end end
function M.clean() function util.clean()
local config = require("catppuccin.config").options local config = require("catppuccin.config").options
local compiled_path = config.compile.path local compiled_path = config.compile.path .. (vim.loop.os_uname().sysname == 'Windows' and "\\" or "/") .. vim.g.catppuccin_flavour .. config.compile.suffix .. ".lua"
.. (vim.loop.os_uname().sysname == "Windows" and "\\" or "/")
.. vim.g.catppuccin_flavour
.. config.compile.suffix
.. ".lua"
os.remove(compiled_path) os.remove(compiled_path)
end end
return M

@ -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
Loading…
Cancel
Save