diff --git a/tasks/neovim.yml b/tasks/neovim.yml index af1957c..112916b 100644 --- a/tasks/neovim.yml +++ b/tasks/neovim.yml @@ -152,6 +152,10 @@ src: 'templates/nvim/lua/nvim-tree.lua.j2', 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: - name: create neovim install directory diff --git a/templates/nvim/init.lua.j2 b/templates/nvim/init.lua.j2 index d38546b..61dfc38 100644 --- a/templates/nvim/init.lua.j2 +++ b/templates/nvim/init.lua.j2 @@ -11,3 +11,4 @@ require('git-signs') require('_telescope') require('indent-blankline') require('_nvim-tree') +require('colorscheme') diff --git a/templates/nvim/lua/auto-commands.lua.j2 b/templates/nvim/lua/auto-commands.lua.j2 index a16ab8d..bc6ef63 100644 --- a/templates/nvim/lua/auto-commands.lua.j2 +++ b/templates/nvim/lua/auto-commands.lua.j2 @@ -1,8 +1,5 @@ -- {{ ansible_managed }} ---colorscheme -vim.cmd('colorscheme catppuccin-latte') - --enable this option here as the events are used in this buffer vim.cmd('syntax on') diff --git a/templates/nvim/lua/colorscheme.lua.j2 b/templates/nvim/lua/colorscheme.lua.j2 new file mode 100644 index 0000000..f24b052 --- /dev/null +++ b/templates/nvim/lua/colorscheme.lua.j2 @@ -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 } ) diff --git a/templates/nvim/lua/options.lua.j2 b/templates/nvim/lua/options.lua.j2 index 0c2f806..be4a259 100644 --- a/templates/nvim/lua/options.lua.j2 +++ b/templates/nvim/lua/options.lua.j2 @@ -49,7 +49,6 @@ vim.o.cursorline = true -- theme related vim.o.termguicolors = true -vim.o.background = 'light' -- enable statusbar vim.o.laststatus = 2