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

Commit 7ddd3f0

Browse files
authored
Merge pull request #222 from feline-nvim/develop
v1.0
2 parents e54e0cc + a318fbe commit 7ddd3f0

File tree

6 files changed

+289
-30
lines changed

6 files changed

+289
-30
lines changed

USAGE.md

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,18 @@ end
164164

165165
If you omit the provider value, it will be set to an empty string. A component with no provider or an empty provider may be useful for things like [applying a highlight to section gaps](#highlight-section-gaps) or just having an icon or separator as a component.
166166

167+
#### Component name
168+
169+
A component can optionally be given a name. While the component is not required to have a name and the name is mostly useless, it can be used to check if the component has been [truncated](#truncation). To give a component a name, just set its `name` value to a `string`, shown below:
170+
171+
```
172+
local my_component = {
173+
name = 'a_unique_name'
174+
}
175+
```
176+
177+
Two components inside the `active` or `inactive` table cannot share the same name, so make sure to give all components unique names.
178+
167179
#### Truncation
168180

169181
Feline has an automatic smart truncation system where components can be automatically truncated if the statusline doesn't fit within the window. It can be useful if you want to make better use of screen space. It also allows you to better manage which providers are truncated, how they are truncated and in which order they are truncated.
@@ -228,6 +240,10 @@ local high_priority_component = {
228240

229241
Priority can also be set to a negative number, which can be used to make a component be truncated earlier than the ones with default priority.
230242

243+
##### Check if component is truncated or hidden
244+
245+
If you give a component a `name`, you can check if that component has been truncated or hidden by Feline's smart truncation system through the utility functions, `require('feline').is_component_truncated` and `require('feline').is_component_hidden`. Both of these functions take two arguments, `winid` which is the window id of the window for which the component's truncation is being checked, the second is the `name` of the component. `is_component_truncated` returns `true` if a component has been truncated or hidden, and `is_component_hidden` returns `true` only if a component has been hidden.
246+
231247
#### Conditionally enable components
232248

233249
The `enabled` value of a component can be a boolean or function. This value determines if the component is enabled or not. If false, the component is not shown in the statusline. For example:
@@ -464,6 +480,37 @@ Now that you know about the components table and how Feline components work, you
464480

465481
- `preset` - Set it to use a preconfigured statusline. Currently it can be equal to either `default` for the default statusline or `noicon` for the default statusline without icons. You don't have to put any of the other values if you use a preset, but if you do, your settings will override the preset's settings. To see more info such as how to modify a preset to build a statusline, see: [Modifying an existing preset](#3.-modifying-an-existing-preset)
466482
- `components` - The [components table](#components).
483+
- `conditional_components` - An array-like table containing conditionally enabled components tables, each element of the table must be a components table with an additional key, `condition`, which would be a function without arguments that returns a boolean value. If the function returns `true` for a certain window, then that components table will be used for the statusline of that window instead of the default components table. If multiple conditional components match a certain window, the first one in the table will be used. An example usage of this option is shown below:
484+
485+
```lua
486+
conditional_components = {
487+
{
488+
-- Only use this components table for the 2nd window
489+
condition = function()
490+
return vim.api.nvim_win_get_number(0) == 2
491+
end,
492+
active = {
493+
-- Components used for active window
494+
},
495+
inactive = {
496+
-- Components used for inactive windows
497+
},
498+
},
499+
{
500+
-- Only use this components table for buffers of filetype 'lua'
501+
condition = function()
502+
return vim.api.nvim_buf_get_option(0, 'filetype') == 'lua'
503+
end,
504+
active = {
505+
-- Components used for active window
506+
},
507+
inactive = {
508+
-- Components used for inactive windows
509+
},
510+
}
511+
}
512+
```
513+
467514
- `custom_providers` - A table containing user-defined [provider functions](#component-providers). For example:
468515

469516
```lua
@@ -657,6 +704,17 @@ The `file_info` provider has some special provider options that can be passed th
657704

658705
<br>Default: `'base-only'`
659706

707+
### File Type
708+
709+
The file type provider has the following options:
710+
711+
- `filetype_icon` (boolean): Whether the file type icon is shown alongside the file type.
712+
Default: `false`
713+
- `colored_icon` (boolean): Determines whether file icon should use color inherited from `nvim-web-devicons`.<br>
714+
Default: `true`
715+
- `case` (string): The case of the file type string. Possible values are: `'uppercase'`, `'titlecase'` and `'lowercase'`.<br>
716+
Default: `'uppercase'`
717+
660718
### Git
661719

662720
The git providers all require [gitsigns.nvim](https://github.com/lewis6991/gitsigns.nvim/), make sure you have it installed when you use those providers, otherwise they'll have no output.

doc/feline.txt

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,28 @@ with no provider or an empty provider may be useful for things like
236236
|feline-applying-a-highlight-to-section-gaps| or just having an icon or
237237
separator as a component.
238238

239+
*feline-Component-name*
240+
241+
Component name A component can optionally be given a
242+
name. While the component is not
243+
required to have a name and the name is
244+
mostly useless, it can be used to check
245+
if the component has been
246+
|feline-truncated|. To give a component
247+
a name, just set its `name` value to a
248+
`string`, shown below:
249+
250+
251+
>
252+
local my_component = {
253+
name = 'a_unique_name'
254+
}
255+
<
256+
257+
258+
Two components inside the `active` or `inactive` table cannot share the same
259+
name, so make sure to give all components unique names.
260+
239261
*feline-Truncation*
240262

241263
Truncation Feline has an automatic smart truncation
@@ -332,6 +354,18 @@ are truncated later on. For example:
332354
Priority can also be set to a negative number, which can be used to make a
333355
component be truncated earlier than the ones with default priority.
334356

357+
CHECK IF COMPONENT IS TRUNCATED OR HIDDEN
358+
359+
If you give a component a `name`, you can check if that component has been
360+
truncated or hidden by Feline’s smart truncation system through the utility
361+
functions, `require('feline').is_component_truncated` and
362+
`require('feline').is_component_hidden`. Both of these functions take two
363+
arguments, `winid` which is the window id of the window for which the
364+
component’s truncation is being checked, the second is the `name` of the
365+
component. `is_component_truncated` returns `true` if a component has been
366+
truncated or hidden, and `is_component_hidden` returns `true` only if a
367+
component has been hidden.
368+
335369
*feline-Conditionally-enable-components*
336370

337371
Conditionally enable components The `enabled` value of a component can
@@ -646,6 +680,40 @@ listed below:
646680

647681
- `preset` - Set it to use a preconfigured statusline. Currently it can be equal to either `default` for the default statusline or `noicon` for the default statusline without icons. You don’t have to put any of the other values if you use a preset, but if you do, your settings will override the preset’s settings. To see more info such as how to modify a preset to build a statusline, see: |feline-modifying-an-existing-preset|
648682
- `components` - The |feline-components-table|.
683+
- `conditional_components` - An array-like table containing conditionally enabled components tables, each element of the table must be a components table with an additional key, `condition`, which would be a function without arguments that returns a boolean value. If the function returns `true` for a certain window, then that components table will be used for the statusline of that window instead of the default components table. If multiple conditional components match a certain window, the first one in the table will be used. An example usage of this option is shown below:
684+
685+
686+
>
687+
conditional_components = {
688+
{
689+
-- Only use this components table for the 2nd window
690+
condition = function()
691+
return vim.api.nvim_win_get_number(0) == 2
692+
end,
693+
active = {
694+
-- Components used for active window
695+
},
696+
inactive = {
697+
-- Components used for inactive windows
698+
},
699+
},
700+
{
701+
-- Only use this components table for buffers of filetype 'lua'
702+
condition = function()
703+
return vim.api.nvim_buf_get_option(0, 'filetype') == 'lua'
704+
end,
705+
active = {
706+
-- Components used for active window
707+
},
708+
inactive = {
709+
-- Components used for inactive windows
710+
},
711+
}
712+
}
713+
<
714+
715+
716+
649717
- `custom_providers` - A table containing user-defined |feline-provider-functions|. For example:
650718

651719

@@ -884,6 +952,19 @@ through the provider `opts`:
884952
<br>Default: `'base-only'`
885953

886954

955+
FILE TYPE ~
956+
957+
The file type provider has the following options:
958+
959+
960+
- `filetype_icon` (boolean): Whether the file type icon is shown alongside the file type.
961+
Default: `false`
962+
- `colored_icon` (boolean): Determines whether file icon should use color inherited from `nvim-web-devicons`.<br>
963+
Default: `true`
964+
- `case` (string): The case of the file type string. Possible values are: `'uppercase'`, `'titlecase'` and `'lowercase'`.<br>
965+
Default: `'uppercase'`
966+
967+
887968
GIT ~
888969

889970
The git providers all require gitsigns.nvim

lua/feline/defaults.lua

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,9 @@ return {
9292
components = {
9393
type = 'table',
9494
},
95+
conditional_components = {
96+
type = 'table',
97+
},
9598
preset = {
9699
type = 'string',
97100
},

lua/feline/generator.lua

Lines changed: 69 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ local feline = require('feline')
66
local M = {
77
-- Cached highlights
88
highlights = {},
9+
-- Used to check if a certain component is truncated
10+
component_truncated = {},
11+
-- Used to check if a certain component is hidden
12+
component_hidden = {},
913
}
1014

1115
-- Return true if any pattern in tbl matches provided value
@@ -320,6 +324,7 @@ end
320324
-- Wrapper around parse_component that handles any errors that happen while parsing the components
321325
-- and points to the location of the component in case of any errors
322326
local function parse_component_handle_errors(
327+
winid,
323328
component,
324329
use_short_provider,
325330
statusline_type,
@@ -331,10 +336,11 @@ local function parse_component_handle_errors(
331336
if not ok then
332337
api.nvim_err_writeln(
333338
string.format(
334-
"Feline: error while processing component number %d on section %d of type '%s': %s",
339+
"Feline: error while processing component number %d on section %d of type '%s' for window %d: %s",
335340
component_number,
336341
component_section,
337342
statusline_type,
343+
winid,
338344
result
339345
)
340346
)
@@ -358,10 +364,35 @@ end
358364

359365
-- Generate statusline by parsing all components and return a string
360366
function M.generate_statusline(is_active)
361-
if not feline.components or is_disabled() then
367+
local components
368+
local winid = api.nvim_get_current_win()
369+
370+
M.component_truncated[winid] = {}
371+
M.component_hidden[winid] = {}
372+
373+
if is_disabled() then
362374
return ''
363375
end
364376

377+
-- If a condition for one of the conditional components is satisfied, use those components
378+
if feline.conditional_components then
379+
for _, v in ipairs(feline.conditional_components) do
380+
if v.condition() then
381+
components = v
382+
break
383+
end
384+
end
385+
end
386+
387+
-- If none of the conditional components match, use the default components
388+
if not components then
389+
if feline.components then
390+
components = feline.components
391+
else
392+
return ''
393+
end
394+
end
395+
365396
local statusline_type
366397

367398
if is_active and not is_forced_inactive() then
@@ -370,7 +401,7 @@ function M.generate_statusline(is_active)
370401
statusline_type = 'inactive'
371402
end
372403

373-
local sections = feline.components[statusline_type]
404+
local sections = components[statusline_type]
374405

375406
if not sections then
376407
return ''
@@ -388,8 +419,23 @@ function M.generate_statusline(is_active)
388419
component_widths[i] = {}
389420

390421
for j, component in ipairs(section) do
391-
local component_str = parse_component_handle_errors(component, false, statusline_type, i, j)
422+
if component.name then
423+
if M.component_truncated[winid][component.name] ~= nil then
424+
api.nvim_err_writeln(
425+
string.format(
426+
'Feline: error while parsing components for window %d: '
427+
.. "Multiple components with name '%s'",
428+
winid,
429+
component.name
430+
)
431+
)
432+
return ''
433+
end
434+
M.component_truncated[winid][component.name] = false
435+
M.component_hidden[winid][component.name] = false
436+
end
392437

438+
local component_str = parse_component_handle_errors(winid, component, false, statusline_type, i, j)
393439
local component_width = get_component_width(component_str)
394440

395441
component_strs[i][j] = component_str
@@ -421,7 +467,14 @@ function M.generate_statusline(is_active)
421467
local component = sections[section][number]
422468

423469
if component.short_provider then
424-
local component_str = parse_component_handle_errors(component, true, statusline_type, section, number)
470+
local component_str = parse_component_handle_errors(
471+
winid,
472+
component,
473+
true,
474+
statusline_type,
475+
section,
476+
number
477+
)
425478

426479
local component_width = get_component_width(component_str)
427480

@@ -435,6 +488,10 @@ function M.generate_statusline(is_active)
435488
statusline_width = statusline_width - width_difference
436489
component_strs[section][number] = component_str
437490
component_widths[section][number] = component_width
491+
492+
if component.name then
493+
M.component_truncated[winid][component.name] = true
494+
end
438495
end
439496
end
440497

@@ -449,11 +506,17 @@ function M.generate_statusline(is_active)
449506
if statusline_width > window_width then
450507
for _, indices in ipairs(component_indices) do
451508
local section, number = indices[1], indices[2]
509+
local component = sections[section][number]
452510

453-
if sections[section][number].truncate_hide then
511+
if component.truncate_hide then
454512
statusline_width = statusline_width - component_widths[section][number]
455513
component_strs[section][number] = ''
456514
component_widths[section][number] = 0
515+
516+
if component.name then
517+
M.component_truncated[winid][component.name] = true
518+
M.component_hidden[winid][component.name] = true
519+
end
457520
end
458521

459522
if statusline_width <= window_width then

lua/feline/init.lua

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,26 @@ function M.use_theme(name_or_tbl)
136136
M.reset_highlights()
137137
end
138138

139+
-- Check if component with `name` in the statusline of window `winid` is truncated or hidden
140+
function M.is_component_truncated(winid, name)
141+
if gen.component_truncated[winid][name] == nil then
142+
api.nvim_err_writeln(string.format("Component with name '%s' not found", name))
143+
return
144+
end
145+
146+
return gen.component_truncated[winid][name]
147+
end
148+
149+
-- Check if component with `name` in the statusline of window `winid` is hidden
150+
function M.is_component_hidden(winid, name)
151+
if gen.component_hidden[winid][name] == nil then
152+
api.nvim_err_writeln(string.format("Component with name '%s' not found", name))
153+
return
154+
end
155+
156+
return gen.component_hidden[winid][name]
157+
end
158+
139159
-- Setup Feline using the provided configuration options
140160
function M.setup(config)
141161
-- Check if Neovim version is 0.5 or greater
@@ -189,6 +209,8 @@ function M.setup(config)
189209
M.use_preset(preset)
190210
end
191211

212+
M.conditional_components = config.conditional_components
213+
192214
-- Ensures custom quickfix statusline isn't loaded
193215
g.qf_disable_statusline = true
194216

0 commit comments

Comments
 (0)