feat: color filetype icons when using nvim-devicons (#222)

This commit is contained in:
Jonathan Gin 2021-05-11 08:55:18 -04:00 committed by GitHub
parent 73a6c8fadf
commit bfae320155
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 93 additions and 8 deletions

View File

@ -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
@ -413,7 +419,7 @@ extensions = { 'fzf' }
lualine_c = { {'filename', file_status = true} }, lualine_c = { {'filename', file_status = true} },
lualine_x = { 'encoding', 'fileformat', 'filetype' }, lualine_x = { 'encoding', 'fileformat', 'filetype' },
lualine_y = { 'progress' }, lualine_y = { 'progress' },
lualine_z = { 'location' }, lualine_z = { 'location' },
}, },
inactive_sections = { inactive_sections = {
lualine_a = { }, lualine_a = { },
@ -421,7 +427,7 @@ extensions = { 'fzf' }
lualine_c = { 'filename' }, lualine_c = { 'filename' },
lualine_x = { 'location' }, lualine_x = { 'location' },
lualine_y = { }, lualine_y = { },
lualine_z = { } lualine_z = { }
}, },
extensions = { 'fzf' } extensions = { 'fzf' }
} }

View File

@ -158,7 +158,7 @@ Lualine defaults~
lualine_c = { 'filename' }, lualine_c = { 'filename' },
lualine_x = { 'encoding', 'fileformat', 'filetype' }, lualine_x = { 'encoding', 'fileformat', 'filetype' },
lualine_y = { 'progress' }, lualine_y = { 'progress' },
lualine_z = { 'location' }, lualine_z = { 'location' },
} }
inactive_sections = { inactive_sections = {
lualine_a = { }, lualine_a = { },
@ -166,7 +166,7 @@ Lualine defaults~
lualine_c = { 'filename' }, lualine_c = { 'filename' },
lualine_x = { 'location' }, lualine_x = { 'location' },
lualine_y = { }, lualine_y = { },
lualine_z = { } lualine_z = { }
} }
< <
@ -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

View File

@ -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

View File

@ -3,8 +3,8 @@
local M = {} local M = {}
local function fugitive_branch() local function fugitive_branch()
local icon = '' -- e0a0 local icon = '' -- e0a0
return icon .. ' ' .. vim.fn.FugitiveHead() return icon .. ' ' .. vim.fn.FugitiveHead()
end end
M.sections = {lualine_a = {fugitive_branch}, lualine_z = {'location'}} M.sections = {lualine_a = {fugitive_branch}, lualine_z = {'location'}}

View File

@ -144,7 +144,7 @@ local function setup_theme()
return config.options.theme return config.options.theme
end end
vim.api.nvim_err_writeln('theme ' .. tostring(theme_name) .. vim.api.nvim_err_writeln('theme ' .. tostring(theme_name) ..
' not found, defaulting to gruvbox') ' not found, defaulting to gruvbox')
return require 'lualine.themes.gruvbox' return require 'lualine.themes.gruvbox'
end end
local theme = get_theme_from_config() local theme = get_theme_from_config()

View File

@ -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')