development/templates/nvim/lua/colorscheme.lua.j2

100 lines
2.6 KiB
Django/Jinja

-- {{ ansible_managed }}
local background_callback = function()
if vim.o.background == 'dark' then
vim.cmd('colorscheme github_dark_dimmed')
else
vim.cmd('colorscheme github_light_tritanopia')
end
-- force a full redraw:
vim.cmd('mode')
end
-- set the colorscheme whenever the background setting changes
vim.api.nvim_create_autocmd(
{ 'OptionSet' },
{ pattern = { 'background' }, callback = background_callback }
)
{% if ansible_facts['os_family'] == 'Debian' %}
background_callback()
{% endif %}
{% if ansible_facts['os_family'] == 'Archlinux' %}
--[[
Use `busctl --user tree` to show a tree of available services.
Use the `Introspect` option to inspect available options:
dbus-send \
--session \
--print-reply \
--reply-timeout=2000 \
--type=method_call \
--dest=org.freedesktop.portal.Desktop \
/org/freedesktop/portal/desktop \
org.freedesktop.DBus.Introspectable.Introspect
--]]
local oneshot = {
'dbus-send',
'--session',
'--print-reply',
'--reply-timeout=2000',
'--type=method_call',
'--dest=org.freedesktop.portal.Desktop',
'/org/freedesktop/portal/desktop',
'org.freedesktop.portal.Settings.ReadOne',
'string:org.freedesktop.appearance',
'string:color-scheme'
}
local set_background = vim.schedule_wrap(
function(object)
local default_background = 'light'
if object.code ~= 0 then
print(string.format('An error occured: \n %s', object.stderr))
vim.api.nvim_command(string.format('set background=%s', default_background))
end
local colorscheme_output = tonumber(string.match(object.stdout, 'uint32 (%d)'))
if colorscheme_output == 1 then
vim.api.nvim_command('set background=dark')
elseif colorscheme_output == 2 then
vim.api.nvim_command('set background=light')
end
end
)
vim.schedule(
function() vim.system(oneshot, { text = true }, set_background) end
)
-- Note that the last argument is a match rule,
-- see https://dbus.freedesktop.org/doc/dbus-specification.html#message-bus-routing-match-rules.
local monitor_command = {
'dbus-monitor',
'--session',
'path=/org/freedesktop/portal/desktop,' ..
'interface=org.freedesktop.portal.Settings,' ..
'member=SettingChanged,' ..
'arg0=org.freedesktop.appearance,'..
'arg1=color-scheme'
}
local detect_scheme_change = function(channel_id, data, name)
local line = table.concat(data)
local match_output = tonumber(string.match(line, "uint32 (%d)"))
if match_output == 1 then
vim.api.nvim_command('set background=dark')
elseif match_output == 2 then
vim.api.nvim_command('set background=light')
end
end
vim.fn.jobstart(monitor_command, { on_stdout = detect_scheme_change } )
{% endif %}