Skip to content

Commit

Permalink
feat: add support for mini.pick as a picker (#163)
Browse files Browse the repository at this point in the history
* Add support for `mini.pick` as a picker

* Remove old comments

* fix: note title in mini.pick

* Refactor and add support for multi-select

---------

Co-authored-by: Karim Abou Zeid <[email protected]>
  • Loading branch information
pkazmier and kabouzeid authored Apr 14, 2024
1 parent fb0962b commit e2b6d62
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 6 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ Via [lazy.nvim](https://github.com/folke/lazy.nvim)
}
```

To get the best experience, it's recommended to also install either [Telescope](https://github.com/nvim-telescope/telescope.nvim) or [fzf](https://github.com/junegunn/fzf).
To get the best experience, it's recommended to also install either [Telescope](https://github.com/nvim-telescope/telescope.nvim), [fzf](https://github.com/junegunn/fzf), or [mini.pick](https://github.com/echasnovski/mini.nvim/blob/main/readmes/mini-pick.md)

## Setup

Expand All @@ -74,8 +74,8 @@ require("zk").setup()
**The default configuration**
```lua
require("zk").setup({
-- can be "telescope", "fzf", "fzf_lua" or "select" (`vim.ui.select`)
-- it's recommended to use "telescope", "fzf" or "fzf_lua"
-- can be "telescope", "fzf", "fzf_lua", "minipick", or "select" (`vim.ui.select`)
-- it's recommended to use "telescope", "fzf", "fzf_lua", or "minipick"
picker = "select",

lsp = {
Expand Down
6 changes: 3 additions & 3 deletions doc/zk.txt
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ Via vim-plug (https://github.com/junegunn/vim-plug)
Plug "zk-org/zk-nvim"
<

To get the best experience, it's recommended to also install either Telescope (https://github.com/nvim-telescope/telescope.nvim) or fzf (https://github.com/junegunn/fzf).
To get the best experience, it's recommended to also install either Telescope (https://github.com/nvim-telescope/telescope.nvim), fzf (https://github.com/junegunn/fzf), or mini.pick (https://github.com/echasnovski/mini.nvim/blob/main/readmes/mini-pick.md).

--------------------------------------------------------------------------------
SETUP *zk-setup*
Expand All @@ -58,8 +58,8 @@ SETUP *zk-setu
The default configuration
>
require("zk").setup({
-- can be "telescope", "fzf", "fzf_lua" or "select" (`vim.ui.select`)
-- it's recommended to use "telescope", "fzf" or "fzf_lua"
-- can be "telescope", "fzf", "fzf_lua", "minipick", or "select" (`vim.ui.select`)
-- it's recommended to use "telescope", "fzf", "fzf_lua", or "minipick"
picker = "select",
lsp = {
-- `config` is passed to `vim.lsp.start_client(config)`
Expand Down
75 changes: 75 additions & 0 deletions lua/zk/pickers/minipick.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
local M = {}
local H = {}
local minipick = require("mini.pick")

M.note_picker_list_api_selection = { "title", "path", "absPath" }

M.show_note_picker = function(notes, opts, cb)
notes = vim.tbl_map(function(note)
local title = note.title or note.path
return { text = title, path = note.absPath, value = note }
end, notes)
H.item_picker(notes, opts, cb)
end

M.show_tag_picker = function(tags, opts, cb)
local width = H.max_note_count(tags)
tags = vim.tbl_map(function(tag)
local padded_count = H.pad(tag.note_count, width)
return {
text = string.format(" %s │ %s", padded_count, tag.name),
value = tag,
}
end, tags)
H.item_picker(tags, opts, cb)
end

H.item_picker = function(items, opts, cb)
opts = opts or {}
local minipick_opts = vim.tbl_deep_extend("force", {
window = {
prompt_prefix = opts.title .. "> ",
},
source = {
items = items,
name = opts.title,

choose = function(selected_item) -- item guaranteed to be non-nil
local target_window = minipick.get_picker_state().windows.target
vim.api.nvim_win_call(target_window, function()
cb(opts.multi_select and { selected_item.value } or selected_item.value)
end)
return nil
end,

choose_marked = function(selected_items) -- could be empty table
if opts.multi_select and not vim.tbl_isempty(selected_items) then
local target_window = minipick.get_picker_state().windows.target
vim.api.nvim_win_call(target_window, function()
cb(vim.tbl_map(function(item)
return item.value
end, selected_items))
end)
end
return nil
end,
},
}, opts.minipick or {})

minipick.start(minipick_opts)
end

H.max_note_count = function(tags)
local max_count = 0
for _, t in ipairs(tags) do
max_count = math.max(max_count, t.note_count)
end
return vim.fn.strchars(tostring(max_count))
end

H.pad = function(text, width)
local text_width = vim.fn.strchars(text)
return string.rep(" ", width - text_width) .. text
end

return M

0 comments on commit e2b6d62

Please sign in to comment.