Skip to content
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

Open
TeleVoyant opened this issue Aug 4, 2024 · 1 comment
Open

Other Plugins don't fetch colorscheme on Neovim Launch #29

TeleVoyant opened this issue Aug 4, 2024 · 1 comment

Comments

@TeleVoyant
Copy link

TeleVoyant commented Aug 4, 2024

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)

the icons color appear as if in a grey-scale color palette.
the same implies to the texts on the above mentioned plugins.

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:

sourcing rose-pine.lua via autocommand, both on vimEnter or bufferopen, unsuccessful

tried modifying your plugin by adding 'opposite_mode' that switches the theme forth and back once on neovim launch, but didn't work (although switching continuously worked. working on a keymap trigger)

@TeleVoyant
Copy link
Author

TeleVoyant commented Nov 5, 2024

future me here.
the new update broke the plugin. at least for me.

if you face the issues above:

  1. lock the version to 76d9ba9 inside ~/.config/nvim/lua/somewhere/packer.lua
    use({
        "f-person/auto-dark-mode.nvim",
        commit = "76d9ba9b305e492169611cc3ebf5f976c5d6cada",
    })
  1. replace the code in ~/.local/share/nvim/site/pack/packer/start/auto-dark-mode.nvim/lua/auto-dark-mode/init.lua:
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
}
  1. inside ~/.config/nvim/after/plugin/auto-dark-mode.lua, paste the following:
-- --------------------------- --
-- -- 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
Function toogle_opposite_mode_once will be called on successfull nvim load.
This is a custom function that i added to init.lua above, which shakes neovim out of colors confusion.
OR
You could try manually with <leader>dc which will trigger toggle_opposite_mode which flips color scheme between dark and light mode.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant