Skip to content
This repository has been archived by the owner on Sep 20, 2023. It is now read-only.

Commit

Permalink
feat: allow configuring presets Feline on the fly
Browse files Browse the repository at this point in the history
Add utility functions `add_preset` and `use_preset` to allow customizing
Feline presets on the fly.
  • Loading branch information
famiu authored and lukas-reineke committed Dec 22, 2021
1 parent 5d5b9cd commit 29b11b5
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 17 deletions.
43 changes: 33 additions & 10 deletions USAGE.md
Original file line number Diff line number Diff line change
Expand Up @@ -522,6 +522,39 @@ Here is a list of all possible vi_mode names used with the default color associa
- `highlight_reset_triggers` - Feline automatically resets its cached highlights on certain autocommands to prevent the statusline colors from getting messed up. The value of `highlight_reset_triggers` can be set to a table containing a list of autocommands that'll trigger a highlight reset.<br><br>
Default: `{'SessionLoadPost', 'ColorScheme'}`

## Utility functions

Feline provides a few utility functions that allow you to customize or modify Feline on the fly. These are discussed below.

### Reset highlight

If, for some reason, you want to clear all highlights that Feline sets (useful if you want to reload your entire Neovim config which may mess up highlights), you can do:

```lua
require('feline').reset_highlights()
```

And then Feline will automatically regenerate those highlights when it needs them, so you don't have to worry about setting the highlights yourself.

### Adding and using presets

If you want to add your own presets, you can do this through the `require('feline').add_preset` function, like this:

```lua
-- Components table for the preset
local my_preset = {
-- Insert components here
}

require('feline').add_preset('my_preset_name', my_preset)
```

You can also use a preset using `require('feline').use_preset`, like this:

```lua
require('feline').use_preset('my_preset_name')
```

## Example configuration

You can check out the code in the [default preset](lua/feline/presets/default.lua) to see how the components in it are set up so you can get a good practical idea of how to use the tools that Feline gives you to create all kinds of different statusline components.
Expand Down Expand Up @@ -711,16 +744,6 @@ end

Note that the values of both `vim.g.actual_curwin` and `vim.g.actual_curbuf` are strings, not numbers. So if you want to use them as a number, use `tonumber()` to convert the string to a number first, as shown in the second example.

### Reset highlight

If, for some reason, you want to clear all highlights that Feline sets (useful if you want to reload your entire Neovim config which may mess up highlights), you can do:

```lua
require('feline').reset_highlights()
```

And then Feline will automatically regenerate those highlights when it needs them, so you don't have to worry about setting the highlights yourself.

### Highlight section gaps

By default, gaps between two sections inherit the highlight of the last element of the section. If you wish to customize the highlight of the gap between two sections, you can just add a component with only an `hl` value to the end of the first section.
Expand Down
7 changes: 4 additions & 3 deletions lua/feline/generator.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,8 @@ local api = vim.api

local feline = require('feline')
local providers = feline.providers
local components_table = feline.components
local colors = feline.colors
local separators = feline.separators
local disable = feline.disable
local force_inactive = feline.force_inactive

local M = {
-- Cached highlights
Expand All @@ -21,6 +18,7 @@ end

-- Check if current buffer is forced to have inactive statusline
local function is_forced_inactive()
local force_inactive = feline.force_inactive
local buftype = bo.buftype
local filetype = bo.filetype
local bufname = api.nvim_buf_get_name(0)
Expand All @@ -32,6 +30,7 @@ end

-- Check if buffer is configured to have statusline disabled
local function is_disabled()
local disable = feline.disable
local buftype = bo.buftype
local filetype = bo.filetype
local bufname = api.nvim_buf_get_name(0)
Expand Down Expand Up @@ -365,6 +364,8 @@ end

-- Generate statusline by parsing all components and return a string
function M.generate_statusline(is_active)
local components_table = feline.components

if not components_table or is_disabled() then
return ''
end
Expand Down
23 changes: 20 additions & 3 deletions lua/feline/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ local cmd = api.nvim_command

local utils = require('feline.utils')
local gen = utils.lazy_require('feline.generator')
local presets = utils.lazy_require('feline.presets')

local M = {}

Expand Down Expand Up @@ -60,6 +61,23 @@ function M.reset_highlights()
gen.highlights = {}
end

-- Add a new preset for Feline (useful for plugins which intend to extend Feline)
function M.add_preset(name, value)
presets[name] = value
end

-- Use a preset
function M.use_preset(name)
if presets[name] then
M.components = presets[name]
else
api.nvim_err_writeln(string.format(
"Preset '%s' not found!",
name
))
end
end

-- Setup Feline using the provided configuration options
function M.setup(config)
-- Check if Neovim version is 0.5 or greater
Expand All @@ -71,8 +89,8 @@ function M.setup(config)
-- Check if termguicolors is enabled
if not opt.termguicolors:get() then
api.nvim_err_writeln(
'Feline needs \'termguicolors\' to be enabled to work properly\n' ..
'Please do `:help \'termguicolors\'` in Neovim for more information'
"Feline needs 'termguicolors' to be enabled to work properly\n" ..
"Please do `:help 'termguicolors'` in Neovim for more information"
)
return
end
Expand All @@ -97,7 +115,6 @@ function M.setup(config)
M.components = config.components
else
local preset = config.preset
local presets = require('feline.presets')

-- If a valid preset isn't provided, then use the default preset if nvim-web-devicons
-- exists, else use the noicons preset
Expand Down
28 changes: 27 additions & 1 deletion lua/feline/presets/init.lua
Original file line number Diff line number Diff line change
@@ -1,7 +1,33 @@
-- Use lazy_require to only load the preset that's are being used
local api = vim.api
local lazy_require = require('feline.utils').lazy_require

return {
local default_presets = {
default = lazy_require('feline.presets.default'),
noicon = lazy_require('feline.presets.noicon')
}

local custom_presets = {}

local presets_mt = {
__index = function(_, key)
if default_presets[key] then
return default_presets[key]
elseif custom_presets[key] then
return custom_presets[key]
end
end,
__newindex = function(_, key, val)
if default_presets[key] or custom_presets[key] then
api.nvim_err_writeln(string.format(
"Preset '%s' already exists!",
key
))
else
custom_presets[key] = val
end
end,
__metatable = false
}

return setmetatable({}, presets_mt)

0 comments on commit 29b11b5

Please sign in to comment.