|
| 1 | +---@class compiler |
| 2 | +---@source: https://github.com/catppuccin/nvim |
| 3 | +local compiler = {} |
| 4 | + |
| 5 | +local fmt = string.format |
| 6 | +local utils = require "one_monokai.utils" |
| 7 | + |
| 8 | +local function inspect(attr) |
| 9 | + local list = {} |
| 10 | + |
| 11 | + for key, value in pairs(attr) do |
| 12 | + local t = type(value) |
| 13 | + |
| 14 | + if t == "string" then |
| 15 | + table.insert(list, fmt([[%s = "%s"]], key, value)) |
| 16 | + else |
| 17 | + table.insert(list, fmt("%s = %s", key, t == table and inspect(value) or tostring(value))) |
| 18 | + end |
| 19 | + end |
| 20 | + |
| 21 | + return fmt([[{ %s }]], table.concat(list, ", ")) |
| 22 | +end |
| 23 | + |
| 24 | +function compiler.compile() |
| 25 | + local config = require "one_monokai.config" |
| 26 | + local themes = require "one_monokai.themes" |
| 27 | + local cached_themes_path = utils.join_paths(config.compile_path, "themes") |
| 28 | + |
| 29 | + local lines = { |
| 30 | + [[ |
| 31 | +return string.dump(function() |
| 32 | +local hi = vim.api.nvim_set_hl |
| 33 | +if vim.g.colors_name then vim.cmd.hi "clear" end |
| 34 | +vim.o.termguicolors = true |
| 35 | +vim.g.colors_name = "one_monokai" |
| 36 | +vim.o.background = "dark" |
| 37 | +]], |
| 38 | + } |
| 39 | + |
| 40 | + for name, attrs in pairs(themes.groups) do |
| 41 | + for _, attr in pairs(attrs) do |
| 42 | + if not config.italics and attr == "italic" then |
| 43 | + attrs[attr] = false |
| 44 | + end |
| 45 | + end |
| 46 | + |
| 47 | + table.insert(lines, fmt([[hi(0, "%s", %s)]], name, inspect(attrs))) |
| 48 | + end |
| 49 | + |
| 50 | + table.insert(lines, "end, true)") |
| 51 | + |
| 52 | + if vim.fn.isdirectory(config.compile_path) == 0 then |
| 53 | + vim.fn.mkdir(config.compile_path, "p") |
| 54 | + end |
| 55 | + |
| 56 | + local f = loadstring(table.concat(lines, "\n")) |
| 57 | + |
| 58 | + if not f then |
| 59 | + local err_path = os.getenv "TMP" .. "/one_monokai_error.lua" |
| 60 | + |
| 61 | + print(string.format( |
| 62 | + [[One Monokai (error): Most likely some mistake made in your One Monokai config |
| 63 | +You can open %s for debugging |
| 64 | +
|
| 65 | +If you think this is a bug, kindly open an issue and attach %s file |
| 66 | +Below is the error message that we captured: |
| 67 | +]], |
| 68 | + err_path, |
| 69 | + err_path |
| 70 | + )) |
| 71 | + |
| 72 | + local err = io.open(err_path, "wb") |
| 73 | + |
| 74 | + if err then |
| 75 | + err:write(table.concat(lines, "\n")) |
| 76 | + err:close() |
| 77 | + end |
| 78 | + |
| 79 | + dofile(err_path) |
| 80 | + |
| 81 | + return |
| 82 | + end |
| 83 | + |
| 84 | + local file = assert( |
| 85 | + io.open(cached_themes_path, "wb"), |
| 86 | + "Permission denied while writing compiled file to " .. config.compile_path |
| 87 | + ) |
| 88 | + |
| 89 | + file:write(f()) |
| 90 | + file:close() |
| 91 | +end |
| 92 | + |
| 93 | +return compiler |
0 commit comments