Skip to content

Commit

Permalink
feat: add default_hl configuration option
Browse files Browse the repository at this point in the history
  • Loading branch information
famiu committed Sep 27, 2021
1 parent dc18e87 commit ee998c0
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 35 deletions.
46 changes: 21 additions & 25 deletions USAGE.md
Original file line number Diff line number Diff line change
Expand Up @@ -422,6 +422,8 @@ custom_providers = {

- `colors` - A table containing custom [color value presets](#value-presets). The value of `colors.fg` and `colors.bg` also represent the default foreground and background colors, respectively.
- `separators` - A table containing custom [separator value presets](#value-presets).
- `default_hl` - A table defining the default highlights for the active and inactive statusline. It can contain two values inside of it, `active` and `inactive`. They define values of the `StatusLine` and `StatusLineNC` highlight groups, respectively. These two values are configured the same way as a component's `hl` value. For most users this configuration option won't be of any use, but it allows you to do some neat things like [using a thin line instead of the inactive statusline](#thin-line-for-horizontal-splits).<br>
By default, both of these highlights use `colors.fg` as foreground color and `colors.bg` as background color, and have no styling option set.
- `update_triggers` - A list of autocmds that trigger an update of the statusline in inactive windows.<br>
Default: `{'VimEnter', 'WinEnter', 'WinClosed', 'FileChangedShellPost'}`
- `force_inactive` - A table that determines which buffers should always have the inactive statusline, even when they are active. It can have 3 values inside of it, `filetypes`, `buftypes` and `bufnames`, all three of them are tables which contain Lua patterns to match against file type, buffer type and buffer name respectively.<br><br>
Expand Down Expand Up @@ -669,33 +671,27 @@ It's even simpler if you want to use the default `bg` color for the gap between

### Thin line for horizontal splits

If you want, you can have a thin line instead of the inactive statusline to separate your windows, like the vertical window split separator, except in this case it would act as a horizontal window separator of sorts. You can do this through:
It's possible to replace the inactive statusline with a thin line that acts as a separator for your horizontal splits. In order to achieve it, you just have to make use of the `default_hl` option in Feline's [setup function](#setup-function) and set the default highlight style of the inactive statusline to `'underline'` and the default background of the inactive statusline to `'NONE'`. You can also optionally get the foreground color of the `VertSplit` highlight and apply it to the highlight of the inactive statusline so that the thin line looks like the vertical split separator.

```lua
local nvim_exec = vim.api.nvim_exec

-- Get highlight of inactive statusline by parsing the style, fg and bg of VertSplit
local InactiveStatusHL = {
fg = nvim_exec('highlight VertSplit', true):match('guifg=(#[0-9A-Fa-f]+)') or '#444444',
bg = 'NONE',
style = nvim_exec('highlight VertSplit', true):match('gui=(#[0-9A-Fa-f]+)') or '',
}

-- Add underline to inactive statusline highlight style
-- in order to have a thin line instead of the statusline
if InactiveStatusHL.style == '' then
InactiveStatusHL.style = 'underline'
else
InactiveStatusHL.style = InactiveStatusHL.style .. ',underline'
end
Note that you have to make sure that the inactive statusline contains no components, otherwise this trick will not work. Here's an example showing you how to do it:

-- Apply the highlight to the statusline
-- by having an empty provider with the highlight
components.inactive = {
{
{
provider = ' ',
hl = InactiveStatusHL
```lua
local api = vim.api

-- Get foreground of the VertSplit highlight
local VertSplitFG = string.format('#%06x', api.nvim_get_hl_by_name('VertSplit', true).foreground)

-- Remove all inactive statusline components
components.inactive = {}

-- Apply the highlight to the inactive statusline
require('feline').setup {
-- Insert other configuration options here
default_hl = {
inactive = {
fg = VertSplitFG,
bg = 'NONE',
style = 'underline'
}
}
}
Expand Down
32 changes: 22 additions & 10 deletions lua/feline/generator.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ local api = vim.api
local feline = require('feline')
local providers = feline.providers
local components = feline.components
local default_hl = feline.default_hl
local colors = feline.colors
local separators = feline.separators
local disable = feline.disable
Expand Down Expand Up @@ -65,15 +66,6 @@ local function add_hl(name, fg, bg, style)
M.highlights[name] = true
end

-- Return default highlight
local function defhl()
if not M.highlights['StatusComponentDefault'] then
add_hl('StatusComponentDefault', colors.fg, colors.bg, 'NONE')
end

return 'StatusComponentDefault'
end

-- Parse highlight table, inherit default/parent values if values are not given
local function parse_hl(hl, parent_hl)
parent_hl = parent_hl or {}
Expand Down Expand Up @@ -143,6 +135,23 @@ local function get_hlname(hl, parent_hl)
return hlname
end

-- Generates StatusLine and StatusLineNC highlights based on the user configuration
local function generate_defhl(winid)
for statusline_type, hlname in pairs({active = 'StatusLine', inactive = 'StatusLineNC'}) do
-- If default hl for the current statusline type is not defined, just set it to an empty
-- table so that it can be populated by parse_hl later on
if not default_hl[statusline_type] then
default_hl[statusline_type] = {}
end

-- Only re-evaluate and add the highlight if it's a function or when it's not cached
if type(default_hl[statusline_type]) == 'function' or not M.highlights[hlname] then
local hl = parse_hl(evaluate_if_function(default_hl[statusline_type], winid))
add_hl(hlname, hl.fg, hl.bg, hl.style)
end
end
end

-- Parse component seperator
-- By default, foreground color of separator is background color of parent
-- and background color is set to default background color
Expand Down Expand Up @@ -318,6 +327,9 @@ end

-- Generate statusline by parsing all components and return a string
function M.generate_statusline(winid)
-- Generate default highlights for the statusline
generate_defhl(winid)

local statusline_str = ''

if components and not is_disabled(winid) then
Expand Down Expand Up @@ -345,7 +357,7 @@ function M.generate_statusline(winid)
-- Never return an empty string since setting statusline to an empty string will make it
-- use the global statusline value (same as active statusline) for inactive windows
if statusline_str == '' then
return string.format('%%#%s#', defhl())
return ' '
else
return statusline_str
end
Expand Down
1 change: 1 addition & 0 deletions lua/feline/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ function M.setup(config)
end

M.components = components
M.default_hl = parse_config(config, 'default_hl', 'table', {})

-- Ensures custom quickfix statusline isn't loaded
g.qf_disable_statusline = true
Expand Down

0 comments on commit ee998c0

Please sign in to comment.