Skip to content

Commit d3e26ba

Browse files
committed
Merge branch 'release/2.0'
2 parents 65bbc52 + 195bb94 commit d3e26ba

18 files changed

+2524
-389
lines changed

.editorconfig

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
root = true
2+
3+
[*.lua]
4+
charset = utf-8
5+
indent_style = tab
6+
indent_size = 4
7+
trim_trailing_whitespace = true
8+
insert_final_newline = true

.luacheckrc

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
ignore = {
2+
"631", -- max_line_length
3+
}
4+
5+
globals = {
6+
"vim.o",
7+
"vim.wo",
8+
}
9+
read_globals = {
10+
"vim",
11+
"describe",
12+
"it",
13+
"assert"
14+
}

.luarc.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"$schema": "https://raw.githubusercontent.com/sumneko/vscode-lua/master/setting/schema.json",
3+
"Lua.diagnostics.globals": [
4+
"vim"
5+
]
6+
}

README.md

Lines changed: 112 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,92 +1,130 @@
11
# nvim-window-picker
22

3-
https://user-images.githubusercontent.com/18459807/161597479-a3d8cf73-3dca-44b1-9eb6-d00b4e6eb842.mp4
3+
`hint = 'floating-big-letter'`
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+
`hint = 'statusline-winbar'`
6+
7+
This plugins prompts the user to pick a window and returns the window id of the
8+
picked window.
89

910
## Install
1011

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

1314
```lua
14-
use {
15+
{
1516
's1n7ax/nvim-window-picker',
16-
tag = 'v1.*',
17+
name = 'window-picker',
18+
event = 'VeryLazy',
19+
version = '2.*',
1720
config = function()
1821
require'window-picker'.setup()
1922
end,
2023
}
2124
```
2225

23-
**Make sure to `:PackerCompile` after installing**
24-
25-
## How to use
26+
### packer
2627

2728
```lua
28-
local picked_window_id = require('window-picker').pick_window()
29+
use {
30+
's1n7ax/nvim-window-picker',
31+
tag = 'v2.*',
32+
config = function()
33+
require'window-picker'.setup()
34+
end,
35+
}
2936
```
3037

31-
**You can put the picked window id to good use**,
32-
for example, set a keymap to switch the current window like this:
38+
## How to use
3339

3440
```lua
35-
vim.keymap.set("n", "<leader>w", function()
36-
local picked_window_id = picker.pick_window() or vim.api.nvim_get_current_win()
37-
vim.api.nvim_set_current_win(picked_window_id)
38-
end, { desc = "Pick a window" })
41+
local picked_window_id = require('window-picker').pick_window()
3942
```
4043

44+
You can put the picked window id to good use
45+
4146
## Configuration
4247

4348
If you want to have custom properties just for one time, you can pass any of
44-
following directly to `pick_window()` function itself.
49+
following directly to `pick_window()` function itself to override the default
50+
behaviour.
4551

