Bekaboo / deadcolumn.nvim

A neovim plugin that shows colorcolumn dynamically
GNU General Public License v3.0
303 stars 10 forks source link

How exactly blending works? #15

Closed simonmandlik closed 10 months ago

simonmandlik commented 10 months ago

Hi, I wanted to achieve a simple colorcolumn, that would behave like vanilla colorcolumn, but would start appearing once the cursor is getting closer to it.

What I need from deadcolumn is to show "standard" colorcolumn when the text is longer than textwidth, and only slowly "fade in" and "fade out".

But I can't even make colorcolumn have the same background color as ColorColumn highlight group.

This is my minimal.lua:

local root = vim.fn.fnamemodify("./.repro", ":p")
for _, name in ipairs({ "config", "data", "state", "cache" }) do
    vim.env[("XDG_%s_HOME"):format(name:upper())] = root .. "/" .. name
end
local lazypath = root .. "/plugins/lazy.nvim"
if not vim.loop.fs_stat(lazypath) then
    vim.fn.system({ "git", "clone", "--filter=blob:none", "https://github.com/folke/lazy.nvim.git", lazypath, })
end
vim.opt.runtimepath:prepend(lazypath)

local plugins = {
    {
        "Bekaboo/deadcolumn.nvim",
        init = function()
            require('deadcolumn').setup {
                blend = { hlgroup = { "ColorColumn", "bg" }, },
                warning = { hlgroup = { "ColorColumn", "bg" }, },
            }
        end
    }
}

require("lazy").setup(plugins, {
    root = root .. "/plugins",
})

vim.cmd[[set termguicolors]]
vim.cmd[[hi ColorColumn guibg=blue]]
vim.cmd[[set colorcolumn=20]]

And this is the behavior:

https://github.com/Bekaboo/deadcolumn.nvim/assets/16707112/8d8bf2c6-27e0-440c-b18f-7e4ba026fdfe

Color column has red color instead of blue, and once the text length exceeds the column, it turns into brown.

Can I somehow make the colorcolumn have the same color as the ColorColumn highlight group and basically "turn off" the warning functionality, only display fully opaque color column?

Thanks!

Bekaboo commented 10 months ago

Consider setting opts.warning.offset to math.huge?

simonmandlik commented 10 months ago

If I set opts.warning.offset to 100 I get identical behavior as in the video above

Bekaboo commented 10 months ago

@simonmandlik I noticed that guibg of ColorColumn is set to "Blue"... well that's not really a valid color code, we are expecting something like #ef3fd2. Try using a modern, decent colorscheme.

simonmandlik commented 10 months ago

If I set it to a hex value, the problem persists.

minimal.lua:

local root = vim.fn.fnamemodify("./.repro", ":p")
for _, name in ipairs({ "config", "data", "state", "cache" }) do
    vim.env[("XDG_%s_HOME"):format(name:upper())] = root .. "/" .. name
end
local lazypath = root .. "/plugins/lazy.nvim"
if not vim.loop.fs_stat(lazypath) then
    vim.fn.system({ "git", "clone", "--filter=blob:none", "https://github.com/folke/lazy.nvim.git", lazypath, })
end
vim.opt.runtimepath:prepend(lazypath)

local plugins = {
    {
        "Bekaboo/deadcolumn.nvim",
        init = function()
            require('deadcolumn').setup {
                blend = { hlgroup = { "ColorColumn", "bg" }, },
                warning = { offset = 100, hlgroup = { "ColorColumn", "bg" }, },
            }
        end
    }
}

require("lazy").setup(plugins, {
    root = root .. "/plugins",
})

