Refactor: filetype icon and add disable_text option
This commit is contained in:
commit
1d40e34aef
|
@ -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
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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:
|
||||||
|
|
|
@ -5,45 +5,58 @@ 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
|
||||||
local ok, devicons = pcall(require, 'nvim-web-devicons')
|
new_instance.options.colored = true
|
||||||
if ok then
|
end
|
||||||
local f_name, f_extension = vim.fn.expand('%:t'), vim.fn.expand('%:e')
|
if new_instance.options.disable_text == nil then
|
||||||
local icon, icon_highlight_group = devicons.get_icon(f_name, f_extension)
|
new_instance.options.disable_text = false
|
||||||
self.options.icon = icon
|
end
|
||||||
|
return new_instance
|
||||||
if self.options.icon and
|
end
|
||||||
(self.options.colored or self.options.colored == nil) then
|
|
||||||
self.options.colored = true
|
function FileType.update_status() return vim.bo.filetype or '' end
|
||||||
|
|
||||||
local highlight_color = utils.extract_highlight_colors(icon_highlight_group, 'fg')
|
function FileType:apply_icon()
|
||||||
local is_focused = vim.g.statusline_winid == vim.fn.win_getid()
|
if not self.options.icons_enabled then return end
|
||||||
local default_highlight = highlight.format_highlight(is_focused,
|
|
||||||
self.options.self
|
local icon, icon_highlight_group
|
||||||
.section)
|
local ok, devicons = pcall(require, 'nvim-web-devicons')
|
||||||
local icon_highlight = self.options.self.section .. '_' ..
|
if ok then
|
||||||
icon_highlight_group
|
local f_name, f_extension = vim.fn.expand('%:t'), vim.fn.expand('%:e')
|
||||||
if not utils.highlight_exists(icon_highlight .. '_normal') then
|
icon, icon_highlight_group = devicons.get_icon(f_name, f_extension)
|
||||||
icon_highlight = highlight.create_component_highlight_group(
|
|
||||||
{fg = highlight_color}, icon_highlight_group,
|
if icon and self.options.colored then
|
||||||
self.options)
|
local highlight_color = utils.extract_highlight_colors(
|
||||||
end
|
icon_highlight_group, 'fg')
|
||||||
|
local is_focused = vim.g.statusline_winid == vim.fn.win_getid()
|
||||||
self.options.icon =
|
local default_highlight = highlight.format_highlight(is_focused,
|
||||||
highlight.component_format_highlight(icon_highlight) ..
|
self.options.self
|
||||||
self.options.icon .. default_highlight
|
.section)
|
||||||
end
|
local icon_highlight = self.options.self.section .. '_' ..
|
||||||
else
|
icon_highlight_group
|
||||||
ok = vim.fn.exists('*WebDevIconsGetFileTypeSymbol')
|
if not utils.highlight_exists(icon_highlight .. '_normal') then
|
||||||
if ok ~= 0 then
|
icon_highlight = highlight.create_component_highlight_group(
|
||||||
self.options.icon = vim.fn.WebDevIconsGetFileTypeSymbol()
|
{fg = highlight_color}, icon_highlight_group,
|
||||||
end
|
self.options)
|
||||||
end
|
end
|
||||||
return data
|
|
||||||
|
icon = highlight.component_format_highlight(icon_highlight) .. icon ..
|
||||||
|
default_highlight
|
||||||
|
end
|
||||||
|
else
|
||||||
|
ok = vim.fn.exists('*WebDevIconsGetFileTypeSymbol')
|
||||||
|
if ok ~= 0 then 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 ''
|
|
||||||
end
|
end
|
||||||
|
|
||||||
return FileType
|
return FileType
|
||||||
|
|
|
@ -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)
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue