Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(ui-select): support optional preview of items #124

Merged
merged 1 commit into from
Feb 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading