Skip to content

Commit

Permalink
(mini.pick) FEATURE: Implement window.prompt_cursor and `window.pro…
Browse files Browse the repository at this point in the history
…mpt_prefix` config options.

Details:
- Resolves #581.
  • Loading branch information
echasnovski committed Dec 29, 2023
1 parent 02f5c05 commit e587e75
Show file tree
Hide file tree
Showing 9 changed files with 191 additions and 7 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ vim.keymap.set('i', '<C-z>', '<C-g>u<Esc>[s1z=`]a<C-g>u', { desc = 'Correct late

- Update `bench_time()` to use `vim.loop.hrtime()` (as better designed for benchmarking) instead of `vim.loop.gettimeofday()`.

## mini.pick

- FEATURE: Implement `window.prompt_cursor` and `window.prompt_prefix` config options.

## mini.sessions

- FEATURE: Update `read()` to first `write()` current session (if there is any).
Expand Down
12 changes: 12 additions & 0 deletions doc/mini-pick.txt
Original file line number Diff line number Diff line change
Expand Up @@ -750,6 +750,12 @@ Default values:
window = {
-- Float window config (table or callable returning it)
config = nil,
-- String to use as cursor in prompt
prompt_cursor = '▏',
-- String to use as prefix in prompt
prompt_prefix = '> ',
},
}
<
Expand Down Expand Up @@ -801,6 +807,12 @@ See |MiniPick-source| for more information.
picker window. This can be either a table overriding some parts or a callable
returning such table. See |MiniPick-examples| for some examples.

`window.prompt_cursor` defines how cursor is displayed in window's prompt.
Default: '▏'.

`window.prompt_prefix` defines what prefix is used in window's prompt.
Default: '> '.

------------------------------------------------------------------------------
*MiniPick.start()*
`MiniPick.start`({opts})
Expand Down
26 changes: 19 additions & 7 deletions lua/mini/pick.lua
Original file line number Diff line number Diff line change
Expand Up @@ -745,6 +745,12 @@ end
--- `window.config` defines a (parts of) default floating window config for the main
--- picker window. This can be either a table overriding some parts or a callable
--- returning such table. See |MiniPick-examples| for some examples.
---
--- `window.prompt_cursor` defines how cursor is displayed in window's prompt.
--- Default: '▏'.
---
--- `window.prompt_prefix` defines what prefix is used in window's prompt.
--- Default: '> '.
MiniPick.config = {
-- Delays (in ms; should be at least 1)
delay = {
Expand Down Expand Up @@ -821,6 +827,12 @@ MiniPick.config = {
window = {
-- Float window config (table or callable returning it)
config = nil,

-- String to use as cursor in prompt
prompt_cursor = '',

-- String to use as prefix in prompt
prompt_prefix = '> ',
},
}
--minidoc_afterlines_end
Expand Down Expand Up @@ -1787,6 +1799,7 @@ H.setup_config = function(config)
window = { config.window, 'table' },
})

local is_table_or_callable = function(x) return x == nil or type(x) == 'table' or vim.is_callable(x) end
vim.validate({
['delay.async'] = { config.delay.async, 'number' },
['delay.busy'] = { config.delay.busy, 'number' },
Expand Down Expand Up @@ -1830,11 +1843,9 @@ H.setup_config = function(config)
['source.choose'] = { config.source.choose, 'function', true },
['source.choose_marked'] = { config.source.choose_marked, 'function', true },

['window.config'] = {
config.window.config,
function(x) return x == nil or type(x) == 'table' or vim.is_callable(x) end,
'table or callable',
},
['window.config'] = { config.window.config, is_table_or_callable, 'table or callable' },
['window.prompt_cursor'] = { config.window.prompt_cursor, 'string' },
['window.prompt_prefix'] = { config.window.prompt_prefix, 'string' },
})

return config
Expand Down Expand Up @@ -2322,6 +2333,7 @@ H.picker_get_char_data = function(picker, skip_alternatives)
end

H.picker_set_bordertext = function(picker)
local opts = picker.opts
local win_id = picker.windows.main
if not H.is_valid_win(win_id) then return end

Expand All @@ -2332,7 +2344,7 @@ H.picker_set_bordertext = function(picker)
local query, caret = picker.query, picker.caret
local before_caret = table.concat(vim.list_slice(query, 1, caret - 1), '')
local after_caret = table.concat(vim.list_slice(query, caret, #query), '')
local prompt_text = '> ' .. before_caret .. '' .. after_caret
local prompt_text = opts.window.prompt_prefix .. before_caret .. opts.window.prompt_cursor .. after_caret
local prompt = { { H.win_trim_to_width(win_id, prompt_text), 'MiniPickPrompt' } }
config = { title = prompt }
end
Expand All @@ -2357,7 +2369,7 @@ H.picker_set_bordertext = function(picker)
end

-- Respect `options.content_from_bottom`
if nvim_has_window_footer and picker.opts.options.content_from_bottom then
if nvim_has_window_footer and opts.options.content_from_bottom then
config.title, config.footer = config.footer, config.title
end

Expand Down
6 changes: 6 additions & 0 deletions readmes/mini-pick.md
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,12 @@ Here are code snippets for some common installation methods (use only one):
window = {
-- Float window config (table or callable returning it)
config = nil,

-- String to use as cursor in prompt
prompt_cursor = '',

-- String to use as prefix in prompt
prompt_prefix = '> ',
},
}
```
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
--|---------|---------|---------|---------|
01|
02|~
03|~
04|~
05|┌> +─────────────────────┐
06|│a │
07|│b │
08|│c │
09|│ │
10|│ │
11|│ │
12|│ │
13|│ │
14|└────────────────────────┘
15|

--|---------|---------|---------|---------|
01|0000000000000000000000000000000000000000
02|1111111111111111111111111111111111111111
03|1111111111111111111111111111111111111111
04|1111111111111111111111111111111111111111
05|2333222222222222222222222211111111111111
06|2444444444444444444444444211111111111111
07|2555555555555555555555555211111111111111
08|2555555555555555555555555211111111111111
09|2666666666666666666666666211111111111111
10|2666666666666666666666666211111111111111
11|2666666666666666666666666211111111111111
12|2666666666666666666666666211111111111111
13|2666666666666666666666666211111111111111
14|2222222222222222222222222211111111111111
15|0000000000000000000000000000000000000000
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
--|---------|---------|---------|---------|
01|
02|~
03|~
04|~
05|┌> a+b───────────────────┐
06|│ │
07|│ │
08|│ │
09|│ │
10|│ │
11|│ │
12|│ │
13|│ │
14|└────────────────────────┘
15|

--|---------|---------|---------|---------|
01|0000000000000000000000000000000000000000
02|1111111111111111111111111111111111111111
03|1111111111111111111111111111111111111111
04|1111111111111111111111111111111111111111
05|2333332222222222222222222211111111111111
06|2444444444444444444444444211111111111111
07|2555555555555555555555555211111111111111
08|2555555555555555555555555211111111111111
09|2555555555555555555555555211111111111111
10|2555555555555555555555555211111111111111
11|2555555555555555555555555211111111111111
12|2555555555555555555555555211111111111111
13|2555555555555555555555555211111111111111
14|2222222222222222222222222211111111111111
15|0000000000000000000000000000000000000000
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
--|---------|---------|---------|---------|
01|
02|~
03|~
04|~
05|┌$> ▏───────────────────┐
06|│a │
07|│b │
08|│c │
09|│ │
10|│ │
11|│ │
12|│ │
13|│ │
14|└────────────────────────┘
15|

--|---------|---------|---------|---------|
01|0000000000000000000000000000000000000000
02|1111111111111111111111111111111111111111
03|1111111111111111111111111111111111111111
04|1111111111111111111111111111111111111111
05|2333332222222222222222222211111111111111
06|2444444444444444444444444211111111111111
07|2555555555555555555555555211111111111111
08|2555555555555555555555555211111111111111
09|2666666666666666666666666211111111111111
10|2666666666666666666666666211111111111111
11|2666666666666666666666666211111111111111
12|2666666666666666666666666211111111111111
13|2666666666666666666666666211111111111111
14|2222222222222222222222222211111111111111
15|0000000000000000000000000000000000000000
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
--|---------|---------|---------|---------|
01|
02|~
03|~
04|~
05|┌$> a▏b─────────────────┐
06|│ │
07|│ │
08|│ │
09|│ │
10|│ │
11|│ │
12|│ │
13|│ │
14|└────────────────────────┘
15|

--|---------|---------|---------|---------|
01|0000000000000000000000000000000000000000
02|1111111111111111111111111111111111111111
03|1111111111111111111111111111111111111111
04|1111111111111111111111111111111111111111
05|2333333322222222222222222211111111111111
06|2444444444444444444444444211111111111111
07|2555555555555555555555555211111111111111
08|2555555555555555555555555211111111111111
09|2555555555555555555555555211111111111111
10|2555555555555555555555555211111111111111
11|2555555555555555555555555211111111111111
12|2555555555555555555555555211111111111111
13|2555555555555555555555555211111111111111
14|2222222222222222222222222211111111111111
15|0000000000000000000000000000000000000000
18 changes: 18 additions & 0 deletions tests/test_pick.lua
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,8 @@ T['setup()']['creates `config` field'] = function()
expect_config('source.choose_marked', vim.NIL)

expect_config('window.config', vim.NIL)
expect_config('window.prompt_cursor', '')
expect_config('window.prompt_prefix', '> ')
end

T['setup()']['respects `config` argument'] = function()
Expand Down Expand Up @@ -374,6 +376,8 @@ T['setup()']['validates `config` argument'] = function()

expect_config_error({ window = 'a' }, 'window', 'table')
expect_config_error({ window = { config = 1 } }, 'window.config', 'table or callable')
expect_config_error({ window = { prompt_cursor = 1 } }, 'window.prompt_cursor', 'string')
expect_config_error({ window = { prompt_prefix = 1 } }, 'window.prompt_prefix', 'string')
end

-- This set mostly contains general function testing which doesn't fit into
Expand Down Expand Up @@ -822,6 +826,20 @@ T['start()']['respects `window.config`'] = function()
stop()
end

T['start()']['respects `window.prompt_cursor`'] = function()
start({ source = { items = { 'a', 'b', 'c' } }, window = { prompt_cursor = '+' } })
child.expect_screenshot()
type_keys('a', 'b', '<Left>')
child.expect_screenshot()
end

T['start()']['respects `window.prompt_prefix`'] = function()
start({ source = { items = { 'a', 'b', 'c' } }, window = { prompt_prefix = '$> ' } })
child.expect_screenshot()
type_keys('a', 'b', '<Left>')
child.expect_screenshot()
end

T['start()']['stops currently active picker'] = function()
start_with_items({ 'a', 'b', 'c' })
eq(is_picker_active(), true)
Expand Down

0 comments on commit e587e75

Please sign in to comment.