Skip to content

Commit

Permalink
feat(ui-select): support optional preview of items (#124)
Browse files Browse the repository at this point in the history
  • Loading branch information
willothy committed Feb 7, 2024
1 parent c3de6fe commit f835519
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2062,8 +2062,10 @@ Declared in [`lua/dropbar/utils/menu.lua`](https://github.com/Bekaboo/dropbar.nv

| Field | Type | Description |
| ------ | ------ | ------ |
| `prompt` | `string?` | determines what will be shown at the top of the select menu. |
| `format_item` | `fun(item: any): string, string[][]?` | formats the list items for display in the menu, and optionally formats virtual text chunks to be shown below the item. |
| `prompt` | `string?` | determines what will be shown at the top of the select menu. |
| `format_item` | `fun(item: any): string, string[][]?` | formats the list items for display in the menu, and optionally formats virtual text chunks to be shown below the item. |
| `preview` | `fun(self: dropbar_symbol_t, item: any, idx: integer)` | previews the list item under the cursor. |
| `preview_close` | `fun(self: dropbar_symbol_t, item: any, idx: integer)` | closes the preview when the menu is closed. |

### Making a New Source

Expand Down
12 changes: 12 additions & 0 deletions lua/dropbar/utils/menu.lua
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,8 @@ end
---The second return value is a list of virtual text chunks to be displayed below the item. If
---nothing is returned for the second value, no virtual text will be displayed.
---@field format_item? fun(item: any): string, string[][]?
---@field preview? fun(self: dropbar_symbol_t, item: any, idx: integer)
---@field preview_close? fun(self: dropbar_symbol_t, item: any, idx: integer)

---@param items string[]|table[] list of items to be selected
---@param opts dropbar_select_opts_t
Expand Down Expand Up @@ -149,6 +151,16 @@ function M.select(items, opts, on_choice)
),
icon_hl = 'DropBarIconUIIndicator',
name = text,
preview = function(self)
if opts.preview then
opts.preview(self, item, idx)
end
end,
preview_restore_view = function(self)
if opts.preview_close then
opts.preview_close(self, item, idx)
end
end,
on_click = function(self)
self.entry.menu:close()
if on_choice then
Expand Down

0 comments on commit f835519

Please sign in to comment.