.dotfiles/configs/nvim/lua/utils/theme-reloader.lua

44 lines
1.0 KiB
Lua
Raw Normal View History

2022-04-23 03:41:04 +02:00
-- see if the file exists
local function file_exists(file)
local f = io.open(file, "rb")
2022-09-02 16:15:54 +02:00
if f then
f:close()
end
2022-04-23 03:41:04 +02:00
return f ~= nil
end
-- get all lines from a file, returns an empty
-- list/table if the file does not exist
local function line_from(file)
2022-09-02 16:15:54 +02:00
if not file_exists(file) then
return ""
end
2022-04-23 03:41:04 +02:00
local f = io.open(file) -- 'r' is unnecessary because it's a default value.
2022-09-02 16:15:54 +02:00
if f == nil then
return ""
end
2022-04-23 03:41:04 +02:00
local line = f:read() -- '*l' is unnecessary because it's a default value.
f:close()
2022-09-02 16:15:54 +02:00
return line
2022-04-23 03:41:04 +02:00
end
2022-09-02 16:15:54 +02:00
local filePath = os.getenv("HOME") .. "/.cache/dark-mode"
2022-04-23 03:41:04 +02:00
local function updateTheme()
2022-09-02 16:15:54 +02:00
local line = line_from(filePath)
2022-04-23 03:41:04 +02:00
if line then
2022-09-02 16:15:54 +02:00
local light = string.find(line, "light")
2022-06-23 00:24:06 +02:00
if light then
2022-10-06 21:14:32 +02:00
vim.g.catppuccin_flavour = "latte"
2022-11-19 18:38:42 +01:00
vim.cmd("Catppuccin latte")
2022-06-23 00:24:06 +02:00
else
2022-10-06 21:14:32 +02:00
vim.g.catppuccin_flavour = "mocha"
2022-11-19 18:38:42 +01:00
vim.cmd("Catppuccin mocha")
2022-04-23 03:41:04 +02:00
end
2022-10-06 21:14:32 +02:00
vim.cmd("colorscheme catppuccin")
2022-04-23 03:41:04 +02:00
end
end
updateTheme()
local w = vim.loop.new_fs_event()
w:start(filePath, {}, vim.schedule_wrap(updateTheme))