-
-
Notifications
You must be signed in to change notification settings - Fork 27
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Other Plugins don't fetch colorscheme on Neovim Launch #29
Comments
future me here. if you face the issues above:
use({
"f-person/auto-dark-mode.nvim",
commit = "76d9ba9b305e492169611cc3ebf5f976c5d6cada",
})
local utils = require("auto-dark-mode.utils")
---@type number
local timer_id
---@type boolean
local is_currently_dark_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 is_opposite_mode
---@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
-- https://github.com/flatpak/xdg-desktop-portal/blob/c0f0eb103effdcf3701a1bf53f12fe953fbf0b75/data/org.freedesktop.impl.portal.Settings.xml#L32-L46
-- 0: no preference
-- 1: dark
-- 2: light
return string.match(res, "uint32 1") ~= nil
elseif system == "Darwin" then
return res == "Dark"
elseif system == "Windows_NT" or system == "WSL" then
-- AppsUseLightTheme REG_DWORD 0x0 : dark
-- AppsUseLightTheme REG_DWORD 0x1 : light
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)
-- we only care about the first line of the response
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
-- Don't swap the quotes; it breaks the code
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
check_is_dark_mode(change_theme_if_needed)
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
toggle_keymap = options.toggle_keymap or "<leader>dc"
if toggle_keymap then
vim.api.nvim_set_keymap(
"n",
toggle_keymap,
":lua require('auto-dark-mode').toggle_opposite_mode()<CR>",
{ noremap = true, silent = true }
)
end
init()
end
local function toggle_opposite_mode()
is_opposite_mode = not is_opposite_mode
check_is_dark_mode(change_theme_if_needed)
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, 200) -- 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,
toggle_opposite_mode = toggle_opposite_mode
}
-- --------------------------- --
-- -- Auto-Dark-Mode Config -- --
-- --------------------------- --
require("auto-dark-mode").setup({
update_interval = 300,
set_dark_mode = function()
vim.api.nvim_set_option_value("background", "dark", {})
ColorMyPencils()
end,
set_light_mode = function()
vim.api.nvim_set_option_value("background", "light", {})
ColorMyPencils()
end,
-- toggle opposite mode without changing theme
toggle_keymap = "<leader>dc",
})
require("auto-dark-mode").toggle_opposite_mode_once()
-- Gets called from the packer plugin initalizer
-- yap, straight from theprimeagen
function ColorMyPencils(color)
color = color or "rose-pine"
vim.cmd.colorscheme(color)
end
ColorMyPencils()
You'll be golden! EXPLANATION |
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:
The text was updated successfully, but these errors were encountered: