Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 7 additions & 17 deletions lua/lualine/components/buffers/buffer.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ local Buffer = require('lualine.utils.class'):extend()

local modules = require('lualine_require').lazy_require {
highlight = 'lualine.highlight',
icons = 'lualine.icons',
utils = 'lualine.utils.utils',
}

Expand Down Expand Up @@ -34,25 +35,14 @@ function Buffer:get_props()
self.alternate_file_icon = self:is_alternate() and self.options.symbols.alternate_file or ''
self.icon = ''
if self.options.icons_enabled then
local dev
local status, _ = pcall(require, 'nvim-web-devicons')
if not status then
dev, _ = '', ''
elseif self.filetype == 'TelescopePrompt' then
dev, _ = require('nvim-web-devicons').get_icon('telescope')
elseif self.filetype == 'fugitive' then
dev, _ = require('nvim-web-devicons').get_icon('git')
elseif self.filetype == 'vimwiki' then
dev, _ = require('nvim-web-devicons').get_icon('markdown')
elseif self.buftype == 'terminal' then
dev, _ = require('nvim-web-devicons').get_icon('zsh')
elseif vim.fn.isdirectory(self.file) == 1 then
dev, _ = self.options.symbols.directory, nil
local icon
if vim.fn.isdirectory(self.file) == 1 then
icon = self.options.symbols.directory
else
dev, _ = require('nvim-web-devicons').get_icon(self.file, vim.fn.expand('#' .. self.bufnr .. ':e'))
icon = modules.icons.file(self.file, self.bufnr, self.buftype, self.filetype)
end
if dev then
self.icon = dev .. ' '
if icon then
self.icon = icon .. ' '
end
end
end
Expand Down
52 changes: 15 additions & 37 deletions lua/lualine/components/filetype.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
local lualine_require = require('lualine_require')
local modules = lualine_require.lazy_require {
highlight = 'lualine.highlight',
icons = 'lualine.icons',
utils = 'lualine.utils.utils',
}
local M = lualine_require.require('lualine.component'):extend()
Expand All @@ -28,46 +29,23 @@ function M:apply_icon()
return
end

local icon, icon_highlight_group
local ok, devicons = pcall(require, 'nvim-web-devicons')
if ok then
icon, icon_highlight_group = devicons.get_icon(vim.fn.expand('%:t'))
if icon == nil then
icon, icon_highlight_group = devicons.get_icon_by_filetype(vim.bo.filetype)
end

if icon == nil and icon_highlight_group == nil then
icon = ''
icon_highlight_group = 'DevIconDefault'
end
if icon then
icon = icon .. ' '
end
if self.options.colored then
local highlight_color = modules.utils.extract_highlight_colors(icon_highlight_group, 'fg')
if highlight_color then
local default_highlight = self:get_default_hl()
local icon_highlight = self.icon_hl_cache[highlight_color]
if not icon_highlight or not modules.highlight.highlight_exists(icon_highlight.name .. '_normal') then
icon_highlight = self:create_hl({ fg = highlight_color }, icon_highlight_group)
self.icon_hl_cache[highlight_color] = icon_highlight
end
local icon, icon_highlight_group = modules.icons.filetype()
if not icon then
return
end

icon = self:format_hl(icon_highlight) .. icon .. default_highlight
icon = icon .. ' '
if self.options.colored and icon_highlight_group then
local highlight_color = modules.utils.extract_highlight_colors(icon_highlight_group, 'fg')
if highlight_color then
local default_highlight = self:get_default_hl()
local icon_highlight = self.icon_hl_cache[highlight_color]
if not icon_highlight or not modules.highlight.highlight_exists(icon_highlight.name .. '_normal') then
icon_highlight = self:create_hl({ fg = highlight_color }, icon_highlight_group)
self.icon_hl_cache[highlight_color] = icon_highlight
end
icon = self:format_hl(icon_highlight) .. icon .. default_highlight
end
else
ok = vim.fn.exists('*WebDevIconsGetFileTypeSymbol')
if ok ~= 0 then
icon = vim.fn.WebDevIconsGetFileTypeSymbol()
if icon then
icon = icon .. ' '
end
end
end

if not icon then
return
end

if self.options.icon_only then
Expand Down
58 changes: 58 additions & 0 deletions lua/lualine/icons.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
local M = {}

---@param file string
---@param bufnr integer
---@param buftype string
---@param filetype string
---@return string|nil
function M.file(file, bufnr, buftype, filetype)
local has_miniicons, miniicons = pcall(require, 'mini.icons')
if has_miniicons and _G.MiniIcons then
return miniicons.get('file', file)
end

local has_devicons, devicons = pcall(require, 'nvim-web-devicons')
if has_devicons then
if filetype == 'TelescopePrompt' then
return devicons.get_icon('telescope')
elseif filetype == 'fugitive' then
return devicons.get_icon('git')
elseif filetype == 'vimwiki' then
return devicons.get_icon('markdown')
elseif buftype == 'terminal' then
return devicons.get_icon('zsh')
else
return devicons.get_icon(file, vim.fn.expand('#' .. bufnr .. ':e'))
end
end

return nil
end

---@return string|nil, string|nil
function M.filetype()
local has_miniicons, miniicons = pcall(require, 'mini.icons')
if has_miniicons and _G.MiniIcons then
return miniicons.get('filetype', vim.bo.filetype)
end

local has_devicons, devicons = pcall(require, 'nvim-web-devicons')
if has_devicons then
local icon, highlight = devicons.get_icon(vim.fn.expand('%:t'))
if not icon then
icon, highlight = devicons.get_icon_by_filetype(vim.bo.filetype)
end
if not icon and not highlight then
icon, highlight = '', 'DevIconDefault'
end
return icon, highlight
end

if vim.fn.exists('*WebDevIconsGetFileTypeSymbol') ~= 0 then
return vim.fn.WebDevIconsGetFileTypeSymbol()
end

return nil, nil
end

return M