Skip to content

Would like to use xterm_palette in custom parser for LS_COLORS #212

@MartenBE

Description

@MartenBE

I currently have the following in my config:

vim.pack.add({ "https://github.com/catgoose/nvim-colorizer.lua" })

-- TODO: https://github.com/catgoose/nvim-colorizer.lua/issues/212
-- https://github.com/catgoose/nvim-colorizer.lua/blob/5cfe7fffbd01e17b3c1e14af85d5febdef88bd8c/lua/colorizer/parser/xterm.lua#L13
local create_xterm_palette = function()
    local xterm_palette = {
        "000000",
        "800000",
        "008000",
        "808000",
        "000080",
        "800080",
        "008080",
        "c0c0c0",
        "808080",
        "ff0000",
        "00ff00",
        "ffff00",
        "0000ff",
        "ff00ff",
        "00ffff",
        "ffffff",
        -- 16-231: 6x6x6 color cube
    }

    -- Fill in the 6x6x6 color cube
    for r = 0, 5 do
        for g = 0, 5 do
            for b = 0, 5 do
                local idx = 16 + 36 * r + 6 * g + b
                local function scale(x)
                    return x == 0 and 0 or 95 + 40 * (x - 1)
                end
                xterm_palette[idx + 1] = string.format("%02x%02x%02x", scale(r), scale(g), scale(b))
            end
        end
    end

    -- 232-255: grayscale ramp
    for i = 0, 23 do
        local level = 8 + i * 10
        xterm_palette[233 + i] = string.format("%02x%02x%02x", level, level, level)
    end

    return xterm_palette
end

local xterm_palette = create_xterm_palette()

require("colorizer").setup({
    options = {
        parsers = {
            -- ...
            custom = {
                {
                    name = "ls_colors",
                    prefixes = { "=0", "=1", "=2", "=3", "=4", "=5", "=6", "=7", "=8", "=9" }, -- Does not allow for a regex, so we need to specify all possible prefixes
                    parse = function(ctx)
                        local full_match, color_match = ctx.line:match("(=[%d;]*[34]8;5;(%d+):)", ctx.col)
                        if full_match and color_match then
                            local hex = xterm_palette[tonumber(color_match) + 1]
                            return #full_match, hex
                        end
                    end,
                },
            },
        },
    },
})

This works, but I was wondering if it would be possible to use xterm_palette from within the plugin instead of copy pasting it here in my config?

Image Image

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions