Skip to content

Commit

Permalink
feat!: add builtin source for terminal buffers (Bekaboo#78)
Browse files Browse the repository at this point in the history
allows using the dropdown menu to quickly switch between terminal buffers
  • Loading branch information
willothy committed Sep 3, 2023
1 parent 61831e5 commit 1af9a2f
Show file tree
Hide file tree
Showing 4 changed files with 182 additions and 5 deletions.
71 changes: 67 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
- [Treesitter](#treesitter)
- [LSP](#lsp)
- [Markdown](#markdown)
- [Terminal](#terminal)
- [API](#api)
- [Utility Functions](#utility-functions)
- [Bar Utility Functions](#bar-utility-functions)
Expand Down Expand Up @@ -69,7 +70,7 @@ https://github.com/Bekaboo/dropbar.nvim/assets/76579810/e8c1ac26-0321-4762-9975-

- [x] Multiple backends that support fall-backs

`dropbar.nvim` comes with four builtin sources:
`dropbar.nvim` comes with five builtin sources:

- [x] [lsp](https://github.com/Bekaboo/dropbar.nvim/blob/master/lua/dropbar/sources/lsp.lua): gets symbols from language servers using nvim's builtin LSP framework

Expand All @@ -79,6 +80,8 @@ https://github.com/Bekaboo/dropbar.nvim/assets/76579810/e8c1ac26-0321-4762-9975-

- [x] [treesitter](https://github.com/Bekaboo/dropbar.nvim/blob/master/lua/dropbar/sources/treesitter.lua): gets symbols from treesitter parsers using nvim's builtin treesitter integration

- [x] [terminal](https://github.com/Bekaboo/dropbar.nvim/blob/master/lua/dropbar/sources/terminal.lua): easily switch terminal buffers using the dropdown menu

To make a new source yourself, see [making a new source](#making-a-new-source).

For source fall-backs support, see [bar options](#bar).
Expand Down Expand Up @@ -332,6 +335,7 @@ https://github.com/Bekaboo/dropbar.nvim/assets/76579810/e8c1ac26-0321-4762-9975-
Value = '󰎠 ',
Variable = '󰀫 ',
WhileStatement = '󰑖 ',
Terminal = ''
},
},
ui = {
Expand Down Expand Up @@ -776,6 +780,23 @@ https://github.com/Bekaboo/dropbar.nvim/assets/76579810/e8c1ac26-0321-4762-9975-
look_ahead = 200,
},
},
terminal = {
---@type string|fun(buf: integer): string
icon = function(buf)
local icon = M.opts.icons.kinds.symbols.Terminal
if M.opts.icons.kinds.use_devicons then
icon = require('nvim-web-devicons').get_icon_by_filetype(
vim.bo[buf].filetype
) or icon
end
return icon
end,
---@type string|fun(buf: integer): string
name = vim.api.nvim_buf_get_name,
---@type boolean
---Show the current terminal buffer in the menu
show_current = true,
}
},
}
```
Expand Down Expand Up @@ -995,7 +1016,7 @@ the symbols:
view.topline = topline
vim.fn.winrestview(view)
end
end,
end
```

#### Bar
Expand All @@ -1015,7 +1036,6 @@ winbar:
```lua
function(buf, _)
local sources = require('dropbar.sources')
local utils = require('dropbar.utils')
if vim.bo[buf].ft == 'markdown' then
return {
sources.path,
Expand All @@ -1026,14 +1046,19 @@ winbar:
}),
}
end
if vim.bo[buf].buftype == 'terminal' then
return {
sources.terminal,
}
end
return {
sources.path,
utils.source.fallback({
sources.lsp,
sources.treesitter,
}),
}
end,
end
```
- For more information about sources, see [`dropbar_source_t`](#dropbar_source_t).
- `opts.bar.padding`: `{ left: number, right: number }`
Expand Down Expand Up @@ -1507,6 +1532,43 @@ each sources.
- Number of lines to update when cursor moves out of the parsed range
- Default: `200`

##### Terminal

- `opts.sources.terminal.icon`: `string` or `fun(buf: integer): string`
- Icon to show before terminal names
- Default:
```lua
icon = function(buf)
local icon = M.opts.icons.kinds.symbols.Terminal
if M.opts.icons.kinds.use_devicons then
icon = require('nvim-web-devicons').get_icon_by_filetype(
vim.bo[buf].filetype
) or icon
end
return icon
end
```

- `opts.sources.terminal.name`: `string` or `fun(buf: integer): string`
- Easy to integrate with other plugins (for example, [toggleterm.nvim](https://github.com/akinsho/toggleterm.nvim)):
```lua
name = function(buf)
local name = vim.api.nvim_buf_get_name(buf)
-- the second result val is the terminal object
local term = select(2, require("toggleterm.terminal").indentify(name))
if term then
return term.display_name or term.name
else
return name
end
end
```
- Default: `vim.api.nvim_buf_get_name`

- `opts.sources.terminal.show_current: boolean`
- Show the current terminal buffer in the menu
- Default: `true`

### API

`dropbar.nvim` exposes a few functions in `lua/dropbar/api.lua` that can be
Expand Down Expand Up @@ -1659,6 +1721,7 @@ should be self-explanatory:
| DropBarIconKindValue | `{ link = 'Number' }` |
| DropBarIconKindVariable | `{ link = 'CmpItemKindVariable' }` |
| DropBarIconKindWhileStatement | `{ link = 'Repeat' }` |
| DropBarIconKindTerminal | `{ link = 'Number' }` |
| DropBarIconUIIndicator | `{ link = 'SpecialChar' }` |
| DropBarIconUIPickPivot | `{ link = 'Error' }` |
| DropBarIconUISeparator | `{ link = 'SpecialChar' }` |
Expand Down
24 changes: 23 additions & 1 deletion lua/dropbar/configs.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ M.opts = {
---@type boolean|fun(buf: integer, win: integer, info: table?): boolean
enable = function(buf, win, _)
return not vim.api.nvim_win_get_config(win).zindex
and vim.bo[buf].buftype == ''
and (vim.bo[buf].buftype == '' or vim.bo[buf].buftype == 'terminal')
and vim.api.nvim_buf_get_name(buf) ~= ''
and not vim.wo[win].diff
end,
Expand Down Expand Up @@ -176,6 +176,11 @@ M.opts = {
}),
}
end
if vim.bo[buf].buftype == 'terminal' then
return {
sources.terminal,
}
end
return {
sources.path,
utils.source.fallback({
Expand Down Expand Up @@ -560,6 +565,23 @@ M.opts = {
look_ahead = 200,
},
},
terminal = {
---@type string|fun(buf: integer): string
icon = function(buf)
local icon = M.opts.icons.kinds.symbols.Terminal
if M.opts.icons.kinds.use_devicons then
icon = require('nvim-web-devicons').get_icon_by_filetype(
vim.bo[buf].filetype
) or icon
end
return icon
end,
---@type string|fun(buf: integer): string
name = vim.api.nvim_buf_get_name,
---@type boolean
---Show the current terminal buffer in the menu
show_current = true,
},
},
}

Expand Down
1 change: 1 addition & 0 deletions lua/dropbar/hlgroups.lua
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ local hlgroups = {
DropBarIconKindValue = { link = 'Number' },
DropBarIconKindVariable = { link = 'CmpItemKindVariable' },
DropBarIconKindWhileStatement = { link = 'Repeat' },
DropBarIconKindTerminal = { link = 'Number' },
DropBarIconUIIndicator = { link = 'SpecialChar' },
DropBarIconUIPickPivot = { link = 'Error' },
DropBarIconUISeparator = { link = 'SpecialChar' },
Expand Down
91 changes: 91 additions & 0 deletions lua/dropbar/sources/terminal.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
local initialized
local configs

local function init()
initialized = true
configs = require('dropbar.configs')
end

local function buf_info(buf)
local icon = configs.opts.sources.terminal.icon
if type(icon) == 'function' then
icon = icon(buf)
end
local name = configs.opts.sources.terminal.name
if type(name) == 'function' then
name = name(buf)
end
return {
icon = icon,
name = name,
icon_hl = 'DropBarIconKindTerminal',
}
end

---@param sym dropbar_symbol_t
---@return dropbar_menu_entry_t[]
local function get_menu_entries(sym)
return vim
.iter(vim.api.nvim_list_bufs())
:filter(function(buf)
return vim.bo[buf].buftype == 'terminal'
and (buf ~= sym.bar.buf or configs.opts.sources.terminal.show_current)
end)
:map(function(buf)
local entry = buf_info(buf)
entry.on_click = function()
vim.api.nvim_win_set_buf(sym.bar.win, buf)
sym.menu:close(true)
end
return require('dropbar.menu').dropbar_menu_entry_t:new({
components = {
require('dropbar.bar').dropbar_symbol_t:new(entry),
},
})
end)
:totable()
end

local function get_symbols(buf, win)
if not initialized then
init()
end

local symbol = buf_info(buf)
symbol.on_click = function(self)
local entries = get_menu_entries(self)
if #entries > 0 then
self.menu = require('dropbar.menu').dropbar_menu_t:new({
entries = entries,
prev_win = self.bar.win,
})
self.menu:open({
win_configs = {
win = win,
col = function(menu)
if menu.prev_menu then
return menu.prev_menu._win_configs.width
end
local mouse = vim.fn.getmousepos()
local bar = require('dropbar.api').get_dropbar(
vim.api.nvim_win_get_buf(menu.prev_win),
menu.prev_win
)
if not bar then
return 0
end
local _, range =
bar:get_component_at(math.max(0, mouse.wincol - 1))
return range and range.start or 0
end,
},
})
end
end

return {
require('dropbar.bar').dropbar_symbol_t:new(symbol),
}
end

return { get_symbols = get_symbols }

0 comments on commit 1af9a2f

Please sign in to comment.