Refactor: filetype icon and add disable_text option

This commit is contained in:
shadmansaleh 2021-07-25 12:39:20 +06:00
commit 1d40e34aef
4 changed files with 99 additions and 43 deletions

View File

@ -325,7 +325,8 @@ sections = {
lualine_a = { lualine_a = {
{ {
'filetype', 'filetype',
colored = true -- displays filetype icon in color if set to `true` colored = true, -- displays filetype icon in color if set to `true
disable_text = false -- Display only icon for filetype
} }
} }
} }

View File

@ -256,10 +256,12 @@ Component specific local options~
lualine_a = { lualine_a = {
{ {
'filetype', 'filetype',
colored = true -- displays filetype icon in color if set to `true` colored = true, -- displays filetype icon in color if set to `true`
disable_text = false -- Display only icon for filetype
} }
} }
} }
<
* diff~ * diff~
> >
@ -338,4 +340,4 @@ You can disable lualine for specific filetypes
`options = {disabled_filetypes = {'lua'}}` `options = {disabled_filetypes = {'lua'}}`
------------------------------------------------------------------------------ ------------------------------------------------------------------------------
vim:tw=80:sw=4:ts=8:noet:ft=help:norl:et: vim:tw=80:sw=4:ts=8:et:ft=help:norl:et:

View File

@ -5,20 +5,31 @@ local utils = require('lualine.utils.utils')
local FileType = require('lualine.component'):new() local FileType = require('lualine.component'):new()
FileType.update_status = function(self) function FileType:new(options, child)
local data = vim.bo.filetype local new_instance = self._parent:new(options, child or FileType)
if #data > 0 then if new_instance.options.colored == nil then
new_instance.options.colored = true
end
if new_instance.options.disable_text == nil then
new_instance.options.disable_text = false
end
return new_instance
end
function FileType.update_status() return vim.bo.filetype or '' end
function FileType:apply_icon()
if not self.options.icons_enabled then return end
local icon, icon_highlight_group
local ok, devicons = pcall(require, 'nvim-web-devicons') local ok, devicons = pcall(require, 'nvim-web-devicons')
if ok then if ok then
local f_name, f_extension = vim.fn.expand('%:t'), vim.fn.expand('%:e') local f_name, f_extension = vim.fn.expand('%:t'), vim.fn.expand('%:e')
local icon, icon_highlight_group = devicons.get_icon(f_name, f_extension) icon, icon_highlight_group = devicons.get_icon(f_name, f_extension)
self.options.icon = icon
if self.options.icon and if icon and self.options.colored then
(self.options.colored or self.options.colored == nil) then local highlight_color = utils.extract_highlight_colors(
self.options.colored = true icon_highlight_group, 'fg')
local highlight_color = utils.extract_highlight_colors(icon_highlight_group, 'fg')
local is_focused = vim.g.statusline_winid == vim.fn.win_getid() local is_focused = vim.g.statusline_winid == vim.fn.win_getid()
local default_highlight = highlight.format_highlight(is_focused, local default_highlight = highlight.format_highlight(is_focused,
self.options.self self.options.self
@ -31,19 +42,21 @@ FileType.update_status = function(self)
self.options) self.options)
end end
self.options.icon = icon = highlight.component_format_highlight(icon_highlight) .. icon ..
highlight.component_format_highlight(icon_highlight) .. default_highlight
self.options.icon .. default_highlight
end end
else else
ok = vim.fn.exists('*WebDevIconsGetFileTypeSymbol') ok = vim.fn.exists('*WebDevIconsGetFileTypeSymbol')
if ok ~= 0 then if ok ~= 0 then icon = vim.fn.WebDevIconsGetFileTypeSymbol() end
self.options.icon = vim.fn.WebDevIconsGetFileTypeSymbol()
end end
if not icon then return end
if self.options.disable_text then
self.status = icon
else
self.status = icon .. ' ' .. self.status
end end
return data
end
return ''
end end
return FileType return FileType

View File

@ -277,16 +277,56 @@ describe('Filetype component', function()
local opts = build_component_opts({ local opts = build_component_opts({
component_separators = {'', ''}, component_separators = {'', ''},
padding = 0 padding = 0,
}) })
assert_component('filetype', opts, '%#MyCompHl_normal#*%#lualine_c_normal# lua') assert_component('filetype', opts, '%#MyCompHl_normal#*%#lualine_c_normal# lua')
assert.stub(utils.extract_highlight_colors).was_called_with('test_highlight_group', 'fg') assert.stub(utils.extract_highlight_colors).was_called_with('test_highlight_group', 'fg')
opts.icon = '*'
assert.stub(hl.create_component_highlight_group).was_called_with( assert.stub(hl.create_component_highlight_group).was_called_with(
{fg = '#000'}, 'test_highlight_group', opts {fg = '#000'}, 'test_highlight_group', opts
) )
hl.create_component_highlight_group:revert() hl.create_component_highlight_group:revert()
utils.extract_highlight_colors:revert() utils.extract_highlight_colors:revert()
package.loaded['nvim-web-devicons'] = nil
end)
it('Doesn\'t color when colored is false', function()
package.loaded['nvim-web-devicons'] = {
get_icon = function()
return '*', 'test_highlight_group'
end
}
local hl = require 'lualine.highlight'
local utils = require 'lualine.utils.utils'
stub(hl, 'create_component_highlight_group')
stub(utils, 'extract_highlight_colors')
hl.create_component_highlight_group.returns('MyCompHl')
utils.extract_highlight_colors.returns('#000')
local opts = build_component_opts({
component_separators = {'', ''},
padding = 0,
colored = false,
})
assert_component('filetype', opts, '* lua')
hl.create_component_highlight_group:revert()
utils.extract_highlight_colors:revert()
package.loaded['nvim-web-devicons'] = nil
end)
it('displays only icon when disable_text is true', function()
package.loaded['nvim-web-devicons'] = {
get_icon = function()
return '*', 'test_highlight_group'
end
}
local opts = build_component_opts({
component_separators = {'', ''},
padding = 0,
colored = false,
disable_text = true,
})
assert_component('filetype', opts, '*')
package.loaded['nvim-web-devicons'] = nil
end) end)
end) end)