| 1 |
-- Copyright (c) 2020-2021 hoob3rt |
| 2 |
-- MIT license, see LICENSE for more details. |
| 3 |
local lualine_require = require('lualine_require') |
| 4 |
local modules = lualine_require.lazy_require { |
| 5 |
highlight = 'lualine.highlight', |
| 6 |
utils = 'lualine.utils.utils', |
| 7 |
} |
| 8 |
local M = lualine_require.require('lualine.component'):extend() |
| 9 |
|
| 10 |
local default_options = { |
| 11 |
colored = true, |
| 12 |
icon_only = false, |
| 13 |
} |
| 14 |
|
| 15 |
function M:init(options) |
| 16 |
M.super.init(self, options) |
| 17 |
self.options = vim.tbl_deep_extend('keep', self.options or {}, default_options) |
| 18 |
self.icon_hl_cache = {} |
| 19 |
end |
| 20 |
|
| 21 |
function M.update_status() |
| 22 |
local ft = vim.bo.filetype or '' |
| 23 |
return modules.utils.stl_escape(ft) |
| 24 |
end |
| 25 |
|
| 26 |
function M:apply_icon() |
| 27 |
if not self.options.icons_enabled then |
| 28 |
return |
| 29 |
end |
| 30 |
|
| 31 |
local icon, icon_highlight_group |
| 32 |
local ok, devicons = pcall(require, 'nvim-web-devicons') |
| 33 |
if ok then |
| 34 |
icon, icon_highlight_group = devicons.get_icon(vim.fn.expand('%:t')) |
| 35 |
if icon == nil then |
| 36 |
icon, icon_highlight_group = devicons.get_icon_by_filetype(vim.bo.filetype) |
| 37 |
end |
| 38 |
|
| 39 |
if icon == nil and icon_highlight_group == nil then |
| 40 |
icon = '' |
| 41 |
icon_highlight_group = 'DevIconDefault' |
| 42 |
end |
| 43 |
if icon then |
| 44 |
icon = icon .. ' ' |
| 45 |
end |
| 46 |
if self.options.colored then |
| 47 |
local highlight_color = modules.utils.extract_highlight_colors(icon_highlight_group, 'fg') |
| 48 |
if highlight_color then |
| 49 |
local default_highlight = self:get_default_hl() |
| 50 |
local icon_highlight = self.icon_hl_cache[highlight_color] |
| 51 |
if not icon_highlight or not modules.highlight.highlight_exists(icon_highlight.name .. '_normal') then |
| 52 |
icon_highlight = self:create_hl({ fg = highlight_color }, icon_highlight_group) |
| 53 |
self.icon_hl_cache[highlight_color] = icon_highlight |
| 54 |
end |
| 55 |
|
| 56 |
icon = self:format_hl(icon_highlight) .. icon .. default_highlight |
| 57 |
end |
| 58 |
end |
| 59 |
else |
| 60 |
ok = vim.fn.exists('*WebDevIconsGetFileTypeSymbol') |
| 61 |
if ok ~= 0 then |
| 62 |
icon = vim.fn.WebDevIconsGetFileTypeSymbol() |
| 63 |
if icon then |
| 64 |
icon = icon .. ' ' |
| 65 |
end |
| 66 |
end |
| 67 |
end |
| 68 |
|
| 69 |
if not icon then |
| 70 |
return |
| 71 |
end |
| 72 |
|
| 73 |
if self.options.icon_only then |
| 74 |
self.status = icon |
| 75 |
elseif type(self.options.icon) == 'table' and self.options.icon.align == 'right' then |
| 76 |
self.status = self.status .. ' ' .. icon |
| 77 |
else |
| 78 |
self.status = icon .. self.status |
| 79 |
end |
| 80 |
end |
| 81 |
|
| 82 |
return M |