vim.cmd[[set termguicolors]]
vim.cmd[[hi ColorColumn guibg=#0000FF]]
vim.cmd[[set colorcolumn=20]]

I use modern, decent colorscheme, but for the sake MWE I left it out.

This is how the colorcolumn looks like using the same options (and ColorColumn hg from the scheme)

image

which is nowhere near the barely visible, desired color

image
Bekaboo commented 10 months ago

You are using a white background and the background of your color column is #dddcdc, which is also white, how can that be visible? Try hi ColorColumn guibg=#000000

simonmandlik commented 10 months ago

It's not white, it's rather grey, but as you can see, the colorcolumn is green.

Even if I set colorcolumn to black as you suggested nothing changes

https://github.com/Bekaboo/deadcolumn.nvim/assets/16707112/d03d67c0-b618-4712-88c7-b0feb0668f47

Bekaboo commented 10 months ago

Should be fixed now. Can you confirm?

simonmandlik commented 10 months ago

Yes, thanks for the fix.

Would it be possible to expose update_hl_hex so that it can be called programatically from init.lua?

This would help immensely during startup, when autocommands may not (yet) be available.

In my setup, I change several highlight groups (including ColorColumn) programatically after loading the theme. The Autocommand doesn't capture that.

Bekaboo commented 10 months ago

As long as you load this plugin after changing the colors it should be fine.

So the order is: load theme -> change hlgroups -> load deadcolumn.nvim

simonmandlik commented 10 months ago

I know, but this is not possible in my setup.

I use a light and a dark theme, which change automatically according to MacOS dark/light appearance. If neovim is opened during this change, I have to reload the theme and change all hlgroups. In such case, I can't call setup of deadcolumn anymore, that is AFAIK an antipattern.

Please, see for example here: https://github.com/nvim-tree/nvim-web-devicons/issues/298

I can make the PR if you wish

Bekaboo commented 10 months ago

I also have neovim setup to change bg according to system theme, so don't worry the plugin will work in this case because changing &bg will make color scheme to be reloaded automatically, this is an internal machenism of nvim, see :h 'bg':

                        *'background'* *'bg'*
'background' 'bg'   string  (default "dark")
            global
    When set to "dark" or "light", adjusts the default color groups for
    that background type.  The |TUI| or other UI sets this on startup
    (triggering |OptionSet|) if it can detect the background color.

    This option does NOT change the background color, it tells Nvim what
    the "inherited" (terminal/GUI) background looks like.
    See |:hi-normal| if you want to set the background color explicitly.
                        *g:colors_name*
    When a color scheme is loaded (the "g:colors_name" variable is set)
    changing 'background' will cause the color scheme to be reloaded.  If
    the color scheme adjusts to the value of 'background' this will work.
    However, if the color scheme sets 'background' itself the effect may
    be undone.  First delete the "g:colors_name" variable when needed.

    Normally this option would be set in the vimrc file.  Possibly
    depending on the terminal name.  Example: >vim
        if $TERM ==# "xterm"
          set background=dark
        endif
<   When this option is changed, the default settings for the highlight groups
    will change.  To use other settings, place ":highlight" commands AFTER
    the setting of the 'background' option.
Bekaboo commented 10 months ago

Also, have you tried to change the background and observe deadcolumn misbehaviors? If you havn't I suggest you at least try it out and report here if the color is not updated correctly (it should), else our discussion here makes no sense.

simonmandlik commented 10 months ago

I can't use vim.o.background e.g. for reasons mentioned here: https://github.com/RRethy/nvim-base16/issues/77

Believe me, I have explored all options available, and what I'm doing in my setup right now is the best I could come up with so that everything works.

If you are strictly against exposing one more "refresh" function (as other plugins do, e.g. https://github.com/nvim-tree/nvim-web-devicons#usage) I understand, but it makes the plugin unusable for me.

Bekaboo commented 10 months ago

I am not strictly against exposing the function, I am rather trying to understand what problem you encounter and avoid unnecessary change to the souce. However, from your link it is still unclear what is the exact problem prevending you from using this plugin and why the colors are not automatically refreshed on colorscheme/bg change. Could you elaborate?

In the worst case you can still do :doau ColorScheme to refresh the colors.

simonmandlik commented 10 months ago

Hmm... I do not know the internals of vim.o.background, but changing its value breaks some things for me, see e.g. that LineNr even though hi LineNr returns different color:

https://github.com/Bekaboo/deadcolumn.nvim/assets/16707112/da879794-f450-423e-b5c7-47cc8d436268

At the same time, e.g. nvim-web-devicons work based on the value of vim.o.background, so I have to:

load plugins -> set colorscheme -> set vim.o.background -> modify custom hl groups -> refresh plugins that use these hl groups.

:do ColorScheme seems to work fine here, I haven't realized it can be invoked like that. Thanks for the help!

Bekaboo commented 10 months ago

Glad that you find a good solution to the problem. I also noticed that you made a small mistake in the recording, rather than :lua vim.o.background=light you should use :lua vim.o.background='light', because 'light' without quotes in lua is nil (if it is not assignmed before). If you are doing it in vimscript it will be :set bg=light or :set background=light (without the quotes). I suggest you read more about :h set, :h vim.o, :h vim.go, :h vim.opt, :h options, and :h lua-guide. Happy vimming!

simonmandlik commented 10 months ago

Indeed, but still, even vim.o.background='light' behaves the same

Bekaboo commented 10 months ago

behaves the same

Emm what is the “behave”? What do you expect?