|
1 | 1 | --[[ |
2 | | -Highly customisable `statusline`, `statuscolumn`, `winbar` & `tabline` for **Neovim**. |
| 2 | +# 🍫 bars.nvim |
3 | 3 |
|
4 | | -Usage, |
| 4 | +A *fancy* `bars & lines` plugin for `Neovim`. |
5 | 5 |
|
6 | | -```lua |
7 | | -require("bars").setup(); |
8 | | -``` |
| 6 | +[Github](https://github.com/OXY2DEV/bars.nvim) |
| 7 | +[Usage](https://github.com/OXY2DEV/bars.nvim/wiki) |
9 | 8 | ]] |
10 | 9 | local bars = {}; |
11 | 10 |
|
12 | | ----@type bars.config |
13 | | -bars.config = { |
14 | | - ---|fS |
| 11 | +--[[ Executes an `action`. ]] |
| 12 | +---@param action string |
| 13 | +---@param on string[] |
| 14 | +---@param ... any |
| 15 | +bars.exec = function (action, on, ...) |
| 16 | + local modules = { "statusline", "statuscolumn", "winbar", "tabline" }; |
| 17 | + on = on or modules; |
15 | 18 |
|
16 | | - global = true, |
| 19 | + for _, mod_name in ipairs(modules) do |
| 20 | + if vim.list_contains(on, mod_name) then |
| 21 | + local could_load, module = pcall(require, "bars." .. mod_name); |
17 | 22 |
|
18 | | - statuscolumn = true, |
19 | | - statusline = true, |
20 | | - tabline = true, |
21 | | - winbar = true, |
22 | | - |
23 | | - ---|fE |
24 | | -}; |
25 | | - |
26 | | ---[[ |
27 | | -Gets the modules need by `bars.nvim`. |
28 | | -
|
29 | | -NOTE: This is to prevent loading the modules at start. |
30 | | -]] |
31 | | ----@return table<string, table> |
32 | | -local function get_modules() |
33 | | - ---|fS |
34 | | - |
35 | | - return { |
36 | | - statuscolumn = package.loaded["bars.statuscolumn"]; |
37 | | - statusline = package.loaded["bars.statusline"]; |
38 | | - tabline = package.loaded["bars.tabline"]; |
39 | | - winbar = package.loaded["bars.winbar"]; |
40 | | - }; |
41 | | - |
42 | | - ---|fE |
43 | | -end |
44 | | - |
45 | | ---- Various actions that exposes the plugin's |
46 | | ---- functionality outside. |
47 | | ----@type table<string, function> |
48 | | -bars.actions = { |
49 | | - ---|fS |
50 | | - |
51 | | - Toggle = function (affect) |
52 | | - local modules = get_modules(); |
53 | | - affect = affect or vim.tbl_keys(modules); |
54 | | - |
55 | | - for name, module in pairs(modules) do |
56 | | - if vim.list_contains(affect, name) then |
57 | | - pcall(module.Toggle); |
58 | | - end |
59 | | - end |
60 | | - end, |
61 | | - |
62 | | - Enable = function (affect) |
63 | | - local modules = get_modules(); |
64 | | - affect = affect or vim.tbl_keys(modules); |
65 | | - |
66 | | - for name, module in pairs(modules) do |
67 | | - if vim.list_contains(affect, name) then |
68 | | - pcall(module.Enable); |
69 | | - end |
70 | | - end |
71 | | - end, |
72 | | - Disable = function (affect) |
73 | | - local modules = get_modules(); |
74 | | - affect = affect or vim.tbl_keys(modules); |
75 | | - |
76 | | - for name, module in pairs(modules) do |
77 | | - if vim.list_contains(affect, name) then |
78 | | - pcall(module.Disable); |
79 | | - end |
80 | | - end |
81 | | - end, |
82 | | - |
83 | | - Start = function (affect) |
84 | | - local modules = get_modules(); |
85 | | - affect = affect or vim.tbl_keys(modules); |
86 | | - |
87 | | - for name, module in pairs(modules) do |
88 | | - if vim.list_contains(affect, name) then |
89 | | - pcall(module.Start); |
90 | | - end |
91 | | - end |
92 | | - end, |
93 | | - Stop = function (affect) |
94 | | - local modules = get_modules(); |
95 | | - affect = affect or vim.tbl_keys(modules); |
96 | | - |
97 | | - for name, module in pairs(modules) do |
98 | | - if vim.list_contains(affect, name) then |
99 | | - pcall(module.Stop); |
100 | | - end |
101 | | - end |
102 | | - end, |
103 | | - |
104 | | - toggle = function (affect, windows) |
105 | | - windows = windows or { vim.api.nvim_get_current_win() }; |
106 | | - |
107 | | - local modules = get_modules(); |
108 | | - affect = affect or vim.tbl_keys(modules); |
109 | | - |
110 | | - for name, module in pairs(modules) do |
111 | | - if vim.list_contains(affect, name) == false then |
112 | | - goto continue |
113 | | - end |
114 | | - |
115 | | - for _, window in ipairs(windows) do |
116 | | - pcall(module.toggle, window); |
117 | | - end |
118 | | - |
119 | | - ::continue:: |
120 | | - end |
121 | | - end, |
122 | | - enable = function (affect, windows) |
123 | | - windows = windows or { vim.api.nvim_get_current_win() }; |
124 | | - |
125 | | - local modules = get_modules(); |
126 | | - affect = affect or vim.tbl_keys(modules); |
127 | | - |
128 | | - for name, module in pairs(modules) do |
129 | | - if vim.list_contains(affect, name) == false then |
130 | | - goto continue |
131 | | - end |
132 | | - |
133 | | - for _, window in ipairs(windows) do |
134 | | - pcall(module.enable, window); |
135 | | - end |
136 | | - |
137 | | - ::continue:: |
138 | | - end |
139 | | - end, |
140 | | - disable = function (affect, windows) |
141 | | - windows = windows or { vim.api.nvim_get_current_win() }; |
142 | | - |
143 | | - local modules = get_modules(); |
144 | | - affect = affect or vim.tbl_keys(modules); |
145 | | - |
146 | | - for name, module in pairs(modules) do |
147 | | - if vim.list_contains(affect, name) == false then |
148 | | - goto continue |
| 23 | + if could_load and module then |
| 24 | + pcall(module[action], module, ...) |
149 | 25 | end |
150 | | - |
151 | | - for _, window in ipairs(windows) do |
152 | | - pcall(module.disable, window); |
153 | | - end |
154 | | - |
155 | | - ::continue:: |
156 | | - end |
157 | | - end, |
158 | | - |
159 | | - clean = function (affect) |
160 | | - local modules = get_modules(); |
161 | | - affect = affect or vim.tbl_keys(modules); |
162 | | - |
163 | | - for name, module in pairs(modules) do |
164 | | - if vim.list_contains(affect, name) == false then |
165 | | - goto continue |
166 | | - end |
167 | | - |
168 | | - pcall(module.clean); |
169 | | - |
170 | | - ::continue:: |
171 | | - end |
172 | | - end, |
173 | | - update = function (affect, windows) |
174 | | - windows = windows or { vim.api.nvim_get_current_win() }; |
175 | | - |
176 | | - local modules = get_modules(); |
177 | | - affect = affect or vim.tbl_keys(modules); |
178 | | - |
179 | | - for name, module in pairs(modules) do |
180 | | - if vim.list_contains(affect, name) == false then |
181 | | - goto continue |
182 | | - end |
183 | | - |
184 | | - for _, window in ipairs(windows) do |
185 | | - pcall(module.update_style or module.update_id, window); |
186 | | - end |
187 | | - |
188 | | - ::continue:: |
189 | 26 | end |
190 | 27 | end |
| 28 | +end |
191 | 29 |
|
192 | | - ---|fE |
193 | | -}; |
194 | | - |
195 | | ---- Setup function. |
196 | | ---- Should be optional. |
197 | | ----@param config bars.config | nil |
198 | 30 | bars.setup = function (config) |
199 | | - ---|fS |
| 31 | + for k, v in pairs(config) do |
| 32 | + local could_load, submodule = pcall(require, "bars." .. k); |
200 | 33 |
|
201 | | - --- Update the configuration. |
202 | | - if type(config) == "table" then |
203 | | - bars.config = vim.tbl_deep_extend("force", bars.config, config); |
204 | | - end |
205 | | - |
206 | | - local _config = {}; |
207 | | - |
208 | | - for key, value in pairs(bars.config) do |
209 | | - if type(value) == "function" then |
210 | | - local can_call, new_value = pcall(value); |
211 | | - |
212 | | - if can_call == true then |
213 | | - _config[key] = new_value; |
214 | | - else |
215 | | - _config[key] = nil; |
216 | | - end |
217 | | - else |
218 | | - _config[key] = value; |
| 34 | + if could_load and submodule then |
| 35 | + pcall(submodule.setup, v); |
219 | 36 | end |
220 | 37 | end |
221 | | - |
222 | | - require("bars.statusline").setup(_config.statusline); |
223 | | - require("bars.statuscolumn").setup(_config.statuscolumn); |
224 | | - require("bars.winbar").setup(_config.winbar); |
225 | | - |
226 | | - require("bars.tabline").setup(_config.tabline); |
227 | | - |
228 | | - ---|fE |
229 | 38 | end |
230 | 39 |
|
231 | 40 | return bars; |
0 commit comments