From 2f3f3165f03735ed2baa84e3ebdf9017ff19df0f Mon Sep 17 00:00:00 2001 From: Chris Burgin Date: Mon, 13 Sep 2021 21:29:55 -0400 Subject: [PATCH] improve code --- .luacheckrc | 4 ++++ lua/background_nvim.lua | 37 +++++++++++++++++++------------------ 2 files changed, 23 insertions(+), 18 deletions(-) create mode 100644 .luacheckrc diff --git a/.luacheckrc b/.luacheckrc new file mode 100644 index 0000000..9a28af6 --- /dev/null +++ b/.luacheckrc @@ -0,0 +1,4 @@ +std = "min" +cache = true +include_files = {"lua"} +globals = {"vim"} diff --git a/lua/background_nvim.lua b/lua/background_nvim.lua index 985ad77..6cf2f22 100644 --- a/lua/background_nvim.lua +++ b/lua/background_nvim.lua @@ -1,40 +1,41 @@ -function check_which_theme() +local M = {} +local opts = { + ms = 60 * 1000, -- 60 seconds + hour = 17, -- 5pm +} + +function M.check_which_theme() local hour = os.date("*t").hour - if (hour > vim.g.BackgroundNvimHour - 1) then + print(os.date("*t").sec) + if (hour > opts.hour) then vim.opt.background = 'dark' else vim.opt.background = 'light' end end -local M = {} - function M.setup(options) - if (not(vim.g.BackgroundNvimMS)) then - vim.g.BackgroundNvimMS = 60000 - end - - if (not(vim.g.BackgroundNvimHour)) then - vim.g.BackgroundNvimHour = 17 - end - if (options) then if (options.ms) then - vim.g.BackgroundNvimMS = options.ms + opts = options.ms end if (options.hour) then - vim.g.BackgroundNvimHour = options.hour + opts = options.hour end end - check_which_theme(); - + M.check_which_theme(); + + if (vim.g.background_nvim_timer) then + vim.cmd("call timer_stop(" .. vim.g.background_nvim_timer .. ")") + end + vim.cmd([[ func! ThemeChecker(timer) - lua check_which_theme() + lua require('background_nvim').check_which_theme() endfunc - let timer = timer_start(g:BackgroundNvimMS, 'ThemeChecker', {'repeat': -1}) + let g:background_nvim_timer = timer_start(]] .. opts.ms .. [[, 'ThemeChecker', {'repeat': -1}) ]]) end