feat: color filetype icons when using nvim-devicons (#222)
This commit is contained in:
parent
73a6c8fadf
commit
bfae320155
|
@ -304,6 +304,12 @@ file_status | true | Displays file status (readonly status, modified status)
|
||||||
path | 0 | filename `path` option: 0 = just filename, 1 = relative path, 2 = absolute path
|
path | 0 | filename `path` option: 0 = just filename, 1 = relative path, 2 = absolute path
|
||||||
symbols | `{modified = '[+]', readonly = '[-]'}` | changes status symbols | table containing one or more symbols |
|
symbols | `{modified = '[+]', readonly = '[-]'}` | changes status symbols | table containing one or more symbols |
|
||||||
|
|
||||||
|
* `filetype` component options
|
||||||
|
|
||||||
|
Option | Default | Behaviour
|
||||||
|
:------: | :------: | :----:
|
||||||
|
colored | true | Displays filetype icon in color if set to `true`
|
||||||
|
|
||||||
* `diff` component options
|
* `diff` component options
|
||||||
|
|
||||||
Option | Default | Behaviour | Format
|
Option | Default | Behaviour | Format
|
||||||
|
|
|
@ -412,6 +412,10 @@ In addition, some components have unique options.
|
||||||
• symbols (`{modified = '[+]', readonly = '[-]'}`)
|
• symbols (`{modified = '[+]', readonly = '[-]'}`)
|
||||||
changes status symbols
|
changes status symbols
|
||||||
|
|
||||||
|
• filetype~
|
||||||
|
• colored (true)
|
||||||
|
Displays filetype icon in color if set to `true`
|
||||||
|
|
||||||
• fileformat~
|
• fileformat~
|
||||||
• icons_enabled (true)
|
• icons_enabled (true)
|
||||||
Whether to displays icon before component. Colors
|
Whether to displays icon before component. Colors
|
||||||
|
|
|
@ -1,5 +1,8 @@
|
||||||
-- Copyright (c) 2020-2021 hoob3rt
|
-- Copyright (c) 2020-2021 hoob3rt
|
||||||
-- MIT license, see LICENSE for more details.
|
-- MIT license, see LICENSE for more details.
|
||||||
|
local highlight = require('lualine.highlight')
|
||||||
|
local utils = require('lualine.utils.utils')
|
||||||
|
|
||||||
local FileType = require('lualine.component'):new()
|
local FileType = require('lualine.component'):new()
|
||||||
|
|
||||||
FileType.update_status = function(self)
|
FileType.update_status = function(self)
|
||||||
|
@ -8,7 +11,30 @@ FileType.update_status = function(self)
|
||||||
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')
|
||||||
self.options.icon = devicons.get_icon(f_name, f_extension)
|
local icon, icon_highlight_group = devicons.get_icon(f_name, f_extension)
|
||||||
|
self.options.icon = icon
|
||||||
|
|
||||||
|
if self.options.icon and
|
||||||
|
(self.options.colored or self.options.colored == nil) then
|
||||||
|
self.options.colored = true
|
||||||
|
|
||||||
|
local highlight_color = utils.extract_highlight_colors(icon_highlight_group, 'fg')
|
||||||
|
local is_focused = vim.g.statusline_winid == vim.fn.win_getid()
|
||||||
|
local default_highlight = highlight.format_highlight(is_focused,
|
||||||
|
self.options.self
|
||||||
|
.section)
|
||||||
|
local icon_highlight = self.options.self.section .. '_' ..
|
||||||
|
icon_highlight_group
|
||||||
|
if not utils.highlight_exists(icon_highlight .. '_normal') then
|
||||||
|
icon_highlight = highlight.create_component_highlight_group(
|
||||||
|
{fg = highlight_color}, icon_highlight_group,
|
||||||
|
self.options)
|
||||||
|
end
|
||||||
|
|
||||||
|
self.options.icon =
|
||||||
|
highlight.component_format_highlight(icon_highlight) ..
|
||||||
|
self.options.icon .. default_highlight
|
||||||
|
end
|
||||||
else
|
else
|
||||||
ok = vim.fn.exists('*WebDevIconsGetFileTypeSymbol')
|
ok = vim.fn.exists('*WebDevIconsGetFileTypeSymbol')
|
||||||
if ok ~= 0 then
|
if ok ~= 0 then
|
||||||
|
|
|
@ -241,6 +241,55 @@ describe('Fileformat component', function()
|
||||||
end)
|
end)
|
||||||
end)
|
end)
|
||||||
|
|
||||||
|
describe('Filetype component', function()
|
||||||
|
local filetype
|
||||||
|
|
||||||
|
before_each(function()
|
||||||
|
filetype = vim.bo.filetype
|
||||||
|
vim.bo.filetype = 'lua'
|
||||||
|
end)
|
||||||
|
|
||||||
|
after_each(function()
|
||||||
|
vim.bo.filetype = filetype
|
||||||
|
end)
|
||||||
|
|
||||||
|
it('does not add icon when library unavailable', function()
|
||||||
|
local opts = build_component_opts({
|
||||||
|
component_separators = {'', ''},
|
||||||
|
padding = 0
|
||||||
|
})
|
||||||
|
assert_component('filetype', opts, 'lua')
|
||||||
|
end)
|
||||||
|
|
||||||
|
it('colors nvim-web-devicons icons', 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
|
||||||
|
})
|
||||||
|
assert_component('filetype', opts, '%#MyCompHl_normal#*%#lualine_c_normal# lua')
|
||||||
|
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(
|
||||||
|
{fg = '#000'}, 'test_highlight_group', opts
|
||||||
|
)
|
||||||
|
hl.create_component_highlight_group:revert()
|
||||||
|
utils.extract_highlight_colors:revert()
|
||||||
|
end)
|
||||||
|
end)
|
||||||
|
|
||||||
describe('Hostname component', function()
|
describe('Hostname component', function()
|
||||||
it('works', function()
|
it('works', function()
|
||||||
stub(vim.loop, 'os_gethostname')
|
stub(vim.loop, 'os_gethostname')
|
||||||
|
|
Loading…
Reference in New Issue