Skip to content

Commit 94b1c48

Browse files
committed
doc: Updated architecture
1 parent 8a94bf6 commit 94b1c48

1 file changed

Lines changed: 218 additions & 2 deletions

File tree

guides/Architecture.md

Lines changed: 218 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ statusline.state = {
4545
```
4646

4747
+ A renderer(`module.render`). This will be used as the content of the specific bar/line.
48-
```lua $SRC: ../lua/bars/statusline.lua, from: $SRC, field: statusline.render
48+
```lua from: $SRC, field: statusline.render
4949
--- Renders the statusline for a window.
5050
---@return string
5151
statusline.render = function ()
@@ -88,5 +88,221 @@ end
8888

8989
## `components/<module>.lua`
9090

91-
This will hold various components
91+
This file holds,
92+
93+
+ A component retriever to get the various components(and handle errors),
94+
95+
```lua $CMP: ../lua/bars/components/statusline.lua, from: $CMP, field: slC.get
96+
--- Returns the output of the section {name}.
97+
---@param name string
98+
---@param buffer integer
99+
---@param window integer
100+
---@param component_config table
101+
---@param statusline string
102+
---@return string
103+
slC.get = function (name, buffer, window, component_config, statusline)
104+
---|fS
105+
106+
if type(name) ~= "string" then
107+
--- Component doesn't exist.
108+
return "";
109+
elseif type(slC[name]) ~= "function" then
110+
--- Not a valid component.
111+
return "";
112+
else
113+
if component_config.condition ~= nil then
114+
if component_config.condition == false then
115+
--- Component is disabled.
116+
return "";
117+
else
118+
local sucess, val = pcall(component_config.condition, buffer, window, statusline);
119+
120+
if sucess == false then
121+
return "";
122+
elseif val == false then
123+
return "";
124+
end
125+
end
126+
end
127+
128+
local static_config = vim.deepcopy(component_config);
129+
130+
for key, value in pairs(static_config) do
131+
if type(value) ~= "function" then
132+
goto continue;
133+
end
134+
135+
local s_success, s_val = pcall(value, buffer, window, statusline);
136+
137+
if s_success == false then
138+
static_config[key] = nil;
139+
else
140+
static_config[key] = s_val;
141+
end
142+
143+
::continue::
144+
end
145+
146+
--- Return component value.
147+
return slC[name](buffer, window, static_config, statusline) or "";
148+
end
149+
150+
---|fE
151+
end
152+
```
153+
154+
+ Any number of component functions,
155+
156+
```lua from: $CMP, field: slC.custom, field: slC.branch
157+
--- Custom section.
158+
---@param config statusline.components.custom
159+
---@return string
160+
slC.custom = function (_, _, config)
161+
return config.value --[[ @as string ]];
162+
end
163+
164+
--- Shows current git branch.
165+
---@param buffer integer
166+
---@param window integer
167+
---@param main_config statusline.components.branch
168+
---@return string
169+
slC.branch = function (buffer, window, main_config)
170+
---|fS
171+
172+
local cwd;
173+
local ignore = { "default", "condition", "throttle", "kind" };
174+
175+
vim.api.nvim_win_call(window, function ()
176+
cwd = vim.fn.getcwd(window);
177+
end);
178+
179+
if type(cwd) ~= "string" then
180+
return "";
181+
end
182+
183+
local branch;
184+
185+
--- Gets the current git branch.
186+
---@return string
187+
local function get_branch ()
188+
---|fS
189+
190+
if package.loaded["gitsigns"] and type(vim.b[buffer].gitsigns_head) == "string" then
191+
--[[
192+
NOTE: In case `gitsigns.nvim` is available, use their information instead.
193+
194+
Getting the branch data may be expensive, so there should be no reason to do it multiple times.
195+
And `gitsigns.nvim` should handle Git-related stuff better than we do.
196+
197+
See `:h b:gitsigns_head`.
198+
]]
199+
return vim.b[buffer].gitsigns_head;
200+
end
201+
202+
--- Are we in a repo?
203+
---@type string
204+
local in_repo = vim.fn.system({
205+
"git",
206+
"-C",
207+
cwd,
208+
"rev-parse",
209+
"--is-inside-work-tree"
210+
});
211+
212+
if not in_repo or string.match(in_repo, "^true") == nil then
213+
--- The output doesn't exist or it doesn't
214+
--- start with "true" then return.
215+
return "";
216+
end
217+
218+
--- First check if we are inside
219+
--- a branch.
220+
---@type string | ""
221+
local _branch = vim.fn.system({
222+
"git",
223+
"-C",
224+
cwd,
225+
"branch",
226+
"--show-current"
227+
});
228+
229+
if _branch == "" then
230+
--- We are not in a branch.
231+
--- Attempt to get commit hash(short).
232+
---@type string
233+
_branch = vim.fn.system({
234+
"git",
235+
"-C",
236+
cwd,
237+
"rev-parse",
238+
"--short",
239+
"HEAD"
240+
});
241+
end
242+
243+
return _branch or "";
244+
245+
---|fE
246+
end
247+
248+
if not vim.w[window].bars_cached_git_branch then
249+
--- Cached branch name not found.
250+
--- Get current branch name.
251+
---@type string
252+
branch = vim.split(get_branch(), "\n", { trimempty = true });
253+
254+
vim.w[window].bars_cached_git_branch = branch;
255+
vim.w[window].bars_cached_time_git = vim.uv.hrtime();
256+
else
257+
---@type infowhat
258+
local now = vim.uv.hrtime();
259+
---@type integer
260+
local bef = vim.w.bars_cached_time_git or 0;
261+
262+
--- Branch value update delay.
263+
---@type integer
264+
local throttle = main_config.throttle or 2000;
265+
266+
if now - bef >= (throttle * 1e6) then
267+
--- We have waited longer than `throttle`.
268+
---@type string
269+
branch = vim.split(get_branch(), "\n", { trimempty = true });
270+
271+
--- Update cached value & update time.
272+
vim.w[window].bars_cached_git_branch = branch;
273+
vim.w[window].bars_cached_time_git = vim.uv.hrtime();
274+
else
275+
--- Not enough time has passed.
276+
--- Use cached value.
277+
branch = vim.w[window].bars_cached_git_branch;
278+
end
279+
end
280+
281+
if not branch or vim.tbl_isempty(branch) then
282+
return "";
283+
elseif branch[1]:match("^fatal%:") then
284+
return "";
285+
elseif branch[1]:match("^error%:") then
286+
return "";
287+
else
288+
---@type branch.opts
289+
local config = utils.match(main_config, branch[1], ignore);
290+
291+
return table.concat({
292+
utils.set_hl(config.hl),
293+
294+
string.format("%s%s", utils.set_hl(config.corner_left_hl), config.corner_left or ""),
295+
string.format("%s%s", utils.set_hl(config.padding_left_hl), config.padding_left or ""),
296+
string.format("%s%s", utils.set_hl(config.icon_hl), config.icon or ""),
297+
298+
config.text or branch[1] or "",
299+
300+
string.format("%s%s", utils.set_hl(config.padding_right_hl), config.padding_right or ""),
301+
string.format("%s%s", utils.set_hl(config.corner_right_hl), config.corner_right or "")
302+
});
303+
end
304+
305+
---|fE
306+
end
307+
```
92308

0 commit comments

Comments
 (0)