Detect colorscheme from dbus
This commit is contained in:
parent
3587460d37
commit
373dad8448
5 changed files with 96 additions and 4 deletions
91
templates/nvim/lua/colorscheme.lua.j2
Normal file
91
templates/nvim/lua/colorscheme.lua.j2
Normal file
|
|
@ -0,0 +1,91 @@
|
|||
-- {{ ansible_managed }}
|
||||
|
||||
-- set the colorscheme whenever the background setting changes
|
||||
vim.api.nvim_create_autocmd({'OptionSet'}, {
|
||||
pattern = {'background'},
|
||||
callback = function()
|
||||
if vim.o.background == 'dark' then
|
||||
vim.cmd('colorscheme xcodedark')
|
||||
else
|
||||
vim.cmd('colorscheme catppuccin-latte')
|
||||
end
|
||||
|
||||
-- force a full redraw:
|
||||
vim.cmd('mode')
|
||||
end
|
||||
})
|
||||
|
||||
--[[
|
||||
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 } )
|
||||
Loading…
Add table
Add a link
Reference in a new issue