4652
```lua
4753
require 'window-picker'.setup({
48-
-- when there is only one window available to pick from, use that window
49-
-- without prompting the user to select
50-
autoselect_one = true,
51-
52-
-- whether you want to include the window you are currently on to window
53-
-- selection or not
54-
include_current_win = false,
54+
-- type of hints you want to get
55+
-- following types are supported
56+
-- 'statusline-winbar' | 'floating-big-letter'
57+
-- 'statusline-winbar' draw on 'statusline' if possible, if not 'winbar' will be
58+
-- 'floating-big-letter' draw big letter on a floating window
59+
-- used
60+
hint = 'statusline-winbar',
5561

5662
-- when you go to window selection mode, status bar will show one of
5763
-- following letters on them so you can use that letter to select the window
5864
selection_chars = 'FJDKSLA;CMRUEIWOQP',
5965

60-
-- whether you want to use winbar instead of the statusline
61-
-- "always" means to always use winbar,
62-
-- "never" means to never use winbar
63-
-- "smart" means to use winbar if cmdheight=0 and statusline if cmdheight > 0
64-
use_winbar = 'never', -- "always" | "never" | "smart"
66+
-- This section contains picker specific configurations
67+
picker_config = {
68+
statusline_winbar_picker = {
69+
-- You can change the display string in status bar.
70+
-- It supports '%' printf style. Such as `return char .. ': %f'` to display
71+
-- buffer file path. See :h 'stl' for details.
72+
selection_display = function(char, windowid)
73+
return '%=' .. char .. '%='
74+
end,
75+
76+
-- whether you want to use winbar instead of the statusline
77+
-- "always" means to always use winbar,
78+
-- "never" means to never use winbar
79+
-- "smart" means to use winbar if cmdheight=0 and statusline if cmdheight > 0
80+
use_winbar = 'never', -- "always" | "never" | "smart"
81+
},
82+
83+
floating_big_letter = {
84+
-- window picker plugin provides bunch of big letter fonts
85+
-- fonts will be lazy loaded as they are being requested
86+
-- additionally, user can pass in a table of fonts in to font
87+
-- property to use instead
88+
89+
font = 'ansi-shadow', -- ansi-shadow |
90+
},
91+
},
6592

6693
-- whether to show 'Pick window:' prompt
6794
show_prompt = true,
6895

96+
-- prompt message to show to get the user input
97+
prompt_message = 'Pick window: ',
98+
6999
-- if you want to manually filter out the windows, pass in a function that
70-
-- takes two parameters. you should return window ids that should be
100+
-- takes two parameters. You should return window ids that should be
71101
-- included in the selection
72102
-- EX:-
73103
-- function(window_ids, filters)
74-
-- -- filter the window_ids
104+
-- -- folder the window_ids
75105
-- -- return only the ones you want to include
76106
-- return {1000, 1001}
77107
-- end
78108
filter_func = nil,
79109

80110
-- following filters are only applied when you are using the default filter
81-
-- defined by this plugin. if you pass in a function to "filter_func"
111+
-- defined by this plugin. If you pass in a function to "filter_func"
82112
-- property, you are on your own
83113
filter_rules = {
114+
-- when there is only one window available to pick from, use that window
115+
-- without prompting the user to select
116+
autoselect_one = true,
117+
118+
-- whether you want to include the window you are currently on to window
119+
-- selection or not
120+
include_current_win = false,
121+
84122
-- filter using buffer options
85123
bo = {
86124
-- if the file type is one of following, the window will be ignored
87-
filetype = { 'NvimTree', "neo-tree", "notify" },
125+
filetype = { 'NvimTree', 'neo-tree', 'notify' },
88126

89-
-- if the buffer type is one of following, the window will be ignored
127+
-- if the file type is one of following, the window will be ignored
90128
buftype = { 'terminal' },
91129
},
92130

@@ -102,32 +140,48 @@ require 'window-picker'.setup({
102140
file_name_contains = {},
103141
},
104142

105-
-- the foreground (text) color of the picker
106-
fg_color = '#ededed',
107-
108-
-- if you have include_current_win == true, then current_win_hl_color will
109-
-- be highlighted using this background color
110-
current_win_hl_color = '#e35e4f',
111-
112-
-- all the windows except the curren window will be highlighted using this
113-
-- color
114-
other_win_hl_color = '#44cc41',
115-
116-
-- You can change the display string in status bar.
117-
-- It supports '%' printf style. Such as `return char .. ': %f'` to display
118-
-- buffer filepath. See :h 'stl' for details.
119-
selection_display = function (char) return char end,
143+
-- You can pass in the highlight name or a table of content to set as
144+
-- highlight
145+
highlights = {
146+
statusline = {
147+
focused = {
148+
fg = '#ededed',
149+
bg = '#e35e4f',
150+
bold = true,
151+
},
152+
unfocused = {
153+
fg = '#ededed',
154+
bg = '#44cc41',
155+
bold = true,
156+
},
157+
},
158+
winbar = {
159+
focused = {
160+
fg = '#ededed',
161+
bg = '#e35e4f',
162+
bold = true,
163+
},
164+
unfocused = {
165+
fg = '#ededed',
166+
bg = '#44cc41',
167+
bold = true,
168+
},
169+
},
170+
},
120171
})
121172
```
122173

