Open TeleVoyant opened 1 month ago
here are changes made:
local utils = require("auto-dark-mode.utils")
---@type number
local timer_id
---@type boolean
local is_currently_dark_mode
---@type boolean
local is_opposite_mode
---@type fun(): nil | nil
local set_dark_mode
---@type fun(): nil | nil
local set_light_mode
---@type number
local update_interval
---@type string
local query_command
---@type "Linux" | "Darwin" | "Windows_NT" | "WSL"
local system
---@type boolean
local has_toggled_once
-- Parses the query response for each system
---@param res string
---@return boolean
local function parse_query_response(res)
if system == "Linux" then
return string.match(res, "uint32 1") ~= nil
elseif system == "Darwin" then
return res == "Dark"
elseif system == "Windows_NT" or system == "WSL" then
return string.match(res, "1") == nil
end
return false
end
---@param callback fun(is_dark_mode: boolean)
local function check_is_dark_mode(callback)
utils.start_job(query_command, {
on_stdout = function(data)
local is_dark_mode = parse_query_response(data[1])
callback(is_dark_mode)
end,
})
end
---@param is_dark_mode boolean
local function change_theme_if_needed(is_dark_mode)
if is_opposite_mode then
is_dark_mode = not is_dark_mode
end
if is_dark_mode == is_currently_dark_mode then
return
end
is_currently_dark_mode = is_dark_mode
if is_currently_dark_mode then
set_dark_mode()
else
set_light_mode()
end
end
local function start_check_timer()
timer_id = vim.fn.timer_start(update_interval, function()
check_is_dark_mode(change_theme_if_needed)
end, { ["repeat"] = -1 })
end
local function init()
if string.match(vim.loop.os_uname().release, "WSL") then
system = "WSL"
else
system = vim.loop.os_uname().sysname
end
if system == "Darwin" then
query_command = "defaults read -g AppleInterfaceStyle"
elseif system == "Linux" then
if not vim.fn.executable("dbus-send") then
error([[
`dbus-send` is not available. The Linux implementation of
auto-dark-mode.nvim relies on `dbus-send` being on the `$PATH`.
]])
end
query_command = table.concat({
"dbus-send --session --print-reply=literal --reply-timeout=1000",
"--dest=org.freedesktop.portal.Desktop",
"/org/freedesktop/portal/desktop",
"org.freedesktop.portal.Settings.Read",
"string:'org.freedesktop.appearance'",
"string:'color-scheme'",
}, " ")
elseif system == "Windows_NT" or system == "WSL" then
query_command =
'reg.exe Query "HKCU\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Themes\\Personalize" /v AppsUseLightTheme | findstr.exe "AppsUseLightTheme"'
else
return
end
if vim.fn.has("unix") ~= 0 then
if vim.loop.getuid() == 0 then
query_command = "su - $SUDO_USER -c " .. query_command
end
end
if type(set_dark_mode) ~= "function" or type(set_light_mode) ~= "function" then
error([[
Call `setup` first:
require('auto-dark-mode').setup({
set_dark_mode=function()
vim.api.nvim_set_option_value('background', 'dark')
vim.cmd('colorscheme gruvbox')
end,
set_light_mode=function()
vim.api.nvim_set_option_value('background', 'light')
end,
})
]])
end
is_opposite_mode = false
has_toggled_once = false
start_check_timer()
end
local function disable()
vim.fn.timer_stop(timer_id)
end
---@param options AutoDarkModeOptions
local function setup(options)
options = options or {}
---@param background string
local function set_background(background)
vim.api.nvim_set_option_value("background", background, {})
end
set_dark_mode = options.set_dark_mode or function()
set_background("dark")
end
set_light_mode = options.set_light_mode or function()
set_background("light")
end
update_interval = options.update_interval or 3000
-- Keymap setup
if options.toggle_keymap then
vim.api.nvim_set_keymap(
"n",
options.toggle_keymap,
":lua require('auto-dark-mode').toggle_opposite_mode_once()<CR>",
{ noremap = true, silent = true }
)
end
init()
end
local function toggle_opposite_mode_once()
if not has_toggled_once then
is_opposite_mode = not is_opposite_mode
check_is_dark_mode(change_theme_if_needed)
vim.defer_fn(function()
is_opposite_mode = not is_opposite_mode
check_is_dark_mode(change_theme_if_needed)
end, 500) -- Adjust delay as needed
has_toggled_once = true
end
end
return { setup = setup, init = init, disable = disable, toggle_opposite_mode_once = toggle_opposite_mode_once }
problem fixed. fill free to compare the above with original code.
PS: would love to be a contributor
Hope Heading explains it. I use lualine, barbar, and nvim-tree (there are probably more). They don't reflect the colorscheme at launch (even though the opened buffer does)
I had to Either
:so rose-pine.lua
Or change system theme, for the above plugins to be colorful again.And yes, removing auto-dark-mode fixes it.
PS: the plugin is amaizing. :)
Edit: here is what i've tried so far: