tamton-aquib / staline.nvim

A modern lightweight statusline and bufferline plugin for neovim in lua.
MIT License
373 stars 16 forks source link

Avoid using hardcoded colors #29

Closed is0n closed 2 years ago

is0n commented 2 years ago

The default mode_colors setting uses colors that don't work well with certain color schemes. I think that it would be better if staline-nvim would (by default) use colors based on the current color scheme instead of being hardcoded (kind of relates to #26).

I've gotten something like this to work with the following:

local function ExtractHl(name)
    return vim.api.nvim_exec('highlight ' .. name, true):match('guifg=(#[0-9A-Fa-f]+)')
end

require('staline').setup {
    mode_colors = {
        n  = ExtractHl("Normal"),
        i  = ExtractHl("Blue"),
        ic = ExtractHl("Green"),
        c  = ExtractHl("Purple"),
        v  = ExtractHl("Yellow"),
        ['']  = ExtractHl("Yellow"),
        V  = ExtractHl("Orange"),
        t  = ExtractHl("Red"),
    }
}

This isn't perfect as ExtractHl will break if a highlight group does not exist or is linked to another group but it can definitely can be improved upon :D

tamton-aquib commented 2 years ago

Heyyo @is0n, Have you tried with vim.api.nvim_get_hl_by_name(hl_name, true)? 🤔

local ExtractHl = function(hl)
    local nice = vim.api.nvim_get_hl_by_name(hl, true)
    return ("#%06x"):format(nice.foreground or 0)
end
is0n commented 2 years ago

vim.api.nvim_get_hl_by_name(hl_name, true) works great! This fixes the config from returning an error when a highlight group is linked but not when it does not exist. At that point however, I think it would make sense to fallback on the current default colors right?

tamton-aquib commented 2 years ago

Ah yes, setting fallback colors instead of hardcoding makes sense. Will change when i get some free time 😅

tamton-aquib commented 2 years ago

Heyyo @is0n , I have pushed the fallback feature to main, let me know if it works when you have time.

is0n commented 2 years ago

It works perfectly and looks so nice!

Thanks again for your amazing work! 👍🏽