Closed linrongbin16 closed 8 months ago
I think I know now.
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? 🤔
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 runstring.format
to format string, then echo the formatted string.
You can see the 'filetype' is not changed:
My LuaLs diagnostics shows there're type check errors, so I guess the 'type' and 'content' option are not accept by 'filetype' component:
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,
}))
nut.buf.filetype
is actually a wrapper aroundnougat.item
.nut.buf.filetype
does not pass everything tonougat.item
, as you can see here:
content
andtype
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!
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 = " ",
})
thank you! It works great:
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({}),
},
}))
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:
Solution
add 'format' function to allow user do customization.
Alternatives
No response
Additional Context
No response