summaryrefslogtreecommitdiff
path: root/config/nvim/local-plugins/cfg-watch/lua/cfg_watch/init.lua
blob: 3b5c9d609f3b969cb0e9b4753d4820427909bd79 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
local M = {}

local function load_config_path(filepath)
    local uv = vim.uv or vim.loop

    if uv.fs_stat(filepath) == nil then
        local dir = filepath:match("(.*/)")
        if dir and uv.fs_stat(dir) == nil then
            assert(uv.fs_mkdir(dir, 448)) -- 0o700
        end
        local file, err = io.open(filepath, "w")
        if not file then
            vim.schedule(function()
                vim.notify("Error creating file: " .. filepath .. "\n" .. err, vim.log.levels.ERROR)
            end)
            return
        end
        file:write("-- Created by cfg-watch\n")
        file:close()
    else
        vim.schedule(function()
            local ok, err = pcall(dofile, filepath)
            if not ok then
                vim.schedule(function()
                    vim.notify("Error loading config: " .. filepath .. "\n" .. err, vim.log.levels.ERROR)
                end)
            end
        end)
    end
end

function M.load_colors()
    local filepath = vim.fn.stdpath("data") .. "/colors.lua" -- ~/.local/share/nvim/colors.lua
    load_config_path(filepath)

    local ok, fwatch = pcall(require, "fwatch")
    if ok and fwatch and fwatch.once then
        fwatch.once(filepath, {
            on_event = function() M.load_colors() end,
            on_error = function(err)
                vim.notify("fwatch error: " .. err, vim.log.levels.ERROR)
            end,
        })
    else
        vim.notify("cfg-watch: fwatch not available", vim.log.levels.ERROR)
    end

    vim.schedule(function()
        vim.cmd("hi Normal guibg=NONE ctermbg=NONE")
    end)
end

function M.load_config(name)
    local filepath = vim.fn.stdpath("config") .. "/lua/" .. name .. ".lua"
    load_config_path(filepath)
end

return M