|
2 | 2 | ---@field window_options table<string, any> window options |
3 | 3 | ---@field global_options table<string, any> global options |
4 | 4 | ---@field laststatus string current value of the laststatus |
| 5 | +---@field chars string[] list of chars to hint |
| 6 | +---@field selection_display function function to customize the hint |
5 | 7 | local M = {} |
6 | 8 |
|
7 | 9 | function M:new() |
8 | 10 | local o = { |
9 | 11 | window_options = {}, |
10 | 12 | global_options = {}, |
11 | | - laststatus = vim.o.laststatus, |
12 | 13 | } |
13 | 14 |
|
14 | | - setmetatable(o, M) |
| 15 | + setmetatable(o, self) |
15 | 16 | self.__index = self |
16 | 17 |
|
17 | 18 | return o |
18 | 19 | end |
19 | 20 |
|
20 | 21 | function M:set_config(config) |
21 | 22 | self.chars = config.chars |
22 | | - self.current_win_hl_color = config.current_win_hl_color |
23 | | - self.other_win_hl_color = config.other_win_hl_color |
| 23 | + self.selection_display = config.selection_display |
| 24 | + |
| 25 | + vim.api.nvim_set_hl(0, 'WindowPickerStatusLine', { |
| 26 | + fg = '#ededed', |
| 27 | + bg = config.current_win_hl_color, |
| 28 | + bold = true, |
| 29 | + }) |
| 30 | + |
| 31 | + vim.api.nvim_set_hl(0, 'WindowPickerStatusLineNC', { |
| 32 | + fg = '#ededed', |
| 33 | + bg = config.other_win_hl_color, |
| 34 | + bold = true, |
| 35 | + }) |
24 | 36 | end |
25 | 37 |
|
26 | | --- M:_set_highlight_groups sets highlight groups |
27 | | --- @param { any } config passes colors |
28 | | -function M:_set_highlight_groups() |
29 | | - vim.cmd( |
30 | | - 'highlight NvimWindoSwitch gui=bold guifg=#ededed guibg=' |
31 | | - .. self.current_win_hl_color |
32 | | - ) |
33 | | - vim.cmd( |
34 | | - 'highlight NvimWindoSwitchNC gui=bold guifg=#ededed guibg=' |
35 | | - .. self.other_win_hl_color |
36 | | - ) |
37 | | -end |
| 38 | +--- M:_save_window_options saves the initial window options later will be changed |
| 39 | +--- @param windows number[] |
| 40 | +function M:save_window_options(windows, win_opt_to_cap) |
| 41 | + self.window_options = {} |
38 | 42 |
|
39 | | --- M:_save_window_options saves the initial window options later will be changed |
40 | | --- @param { Array<number> } window_ids list of window IDs |
41 | | -function M:_save_window_options(windows) |
42 | 43 | for _, window in ipairs(windows) do |
43 | | - self.window_options[window] = { |
44 | | - statusline = vim.wo[window].statusline, |
45 | | - winhl = vim.wo[window].winhl, |
46 | | - } |
47 | | - end |
| 44 | + self.window_options[window] = {} |
48 | 45 |
|
49 | | - self.global_options['cmdheight'] = vim.o.cmdheight |
| 46 | + for _, opt_key in ipairs(win_opt_to_cap) do |
| 47 | + self.window_options[window][opt_key] = vim.wo[window][opt_key] |
| 48 | + end |
| 49 | + end |
50 | 50 | end |
51 | 51 |
|
52 | | --- M:print shows the characters in status line |
53 | | --- @param { table } data window-letter mapping to show on status line |
| 52 | +--- Shows the characters in status line |
| 53 | +--- @param windows number[] windows to draw the hints on |
54 | 54 | function M:draw(windows) |
55 | | - self:_save_window_options(windows) |
56 | | - self:_set_highlight_groups() |
| 55 | + local win_opt_to_cap = { 'statusline', 'winhl' } |
| 56 | + |
| 57 | + if vim.o.cmdheight < 1 then |
| 58 | + self.global_options['cmdheight'] = vim.o['cmdheight'] |
| 59 | + vim.o.cmdheight = 1 |
| 60 | + end |
| 61 | + |
| 62 | + if vim.o.laststatus ~= 2 then |
| 63 | + self.global_options['laststatus'] = vim.o['laststatus'] |
| 64 | + vim.o.laststatus = 2 |
| 65 | + end |
57 | 66 |
|
58 | | - -- make sure every window has status line |
59 | | - vim.o.laststatus = 2 |
| 67 | + self:save_window_options(windows, win_opt_to_cap) |
60 | 68 |
|
61 | 69 | for index, window in ipairs(windows) do |
62 | 70 | local char = self.chars[index] |
63 | | - vim.wo[window].statusline = '%=' .. char .. '%=' |
64 | | - vim.wo[window].winhl = |
65 | | - 'StatusLine:NvimWindoSwitch,StatusLineNC:NvimWindoSwitchNC' |
66 | | - end |
67 | 71 |
|
68 | | - if self.global_options.cmdheight == 0 then |
69 | | - vim.o.cmdheight = 1 |
| 72 | + vim.wo[window].statusline = self.selection_display |
| 73 | + and self.selection_display(char, window) |
| 74 | + or '%=' .. char .. '%=' |
| 75 | + |
| 76 | + vim.wo[window].winhl = |
| 77 | + 'StatusLine:WindowPickerStatusLine,StatusLineNC:WindowPickerStatusLineNC' |
70 | 78 | end |
71 | 79 |
|
72 | | - vim.cmd('redraw') |
| 80 | + vim.cmd.redraw() |
73 | 81 | end |
74 | 82 |
|
75 | | --- M:clear the screen after print |
| 83 | +--- clear the screen after print |
76 | 84 | function M:clear() |
77 | | - vim.o.laststatus = self.laststatus |
78 | | - |
79 | 85 | for window, options in pairs(self.window_options) do |
80 | 86 | for opt_key, opt_value in pairs(options) do |
81 | 87 | vim.wo[window][opt_key] = opt_value |
82 | 88 | end |
83 | 89 | end |
84 | 90 |
|
85 | | - self.window_options = {} |
86 | | - |
87 | 91 | for key, value in pairs(self.global_options) do |
88 | 92 | vim.o[key] = value |
89 | 93 | end |
| 94 | + |
| 95 | + self.window_options = {} |
| 96 | + self.global_options = {} |
90 | 97 | end |
91 | 98 |
|
92 | 99 | return M |
0 commit comments