123174
```lua
124-
require(package_name).pick_window({
125-
include_current_win = true,
126-
selection_chars = '123345',
127-
filter_rules = {
128-
bo = {
129-
filetype = {'markdown'}
130-
}
131-
},
175+
require('window-picker').pick_window({
176+
hint = 'floating-big-letter'
132177
})
133178
```
179+
180+
## Breaking changes in v2.0.0
181+
182+
_Before_: return value from `selection_display` will be wrapped by `'%='` and
183+
`'%='` to fill the empty space of status line or winbar.
184+
185+
_After_: return value of `selection_display` will be passed directly to the
186+
status line or winbar. This allows all the customizations available from
187+
statusline syntax. You can check `:help statusline` for more info.

dev/init.lua

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,42 @@
11
--[[
22
-- plugin name will be used to reload the loaded modules
3-
--]] local package_name = 'window-picker'
3+
--]]
4+
local package_name = 'window-picker'
45

56
-- add the escape character to special characters
67
local escape_pattern = function(text)
7-
return text:gsub('([^%w])', '%%%1')
8+
return text:gsub('([^%w])', '%%%1')
89
end
910

1011
-- unload loaded modules by the matching text
1112
local unload_packages = function()
12-
local esc_package_name = escape_pattern(package_name)
13+
local esc_package_name = escape_pattern(package_name)
1314

14-
for module_name, _ in pairs(package.loaded) do
15-
if string.find(module_name, esc_package_name) then
16-
package.loaded[module_name] = nil
17-
end
18-
end
15+
for module_name, _ in pairs(package.loaded) do
16+
if string.find(module_name, esc_package_name) then
17+
package.loaded[module_name] = nil
18+
end
19+
end
1920
end
2021

2122
-- executes the run method in the package
2223
local run_action = function()
23-
require(package_name).setup()
24-
print('you picked: ', require(package_name).pick_window())
24+
require(package_name).setup()
25+
local window = require(package_name).pick_window()
26+
27+
vim.pretty_print('>>>', window)
28+
29+
if not window then
30+
return
31+
end
32+
33+
vim.api.nvim_set_current_win(window)
2534
end
2635

2736
-- unload and run the function from the package
2837
function Reload_and_run()
29-
unload_packages()
30-
run_action()
38+
unload_packages()
39+
run_action()
3140
end
3241

3342
---@diagnostic disable-next-line: undefined-global

lua/window-picker/builder.lua

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
local dconfig = require('window-picker.config')
2+
local dfilter = require('window-picker.filters.default-window-filter')
3+
local dpicker = require('window-picker.pickers.window-picker')
4+
local dhint = require('window-picker.hints.statusline-winbar-hint')
5+
local dconfigurer = require('window-picker.configurer')
6+
7+
--- @class DefaultBuilder
8+
--- @field configurer Configurer
9+
local M = {}
10+
11+
function M:new(configurer)
12+
local o = { configurer = configurer }
13+
14+
setmetatable(o, self)
15+
self.__index = self
16+
17+
return self
18+
end
19+
20+
function M:set_config(config)
21+
self.config = config
22+
return self
23+
end
24+
25+
function M:set_picker(picker)
26+
self.picker = picker
27+
return self
28+
end
29+
30+
function M:set_hint(hint)
31+
self.hint = hint
32+
return self
33+
end
34+
35+
function M:set_filter(filter)
36+
self.filter = filter
37+
return self
38+
end
39+
40+
function M:build()
41+
local configurer = self.configurer or dconfigurer:new(self.config)
42+
43+
local hint = configurer:config_hint(self.hint or dhint:new())
44+
local filter = configurer:config_filter(self.filter or dfilter:new())
45+
local picker = configurer:config_picker(self.picker or dpicker:new())
46+
47+
picker:set_filter(filter)
48+
picker:set_hint(hint)
49+
50+
return picker
51+
end
52+
53+
return M

0 commit comments

Comments
 (0)