Skip to content

Commit

Permalink
feat(nut): add buf.filetype_icon
Browse files Browse the repository at this point in the history
  • Loading branch information
MunifTanjim committed Jan 2, 2024
1 parent 6024f75 commit 700708b
Show file tree
Hide file tree
Showing 4 changed files with 141 additions and 45 deletions.
79 changes: 79 additions & 0 deletions lua/nougat/nut/buf/filetype_icon.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
local Item = require("nougat.item")
local buf_cache = require("nougat.cache.buffer")

local has_devicons, devicons = pcall(require, "nvim-web-devicons")

--luacheck: push no max line length

---@class nougat.nut.buf.filetype_icon_config: nougat_item_config__function
---@field config? nil
---@field _get_bufnr? fun(ctx:nougat_bar_ctx):integer

--luacheck: pop

local filetype_overide = {
fugitive = "git",
gitcommit = "git",
}

local function prepare(item, ctx)
local filetype = buf_cache.get("filetype", item._get_bufnr(ctx)) or ""
local cache = item:cache(ctx)
if not cache.c then
local ft = filetype_overide[filetype] or filetype
cache.c, cache.hl.fg = devicons.get_icon_color_by_filetype(ft, { default = true })
end
end

local function hl(item, ctx)
return item:cache(ctx).hl
end

local function content(item, ctx)
return item:cache(ctx).c
end

local function default_get_bufnr(ctx)
return ctx.bufnr
end

local cache_initial_value = { c = nil, hl = { bg = "bg" } }

local mod = {}

---@param config nougat.nut.buf.filetype_icon_config
function mod.create(config)
buf_cache.enable("filetype")

local get_bufnr = config._get_bufnr or default_get_bufnr

local item = Item({
init = function(item)
item._get_bufnr = get_bufnr
end,
priority = config.priority,
prepare = has_devicons and prepare or config.prepare,
hidden = config.hidden,
hl = has_devicons and hl or config.hl,
sep_left = config.sep_left,
prefix = config.prefix,
content = has_devicons and content or config.content or "",
suffix = config.suffix,
sep_right = config.sep_right,
on_click = config.on_click,
context = config.context,
cache = {
name = "nut.buf.filetype_icon",
scope = "buf",
get = function(store, ctx)
return store[get_bufnr(ctx)]
end,
initial_value = cache_initial_value,
clear = "BufFilePost",
},
})

return item
end

return mod
52 changes: 8 additions & 44 deletions lua/nougat/nut/tab/tablist/icon.lua
Original file line number Diff line number Diff line change
@@ -1,66 +1,30 @@
local Item = require("nougat.item")
local buf_cache = require("nougat.cache.buffer")

local has_devicons, devicons = pcall(require, "nvim-web-devicons")

--luacheck: push no max line length

---@class nougat.nut.tab.tablist.icon_config: nougat_item_config__function
---@field config? nil

--luacheck: pop

local filetype_overide = {
fugitive = "git",
gitcommit = "git",
}

local function prepare(item, ctx)
local filetype = buf_cache.get("filetype", ctx.tab.bufnr) or ""
local cache = item:cache(ctx)
if not cache.c then
local ft = filetype_overide[filetype] or filetype
cache.c, cache.hl.fg = devicons.get_icon_color_by_filetype(ft, { default = true })
end
end

local function hl(item, ctx)
return item:cache(ctx).hl
end

local function content(item, ctx)
return item:cache(ctx).c
end

local cache_initial_value = { c = nil, hl = {} }

local mod = {}

---@param config nougat.nut.tab.tablist.icon_config
function mod.create(config)
buf_cache.enable("filetype")

local item = Item({
local item = require("nougat.nut.buf.filetype_icon").create({
priority = config.priority,
prepare = has_devicons and prepare or config.prepare,
prepare = config.prepare,
hidden = config.hidden,
hl = has_devicons and hl or config.hl,
hl = config.hl,
sep_left = config.sep_left,
prefix = config.prefix,
content = has_devicons and content or config.content or "",
content = config.content,
suffix = config.suffix,
sep_right = config.sep_right,
on_click = config.on_click,
context = config.context,
cache = {
name = "nut.tab.tablist.icon",
scope = "buf",
get = function(store, ctx)
return store[ctx.tab.bufnr]
end,
initial_value = cache_initial_value,
clear = "BufFilePost",
},
---@param ctx nougat.nut.tab.tablist_ctx
_get_bufnr = function(ctx)
return ctx.tab.bufnr
end,
})

return item
Expand Down
52 changes: 52 additions & 0 deletions tests/nougat/nut/buf/filetype_icon_spec.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
pcall(require, "luacov")

local Bar = require("nougat.bar")

local t = require("tests.util")

describe("nut.buf.filetype_icon", function()
local bar, ctx

local nut

before_each(function()
require("nougat.store")._clear()

package.loaded["nougat.nut.buf.filetype_icon"] = nil

package.loaded["nvim-web-devicons"] = {
get_icon_color_by_filetype = function()
return "~", "yellow"
end,
}

bar = Bar("statusline")
ctx = t.make_ctx(0, {
ctx = {},
width = vim.api.nvim_win_get_width(0),
})

nut = require("nougat.nut.buf.filetype_icon")
end)

after_each(function()
if ctx.bufnr then
vim.api.nvim_buf_delete(ctx.bufnr, { force = true })
end
end)

it("works", function()
vim.api.nvim_buf_set_name(ctx.bufnr, "_A_.md")

bar:add_item(nut.create({}))

t.eq(
bar:generate(ctx),
table.concat({
"%#bg_ffcd00_fg_yellow_#",
"~",
"%#bg_ffcd00_fg_663399_#",
}, "")
)
end)
end)
3 changes: 2 additions & 1 deletion tests/nougat/nut/tab/tablist_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -138,11 +138,12 @@ describe("nut.tab.tablist", function()

describe("icon", function()
before_each(function()
require("nougat.store")._cleanup("buf", "nut.tab.tablist.icon")
require("nougat.store")._cleanup("buf", "nut.buf.filetype_icon")
end)

it("works", function()
package.loaded["nougat.nut.tab.tablist.icon"] = nil
package.loaded["nougat.nut.buf.filetype_icon"] = nil

package.loaded["nvim-web-devicons"] = {
get_icon_color_by_filetype = function()
Expand Down

0 comments on commit 700708b

Please sign in to comment.