|
| 1 | +local present, base46 = pcall(require, "base46") |
| 2 | +if not present or not base46._DMS_SUPPORT then |
| 3 | + vim.notify( |
| 4 | + "base46 plugin not found or incorrect, make sure to install AvengeMedia/base46", |
| 5 | + vim.log.levels.ERROR, |
| 6 | + { title = "dms integration" } |
| 7 | + ) |
| 8 | + return |
| 9 | +end |
| 10 | + |
| 11 | +local config_home = vim.env.XDG_CONFIG_HOME |
| 12 | +if config_home == nil or #config_home == 0 then |
| 13 | + config_home = vim.fs.joinpath(vim.env.HOME, ".config") |
| 14 | +end |
| 15 | +local settings_file_path = vim.fs.joinpath(config_home, "DankMaterialShell", "settings.json") |
| 16 | +local settings_file = io.open(settings_file_path, "r") |
| 17 | +if settings_file == nil then |
| 18 | + vim.notify( |
| 19 | + "cannnot read dms settings file at '" .. settings_file_path .. "'", |
| 20 | + vim.log.levels.ERROR, |
| 21 | + { title = "dms integration" } |
| 22 | + ) |
| 23 | + return |
| 24 | +end |
| 25 | +local settings = vim.json.decode(settings_file:read("*a")) |
| 26 | +settings_file:close() |
| 27 | + |
| 28 | +local function deepGet(t, k) |
| 29 | + for _, s in ipairs(k) do |
| 30 | + if type(t) ~= "table" then |
| 31 | + return |
| 32 | + end |
| 33 | + t = t[s] |
| 34 | + end |
| 35 | + return t |
| 36 | +end |
| 37 | + |
| 38 | +local current_file_path = debug.getinfo(1, "S").source:sub(2) |
| 39 | +local theme_base = deepGet(settings, { "matugenTemplateNeovimSettings", vim.o.background, "baseTheme" }) |
| 40 | + or ("github_" .. vim.o.background) |
| 41 | +local harmony = deepGet(settings, { "matugenTemplateNeovimSettings", vim.o.background, "harmony" }) or 0.5 |
| 42 | +local theme_name = "dms" |
| 43 | + |
| 44 | +if not _G._matugen_theme_watcher then |
| 45 | + local uv = vim.uv or vim.loop |
| 46 | + _G._matugen_theme_watcher = { uv.new_fs_event(), uv.new_fs_event(), reload_timer = uv.new_timer() } |
| 47 | + |
| 48 | + local debounce_time = 100 -- ms |
| 49 | + local function handler() |
| 50 | + _G._matugen_theme_watcher.reload_timer:stop() |
| 51 | + _G._matugen_theme_watcher.reload_timer:start( |
| 52 | + debounce_time, |
| 53 | + 0, |
| 54 | + vim.schedule_wrap(function() |
| 55 | + base46.theme_tables[theme_name] = nil |
| 56 | + if vim.g.colors_name == theme_name then |
| 57 | + vim.cmd.colorscheme(theme_name) |
| 58 | + vim.notify("Theme reload", vim.log.levels.INFO, { title = "dms integration" }) |
| 59 | + end |
| 60 | + -- NOTE: contrary to what the documentation says, uv fs events usually do not manage to react to more than one edit. |
| 61 | + -- I understand that this is not intended: some edit processes in a typical system (e.g. the one neovim uses with |
| 62 | + -- multiple renames and changes) make things hard to follow for libuv. Therefore, a restart is the best option. |
| 63 | + _G._matugen_theme_watcher[1]:stop() |
| 64 | + _G._matugen_theme_watcher[2]:stop() |
| 65 | + _G._matugen_theme_watcher[1]:start(current_file_path, {}, handler) |
| 66 | + _G._matugen_theme_watcher[2]:start(settings_file_path, {}, handler) |
| 67 | + end) |
| 68 | + ) |
| 69 | + end |
| 70 | + _G._matugen_theme_watcher[1]:start(current_file_path, {}, handler) |
| 71 | + _G._matugen_theme_watcher[2]:start(settings_file_path, {}, handler) |
| 72 | +end |
| 73 | + |
| 74 | +if not base46.theme_tables[theme_name] or base46.theme_tables[theme_name].type ~= vim.o.background then |
| 75 | + local builtin = vim.deepcopy(assert(base46.get_builtin_theme(theme_base))) |
| 76 | + local harmonized = base46.theme_harmonize(builtin, "#89b4fa", harmony) |
| 77 | + harmonized = base46.theme_set_bg(harmonized, "#1e1e2e") |
| 78 | + |
| 79 | + base46.theme_tables[theme_name] = harmonized |
| 80 | +end |
| 81 | + |
| 82 | +base46.load(theme_name) |
| 83 | +vim.g.colors_name = theme_name |
0 commit comments