Detect colorscheme from dbus

This commit is contained in:
Sonny Bakker 2024-09-20 21:20:01 +02:00
parent 3587460d37
commit 373dad8448
5 changed files with 96 additions and 4 deletions

View file

@ -152,6 +152,10 @@
src: 'templates/nvim/lua/nvim-tree.lua.j2', src: 'templates/nvim/lua/nvim-tree.lua.j2',
dest: '{{ xdg_config_dir }}/nvim/lua/_nvim-tree.lua', dest: '{{ xdg_config_dir }}/nvim/lua/_nvim-tree.lua',
} }
- {
src: 'templates/nvim/lua/colorscheme.lua.j2',
dest: '{{ xdg_config_dir }}/nvim/lua/colorscheme.lua',
}
- block: - block:
- name: create neovim install directory - name: create neovim install directory

View file

@ -11,3 +11,4 @@ require('git-signs')
require('_telescope') require('_telescope')
require('indent-blankline') require('indent-blankline')
require('_nvim-tree') require('_nvim-tree')
require('colorscheme')

View file

@ -1,8 +1,5 @@
-- {{ ansible_managed }} -- {{ ansible_managed }}
--colorscheme
vim.cmd('colorscheme catppuccin-latte')
--enable this option here as the events are used in this buffer --enable this option here as the events are used in this buffer
vim.cmd('syntax on') vim.cmd('syntax on')

View 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 } )

View file

@ -49,7 +49,6 @@ vim.o.cursorline = true
-- theme related -- theme related
vim.o.termguicolors = true vim.o.termguicolors = true
vim.o.background = 'light'
-- enable statusbar -- enable statusbar
vim.o.laststatus = 2 vim.o.laststatus = 2