Skip to content

Commit

Permalink
feat: icons.preset (#486)
Browse files Browse the repository at this point in the history
* docs: `barbar.preset`

* feat: `icons.preset`

* fix(utils): `highlight.set` modifies cached `attributes`

* feat(highlight): respond to `icons.preset`

This allows the powerline and slanted presets to look good by default.

* perf(events): remove unecessary table wrapping
  • Loading branch information
Iron-E authored May 15, 2023
1 parent cefe44f commit b2a99b4
Show file tree
Hide file tree
Showing 6 changed files with 275 additions and 117 deletions.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,7 @@ map('n', '<Space>bw', '<Cmd>BufferOrderByWindowNumber<CR>', opts)
```lua
vim.g.barbar_auto_setup = false -- disable auto-setup
require'barbar'.setup {
-- WARN: do not copy everything below into your config!
-- It is just an example of what configuration options there are.
Expand Down Expand Up @@ -328,7 +329,10 @@ require'barbar'.setup {
-- Configure the icons on the bufferline when modified or pinned.
-- Supports all the base icon options.
modified = {button = '●'},
pinned = {button = '車', filename = true, separator = {right = ''}},
pinned = {button = '', filename = true},
-- Use a preconfigured buffer appearance— can be 'default', 'powerline', or 'slanted'
preset = 'default',
-- Configure the icons on the bufferline based on the visibility of a buffer.
-- Supports all the base icon options, plus `modified` and `pinned`.
Expand Down
13 changes: 10 additions & 3 deletions doc/barbar.txt
Original file line number Diff line number Diff line change
Expand Up @@ -122,10 +122,8 @@ Highlight groups are created in this way: `Buffer<STATUS><PART>`.
To disable the auto-setup, set this variable to `false`: >
vim.g.barbar_auto_setup = false -- Lua
let g:barbar_auto_setup = v:false " Vim script
<
`barbar`.setup({options}) *barbar.setup()*

To configure barbar, you must call this `setup` function. The valid
{options} are listed below.

Expand Down Expand Up @@ -365,7 +363,7 @@ icons ~
}}
<

Lastly, you can customize icons based on the visibility of a buffer:
You can also customize icons based on the visibility of a buffer:

*barbar-setup.icons.alternate*
icons.alternate ~
Expand Down Expand Up @@ -408,6 +406,15 @@ icons ~
}}
<

*barbar-seutup.icons.preset*
icons.preset ~
`'default'|'powerline'|'slanted'` (default: `'default'`)
Base all |barbar-setup.icons| configuration off of this set of defaults.

- `'default'`: the classic |barbar.nvim| look.
- `'powerline'`: like <https://github.com/powerline/powerline>
- `'slanted'`: like old Google Chrome tabs

*barbar-setup.insert_at_start*
insert_at_start ~
`boolean` (default: `false`)
Expand Down
124 changes: 81 additions & 43 deletions lua/barbar/config.lua
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
local strwidth = vim.api.nvim_strwidth --- @type function
local table_concat = table.concat
local tbl_deep_extend = vim.tbl_deep_extend

Expand Down Expand Up @@ -90,49 +91,88 @@ local GIT_STATUSES = {'added', 'changed', 'deleted'}
--- @field left string
--- @field right string

--- @alias barbar.config.options.icons.preset 'default'|'powerline'|'slanted'

--- @class barbar.config.options.icons: barbar.config.options.icons.state
--- @field alternate barbar.config.options.icons.state the icons used for an alternate buffer
--- @field current barbar.config.options.icons.state the icons for the current buffer
--- @field inactive barbar.config.options.icons.state the icons for inactive buffers
--- @field visible barbar.config.options.icons.state the icons for visible buffers
--- @field preset barbar.config.options.icons.preset
--- @field scroll barbar.config.options.icons.scroll the scroll arrows
--- @field visible barbar.config.options.icons.state the icons for visible buffers

--- @alias barbar.config.options.icons.preset boolean|"both"|"buffer_number_with_icon"|"buffer_numbers"|"numbers"
--- @type {[barbar.config.options.icons.preset]: fun(default_icons: barbar.config.options.icons, user_icons?: table)}
local ICON_PRESETS = {
default = function(default_icons, user_icons)
default_icons.inactive = { separator = { left = '', right = '' } }
default_icons.separator = { left = '', right = '' }

local pinned_icons = user_icons and user_icons.pinned
if pinned_icons == nil or
pinned_icons.button == false or
(pinned_icons.button and strwidth(pinned_icons.button) < 1)
then
default_icons.pinned.separator = { right = ' ' }
end
end,

--- @type {[barbar.config.options.icons.preset]: barbar.config.options.icons}
local PRESETS = {
[false] = {
buffer_number = false,
buffer_index = false,
filetype = { enabled = false },
},
[true] = {
buffer_number = false,
buffer_index = false,
filetype = { enabled = true },
},
both = {
buffer_index = true,
buffer_number = false,
filetype = { enabled = true },
},
buffer_number_with_icon = {
buffer_index = false,
buffer_number = true,
filetype = { enabled = true },
},
buffer_numbers = {
buffer_index = false,
buffer_number = true,
filetype = { enabled = false },
},
numbers = {
buffer_index = true,
buffer_number = false,
filetype = { enabled = false },
},
powerline = function(default_icons)
default_icons.inactive = { separator = { left = '', right = '' } }
default_icons.separator = { left = '', right = '' }
end,

slanted = function(default_icons)
default_icons.inactive = { separator = { left = '', right = '' } }
default_icons.separator = { left = '', right = '' }
end,
}

--- @alias barbar.config.options.icons.preset.deprecated boolean|'both'|'buffer_number_with_icon'|'buffer_numbers'|'numbers'

--- @type {[barbar.config.options.icons.preset.deprecated]: barbar.config.options.icons}
local DEPRECATED_ICON_PRESETS = setmetatable({}, {__index = function(_, key)
local icons
if key == false then
icons = {
buffer_number = false,
buffer_index = false,
filetype = { enabled = false },
}
elseif key == true then
icons = {
buffer_number = false,
buffer_index = false,
filetype = { enabled = true },
}
elseif key == 'both' then
icons = {
buffer_index = true,
buffer_number = false,
filetype = { enabled = true },
}
elseif key == 'buffer_number_with_icon' then
icons = {
buffer_index = false,
buffer_number = true,
filetype = { enabled = true },
}
elseif key == 'buffer_numbers' then
icons = {
buffer_index = false,
buffer_number = true,
filetype = { enabled = false },
}
elseif key == 'numbers' then
icons = {
buffer_index = true,
buffer_number = false,
filetype = { enabled = false },
}
end

return icons
end})

--- A table of options that used to exist, and where they are located now.
--- @type {[string]: string[]}
local DEPRECATED_OPTIONS = {
Expand Down Expand Up @@ -165,8 +205,8 @@ local DEPRECATED_OPTIONS = {
--- @field insert_at_start boolean
--- @field letters string
--- @field maximum_length integer
--- @field minimum_length integer
--- @field maximum_padding integer
--- @field minimum_length integer
--- @field minimum_padding integer
--- @field no_name_title? string
--- @field semantic_letters boolean
Expand All @@ -188,7 +228,7 @@ function config.setup(options)
do -- TODO: remove after v2
local icons_type = type(options.icons)
if icons_type == 'string' or icons_type == 'boolean' then
local preset = PRESETS[options.icons]
local preset = DEPRECATED_ICON_PRESETS[options.icons]
utils.deprecate(
DEPRECATE_PREFIX .. utils.markdown_inline_code('icons = ' .. vim.inspect(options.icons)),
utils.markdown_inline_code('icons = ' .. vim.inspect(
Expand Down Expand Up @@ -252,18 +292,15 @@ function config.setup(options)
},
filename = true,
filetype = { enabled = true },
inactive = { separator = { left = '', right = '' } },
modified = { button = '' },
pinned = { button = false, filename = false },
separator = { left = '', right = '' },
scroll = { left = '', right = '' }
preset = 'default',
scroll = { left = '', right = '' },
}

do
local pinned_icons = options.icons and options.icons.pinned
if pinned_icons == nil or pinned_icons.button == false or #pinned_icons.button < 1 then
default_icons.pinned.separator = { right = ' ' }
end
local icons = options.icons
ICON_PRESETS[icons and icons.preset or default_icons.preset](default_icons, icons)
end

config.options = tbl_deep_extend('keep', options, {
Expand All @@ -286,6 +323,7 @@ function config.setup(options)
minimum_length = 0,
minimum_padding = 1,
no_name_title = nil,
preset = 'default',
semantic_letters = true,
sidebar_filetypes = {},
tabpages = true,
Expand Down
4 changes: 2 additions & 2 deletions lua/barbar/events.lua
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,8 @@ end)
function events.enable()
local augroup_misc, augroup_render = events.augroups()

create_autocmd({'VimEnter'}, { callback = state.load_recently_closed, group = augroup_misc })
create_autocmd({'VimLeave'}, { callback = state.save_recently_closed, group = augroup_misc })
create_autocmd('VimEnter', { callback = state.load_recently_closed, group = augroup_misc })
create_autocmd('VimLeave', { callback = state.save_recently_closed, group = augroup_misc })

create_autocmd({'BufNewFile', 'BufReadPost'}, {
callback = vim.schedule_wrap(function(event)
Expand Down
Loading

0 comments on commit b2a99b4

Please sign in to comment.