Skip to content

Commit 5fca333

Browse files
committed
feat: add compatibility to old config style
1 parent bed6555 commit 5fca333

File tree

8 files changed

+126
-40
lines changed

8 files changed

+126
-40
lines changed

README.md

Lines changed: 54 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,20 @@
11
# nvim-window-picker
22

3-
https://user-images.githubusercontent.com/18459807/161597479-a3d8cf73-3dca-44b1-9eb6-d00b4e6eb842.mp4
3+
<https://user-images.githubusercontent.com/18459807/161597479-a3d8cf73-3dca-44b1-9eb6-d00b4e6eb842.mp4>
44

5-
This plugins prompts the user to pick a window and returns the window id of the picked window.
6-
Part of the code is from [nvim-tree](https://github.com/kyazdani42/nvim-tree.lua) so shout out to
7-
them for coming up with this idea.
5+
This plugins prompts the user to pick a window and returns the window id of the
6+
picked window. Part of the code is from
7+
[nvim-tree](https://github.com/kyazdani42/nvim-tree.lua) so shout out to them
8+
for coming up with this idea.
89

910
## Install
1011

11-
#### packer
12+
### packer
1213

1314
```lua
1415
use {
1516
's1n7ax/nvim-window-picker',
16-
tag = 'v1.*',
17+
tag = 'v2.*',
1718
config = function()
1819
require'window-picker'.setup()
1920
end,
@@ -28,33 +29,45 @@ use {
2829
local picked_window_id = require('window-picker').pick_window()
2930
```
3031

31-
**You can put the picked window id to good use**
32+
You can put the picked window id to good use
3233

3334
## Configuration
3435

3536
If you want to have custom properties just for one time, you can pass any of
36-
following directly to `pick_window()` function itself.
37+
following directly to `pick_window()` function itself to override the default
38+
behaviour.
3739

3840
```lua
3941
require 'window-picker'.setup({
40-
-- when there is only one window available to pick from, use that window
41-
-- without prompting the user to select
42-
autoselect_one = true,
43-
44-
-- whether you want to include the window you are currently on to window
45-
-- selection or not
46-
include_current_win = false,
47-
4842
-- when you go to window selection mode, status bar will show one of
4943
-- following letters on them so you can use that letter to select the window
5044
selection_chars = 'FJDKSLA;CMRUEIWOQP',
5145

46+
-- You can change the display string in status bar.
47+
-- It supports '%' printf style. Such as `return char .. ': %f'` to display
48+
-- buffer filepath. See :h 'stl' for details.
49+
selection_display = function(char, windowid)
50+
return '%=' .. char .. '%='
51+
end,
52+
53+
-- whether you want to use winbar instead of the statusline
54+
-- "always" means to always use winbar,
55+
-- "never" means to never use winbar
56+
-- "smart" means to use winbar if cmdheight=0 and statusline if cmdheight > 0
57+
use_winbar = 'smart', -- "always" | "never" | "smart"
58+
59+
-- whether to show 'Pick window:' prompt
60+
show_prompt = false,
61+
62+
-- prompt message to show to get the user input
63+
prompt_message = 'Pick window: ',
64+
5265
-- if you want to manually filter out the windows, pass in a function that
53-
-- takes two parameters. you should return window ids that should be
66+
-- takes two parameters. You should return window ids that should be
5467
-- included in the selection
5568
-- EX:-
5669
-- function(window_ids, filters)
57-
-- -- filter the window_ids
70+
-- -- folder the window_ids
5871
-- -- return only the ones you want to include
5972
-- return {1000, 1001}
6073
-- end
@@ -64,12 +77,20 @@ require 'window-picker'.setup({
6477
-- defined by this plugin. if you pass in a function to "filter_func"
6578
-- property, you are on your own
6679
filter_rules = {
80+
-- when there is only one window available to pick from, use that window
81+
-- without prompting the user to select
82+
autoselect_one = true,
83+
84+
-- whether you want to include the window you are currently on to window
85+
-- selection or not
86+
include_current_win = false,
87+
6788
-- filter using buffer options
6889
bo = {
6990
-- if the file type is one of following, the window will be ignored
70-
filetype = { 'NvimTree', "neo-tree", "notify" },
91+
filetype = { 'NvimTree', 'neo-tree', 'notify' },
7192

72-
-- if the buffer type is one of following, the window will be ignored
93+
-- if the file type is one of following, the window will be ignored
7394
buftype = { 'terminal' },
7495
},
7596

@@ -85,6 +106,9 @@ require 'window-picker'.setup({
85106
file_name_contains = {},
86107
},
87108

109+
-- the foreground (text) color of the picker
110+
fg_color = '#ededed',
111+
88112
-- if you have include_current_win == true, then current_win_hl_color will
89113
-- be highlighted using this background color
90114
current_win_hl_color = '#e35e4f',
@@ -97,12 +121,21 @@ require 'window-picker'.setup({
97121

98122
```lua
99123
require(package_name).pick_window({
100-
include_current_win = true,
101124
selection_chars = '123345',
102125
filter_rules = {
126+
include_current_win = true,
103127
bo = {
104128
filetype = {'markdown'}
105129
}
106130
},
107131
})
108132
```
133+
134+
## Breaking changes in v2.0.0
135+
136+
_Before_: return value from `selection_display` will be wrapped by `'%='` and
137+
`'%='` to fill the empty space of status line or winbar.
138+
139+
_After_: return value of `selection_display` will be passed directly to the
140+
status line or winbar. This allows all the customizations available from
141+
statusline syntax. You can check `:help statusline` for more info.

lua/window-picker/builder.lua

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1+
local dconfig = require('window-picker.config')
12
local dfilter = require('window-picker.filters.default-window-filter')
23
local dpicker = require('window-picker.pickers.window-picker')
3-
local dhint = require('window-picker.hints.statusline-hint')
4+
local dhint = require('window-picker.hints.statusline-winbar-hint')
45
local dconfigurer = require('window-picker.configurer')
56

67
--- @class DefaultBuilder
@@ -17,7 +18,7 @@ function M:new(configurer)
1718
end
1819

1920
function M:set_config(config)
20-
self.config = config
21+
self.config = vim.tbl_deep_extend('force', dconfig, config or {})
2122
return self
2223
end
2324

lua/window-picker/config.lua

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,4 @@
11
local config = {
2-
-- when there is only one window available to pick from, use that window
3-
-- without prompting the user to select
4-
autoselect_one = true,
5-
6-
-- whether you want to include the window you are currently on to window
7-
-- selection or not
8-
include_current_win = false,
9-
102
-- when you go to window selection mode, status bar will show one of
113
-- following letters on them so you can use that letter to select the window
124
selection_chars = 'FJDKSLA;CMRUEIWOQP',
@@ -22,11 +14,14 @@ local config = {
2214
-- "always" means to always use winbar,
2315
-- "never" means to never use winbar
2416
-- "smart" means to use winbar if cmdheight=0 and statusline if cmdheight > 0
25-
use_winbar = 'smart', -- "always" | "never" | "smart"
17+
use_winbar = 'never', -- "always" | "never" | "smart"
2618

2719
-- whether to show 'Pick window:' prompt
2820
show_prompt = true,
2921

22+
-- prompt message to show to get the user input
23+
prompt_message = 'Pick window: ',
24+
3025
-- if you want to manually filter out the windows, pass in a function that
3126
-- takes two parameters. You should return window ids that should be
3227
-- included in the selection
@@ -42,6 +37,14 @@ local config = {
4237
-- defined by this plugin. if you pass in a function to "filter_func"
4338
-- property, you are on your own
4439
filter_rules = {
40+
-- when there is only one window available to pick from, use that window
41+
-- without prompting the user to select
42+
autoselect_one = true,
43+
44+
-- whether you want to include the window you are currently on to window
45+
-- selection or not
46+
include_current_win = false,
47+
4548
-- filter using buffer options
4649
bo = {
4750
-- if the file type is one of following, the window will be ignored

lua/window-picker/configurer.lua

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,42 @@ local dconfig = require('window-picker.config')
66
local M = {}
77

88
function M:new(config)
9+
config = config and util.merge_config(dconfig, config) or dconfig
10+
config = M._backward_compatibility_config_changes(config)
11+
config = M._basic_config_manipulations(config)
12+
913
local o = {
10-
config = config and util.merge_config(dconfig, config) or dconfig,
14+
config = config,
1115
}
1216

13-
o.config.chars = self._str_to_char_list(
14-
o.config.selection_chars
15-
)
16-
1717
setmetatable(o, self)
18+
1819
self.__index = self
1920

2021
return o
2122
end
2223

24+
function M._basic_config_manipulations(config)
25+
config.chars = M._str_to_char_list(config.selection_chars)
26+
return config
27+
end
28+
29+
function M._backward_compatibility_config_changes(config)
30+
local filter_rules = config.filter_rules
31+
32+
-- backward compatibility to config.autoselect_one
33+
if config.autoselect_one then
34+
filter_rules.autoselect_one = config.autoselect_one
35+
end
36+
37+
-- backward compatibility to config.include_current_win
38+
if config.include_current_win then
39+
filter_rules.include_current_win = config.include_current_win
40+
end
41+
42+
return config
43+
end
44+
2345
function M:config_filter(filter)
2446
filter:set_config(self.config.filter_rules)
2547
return filter

lua/window-picker/filters/default-window-filter.lua

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ function M:new()
1919
o._buffer_options_filter,
2020
o._file_path_contains_filter,
2121
o._file_path_contains_filter,
22+
o._current_window_filter,
2223
}
2324

2425
return o
@@ -29,6 +30,7 @@ function M:set_config(config)
2930
self.buffer_options = config.bo or {}
3031
self.file_name_contains = config.file_name_contains or {}
3132
self.file_path_contains = config.file_path_contains or {}
33+
self.include_current_win = config.include_current_win
3234
end
3335

3436
function M:filter_windows(windows)
@@ -136,4 +138,16 @@ function M:_file_name_contains_filter(windows)
136138
end
137139
end
138140

141+
function M:_current_window_filter(windows)
142+
if self.include_current_win then
143+
return windows
144+
end
145+
146+
local curr_win = vim.api.nvim_get_current_win()
147+
148+
return util.tbl_filter(windows, function(winid)
149+
return winid ~= curr_win
150+
end)
151+
end
152+
139153
return M

lua/window-picker/init.lua

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
local hint = require('window-picker.hints.floating-big-letter-hint')
21
local builder = require('window-picker.builder')
32

43
local M = {}
@@ -7,7 +6,7 @@ function M.pick_window(custom_config)
76
return builder
87
:new()
98
:set_config(custom_config)
10-
:set_hint(hint:new())
9+
:set_hint()
1110
:set_picker()
1211
:set_filter()
1312
:build()

lua/window-picker/pickers/window-picker.lua

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ end
1414

1515
function M:set_config(config)
1616
self.chars = config.chars
17+
self.show_prompt = config.show_prompt
18+
self.prompt_message = config.prompt_message
19+
self.autoselect_one = config.filter_rules.autoselect_one
1720
return self
1821
end
1922

@@ -42,13 +45,25 @@ end
4245

4346
function M:pick_window()
4447
local windows = self:_get_windows()
48+
49+
if self.autoselect_one and #windows == 1 then
50+
return windows[1]
51+
end
52+
4553
local window = nil
4654

4755
self.hint:draw(windows)
4856

4957
vim.cmd.redraw()
5058

59+
if self.show_prompt then
60+
print(self.prompt_message)
61+
end
62+
5163
local char = util.get_user_input_char()
64+
65+
vim.cmd.redraw()
66+
5267
self.hint:clear()
5368

5469
if char then

lua/window-picker/util.lua

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ function M.map_find(tbl, match_func)
2727
end
2828

2929
function M.get_user_input_char()
30-
print('Window: ')
3130
local c = vim.fn.getchar()
3231
while type(c) ~= 'number' do
3332
c = vim.fn.getchar()

0 commit comments

Comments
 (0)