MunifTanjim / nougat.nvim

🍫 Hyperextensible Statusline / Tabline / Winbar for Neovim 🚀
MIT License
197 stars 3 forks source link

Add filetype icon for filetype component #77

Closed linrongbin16 closed 8 months ago

linrongbin16 commented 8 months ago

Did you check the docs and existing issues?

Problem

https://github.com/MunifTanjim/nougat.nvim/blob/main/lua/nougat/nut/buf/filetype.lua, the filetype component don't have 'format' function, which cannot use devicons to show the colored file type icons.

lualine have filetype icon:

SAVE_20240102_180639

Solution

add 'format' function to allow user do customization.

Alternatives

No response

Additional Context

No response

linrongbin16 commented 8 months ago

I think I know now.

MunifTanjim commented 8 months ago

Hey @linrongbin16 , I think this is worth having in the built-in filetype component.

Not sure why you closed the issue... Did you think of any alternatives? 🤔

linrongbin16 commented 8 months ago

Hey @linrongbin16 , I think this is worth having in the built-in filetype component.

Not sure why you closed the issue... Did you think of any alternatives? 🤔

I read the item.content here: https://github.com/MunifTanjim/nougat.nvim/tree/main/lua/nougat/item#content, and item.type here: https://github.com/MunifTanjim/nougat.nvim/tree/main/lua/nougat/item#type.

I think if I set type to 'lua_expr' and content to be a lua function, so I can render the icon in the lua function?

I didn't test it yet.

And I think yes it's worth to provide filetype icon as a builtin feature.


update-1:

I think I failed to customize the 'type' and 'content' option in 'item'.

I configured the 'filetype' with:

-- file type
stl:add_item(nut.buf.filetype({
    hl = { bg = color.bg1 },
    sep_left = sep.left_lower_triangle_solid(true),
    prefix = " ",
    suffix = " ",
    type = "lua_expr",
    content = function(ctx)
        message.info("|nougat.filetype| ctx:%s", vim.inspect(ctx))
        local ft = vim.bo.filetype
        local ok, devicons = pcall(require, "nvim-web-devicons")
        if not ok then
            return ft or ""
        end
        local icon_text, icon_color =
            devicons.get_icon_cterm_color_by_filetype(ft)
        message.info(
            "|nougat.filetype| ctx:%s, icon_text:%s, icon_color:%s",
            vim.inspect(ctx),
            vim.inspect(icon_text),
            vim.inspect(icon_color)
        )
        return icon_text .. " " .. ft
    end,
}))

Note: the message.info is a builtin function in my nvim config, just think it's a helper that first run string.format to format string, then echo the formatted string.

You can see the 'filetype' is not changed:

image

My LuaLs diagnostics shows there're type check errors, so I guess the 'type' and 'content' option are not accept by 'filetype' component:

image
MunifTanjim commented 8 months ago

nut.buf.filetype is actually a wrapper around nougat.item. nut.buf.filetype does not pass everything to nougat.item, as you can see here: https://github.com/MunifTanjim/nougat.nvim/blob/6024f7558fe4759ce81723fd770a6abe5ceb7010/lua/nougat/nut/buf/filetype.lua#L15-L30

content and type is always hardcoded.


My LuaLs diagnostics shows there're type check errors, so I guess the 'type' and 'content' option are not accept by 'filetype' component:

Yep, that's right. The built-in nut.buf.filetype component is not very configurable right now.


What you want to do is this:

local Item = require("nougat.item")

stl:add_item(Item({
    hl = { bg = color.bg1 },
    sep_left = sep.left_lower_triangle_solid(true),
    prefix = " ",
    suffix = " ",
    content = function(_, ctx)
        message.info("|nougat.filetype| ctx:%s", vim.inspect(ctx))
        local ft = vim.bo.filetype
        local ok, devicons = pcall(require, "nvim-web-devicons")
        if not ok then
            return ft or ""
        end
        local icon_text, icon_color =
            devicons.get_icon_cterm_color_by_filetype(ft)
        message.info(
            "|nougat.filetype| ctx:%s, icon_text:%s, icon_color:%s",
            vim.inspect(ctx),
            vim.inspect(icon_text),
            vim.inspect(icon_color)
        )
        return icon_text .. " " .. ft
    end,
}))
linrongbin16 commented 8 months ago

nut.buf.filetype is actually a wrapper around nougat.item. nut.buf.filetype does not pass everything to nougat.item, as you can see here:

https://github.com/MunifTanjim/nougat.nvim/blob/6024f7558fe4759ce81723fd770a6abe5ceb7010/lua/nougat/nut/buf/filetype.lua#L15-L30

content and type is always hardcoded.

My LuaLs diagnostics shows there're type check errors, so I guess the 'type' and 'content' option are not accept by 'filetype' component:

Yep, that's right. The built-in nut.buf.filetype component is not very configurable right now.

What you want to do is this:

local Item = require("nougat.item")

stl:add_item(Item({
    hl = { bg = color.bg1 },
    sep_left = sep.left_lower_triangle_solid(true),
    prefix = " ",
    suffix = " ",
    content = function(_, ctx)
        message.info("|nougat.filetype| ctx:%s", vim.inspect(ctx))
        local ft = vim.bo.filetype
        local ok, devicons = pcall(require, "nvim-web-devicons")
        if not ok then
            return ft or ""
        end
        local icon_text, icon_color =
            devicons.get_icon_cterm_color_by_filetype(ft)
        message.info(
            "|nougat.filetype| ctx:%s, icon_text:%s, icon_color:%s",
            vim.inspect(ctx),
            vim.inspect(icon_text),
            vim.inspect(icon_color)
        )
        return icon_text .. " " .. ft
    end,
}))

thank, will try later!

MunifTanjim commented 8 months ago

Example usage for the newly added filetype_icon:

local nut = {
  buf = {
    filetype = require("nougat.nut.buf.filetype").create,
    filetype_icon = require("nougat.nut.buf.filetype_icon").create,
  },
}

stl:add_item({
  hl = { bg = color.bg1 },
  prefix = " ",
  content = {
    nut.buf.filetype_icon({ suffix = " " }),
    nut.buf.filetype({}),
  },
  suffix = " ",
})
linrongbin16 commented 8 months ago

thank you! It works great:

image

I'm configure it with:

-- file type
stl:add_item(Item({
    hl = { bg = color.bg1 },
    sep_left = sep.left_lower_triangle_solid(true),
    prefix = " ",
    suffix = " ",
    content = {
        nut.buf.filetype_icon({ suffix = " " }),
        nut.buf.filetype({}),
    },
}))